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 *****************************************************************************/
27 #include "ml_configuration.hpp"
31 #include <QPushButton>
32 #include <QDialogButtonBox>
34 MLConfDialog *MLConfDialog::instance = NULL;
37 /** **************************************************************************
38 * MODEL FOR THE FILESYSTEM
39 *****************************************************************************/
41 Qt::ItemFlags MLDirModel::flags( const QModelIndex &index ) const
43 Qt::ItemFlags flags = QDirModel::flags( index );
44 flags |= Qt::ItemIsUserCheckable;
47 for( int i = 0; i < monitoredDirs.size(); i++ )
49 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
51 if( monitoredDirs.at( i ) != filePath( index ) )
52 flags ^= Qt::ItemIsEnabled;
60 QVariant MLDirModel::data( const QModelIndex &index, int role ) const
62 if( index.column() == 0 && role == Qt::CheckStateRole )
64 if( itemCheckState.contains( filePath( index ) ) )
65 return itemCheckState.value( filePath( index ) );
66 else if( b_recursive )
68 for( int i = 0; i < monitoredDirs.size(); i++ )
70 if( filePath( index ).startsWith( monitoredDirs.at( i ) ) )
79 return QDirModel::data( index, role );
82 bool MLDirModel::setData( const QModelIndex &index, const QVariant &value,
86 QModelIndex topLeft, bottomRight; // index to signal they have changed
87 if( role == Qt::CheckStateRole )
89 if( value == Qt::Checked )
91 monitoredDirs << filePath( index );
92 itemCheckState[filePath( index )] = Qt::Checked;
93 /* We have to make his parents Qt::PartiallyChecked */
95 while( idx != QModelIndex() )
97 if( !( !b_recursive && monitoredDirs.contains( filePath(idx) ) ) )
98 itemCheckState[filePath(idx)] = Qt::PartiallyChecked;
102 /* We have to remove his children that are monitored if we are
106 for( int i = 0; i < monitoredDirs.size()-1; i++ )
108 if( monitoredDirs.at( i ).startsWith( filePath( index ) ) )
110 itemCheckState.take( monitoredDirs.at( i ) );
111 monitoredDirs.removeAt( i );
117 else if( monitoredDirs.removeOne( filePath( index ) ) )
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() )
124 if( monitoredDirs.size() == 0 )
126 itemCheckState.take( filePath(idx) );
130 for( int i = 0; i < monitoredDirs.size(); i++ )
132 if( monitoredDirs.at( i ).startsWith( filePath( idx ) ) )
134 itemCheckState.take( filePath(idx) );
140 emit dataChanged( topLeft, index );
143 return QDirModel::setData( index, value, role );
146 int MLDirModel::columnCount( const QModelIndex &parent ) const
151 void MLDirModel::reset( bool _b_recursive, vlc_array_t *p_array )
153 b_recursive = _b_recursive;
155 itemCheckState.clear();
156 monitoredDirs.clear();
157 for( int i = 0; i < vlc_array_count( p_array ); i++ )
159 setData( index( qfu((char*)vlc_array_item_at_index(p_array, i)) ),
160 Qt::Checked, Qt::CheckStateRole );
162 emit layoutChanged();
165 void MLDirModel::setRecursivity( bool _b_recursive )
167 /* If the selection becomes recursive, we may have to delete from
168 monitoredDirs some directories */
169 if( !b_recursive && _b_recursive )
171 for( int i = 0; i < monitoredDirs.size(); i++ )
173 for( int j = i+1; j < monitoredDirs.size(); j++ )
175 if( monitoredDirs.at( i ).startsWith( monitoredDirs.at( j ) ) )
177 setData( index( monitoredDirs.at( i ) ),
178 Qt::Unchecked, Qt::CheckStateRole );
181 else if( monitoredDirs.at( j ).startsWith( monitoredDirs.at( i ) ) )
182 setData( index( monitoredDirs.at( j ) ),
183 Qt::Unchecked, Qt::CheckStateRole );
187 b_recursive = _b_recursive;
188 emit layoutChanged();
191 /** **************************************************************************
192 * PREFERENCES DIALOG FOR THE MEDIA LIBRARY
193 *****************************************************************************/
195 MLConfDialog::MLConfDialog( QWidget *parent, intf_thread_t *_p_intf )
196 : QVLCDialog( parent, _p_intf ), p_intf( _p_intf )
198 p_monitored_dirs = NULL;
200 setWindowTitle( qtr( "Media library Preferences" ) );
201 setMinimumSize( 400, 300 );
202 setParent( parent, Qt::Window );
203 setWindowModality( Qt::NonModal );
206 QGridLayout *main_layout = new QGridLayout( this );
208 /* Directories selection */
209 QStringList nameFilters;
210 model = new MLDirModel( nameFilters,
211 QDir::Dirs | QDir::NoDotAndDotDot,
213 QTreeView *tree = new QTreeView( this );
214 tree->setModel( model );
217 recursivity = new QCheckBox( qtr( "Subdirectory recursive scanning" ) );
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" ) );
225 buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
226 buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
227 buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
229 main_layout->addWidget( tree, 0, 0 );
230 main_layout->addWidget( recursivity, 1, 0 );
231 main_layout->addWidget( buttonsBox, 2, 0 );
233 p_ml = ml_Hold( p_intf );
236 BUTTONACT( save, save() );
237 BUTTONACT( cancel, cancel() );
238 BUTTONACT( reset, reset() );
239 CONNECT( recursivity, toggled( bool ), model, setRecursivity( bool ) );
242 MLConfDialog::~MLConfDialog()
244 ml_Release( p_intf );
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.size(); i++ )
276 ml_Control( p_ml, ML_DEL_MONITORED, qtu( toDelete.at( i ) ) );
278 for( int i = 0; i < newDirs.size(); 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()