1 /*****************************************************************************
2 * ml_configuration.cpp: ML's configuration dialog (folder view)
3 *****************************************************************************
4 * Copyright (C) 2008-2010 the VideoLAN Team and AUTHORS
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>
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.
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.
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 *****************************************************************************/
33 #include "ml_configuration.hpp"
36 #include <QPushButton>
37 #include <QDialogButtonBox>
39 MLConfDialog *MLConfDialog::instance = NULL;
42 /** **************************************************************************
43 * MODEL FOR THE FILESYSTEM
44 *****************************************************************************/
46 Qt::ItemFlags MLDirModel::flags( const QModelIndex &index ) const
48 Qt::ItemFlags flags = QDirModel::flags( index );
49 flags |= Qt::ItemIsUserCheckable;
52 for( int i = 0; i < monitoredDirs.count(); i++ )
54 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
56 if( monitoredDirs.at( i ) != filePath( index ) )
57 flags ^= Qt::ItemIsEnabled;
65 QVariant MLDirModel::data( const QModelIndex &index, int role ) const
67 if( index.column() == 0 && role == Qt::CheckStateRole )
69 if( itemCheckState.contains( filePath( index ) ) )
70 return itemCheckState.value( filePath( index ) );
71 else if( b_recursive )
73 for( int i = 0; i < monitoredDirs.count(); i++ )
75 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
84 return QDirModel::data( index, role );
87 bool MLDirModel::setData( const QModelIndex &index, const QVariant &value,
91 QModelIndex topLeft, bottomRight; // index to signal they have changed
92 if( role == Qt::CheckStateRole )
94 if( value == Qt::Checked )
96 monitoredDirs << filePath( index );
97 itemCheckState[filePath( index )] = Qt::Checked;
98 /* We have to make his parents Qt::PartiallyChecked */
100 while( idx != QModelIndex() )
102 if( !( !b_recursive && monitoredDirs.contains( filePath(idx) ) ) )
103 itemCheckState[filePath(idx)] = Qt::PartiallyChecked;
107 /* We have to remove his children that are monitored if we are
111 for( int i = 0; i < monitoredDirs.count()-1; i++ )
113 if( monitoredDirs.at( i ).startsWith( filePath( index ) ) )
115 itemCheckState.take( monitoredDirs.at( i ) );
116 monitoredDirs.removeAt( i );
122 else if( monitoredDirs.removeOne( filePath( index ) ) )
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() )
129 if( monitoredDirs.count() == 0 )
131 itemCheckState.take( filePath(idx) );
135 for( int i = 0; i < monitoredDirs.count(); i++ )
137 if( monitoredDirs.at( i ).startsWith( filePath( idx ) ) )
139 itemCheckState.take( filePath(idx) );
145 emit dataChanged( topLeft, index );
148 return QDirModel::setData( index, value, role );
151 int MLDirModel::columnCount( const QModelIndex & ) const
156 void MLDirModel::reset( bool _b_recursive, vlc_array_t *p_array )
158 b_recursive = _b_recursive;
160 itemCheckState.clear();
161 monitoredDirs.clear();
162 for( int i = 0; i < vlc_array_count( p_array ); i++ )
164 setData( index( qfu((char*)vlc_array_item_at_index(p_array, i)) ),
165 Qt::Checked, Qt::CheckStateRole );
167 emit layoutChanged();
170 void MLDirModel::setRecursivity( bool _b_recursive )
172 /* If the selection becomes recursive, we may have to delete from
173 monitoredDirs some directories */
174 if( !b_recursive && _b_recursive )
176 for( int i = 0; i < monitoredDirs.count(); i++ )
178 for( int j = i+1; j < monitoredDirs.count(); j++ )
180 if( monitoredDirs.at( i ).startsWith( monitoredDirs.at( j ) ) )
182 setData( index( monitoredDirs.at( i ) ),
183 Qt::Unchecked, Qt::CheckStateRole );
186 else if( monitoredDirs.at( j ).startsWith( monitoredDirs.at( i ) ) )
187 setData( index( monitoredDirs.at( j ) ),
188 Qt::Unchecked, Qt::CheckStateRole );
192 b_recursive = _b_recursive;
193 emit layoutChanged();
196 /** **************************************************************************
197 * PREFERENCES DIALOG FOR THE MEDIA LIBRARY
198 *****************************************************************************/
200 MLConfDialog::MLConfDialog( QWidget *parent, intf_thread_t *_p_intf )
201 : QVLCDialog( parent, _p_intf ), p_intf( _p_intf )
203 p_monitored_dirs = NULL;
205 setWindowTitle( qtr( "Media library Preferences" ) );
206 setMinimumSize( 400, 300 );
207 setParent( parent, Qt::Window );
208 setWindowModality( Qt::NonModal );
211 QGridLayout *main_layout = new QGridLayout( this );
213 /* Directories selection */
214 QStringList nameFilters;
215 model = new MLDirModel( nameFilters,
216 QDir::Dirs | QDir::NoDotAndDotDot,
218 QTreeView *tree = new QTreeView( this );
219 tree->setModel( model );
222 recursivity = new QCheckBox( qtr( "Subdirectory recursive scanning" ) );
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" ) );
230 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
231 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
232 buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
234 main_layout->addWidget( tree, 0, 0 );
235 main_layout->addWidget( recursivity, 1, 0 );
236 main_layout->addWidget( buttonsBox, 2, 0 );
238 p_ml = ml_Get( p_intf );
241 BUTTONACT( save, save() );
242 BUTTONACT( cancel, cancel() );
243 BUTTONACT( reset, reset() );
244 CONNECT( recursivity, toggled( bool ), model, setRecursivity( bool ) );
247 void MLConfDialog::init()
249 bool b_recursive = var_CreateGetBool( p_ml, "ml-recursive-scan" );
250 recursivity->setChecked( b_recursive );
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 );
257 model->reset( b_recursive, p_monitored_dirs );
260 void MLConfDialog::save()
262 QStringList newDirs = model->monitoredDirs;
263 QStringList toDelete;
265 for( int i = 0; i < vlc_array_count( p_monitored_dirs ); i++ )
267 if( newDirs.removeAll(
268 qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i)) ) == 0 )
270 toDelete << qfu((char*)vlc_array_item_at_index(p_monitored_dirs, i));
274 for( int i = 0; i < toDelete.count(); i++ )
276 ml_Control( p_ml, ML_DEL_MONITORED, qtu( toDelete.at( i ) ) );
278 for( int i = 0; i < newDirs.count(); i++ )
280 ml_Control( p_ml, ML_ADD_MONITORED, qtu( newDirs.at( i ) ) );
283 var_SetBool( p_ml, "ml-recursive-scan", recursivity->isChecked() );
289 void MLConfDialog::cancel()
295 void MLConfDialog::reset()