1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "pixmaps/codec.xpm"
27 #include <QApplication>
28 #include "playlist_model.hpp"
31 #include "pixmaps/type_unknown.xpm"
32 #include "pixmaps/type_afile.xpm"
33 #include "pixmaps/type_vfile.xpm"
34 #include "pixmaps/type_net.xpm"
35 #include "pixmaps/type_card.xpm"
36 #include "pixmaps/type_disc.xpm"
37 #include "pixmaps/type_cdda.xpm"
38 #include "pixmaps/type_directory.xpm"
39 #include "pixmaps/type_playlist.xpm"
40 #include "pixmaps/type_node.xpm"
42 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
44 static int PlaylistChanged( vlc_object_t *, const char *,
45 vlc_value_t, vlc_value_t, void * );
46 static int PlaylistNext( vlc_object_t *, const char *,
47 vlc_value_t, vlc_value_t, void * );
48 static int ItemChanged( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
50 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
51 vlc_value_t oval, vlc_value_t nval, void *param );
52 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
53 vlc_value_t oval, vlc_value_t nval, void *param );
55 /*************************************************************************
56 * Playlist item implementation
57 *************************************************************************/
66 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
69 i_id = _i_id; i_input_id = _i_input_id;
76 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
78 init( _i_id, _i_input_id, parent, m );
81 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
83 init( p_item->i_id, p_item->p_input->i_id, parent, m );
91 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
95 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
96 children.append( item );
98 model->endInsertRows();
101 int PLItem::row() const
104 return parentItem->children.indexOf(const_cast<PLItem*>(this));
108 void PLItem::update( playlist_item_t *p_item )
110 assert( p_item->p_input->i_id == i_input_id );
111 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
112 if( p_item->p_input->p_meta )
114 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
116 type = p_item->p_input->i_type;
119 /*************************************************************************
120 * Playlist model implementation
121 *************************************************************************/
123 PLModel::PLModel( playlist_t *_p_playlist,
124 playlist_item_t * p_root, int _i_depth, QObject *parent)
125 : QAbstractItemModel(parent)
128 assert( i_depth == 1 || i_depth == -1 );
129 p_playlist= _p_playlist;
130 i_items_to_append = 0;
131 b_need_update = false;
133 i_cached_input_id = -1;
135 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
136 ADD_ICON( UNKNOWN , unknown );
137 ADD_ICON( AFILE,afile );
138 ADD_ICON( VFILE, vfile );
139 ADD_ICON( DIRECTORY, directory );
140 ADD_ICON( DISC, disc );
141 ADD_ICON( CDDA, cdda );
142 ADD_ICON( CARD, card );
143 ADD_ICON( NET, net );
144 ADD_ICON( PLAYLIST, playlist );
145 ADD_ICON( NODE, node );
148 rebuildRoot( p_root );
153 void PLModel::rebuildRoot( playlist_item_t *p_root )
155 if( rootItem ) delete rootItem;
156 rootItem = new PLItem( p_root, NULL, this );
157 rootItem->strings[0] = qtr("Name");
158 rootItem->strings[1] = qtr("Artist");
167 void PLModel::addCallbacks()
169 /* Some global changes happened -> Rebuild all */
170 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
171 /* We went to the next item */
172 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
173 /* One item has been updated */
174 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
175 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
176 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
179 void PLModel::delCallbacks()
181 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
182 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
183 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
184 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
185 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
188 void PLModel::activateItem( const QModelIndex &index )
190 assert( index.isValid() );
191 PLItem *item = static_cast<PLItem*>(index.internalPointer());
194 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
195 playlist_item_t *p_parent = p_item;
198 if( p_parent->i_id == rootItem->i_id ) break;
199 p_parent = p_parent->p_parent;
203 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
208 /****************** Base model mandatory implementations *****************/
209 QVariant PLModel::data(const QModelIndex &index, int role) const
211 assert( index.isValid() );
212 PLItem *item = static_cast<PLItem*>(index.internalPointer());
213 if( role == Qt::DisplayRole )
215 return QVariant( item->columnString( index.column() ) );
217 else if( role == Qt::DecorationRole && index.column() == 0 )
219 if( item->type >= 0 )
220 return QVariant( PLModel::icons[item->type] );
225 int PLModel::itemId( const QModelIndex &index ) const
227 assert( index.isValid() );
228 return static_cast<PLItem*>(index.internalPointer())->i_id;
231 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
233 if( !index.isValid() ) return Qt::ItemIsEnabled;
234 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
237 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
240 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
241 return QVariant( rootItem->columnString( section ) );
245 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
249 if (!parent.isValid())
250 parentItem = rootItem;
252 parentItem = static_cast<PLItem*>(parent.internalPointer());
254 PLItem *childItem = parentItem->child(row);
256 return createIndex(row, column, childItem);
258 return QModelIndex();
261 /* Return the index of a given item */
262 QModelIndex PLModel::index( PLItem *item, int column ) const
264 if( !item ) return QModelIndex();
265 const PLItem *parent = item->parent();
267 return createIndex( parent->children.lastIndexOf( item ), column, item );
268 return QModelIndex();
271 QModelIndex PLModel::parent(const QModelIndex &index) const
273 if (!index.isValid()) return QModelIndex();
275 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
276 PLItem *parentItem = childItem->parent();
278 if (parentItem == rootItem) return QModelIndex();
280 return createIndex(parentItem->row(), 0, parentItem);
283 int PLModel::columnCount( const QModelIndex &i) const
285 if( i_depth == 1 ) return 1;
289 int PLModel::childrenCount(const QModelIndex &parent) const
291 return rowCount( parent );
294 int PLModel::rowCount(const QModelIndex &parent) const
298 if (!parent.isValid())
299 parentItem = rootItem;
301 parentItem = static_cast<PLItem*>(parent.internalPointer());
303 return parentItem->childCount();
306 /************************* Lookups *****************************/
308 PLItem *PLModel::FindById( PLItem *root, int i_id )
310 return FindInner( root, i_id, false );
313 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
315 return FindInner( root, i_id, true );
318 #define CACHE( i, p ) i_cached_id = i; p_cached_item = p;
319 #define ICACHE( i, p ) i_cached_input_id = i; p_cached_item_bi = p;
321 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
323 if( ( !b_input && i_cached_id == i_id) ||
324 ( b_input && i_cached_input_id ==i_id ) )
326 return b_input ? p_cached_item_bi : p_cached_item;
329 if( !b_input && root->i_id == i_id )
334 else if( b_input && root->i_input_id == i_id )
336 ICACHE( i_id, root );
340 QList<PLItem *>::iterator it = root->children.begin();
341 while ( it != root->children.end() )
343 if( !b_input && (*it)->i_id == i_id )
345 CACHE( i_id, (*it) );
346 return p_cached_item;
348 else if( b_input && (*it)->i_input_id == i_id )
350 ICACHE( i_id, (*it) );
351 return p_cached_item_bi;
353 if( (*it)->children.size() )
355 PLItem *childFound = FindInner( (*it), i_id, b_input );
360 ICACHE( i_id, childFound );
364 CACHE( i_id, childFound );
377 /************************* Updates handling *****************************/
378 void PLModel::customEvent( QEvent *event )
380 int type = event->type();
381 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
382 type != ItemDelete_Type )
385 PLEvent *ple = static_cast<PLEvent *>(event);
387 if( type == ItemUpdate_Type )
388 ProcessInputItemUpdate( ple->i_id );
389 else if( type == ItemAppend_Type )
390 ProcessItemAppend( ple->p_add );
392 ProcessItemRemoval( ple->i_id );
395 /**** Events processing ****/
396 void PLModel::ProcessInputItemUpdate( int i_input_id )
398 if( i_input_id <= 0 ) return;
399 PLItem *item = FindByInput( rootItem, i_input_id );
401 UpdateTreeItem( item, true );
404 void PLModel::ProcessItemRemoval( int i_id )
406 if( i_id <= 0 ) return;
407 if( i_id == i_cached_id ) i_cached_id = -1;
408 i_cached_input_id = -1;
413 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
415 playlist_item_t *p_item = NULL;
416 PLItem *newItem = NULL;
418 if( b_need_update ) return;
420 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
421 if( !nodeItem ) goto end;
423 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
424 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
425 if( i_depth == 1 && p_item->p_parent &&
426 p_item->p_parent->i_id != rootItem->i_id )
429 newItem = new PLItem( p_item, nodeItem, this );
430 nodeItem->appendChild( newItem );
431 UpdateTreeItem( p_item, newItem, true );
436 void PLModel::Rebuild()
438 /* Remove callbacks before locking to avoid deadlocks */
442 /* Invalidate cache */
443 i_cached_id = i_cached_input_id = -1;
446 qDeleteAll( rootItem->children );
448 /* Recreate from root */
449 UpdateNodeChildren( rootItem );
451 /* And signal the view */
452 emit layoutChanged();
458 void PLModel::UpdateNodeChildren( PLItem *root )
460 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
461 UpdateNodeChildren( p_node, root );
464 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
466 for( int i = 0; i < p_node->i_children ; i++ )
468 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
469 root->appendChild( newItem, false );
470 UpdateTreeItem( newItem, false, true );
471 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
472 UpdateNodeChildren( p_node->pp_children[i], newItem );
476 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
478 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
479 UpdateTreeItem( p_item, item, signal, force );
482 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
483 bool signal, bool force )
485 if( !force && i_depth == 1 && p_item->p_parent &&
486 p_item->p_parent->i_id != rootItem->i_id )
488 item->update( p_item );
494 /**********************************************************************
496 **********************************************************************/
497 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
498 vlc_value_t oval, vlc_value_t nval, void *param )
500 PLModel *p_model = (PLModel *) param;
501 p_model->b_need_update = VLC_TRUE;
505 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
506 vlc_value_t oval, vlc_value_t nval, void *param )
508 PLModel *p_model = (PLModel *) param;
509 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
510 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
511 event = new PLEvent( ItemUpdate_Type, nval.i_int );
512 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
516 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
517 vlc_value_t oval, vlc_value_t nval, void *param )
519 PLModel *p_model = (PLModel *) param;
520 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
521 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
525 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
526 vlc_value_t oval, vlc_value_t nval, void *param )
528 PLModel *p_model = (PLModel *) param;
529 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
530 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
534 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
535 vlc_value_t oval, vlc_value_t nval, void *param )
537 PLModel *p_model = (PLModel *) param;
538 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
539 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
541 if( ++p_model->i_items_to_append >= 50 )
543 p_model->b_need_update = VLC_TRUE;
546 PLEvent *event = new PLEvent( p_add );
547 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );