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 /************************* General playlist status ***********************/
330 bool PLModel::hasRandom()
332 if( var_GetBool( p_playlist, "random" ) ) return true;
335 bool PLModel::hasRepeat()
337 if( var_GetBool( p_playlist, "repeat" ) ) return true;
340 bool PLModel::hasLoop()
342 if( var_GetBool( p_playlist, "loop" ) ) return true;
345 void PLModel::setLoop( bool on )
347 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
349 void PLModel::setRepeat( bool on )
351 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
353 void PLModel::setRandom( bool on )
355 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
358 /************************* Lookups *****************************/
360 PLItem *PLModel::FindById( PLItem *root, int i_id )
362 return FindInner( root, i_id, false );
365 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
367 return FindInner( root, i_id, true );
370 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
371 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
373 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
375 if( ( !b_input && i_cached_id == i_id) ||
376 ( b_input && i_cached_input_id ==i_id ) )
378 return b_input ? p_cached_item_bi : p_cached_item;
381 if( !b_input && root->i_id == i_id )
386 else if( b_input && root->i_input_id == i_id )
388 ICACHE( i_id, root );
392 QList<PLItem *>::iterator it = root->children.begin();
393 while ( it != root->children.end() )
395 if( !b_input && (*it)->i_id == i_id )
397 CACHE( i_id, (*it) );
398 return p_cached_item;
400 else if( b_input && (*it)->i_input_id == i_id )
402 ICACHE( i_id, (*it) );
403 return p_cached_item_bi;
405 if( (*it)->children.size() )
407 PLItem *childFound = FindInner( (*it), i_id, b_input );
411 ICACHE( i_id, childFound )
413 CACHE( i_id, childFound )
425 /************************* Updates handling *****************************/
426 void PLModel::customEvent( QEvent *event )
428 int type = event->type();
429 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
430 type != ItemDelete_Type )
433 PLEvent *ple = static_cast<PLEvent *>(event);
435 if( type == ItemUpdate_Type )
436 ProcessInputItemUpdate( ple->i_id );
437 else if( type == ItemAppend_Type )
438 ProcessItemAppend( ple->p_add );
440 ProcessItemRemoval( ple->i_id );
443 /**** Events processing ****/
444 void PLModel::ProcessInputItemUpdate( int i_input_id )
446 if( i_input_id <= 0 ) return;
447 PLItem *item = FindByInput( rootItem, i_input_id );
449 UpdateTreeItem( item, true );
452 void PLModel::ProcessItemRemoval( int i_id )
454 if( i_id <= 0 ) return;
455 if( i_id == i_cached_id ) i_cached_id = -1;
456 i_cached_input_id = -1;
458 PLItem *item = FindById( rootItem, i_id );
460 item->remove( item );
463 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
465 playlist_item_t *p_item = NULL;
466 PLItem *newItem = NULL;
468 if( b_need_update ) return;
470 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
471 if( !nodeItem ) goto end;
474 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
475 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
476 if( i_depth == 1 && p_item->p_parent &&
477 p_item->p_parent->i_id != rootItem->i_id )
480 newItem = new PLItem( p_item, nodeItem, this );
481 nodeItem->appendChild( newItem );
482 UpdateTreeItem( p_item, newItem, true );
488 void PLModel::Rebuild()
490 /* Remove callbacks before locking to avoid deadlocks */
492 /* Invalidate cache */
493 i_cached_id = i_cached_input_id = -1;
497 qDeleteAll( rootItem->children );
498 /* Recreate from root */
499 UpdateNodeChildren( rootItem );
500 if( p_playlist->status.p_item )
502 fprintf( stderr, "Playlist is playing" );
503 PLItem *currentItem = FindByInput( rootItem,
504 p_playlist->status.p_item->p_input->i_id );
507 fprintf( stderr, "Updating item\n" );
508 UpdateTreeItem( p_playlist->status.p_item, currentItem,
514 /* And signal the view */
515 emit layoutChanged();
519 /* This function must be entered WITH the playlist lock */
520 void PLModel::UpdateNodeChildren( PLItem *root )
522 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
523 UpdateNodeChildren( p_node, root );
526 /* This function must be entered WITH the playlist lock */
527 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
529 for( int i = 0; i < p_node->i_children ; i++ )
531 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
532 root->appendChild( newItem, false );
533 UpdateTreeItem( newItem, false, true );
534 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
535 UpdateNodeChildren( p_node->pp_children[i], newItem );
539 /* This function must be entered WITH the playlist lock */
540 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
542 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
543 UpdateTreeItem( p_item, item, signal, force );
546 /* This function must be entered WITH the playlist lock */
547 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
548 bool signal, bool force )
550 if( !force && i_depth == 1 && p_item->p_parent &&
551 p_item->p_parent->i_id != rootItem->i_id )
553 item->update( p_item, p_item == p_playlist->status.p_item );
555 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
558 /************************* Actions ******************************/
561 * Deletion, here we have to do a ugly slow hack as we retrieve the full
562 * list of indexes to delete at once: when we delete a node and all of
563 * its children, we need to update the list.
564 * Todo: investigate whethere we can use ranges to be sure to delete all items?
566 void PLModel::doDelete( QModelIndexList selected )
568 for( int i = selected.size() -1 ; i >= 0; i-- )
570 QModelIndex index = selected[i];
571 if( index.column() != 0 ) continue;
572 PLItem *item = static_cast<PLItem*>(index.internalPointer());
575 if( item->children.size() )
576 recurseDelete( item->children, &selected );
577 doDeleteItem( item, &selected );
582 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
584 for( int i = children.size() - 1; i >= 0 ; i-- )
586 PLItem *item = children[i];
587 if( item->children.size() )
588 recurseDelete( item->children, fullList );
589 doDeleteItem( item, fullList );
593 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
595 QModelIndex deleteIndex = index( item, 0 );
596 fullList->removeAll( deleteIndex );
599 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
604 if( p_item->i_children == -1 )
605 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
607 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
608 /* And finally, remove it from the tree */
609 item->remove( item );
613 /**********************************************************************
615 **********************************************************************/
616 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
617 vlc_value_t oval, vlc_value_t nval, void *param )
619 PLModel *p_model = (PLModel *) param;
620 p_model->b_need_update = VLC_TRUE;
624 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
625 vlc_value_t oval, vlc_value_t nval, void *param )
627 PLModel *p_model = (PLModel *) param;
628 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
629 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
630 event = new PLEvent( ItemUpdate_Type, nval.i_int );
631 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
635 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
636 vlc_value_t oval, vlc_value_t nval, void *param )
638 PLModel *p_model = (PLModel *) param;
639 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
640 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
644 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
645 vlc_value_t oval, vlc_value_t nval, void *param )
647 PLModel *p_model = (PLModel *) param;
648 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
649 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
653 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
654 vlc_value_t oval, vlc_value_t nval, void *param )
656 PLModel *p_model = (PLModel *) param;
657 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
658 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
660 if( ++p_model->i_items_to_append >= 50 )
662 p_model->b_need_update = VLC_TRUE;
665 PLEvent *event = new PLEvent( p_add );
666 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );