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 *****************************************************************************/
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 void PLItem::remove( PLItem *removed )
103 assert( model && parentItem );
104 int i_index = parentItem->children.indexOf( removed );
105 model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
106 parentItem->children.removeAt( i_index );
107 model->endRemoveRows();
110 int PLItem::row() const
113 return parentItem->children.indexOf(const_cast<PLItem*>(this));
117 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
119 assert( p_item->p_input->i_id == i_input_id );
120 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
121 if( p_item->p_input->p_meta )
123 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
125 type = p_item->p_input->i_type;
129 /*************************************************************************
130 * Playlist model implementation
131 *************************************************************************/
133 PLModel::PLModel( playlist_t *_p_playlist,
134 playlist_item_t * p_root, int _i_depth, QObject *parent)
135 : QAbstractItemModel(parent)
138 assert( i_depth == 1 || i_depth == -1 );
139 p_playlist= _p_playlist;
140 i_items_to_append = 0;
141 b_need_update = false;
143 i_cached_input_id = -1;
145 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
146 ADD_ICON( UNKNOWN , unknown );
147 ADD_ICON( AFILE,afile );
148 ADD_ICON( VFILE, vfile );
149 ADD_ICON( DIRECTORY, directory );
150 ADD_ICON( DISC, disc );
151 ADD_ICON( CDDA, cdda );
152 ADD_ICON( CARD, card );
153 ADD_ICON( NET, net );
154 ADD_ICON( PLAYLIST, playlist );
155 ADD_ICON( NODE, node );
158 rebuildRoot( p_root );
163 void PLModel::rebuildRoot( playlist_item_t *p_root )
165 if( rootItem ) delete rootItem;
166 rootItem = new PLItem( p_root, NULL, this );
167 rootItem->strings[0] = qtr("Name");
168 rootItem->strings[1] = qtr("Artist");
177 void PLModel::addCallbacks()
179 /* Some global changes happened -> Rebuild all */
180 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
181 /* We went to the next item */
182 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
183 /* One item has been updated */
184 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
185 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
186 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
189 void PLModel::delCallbacks()
191 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
192 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
193 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
194 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
195 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
198 void PLModel::activateItem( const QModelIndex &index )
200 assert( index.isValid() );
201 PLItem *item = static_cast<PLItem*>(index.internalPointer());
204 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
205 playlist_item_t *p_parent = p_item;
208 if( p_parent->i_id == rootItem->i_id ) break;
209 p_parent = p_parent->p_parent;
213 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
218 /****************** Base model mandatory implementations *****************/
219 QVariant PLModel::data(const QModelIndex &index, int role) const
221 assert( index.isValid() );
222 PLItem *item = static_cast<PLItem*>(index.internalPointer());
223 if( role == Qt::DisplayRole )
225 return QVariant( item->columnString( index.column() ) );
227 else if( role == Qt::DecorationRole && index.column() == 0 )
229 if( item->type >= 0 )
230 return QVariant( PLModel::icons[item->type] );
232 else if( role == Qt::FontRole )
234 if( item->current == true )
236 QFont f; f.setBold( true ); return QVariant( f );
242 bool PLModel::isCurrent( const QModelIndex &index )
244 assert( index.isValid() );
245 return static_cast<PLItem*>(index.internalPointer())->current;
248 int PLModel::itemId( const QModelIndex &index ) const
250 assert( index.isValid() );
251 return static_cast<PLItem*>(index.internalPointer())->i_id;
254 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
256 if( !index.isValid() ) return Qt::ItemIsEnabled;
257 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
260 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
263 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
264 return QVariant( rootItem->columnString( section ) );
268 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
272 if (!parent.isValid())
273 parentItem = rootItem;
275 parentItem = static_cast<PLItem*>(parent.internalPointer());
277 PLItem *childItem = parentItem->child(row);
279 return createIndex(row, column, childItem);
281 return QModelIndex();
284 /* Return the index of a given item */
285 QModelIndex PLModel::index( PLItem *item, int column ) const
287 if( !item ) return QModelIndex();
288 const PLItem *parent = item->parent();
290 return createIndex( parent->children.lastIndexOf( item ), column, item );
291 return QModelIndex();
294 QModelIndex PLModel::parent(const QModelIndex &index) const
296 if( !index.isValid() ) return QModelIndex();
298 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
299 PLItem *parentItem = childItem->parent();
301 if (parentItem == rootItem) return QModelIndex();
302 return createIndex(parentItem->row(), 0, parentItem);
305 int PLModel::columnCount( const QModelIndex &i) const
307 if( i_depth == 1 ) return 1;
311 int PLModel::childrenCount(const QModelIndex &parent) const
313 return rowCount( parent );
316 int PLModel::rowCount(const QModelIndex &parent) const
320 if (!parent.isValid())
321 parentItem = rootItem;
323 parentItem = static_cast<PLItem*>(parent.internalPointer());
325 return parentItem->childCount();
328 /************************* Lookups *****************************/
330 PLItem *PLModel::FindById( PLItem *root, int i_id )
332 return FindInner( root, i_id, false );
335 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
337 return FindInner( root, i_id, true );
340 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
341 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
343 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
345 if( ( !b_input && i_cached_id == i_id) ||
346 ( b_input && i_cached_input_id ==i_id ) )
348 return b_input ? p_cached_item_bi : p_cached_item;
351 if( !b_input && root->i_id == i_id )
356 else if( b_input && root->i_input_id == i_id )
358 ICACHE( i_id, root );
362 QList<PLItem *>::iterator it = root->children.begin();
363 while ( it != root->children.end() )
365 if( !b_input && (*it)->i_id == i_id )
367 CACHE( i_id, (*it) );
368 return p_cached_item;
370 else if( b_input && (*it)->i_input_id == i_id )
372 ICACHE( i_id, (*it) );
373 return p_cached_item_bi;
375 if( (*it)->children.size() )
377 PLItem *childFound = FindInner( (*it), i_id, b_input );
381 ICACHE( i_id, childFound )
383 CACHE( i_id, childFound )
395 /************************* Updates handling *****************************/
396 void PLModel::customEvent( QEvent *event )
398 int type = event->type();
399 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
400 type != ItemDelete_Type )
403 PLEvent *ple = static_cast<PLEvent *>(event);
405 if( type == ItemUpdate_Type )
406 ProcessInputItemUpdate( ple->i_id );
407 else if( type == ItemAppend_Type )
408 ProcessItemAppend( ple->p_add );
410 ProcessItemRemoval( ple->i_id );
413 /**** Events processing ****/
414 void PLModel::ProcessInputItemUpdate( int i_input_id )
416 if( i_input_id <= 0 ) return;
417 PLItem *item = FindByInput( rootItem, i_input_id );
419 UpdateTreeItem( item, true );
422 void PLModel::ProcessItemRemoval( int i_id )
424 if( i_id <= 0 ) return;
425 if( i_id == i_cached_id ) i_cached_id = -1;
426 i_cached_input_id = -1;
428 PLItem *item = FindById( rootItem, i_id );
430 item->remove( item );
433 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
435 playlist_item_t *p_item = NULL;
436 PLItem *newItem = NULL;
438 if( b_need_update ) return;
440 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
441 if( !nodeItem ) goto end;
444 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
445 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
446 if( i_depth == 1 && p_item->p_parent &&
447 p_item->p_parent->i_id != rootItem->i_id )
450 newItem = new PLItem( p_item, nodeItem, this );
451 nodeItem->appendChild( newItem );
452 UpdateTreeItem( p_item, newItem, true );
458 void PLModel::Rebuild()
460 /* Remove callbacks before locking to avoid deadlocks */
462 /* Invalidate cache */
463 i_cached_id = i_cached_input_id = -1;
467 qDeleteAll( rootItem->children );
468 /* Recreate from root */
469 UpdateNodeChildren( rootItem );
472 /* And signal the view */
473 emit layoutChanged();
474 /// \todo Force current item to be updated
479 /* This function must be entered WITH the playlist lock */
480 void PLModel::UpdateNodeChildren( PLItem *root )
482 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
483 UpdateNodeChildren( p_node, root );
486 /* This function must be entered WITH the playlist lock */
487 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
489 for( int i = 0; i < p_node->i_children ; i++ )
491 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
492 root->appendChild( newItem, false );
493 UpdateTreeItem( newItem, false, true );
494 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
495 UpdateNodeChildren( p_node->pp_children[i], newItem );
499 /* This function must be entered WITH the playlist lock */
500 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
502 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
503 UpdateTreeItem( p_item, item, signal, force );
506 /* This function must be entered WITH the playlist lock */
507 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
508 bool signal, bool force )
510 if( !force && i_depth == 1 && p_item->p_parent &&
511 p_item->p_parent->i_id != rootItem->i_id )
513 item->update( p_item, p_item == p_playlist->status.p_item );
515 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
518 /**********************************************************************
520 **********************************************************************/
521 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
522 vlc_value_t oval, vlc_value_t nval, void *param )
524 PLModel *p_model = (PLModel *) param;
525 p_model->b_need_update = VLC_TRUE;
529 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
530 vlc_value_t oval, vlc_value_t nval, void *param )
532 PLModel *p_model = (PLModel *) param;
533 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
534 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
535 event = new PLEvent( ItemUpdate_Type, nval.i_int );
536 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
540 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
541 vlc_value_t oval, vlc_value_t nval, void *param )
543 PLModel *p_model = (PLModel *) param;
544 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
545 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
549 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
550 vlc_value_t oval, vlc_value_t nval, void *param )
552 PLModel *p_model = (PLModel *) param;
553 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
554 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
558 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
559 vlc_value_t oval, vlc_value_t nval, void *param )
561 PLModel *p_model = (PLModel *) param;
562 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
563 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
565 if( ++p_model->i_items_to_append >= 50 )
567 p_model->b_need_update = VLC_TRUE;
570 PLEvent *event = new PLEvent( p_add );
571 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );