]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/ml_configuration.cpp
Qt: complete_preferences: toggle prefs for current modules.
[vlc] / modules / gui / qt4 / dialogs / ml_configuration.cpp
1 /*****************************************************************************
2  * ml_configuration.cpp: ML's configuration dialog (folder view)
3  *****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN Team and AUTHORS
5  * $Id$
6  *
7  * Authors: Antoine Lejeune <phytos@videolan.org>
8  *          Jean-Philippe André <jpeg@videolan.org>
9  *          Rémi Duraffort <ivoire@videolan.org>
10  *          Adrien Maglo <magsoft@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #ifdef MEDIA_LIBRARY
32
33 #include "ml_configuration.hpp"
34
35 #include <QTreeView>
36 #include <QPushButton>
37 #include <QDialogButtonBox>
38
39 MLConfDialog *MLConfDialog::instance = NULL;
40
41
42 /** **************************************************************************
43  * MODEL FOR THE FILESYSTEM
44  *****************************************************************************/
45
46 Qt::ItemFlags MLDirModel::flags( const QModelIndex &index ) const
47 {
48     Qt::ItemFlags flags = QDirModel::flags( index );
49     flags |= Qt::ItemIsUserCheckable;
50     if( b_recursive )
51     {
52         for( int i = 0; i < monitoredDirs.count(); i++ )
53         {
54             if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
55             {
56                 if( monitoredDirs.at( i ) != filePath( index ) )
57                     flags ^= Qt::ItemIsEnabled;
58                 break;
59             }
60         }
61     }
62     return flags;
63 }
64
65 QVariant MLDirModel::data( const QModelIndex &index, int role ) const
66 {
67     if( index.column() == 0 && role == Qt::CheckStateRole )
68     {
69         if( itemCheckState.contains( filePath( index ) ) )
70             return itemCheckState.value( filePath( index ) );
71         else if( b_recursive )
72         {
73             for( int i = 0; i < monitoredDirs.count(); i++ )
74             {
75                 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
76                     return Qt::Checked;
77             }
78             return Qt::Unchecked;
79         }
80         else
81             return Qt::Unchecked;
82     }
83
84     return QDirModel::data( index, role );
85 }
86
87 bool MLDirModel::setData( const QModelIndex &index, const QVariant &value,
88                           int role )
89 {
90     QModelIndex idx;
91     QModelIndex topLeft, bottomRight; // index to signal they have changed
92     if( role == Qt::CheckStateRole )
93     {
94         if( value == Qt::Checked )
95         {
96             monitoredDirs << filePath( index );
97             itemCheckState[filePath( index )] = Qt::Checked;
98             /* We have to make his parents Qt::PartiallyChecked */
99             idx = index.parent();
100             while( idx != QModelIndex() )
101             {
102                 if( !( !b_recursive && monitoredDirs.contains( filePath(idx) ) ) )
103                     itemCheckState[filePath(idx)] = Qt::PartiallyChecked;
104                 topLeft = idx;
105                 idx = idx.parent();
106             }
107             /* We have to remove his children that are monitored if we are
108                in recursive mode */
109             if( b_recursive )
110             {
111                 for( int i = 0; i < monitoredDirs.count()-1; i++ )
112                 {
113                     if( monitoredDirs.at( i ).startsWith( filePath( index ) ) )
114                     {
115                         itemCheckState.take( monitoredDirs.at( i ) );
116                         monitoredDirs.removeAt( i );
117                         i--;
118                     }
119                 }
120             }
121         }
122         else if( monitoredDirs.removeOne( filePath( index ) ) )
123         {
124             itemCheckState.take( filePath( index ) );
125             /* We have to make his parent Qt::Unchecked
126                if index is his only child */
127             for( idx = index.parent(); idx != QModelIndex(); idx = idx.parent() )
128             {
129                 if( monitoredDirs.count() == 0 )
130                 {
131                     itemCheckState.take( filePath(idx) );
132                     topLeft = idx;
133                     continue;
134                 }
135                 for( int i = 0; i < monitoredDirs.count(); i++ )
136                 {
137                     if( monitoredDirs.at( i ).startsWith( filePath( idx ) ) )
138                         break;
139                     itemCheckState.take( filePath(idx) );
140                     topLeft = idx;
141                 }
142             }
143         }
144
145         emit dataChanged( topLeft, index );
146         return true;
147     }
148     return QDirModel::setData( index, value, role );
149 }
150
151 int MLDirModel::columnCount( const QModelIndex & ) const
152 {
153     return 1;
154 }
155
156 void MLDirModel::reset( bool _b_recursive, vlc_array_t *p_array )
157 {
158     b_recursive = _b_recursive;
159
160     itemCheckState.clear();
161     monitoredDirs.clear();
162     for( int i = 0; i < vlc_array_count( p_array ); i++ )
163     {
164         setData( index( qfu((char*)vlc_array_item_at_index(p_array, i)) ),
165                  Qt::Checked, Qt::CheckStateRole );
166     }
167     emit layoutChanged();
168 }
169
170 void MLDirModel::setRecursivity( bool _b_recursive )
171 {
172     /* If the selection becomes recursive, we may have to delete from
173        monitoredDirs some directories  */
174     if( !b_recursive && _b_recursive )
175     {
176         for( int i = 0; i < monitoredDirs.count(); i++ )
177         {
178             for( int j = i+1; j < monitoredDirs.count(); j++ )
179             {
180                 if( monitoredDirs.at( i ).startsWith( monitoredDirs.at( j ) ) )
181                 {
182                     setData( index( monitoredDirs.at( i ) ),
183                              Qt::Unchecked, Qt::CheckStateRole );
184                     i--;
185                 }
186                 else if( monitoredDirs.at( j ).startsWith( monitoredDirs.at( i ) ) )
187                     setData( index( monitoredDirs.at( j ) ),
188                              Qt::Unchecked, Qt::CheckStateRole );
189             }
190         }
191     }
192     b_recursive = _b_recursive;
193     emit layoutChanged();
194 }
195
196 /** **************************************************************************
197  * PREFERENCES DIALOG FOR THE MEDIA LIBRARY
198  *****************************************************************************/
199
200 MLConfDialog::MLConfDialog( QWidget *parent, intf_thread_t *_p_intf )
201             : QVLCDialog( parent, _p_intf ), p_intf( _p_intf )
202 {
203     p_monitored_dirs = NULL;
204
205     setWindowTitle( qtr( "Media library Preferences" ) );
206     setMinimumSize( 400, 300 );
207     setParent( parent, Qt::Window );
208     setWindowModality( Qt::NonModal );
209     resize( 550, 450 );
210
211     QGridLayout *main_layout = new QGridLayout( this );
212
213     /* Directories selection */
214     QStringList nameFilters;
215     model = new MLDirModel( nameFilters,
216                             QDir::Dirs | QDir::NoDotAndDotDot,
217                             QDir::Name, this );
218     QTreeView *tree = new QTreeView( this );
219     tree->setModel( model );
220
221     /* recursivity */
222     recursivity = new QCheckBox( qtr( "Subdirectory recursive scanning" ) );
223
224     synchronous = new QCheckBox( qtr( "Use safe transactions" ) );
225
226     /* Buttons */
227     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
228     QPushButton *save = new QPushButton( qtr( "&Save" ) );
229     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
230     QPushButton *reset = new QPushButton( qtr( "&Reset" ) );
231
232     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
233     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
234     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
235
236     main_layout->addWidget( tree, 0, 0 );
237     main_layout->addWidget( recursivity, 1, 0 );
238     main_layout->addWidget( synchronous, 2, 0 );
239     main_layout->addWidget( buttonsBox, 3, 0 );
240
241     p_ml = ml_Get( p_intf );
242     init();
243
244     BUTTONACT( save, save() );
245     BUTTONACT( cancel, cancel() );
246     BUTTONACT( reset, reset() );
247     CONNECT( recursivity, toggled( bool ), model, setRecursivity( bool ) );
248 }
249
250 void MLConfDialog::init()
251 {
252     bool b_recursive = var_CreateGetBool( p_ml, "ml-recursive-scan" );
253     recursivity->setChecked( b_recursive );
254
255     bool b_sync = var_CreateGetBool( p_ml, "ml-synchronous" );
256     synchronous->setChecked( b_sync );
257
258     if( p_monitored_dirs )
259         vlc_array_destroy( p_monitored_dirs );
260     p_monitored_dirs = vlc_array_new();
261     ml_Control( p_ml, ML_GET_MONITORED, p_monitored_dirs );
262
263     model->reset( b_recursive, p_monitored_dirs );
264 }
265
266 void MLConfDialog::save()
267 {
268     QStringList newDirs = model->monitoredDirs;
269     QStringList toDelete;
270
271     for( int i = 0; i < vlc_array_count( p_monitored_dirs ); i++ )
272     {
273         if( newDirs.removeAll(
274             qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i)) ) == 0 )
275         {
276             toDelete << qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i));
277         }
278     }
279
280     for( int i = 0; i < toDelete.count(); i++ )
281     {
282         ml_Control( p_ml, ML_DEL_MONITORED, qtu( toDelete.at( i ) ) );
283     }
284     for( int i = 0; i < newDirs.count(); i++ )
285     {
286         ml_Control( p_ml, ML_ADD_MONITORED, qtu( newDirs.at( i ) ) );
287     }
288
289     var_SetBool( p_ml, "ml-recursive-scan", recursivity->isChecked() );
290     var_SetBool( p_ml, "ml-synchronous", synchronous->isChecked() );
291
292     init();
293     hide();
294 }
295
296 void MLConfDialog::cancel()
297 {
298     init();
299     hide();
300 }
301
302 void MLConfDialog::reset()
303 {
304     init();
305 }
306
307 #endif
308