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 <vlc_intf_strings.h>
33 #include "pixmaps/type_unknown.xpm"
34 #include "pixmaps/type_afile.xpm"
35 #include "pixmaps/type_vfile.xpm"
36 #include "pixmaps/type_net.xpm"
37 #include "pixmaps/type_card.xpm"
38 #include "pixmaps/type_disc.xpm"
39 #include "pixmaps/type_cdda.xpm"
40 #include "pixmaps/type_directory.xpm"
41 #include "pixmaps/type_playlist.xpm"
42 #include "pixmaps/type_node.xpm"
44 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
46 static int PlaylistChanged( vlc_object_t *, const char *,
47 vlc_value_t, vlc_value_t, void * );
48 static int PlaylistNext( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
50 static int ItemChanged( vlc_object_t *, const char *,
51 vlc_value_t, vlc_value_t, void * );
52 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
53 vlc_value_t oval, vlc_value_t nval, void *param );
54 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
55 vlc_value_t oval, vlc_value_t nval, void *param );
57 /*************************************************************************
58 * Playlist item implementation
59 *************************************************************************/
68 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
71 i_id = _i_id; i_input_id = _i_input_id;
79 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
81 init( _i_id, _i_input_id, parent, m );
84 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
86 init( p_item->i_id, p_item->p_input->i_id, parent, m );
95 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
99 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
100 children.append( item );
102 model->endInsertRows();
105 void PLItem::remove( PLItem *removed )
107 assert( model && parentItem );
108 int i_index = parentItem->children.indexOf( removed );
109 model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
110 parentItem->children.removeAt( i_index );
111 model->endRemoveRows();
114 int PLItem::row() const
117 return parentItem->children.indexOf(const_cast<PLItem*>(this));
121 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
123 char psz_duration[MSTRTIME_MAX_SIZE];
124 assert( p_item->p_input->i_id == i_input_id );
125 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
126 if( p_item->p_input->p_meta )
128 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
130 secstotimestr( psz_duration, p_item->p_input->i_duration / 1000000 );
131 strings[2] = QString( psz_duration );
132 type = p_item->p_input->i_type;
136 /*************************************************************************
137 * Playlist model implementation
138 *************************************************************************/
140 PLModel::PLModel( playlist_t *_p_playlist,
141 playlist_item_t * p_root, int _i_depth, QObject *parent)
142 : QAbstractItemModel(parent)
145 assert( i_depth == 1 || i_depth == -1 );
146 p_playlist= _p_playlist;
147 i_items_to_append = 0;
148 b_need_update = false;
150 i_cached_input_id = -1;
151 i_popup_item = i_popup_parent = -1;
153 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
154 ADD_ICON( UNKNOWN , unknown );
155 ADD_ICON( AFILE,afile );
156 ADD_ICON( VFILE, vfile );
157 ADD_ICON( DIRECTORY, directory );
158 ADD_ICON( DISC, disc );
159 ADD_ICON( CDDA, cdda );
160 ADD_ICON( CARD, card );
161 ADD_ICON( NET, net );
162 ADD_ICON( PLAYLIST, playlist );
163 ADD_ICON( NODE, node );
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 activateItem( p_item );
208 /* Must be entered with lock */
209 void PLModel::activateItem( playlist_item_t *p_item )
211 if( !p_item ) return;
212 playlist_item_t *p_parent = p_item;
215 if( p_parent->i_id == rootItem->i_id ) break;
216 p_parent = p_parent->p_parent;
219 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
222 /****************** Base model mandatory implementations *****************/
223 QVariant PLModel::data(const QModelIndex &index, int role) const
225 assert( index.isValid() );
226 PLItem *item = static_cast<PLItem*>(index.internalPointer());
227 if( role == Qt::DisplayRole )
229 return QVariant( item->columnString( index.column() ) );
231 else if( role == Qt::DecorationRole && index.column() == 0 )
233 if( item->type >= 0 )
234 return QVariant( PLModel::icons[item->type] );
236 else if( role == Qt::FontRole )
238 if( item->current == true )
240 QFont f; f.setBold( true ); return QVariant( f );
246 bool PLModel::isCurrent( const QModelIndex &index )
248 assert( index.isValid() );
249 return static_cast<PLItem*>(index.internalPointer())->current;
252 int PLModel::itemId( const QModelIndex &index ) const
254 assert( index.isValid() );
255 return static_cast<PLItem*>(index.internalPointer())->i_id;
258 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
260 if( !index.isValid() ) return Qt::ItemIsEnabled;
261 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
264 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
267 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
268 return QVariant( rootItem->columnString( section ) );
272 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
276 if (!parent.isValid())
277 parentItem = rootItem;
279 parentItem = static_cast<PLItem*>(parent.internalPointer());
281 PLItem *childItem = parentItem->child(row);
283 return createIndex(row, column, childItem);
285 return QModelIndex();
288 /* Return the index of a given item */
289 QModelIndex PLModel::index( PLItem *item, int column ) const
291 if( !item ) return QModelIndex();
292 const PLItem *parent = item->parent();
294 return createIndex( parent->children.lastIndexOf( item ),
296 return QModelIndex();
299 QModelIndex PLModel::parent(const QModelIndex &index) const
301 if( !index.isValid() ) return QModelIndex();
303 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
304 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
305 PLItem *parentItem = childItem->parent();
306 if( !parentItem || parentItem == rootItem ) return QModelIndex();
307 if( ! parentItem->parentItem )
309 msg_Err( p_playlist, "No parent parent, trying row 0 " );
310 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
311 return createIndex( 0, 0, parentItem );
313 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
317 int PLModel::columnCount( const QModelIndex &i) const
319 if( i_depth == 1 ) return 1;
323 int PLModel::childrenCount(const QModelIndex &parent) const
325 return rowCount( parent );
328 int PLModel::rowCount(const QModelIndex &parent) const
332 if (!parent.isValid())
333 parentItem = rootItem;
335 parentItem = static_cast<PLItem*>(parent.internalPointer());
337 return parentItem->childCount();
340 /************************* General playlist status ***********************/
342 bool PLModel::hasRandom()
344 if( var_GetBool( p_playlist, "random" ) ) return true;
347 bool PLModel::hasRepeat()
349 if( var_GetBool( p_playlist, "repeat" ) ) return true;
352 bool PLModel::hasLoop()
354 if( var_GetBool( p_playlist, "loop" ) ) return true;
357 void PLModel::setLoop( bool on )
359 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
361 void PLModel::setRepeat( bool on )
363 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
365 void PLModel::setRandom( bool on )
367 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
370 /************************* Lookups *****************************/
372 PLItem *PLModel::FindById( PLItem *root, int i_id )
374 return FindInner( root, i_id, false );
377 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
379 return FindInner( root, i_id, true );
382 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
383 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
385 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
387 if( ( !b_input && i_cached_id == i_id) ||
388 ( b_input && i_cached_input_id ==i_id ) )
390 return b_input ? p_cached_item_bi : p_cached_item;
393 if( !b_input && root->i_id == i_id )
398 else if( b_input && root->i_input_id == i_id )
400 ICACHE( i_id, root );
404 QList<PLItem *>::iterator it = root->children.begin();
405 while ( it != root->children.end() )
407 if( !b_input && (*it)->i_id == i_id )
409 CACHE( i_id, (*it) );
410 return p_cached_item;
412 else if( b_input && (*it)->i_input_id == i_id )
414 ICACHE( i_id, (*it) );
415 return p_cached_item_bi;
417 if( (*it)->children.size() )
419 PLItem *childFound = FindInner( (*it), i_id, b_input );
423 ICACHE( i_id, childFound )
425 CACHE( i_id, childFound )
437 /************************* Updates handling *****************************/
438 void PLModel::customEvent( QEvent *event )
440 int type = event->type();
441 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
442 type != ItemDelete_Type )
445 PLEvent *ple = static_cast<PLEvent *>(event);
447 if( type == ItemUpdate_Type )
448 ProcessInputItemUpdate( ple->i_id );
449 else if( type == ItemAppend_Type )
450 ProcessItemAppend( ple->p_add );
452 ProcessItemRemoval( ple->i_id );
455 /**** Events processing ****/
456 void PLModel::ProcessInputItemUpdate( int i_input_id )
458 if( i_input_id <= 0 ) return;
459 PLItem *item = FindByInput( rootItem, i_input_id );
461 UpdateTreeItem( item, true );
464 void PLModel::ProcessItemRemoval( int i_id )
466 if( i_id <= 0 ) return;
467 if( i_id == i_cached_id ) i_cached_id = -1;
468 i_cached_input_id = -1;
470 PLItem *item = FindById( rootItem, i_id );
472 item->remove( item );
475 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
477 playlist_item_t *p_item = NULL;
478 PLItem *newItem = NULL;
480 if( b_need_update ) return;
482 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
483 if( !nodeItem ) goto end;
486 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
487 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
488 if( i_depth == 1 && p_item->p_parent &&
489 p_item->p_parent->i_id != rootItem->i_id )
492 newItem = new PLItem( p_item, nodeItem, this );
493 nodeItem->appendChild( newItem );
494 UpdateTreeItem( p_item, newItem, true );
501 void PLModel::rebuild()
506 void PLModel::rebuild( playlist_item_t *p_root )
508 /* Remove callbacks before locking to avoid deadlocks */
510 /* Invalidate cache */
511 i_cached_id = i_cached_input_id = -1;
517 beginRemoveRows( index( rootItem, 0 ), 0,
518 rootItem->children.size() -1 );
519 qDeleteAll( rootItem->children );
520 rootItem->children.clear();
525 //if( rootItem ) delete rootItem;
526 rootItem = new PLItem( p_root, NULL, this );
527 rootItem->strings[0] = qtr("Name");
528 rootItem->strings[1] = qtr("Artist");
531 /* Recreate from root */
532 UpdateNodeChildren( rootItem );
533 if( p_playlist->status.p_item )
535 PLItem *currentItem = FindByInput( rootItem,
536 p_playlist->status.p_item->p_input->i_id );
539 UpdateTreeItem( p_playlist->status.p_item, currentItem,
545 /* And signal the view */
546 emit layoutChanged();
550 /* This function must be entered WITH the playlist lock */
551 void PLModel::UpdateNodeChildren( PLItem *root )
553 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
554 UpdateNodeChildren( p_node, root );
557 /* This function must be entered WITH the playlist lock */
558 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
560 for( int i = 0; i < p_node->i_children ; i++ )
562 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
563 root->appendChild( newItem, false );
564 UpdateTreeItem( newItem, false, true );
565 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
566 UpdateNodeChildren( p_node->pp_children[i], newItem );
570 /* This function must be entered WITH the playlist lock */
571 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
573 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
574 UpdateTreeItem( p_item, item, signal, force );
577 /* This function must be entered WITH the playlist lock */
578 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
579 bool signal, bool force )
581 if( !force && i_depth == 1 && p_item->p_parent &&
582 p_item->p_parent->i_id != rootItem->i_id )
584 item->update( p_item, p_item == p_playlist->status.p_item );
586 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
589 /************************* Actions ******************************/
592 * Deletion, here we have to do a ugly slow hack as we retrieve the full
593 * list of indexes to delete at once: when we delete a node and all of
594 * its children, we need to update the list.
595 * Todo: investigate whethere we can use ranges to be sure to delete all items?
597 void PLModel::doDelete( QModelIndexList selected )
599 for( int i = selected.size() -1 ; i >= 0; i-- )
601 QModelIndex index = selected[i];
602 if( index.column() != 0 ) continue;
603 PLItem *item = static_cast<PLItem*>(index.internalPointer());
606 if( item->children.size() )
607 recurseDelete( item->children, &selected );
608 doDeleteItem( item, &selected );
613 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
615 for( int i = children.size() - 1; i >= 0 ; i-- )
617 PLItem *item = children[i];
618 if( item->children.size() )
619 recurseDelete( item->children, fullList );
620 doDeleteItem( item, fullList );
624 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
626 QModelIndex deleteIndex = index( item, 0 );
627 fullList->removeAll( deleteIndex );
630 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
635 if( p_item->i_children == -1 )
636 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
638 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
639 /* And finally, remove it from the tree */
640 item->remove( item );
644 /******* Volume III: Sorting and searching ********/
645 void PLModel::sort( int column, Qt::SortOrder order )
648 playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
652 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
653 case 1: i_mode = SORT_ARTIST;break;
654 case 2: i_mode = SORT_DURATION; break;
657 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
658 order == Qt::AscendingOrder ? ORDER_NORMAL :
664 /*********** Popup *********/
665 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
667 assert( index.isValid() );
669 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
673 i_popup_item = p_item->i_id;
674 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
676 current_selection = list;
677 QMenu *menu = new QMenu;
678 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
679 menu->addAction( qfu(I_POP_PREPARSE), this, SLOT( popupPreparse() ) );
680 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
681 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
682 if( p_item->i_children > -1 )
684 menu->addSeparator();
685 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
686 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
688 menu->popup( point );
694 void PLModel::popupDel()
696 doDelete( current_selection );
698 void PLModel::popupPlay()
701 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
702 activateItem( p_item );
706 /**********************************************************************
708 **********************************************************************/
709 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
710 vlc_value_t oval, vlc_value_t nval, void *param )
712 PLModel *p_model = (PLModel *) param;
713 p_model->b_need_update = VLC_TRUE;
717 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
718 vlc_value_t oval, vlc_value_t nval, void *param )
720 PLModel *p_model = (PLModel *) param;
721 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
722 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
723 event = new PLEvent( ItemUpdate_Type, nval.i_int );
724 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
728 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
729 vlc_value_t oval, vlc_value_t nval, void *param )
731 PLModel *p_model = (PLModel *) param;
732 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
733 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
737 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
738 vlc_value_t oval, vlc_value_t nval, void *param )
740 PLModel *p_model = (PLModel *) param;
741 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
742 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
746 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
747 vlc_value_t oval, vlc_value_t nval, void *param )
749 PLModel *p_model = (PLModel *) param;
750 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
751 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
753 if( ++p_model->i_items_to_append >= 50 )
755 p_model->b_need_update = VLC_TRUE;
758 PLEvent *event = new PLEvent( p_add );
759 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );