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"
32 #include "pixmaps/type_unknown.xpm"
33 #include "pixmaps/type_afile.xpm"
34 #include "pixmaps/type_vfile.xpm"
35 #include "pixmaps/type_net.xpm"
36 #include "pixmaps/type_card.xpm"
37 #include "pixmaps/type_disc.xpm"
38 #include "pixmaps/type_cdda.xpm"
39 #include "pixmaps/type_directory.xpm"
40 #include "pixmaps/type_playlist.xpm"
41 #include "pixmaps/type_node.xpm"
43 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
45 static int PlaylistChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int PlaylistNext( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int ItemChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
52 vlc_value_t oval, vlc_value_t nval, void *param );
53 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
54 vlc_value_t oval, vlc_value_t nval, void *param );
56 /*************************************************************************
57 * Playlist item implementation
58 *************************************************************************/
67 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
70 i_id = _i_id; i_input_id = _i_input_id;
77 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
79 init( _i_id, _i_input_id, parent, m );
82 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
84 init( p_item->i_id, p_item->p_input->i_id, parent, m );
92 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
96 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
97 children.append( item );
99 model->endInsertRows();
102 void PLItem::remove( PLItem *removed )
104 assert( model && parentItem );
105 int i_index = parentItem->children.indexOf( removed );
106 model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
107 parentItem->children.removeAt( i_index );
108 model->endRemoveRows();
111 int PLItem::row() const
114 return parentItem->children.indexOf(const_cast<PLItem*>(this));
118 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
120 assert( p_item->p_input->i_id == i_input_id );
121 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
122 if( p_item->p_input->p_meta )
124 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
126 type = p_item->p_input->i_type;
130 /*************************************************************************
131 * Playlist model implementation
132 *************************************************************************/
134 PLModel::PLModel( playlist_t *_p_playlist,
135 playlist_item_t * p_root, int _i_depth, QObject *parent)
136 : QAbstractItemModel(parent)
139 assert( i_depth == 1 || i_depth == -1 );
140 p_playlist= _p_playlist;
141 i_items_to_append = 0;
142 b_need_update = false;
144 i_cached_input_id = -1;
145 i_popup_item = i_popup_parent = -1;
147 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
148 ADD_ICON( UNKNOWN , unknown );
149 ADD_ICON( AFILE,afile );
150 ADD_ICON( VFILE, vfile );
151 ADD_ICON( DIRECTORY, directory );
152 ADD_ICON( DISC, disc );
153 ADD_ICON( CDDA, cdda );
154 ADD_ICON( CARD, card );
155 ADD_ICON( NET, net );
156 ADD_ICON( PLAYLIST, playlist );
157 ADD_ICON( NODE, node );
171 void PLModel::addCallbacks()
173 /* Some global changes happened -> Rebuild all */
174 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
175 /* We went to the next item */
176 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
177 /* One item has been updated */
178 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
179 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
180 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
183 void PLModel::delCallbacks()
185 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
186 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
187 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
188 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
189 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
192 void PLModel::activateItem( const QModelIndex &index )
194 assert( index.isValid() );
195 PLItem *item = static_cast<PLItem*>(index.internalPointer());
198 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
199 activateItem( p_item );
202 /* Must be entered with lock */
203 void PLModel::activateItem( playlist_item_t *p_item )
205 if( !p_item ) return;
206 playlist_item_t *p_parent = p_item;
209 if( p_parent->i_id == rootItem->i_id ) break;
210 p_parent = p_parent->p_parent;
213 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
216 /****************** Base model mandatory implementations *****************/
217 QVariant PLModel::data(const QModelIndex &index, int role) const
219 assert( index.isValid() );
220 PLItem *item = static_cast<PLItem*>(index.internalPointer());
221 if( role == Qt::DisplayRole )
223 return QVariant( item->columnString( index.column() ) );
225 else if( role == Qt::DecorationRole && index.column() == 0 )
227 if( item->type >= 0 )
228 return QVariant( PLModel::icons[item->type] );
230 else if( role == Qt::FontRole )
232 if( item->current == true )
234 QFont f; f.setBold( true ); return QVariant( f );
240 bool PLModel::isCurrent( const QModelIndex &index )
242 assert( index.isValid() );
243 return static_cast<PLItem*>(index.internalPointer())->current;
246 int PLModel::itemId( const QModelIndex &index ) const
248 assert( index.isValid() );
249 return static_cast<PLItem*>(index.internalPointer())->i_id;
252 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
254 if( !index.isValid() ) return Qt::ItemIsEnabled;
255 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
258 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
261 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
262 return QVariant( rootItem->columnString( section ) );
266 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
270 if (!parent.isValid())
271 parentItem = rootItem;
273 parentItem = static_cast<PLItem*>(parent.internalPointer());
275 PLItem *childItem = parentItem->child(row);
277 return createIndex(row, column, childItem);
279 return QModelIndex();
282 /* Return the index of a given item */
283 QModelIndex PLModel::index( PLItem *item, int column ) const
285 if( !item ) return QModelIndex();
286 const PLItem *parent = item->parent();
288 return createIndex( parent->children.lastIndexOf( item ), column, item );
289 return QModelIndex();
292 QModelIndex PLModel::parent(const QModelIndex &index) const
294 if( !index.isValid() ) return QModelIndex();
296 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
297 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
298 PLItem *parentItem = childItem->parent();
299 if( !parentItem || parentItem == rootItem ) return QModelIndex();
300 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
304 int PLModel::columnCount( const QModelIndex &i) const
306 if( i_depth == 1 ) return 1;
310 int PLModel::childrenCount(const QModelIndex &parent) const
312 return rowCount( parent );
315 int PLModel::rowCount(const QModelIndex &parent) const
319 if (!parent.isValid())
320 parentItem = rootItem;
322 parentItem = static_cast<PLItem*>(parent.internalPointer());
324 return parentItem->childCount();
327 /************************* General playlist status ***********************/
329 bool PLModel::hasRandom()
331 if( var_GetBool( p_playlist, "random" ) ) return true;
334 bool PLModel::hasRepeat()
336 if( var_GetBool( p_playlist, "repeat" ) ) return true;
339 bool PLModel::hasLoop()
341 if( var_GetBool( p_playlist, "loop" ) ) return true;
344 void PLModel::setLoop( bool on )
346 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
348 void PLModel::setRepeat( bool on )
350 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
352 void PLModel::setRandom( bool on )
354 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
357 /************************* Lookups *****************************/
359 PLItem *PLModel::FindById( PLItem *root, int i_id )
361 return FindInner( root, i_id, false );
364 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
366 return FindInner( root, i_id, true );
369 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
370 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
372 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
374 if( ( !b_input && i_cached_id == i_id) ||
375 ( b_input && i_cached_input_id ==i_id ) )
377 return b_input ? p_cached_item_bi : p_cached_item;
380 if( !b_input && root->i_id == i_id )
385 else if( b_input && root->i_input_id == i_id )
387 ICACHE( i_id, root );
391 QList<PLItem *>::iterator it = root->children.begin();
392 while ( it != root->children.end() )
394 if( !b_input && (*it)->i_id == i_id )
396 CACHE( i_id, (*it) );
397 return p_cached_item;
399 else if( b_input && (*it)->i_input_id == i_id )
401 ICACHE( i_id, (*it) );
402 return p_cached_item_bi;
404 if( (*it)->children.size() )
406 PLItem *childFound = FindInner( (*it), i_id, b_input );
410 ICACHE( i_id, childFound )
412 CACHE( i_id, childFound )
424 /************************* Updates handling *****************************/
425 void PLModel::customEvent( QEvent *event )
427 int type = event->type();
428 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
429 type != ItemDelete_Type )
432 PLEvent *ple = static_cast<PLEvent *>(event);
434 if( type == ItemUpdate_Type )
435 ProcessInputItemUpdate( ple->i_id );
436 else if( type == ItemAppend_Type )
437 ProcessItemAppend( ple->p_add );
439 ProcessItemRemoval( ple->i_id );
442 /**** Events processing ****/
443 void PLModel::ProcessInputItemUpdate( int i_input_id )
445 if( i_input_id <= 0 ) return;
446 PLItem *item = FindByInput( rootItem, i_input_id );
448 UpdateTreeItem( item, true );
451 void PLModel::ProcessItemRemoval( int i_id )
453 if( i_id <= 0 ) return;
454 if( i_id == i_cached_id ) i_cached_id = -1;
455 i_cached_input_id = -1;
457 PLItem *item = FindById( rootItem, i_id );
459 item->remove( item );
462 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
464 playlist_item_t *p_item = NULL;
465 PLItem *newItem = NULL;
467 if( b_need_update ) return;
469 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
470 if( !nodeItem ) goto end;
473 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
474 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
475 if( i_depth == 1 && p_item->p_parent &&
476 p_item->p_parent->i_id != rootItem->i_id )
479 newItem = new PLItem( p_item, nodeItem, this );
480 nodeItem->appendChild( newItem );
481 UpdateTreeItem( p_item, newItem, true );
488 void PLModel::rebuild()
493 void PLModel::rebuild( playlist_item_t *p_root )
495 /* Remove callbacks before locking to avoid deadlocks */
497 /* Invalidate cache */
498 i_cached_id = i_cached_input_id = -1;
505 beginRemoveRows( index( rootItem, 0 ), 0,
506 rootItem->children.size() -1 );
507 qDeleteAll( rootItem->children );
512 //if( rootItem ) delete rootItem;
513 rootItem = new PLItem( p_root, NULL, this );
514 rootItem->strings[0] = qtr("Name");
515 rootItem->strings[1] = qtr("Artist");
518 /* Recreate from root */
519 UpdateNodeChildren( rootItem );
520 if( p_playlist->status.p_item )
522 PLItem *currentItem = FindByInput( rootItem,
523 p_playlist->status.p_item->p_input->i_id );
526 UpdateTreeItem( p_playlist->status.p_item, currentItem,
532 /* And signal the view */
533 emit layoutChanged();
537 /* This function must be entered WITH the playlist lock */
538 void PLModel::UpdateNodeChildren( PLItem *root )
540 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
541 UpdateNodeChildren( p_node, root );
544 /* This function must be entered WITH the playlist lock */
545 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
547 for( int i = 0; i < p_node->i_children ; i++ )
549 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
550 root->appendChild( newItem, false );
551 UpdateTreeItem( newItem, false, true );
552 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
553 UpdateNodeChildren( p_node->pp_children[i], newItem );
557 /* This function must be entered WITH the playlist lock */
558 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
560 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
561 UpdateTreeItem( p_item, item, signal, force );
564 /* This function must be entered WITH the playlist lock */
565 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
566 bool signal, bool force )
568 if( !force && i_depth == 1 && p_item->p_parent &&
569 p_item->p_parent->i_id != rootItem->i_id )
571 item->update( p_item, p_item == p_playlist->status.p_item );
573 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
576 /************************* Actions ******************************/
579 * Deletion, here we have to do a ugly slow hack as we retrieve the full
580 * list of indexes to delete at once: when we delete a node and all of
581 * its children, we need to update the list.
582 * Todo: investigate whethere we can use ranges to be sure to delete all items?
584 void PLModel::doDelete( QModelIndexList selected )
586 for( int i = selected.size() -1 ; i >= 0; i-- )
588 QModelIndex index = selected[i];
589 if( index.column() != 0 ) continue;
590 PLItem *item = static_cast<PLItem*>(index.internalPointer());
593 if( item->children.size() )
594 recurseDelete( item->children, &selected );
595 doDeleteItem( item, &selected );
600 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
602 for( int i = children.size() - 1; i >= 0 ; i-- )
604 PLItem *item = children[i];
605 if( item->children.size() )
606 recurseDelete( item->children, fullList );
607 doDeleteItem( item, fullList );
611 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
613 QModelIndex deleteIndex = index( item, 0 );
614 fullList->removeAll( deleteIndex );
617 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
622 if( p_item->i_children == -1 )
623 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
625 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
626 /* And finally, remove it from the tree */
627 item->remove( item );
631 /*********** Popup *********/
632 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
634 assert( index.isValid() );
636 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
640 i_popup_item = p_item->i_id;
641 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
643 current_selection = list;
644 QMenu *menu = new QMenu;
645 menu->addAction( qtr("Play"), this, SLOT( popupPlay() ) );
646 menu->addAction( qtr("Fetch information"), this,
647 SLOT( popupPreparse() ) );
648 menu->addAction( qtr("Delete"), this, SLOT( popupDel() ) );
649 menu->addAction( qtr("Information"), this, SLOT( popupInfo() ) );
650 if( p_item->i_children > -1 )
652 menu->addSeparator();
653 menu->addAction( qtr("Sort"), this, SLOT( popupSort() ) );
654 menu->addAction( qtr("Add node"), this, SLOT( popupAdd() ) );
656 menu->popup( point );
662 void PLModel::popupDel()
664 doDelete( current_selection );
666 void PLModel::popupPlay()
669 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
670 activateItem( p_item );
674 /**********************************************************************
676 **********************************************************************/
677 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
678 vlc_value_t oval, vlc_value_t nval, void *param )
680 PLModel *p_model = (PLModel *) param;
681 p_model->b_need_update = VLC_TRUE;
685 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
686 vlc_value_t oval, vlc_value_t nval, void *param )
688 PLModel *p_model = (PLModel *) param;
689 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
690 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
691 event = new PLEvent( ItemUpdate_Type, nval.i_int );
692 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
696 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
697 vlc_value_t oval, vlc_value_t nval, void *param )
699 PLModel *p_model = (PLModel *) param;
700 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
701 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
705 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
706 vlc_value_t oval, vlc_value_t nval, void *param )
708 PLModel *p_model = (PLModel *) param;
709 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
710 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
714 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
715 vlc_value_t oval, vlc_value_t nval, void *param )
717 PLModel *p_model = (PLModel *) param;
718 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
719 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
721 if( ++p_model->i_items_to_append >= 50 )
723 p_model->b_need_update = VLC_TRUE;
726 PLEvent *event = new PLEvent( p_add );
727 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );