]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/ml_configuration.cpp
191f4707a17f9077d91f26a4ee337f405910d8b9
[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.size(); 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.size(); 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.size()-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.size() == 0 )
130                 {
131                     itemCheckState.take( filePath(idx) );
132                     topLeft = idx;
133                     continue;
134                 }
135                 for( int i = 0; i < monitoredDirs.size(); 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.size(); i++ )
177         {
178             for( int j = i+1; j < monitoredDirs.size(); 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     /* Buttons */
225     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
226     QPushButton *save = new QPushButton( qtr( "&Save" ) );
227     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
228     QPushButton *reset = new QPushButton( qtr( "&Reset" ) );
229
230     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
231     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
232     buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
233
234     main_layout->addWidget( tree, 0, 0 );
235     main_layout->addWidget( recursivity, 1, 0 );
236     main_layout->addWidget( buttonsBox, 2, 0 );
237
238     p_ml = ml_Get( p_intf );
239     init();
240
241     BUTTONACT( save, save() );
242     BUTTONACT( cancel, cancel() );
243     BUTTONACT( reset, reset() );
244     CONNECT( recursivity, toggled( bool ), model, setRecursivity( bool ) );
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