]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/ml_configuration.cpp
Qt/ML: Configuration dialog for Media Library Directory Monitoring
[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 #include "ml_configuration.hpp"
28 #ifdef MEDIA_LIBRARY
29
30 #include <QTreeView>
31 #include <QPushButton>
32 #include <QDialogButtonBox>
33
34 MLConfDialog *MLConfDialog::instance = NULL;
35
36
37 /** **************************************************************************
38  * MODEL FOR THE FILESYSTEM
39  *****************************************************************************/
40
41 Qt::ItemFlags MLDirModel::flags( const QModelIndex &index ) const
42 {
43     Qt::ItemFlags flags = QDirModel::flags( index );
44     flags |= Qt::ItemIsUserCheckable;
45     if( b_recursive )
46     {
47         for( int i = 0; i < monitoredDirs.size(); i++ )
48         {
49             if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
50             {
51                 if( monitoredDirs.at( i ) != filePath( index ) )
52                     flags ^= Qt::ItemIsEnabled;
53                 break;
54             }
55         }
56     }
57     return flags;
58 }
59
60 QVariant MLDirModel::data( const QModelIndex &index, int role ) const
61 {
62     if( index.column() == 0 && role == Qt::CheckStateRole )
63     {
64         if( itemCheckState.contains( filePath( index ) ) )
65             return itemCheckState.value( filePath( index ) );
66         else if( b_recursive )
67         {
68             for( int i = 0; i < monitoredDirs.size(); i++ )
69             {
70                 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
71                     return Qt::Checked;
72             }
73             return Qt::Unchecked;
74         }
75         else
76             return Qt::Unchecked;
77     }
78
79     return QDirModel::data( index, role );
80 }
81
82 bool MLDirModel::setData( const QModelIndex &index, const QVariant &value,
83                           int role )
84 {
85     QModelIndex idx;
86     QModelIndex topLeft, bottomRight; // index to signal they have changed
87     if( role == Qt::CheckStateRole )
88     {
89         if( value == Qt::Checked )
90         {
91             monitoredDirs << filePath( index );
92             itemCheckState[filePath( index )] = Qt::Checked;
93             /* We have to make his parents Qt::PartiallyChecked */
94             idx = index.parent();
95             while( idx != QModelIndex() )
96             {
97                 if( !( !b_recursive && monitoredDirs.contains( filePath(idx) ) ) )
98                     itemCheckState[filePath(idx)] = Qt::PartiallyChecked;
99                 topLeft = idx;
100                 idx = idx.parent();
101             }
102             /* We have to remove his children that are monitored if we are
103                in recursive mode */
104             if( b_recursive )
105             {
106                 for( int i = 0; i < monitoredDirs.size()-1; i++ )
107                 {
108                     if( monitoredDirs.at( i ).startsWith( filePath( index ) ) )
109                     {
110                         itemCheckState.take( monitoredDirs.at( i ) );
111                         monitoredDirs.removeAt( i );
112                         i--;
113                     }
114                 }
115             }
116         }
117         else if( monitoredDirs.removeOne( filePath( index ) ) )
118         {
119             itemCheckState.take( filePath( index ) );
120             /* We have to make his parent Qt::Unchecked
121                if index is his only child */
122             for( idx = index.parent(); idx != QModelIndex(); idx = idx.parent() )
123             {
124                 if( monitoredDirs.size() == 0 )
125                 {
126                     itemCheckState.take( filePath(idx) );
127                     topLeft = idx;
128                     continue;
129                 }
130                 for( int i = 0; i < monitoredDirs.size(); i++ )
131                 {
132                     if( monitoredDirs.at( i ).startsWith( filePath( idx ) ) )
133                         break;
134                     itemCheckState.take( filePath(idx) );
135                     topLeft = idx;
136                 }
137             }
138         }
139
140         emit dataChanged( topLeft, index );
141         return true;
142     }
143     return QDirModel::setData( index, value, role );
144 }
145
146 int MLDirModel::columnCount( const QModelIndex &parent ) const
147 {
148     return 1;
149 }
150
151 void MLDirModel::reset( bool _b_recursive, vlc_array_t *p_array )
152 {
153     b_recursive = _b_recursive;
154
155     itemCheckState.clear();
156     monitoredDirs.clear();
157     for( int i = 0; i < vlc_array_count( p_array ); i++ )
158     {
159         setData( index( qfu((char*)vlc_array_item_at_index(p_array, i)) ),
160                  Qt::Checked, Qt::CheckStateRole );
161     }
162     emit layoutChanged();
163 }
164
165 void MLDirModel::setRecursivity( bool _b_recursive )
166 {
167     /* If the selection becomes recursive, we may have to delete from
168        monitoredDirs some directories  */
169     if( !b_recursive && _b_recursive )
170     {
171         for( int i = 0; i < monitoredDirs.size(); i++ )
172         {
173             for( int j = i+1; j < monitoredDirs.size(); j++ )
174             {
175                 if( monitoredDirs.at( i ).startsWith( monitoredDirs.at( j ) ) )
176                 {
177                     setData( index( monitoredDirs.at( i ) ),
178                              Qt::Unchecked, Qt::CheckStateRole );
179                     i--;
180                 }
181                 else if( monitoredDirs.at( j ).startsWith( monitoredDirs.at( i ) ) )
182                     setData( index( monitoredDirs.at( j ) ),
183                              Qt::Unchecked, Qt::CheckStateRole );
184             }
185         }
186     }
187     b_recursive = _b_recursive;
188     emit layoutChanged();
189 }
190
191 /** **************************************************************************
192  * PREFERENCES DIALOG FOR THE MEDIA LIBRARY
193  *****************************************************************************/
194
195 MLConfDialog::MLConfDialog( QWidget *parent, intf_thread_t *_p_intf )
196             : QVLCDialog( parent, _p_intf ), p_intf( _p_intf )
197 {
198     p_monitored_dirs = NULL;
199
200     setWindowTitle( qtr( "Media library Preferences" ) );
201     setMinimumSize( 400, 300 );
202     setParent( parent, Qt::Window );
203     setWindowModality( Qt::NonModal );
204     resize( 550, 450 );
205
206     QGridLayout *main_layout = new QGridLayout( this );
207
208     /* Directories selection */
209     QStringList nameFilters;
210     model = new MLDirModel( nameFilters,
211                             QDir::Dirs | QDir::NoDotAndDotDot,
212                             QDir::Name, this );
213     QTreeView *tree = new QTreeView( this );
214     tree->setModel( model );
215
216     /* recursivity */
217     recursivity = new QCheckBox( qtr( "Subdirectory recursive scanning" ) );
218
219     /* Buttons */
220     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
221     QPushButton *save = new QPushButton( qtr( "&Save" ) );
222     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
223     QPushButton *reset = new QPushButton( qtr( "&Reset" ) );
224
225     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
226     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
227     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
228
229     main_layout->addWidget( tree, 0, 0 );
230     main_layout->addWidget( recursivity, 1, 0 );
231     main_layout->addWidget( buttonsBox, 2, 0 );
232
233     p_ml = ml_Hold( p_intf );
234     init();
235
236     BUTTONACT( save, save() );
237     BUTTONACT( cancel, cancel() );
238     BUTTONACT( reset, reset() );
239     CONNECT( recursivity, toggled( bool ), model, setRecursivity( bool ) );
240 }
241
242 MLConfDialog::~MLConfDialog()
243 {
244     ml_Release( p_intf );
245 }
246
247 void MLConfDialog::init()
248 {
249     bool b_recursive = var_CreateGetBool( p_ml, "ml-recursive-scan" );
250     recursivity->setChecked( b_recursive );
251
252     if( p_monitored_dirs )
253         vlc_array_destroy( p_monitored_dirs );
254     p_monitored_dirs = vlc_array_new();
255     ml_Control( p_ml, ML_GET_MONITORED, p_monitored_dirs );
256
257     model->reset( b_recursive, p_monitored_dirs );
258 }
259
260 void MLConfDialog::save()
261 {
262     QStringList newDirs = model->monitoredDirs;
263     QStringList toDelete;
264
265     for( int i = 0; i < vlc_array_count( p_monitored_dirs ); i++ )
266     {
267         if( newDirs.removeAll(
268             qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i)) ) == 0 )
269         {
270             toDelete << qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i));
271         }
272     }
273
274     for( int i = 0; i < toDelete.size(); i++ )
275     {
276         ml_Control( p_ml, ML_DEL_MONITORED, qtu( toDelete.at( i ) ) );
277     }
278     for( int i = 0; i < newDirs.size(); i++ )
279     {
280         ml_Control( p_ml, ML_ADD_MONITORED, qtu( newDirs.at( i ) ) );
281     }
282
283     var_SetBool( p_ml, "ml-recursive-scan", recursivity->isChecked() );
284
285     init();
286     hide();
287 }
288
289 void MLConfDialog::cancel()
290 {
291     init();
292     hide();
293 }
294
295 void MLConfDialog::reset()
296 {
297     init();
298 }
299
300 #endif
301