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 *****************************************************************************/
28 #include <QApplication>
31 #include "playlist_model.hpp"
32 #include <vlc_intf_strings.h>
34 #include "pixmaps/type_unknown.xpm"
35 #include "pixmaps/type_afile.xpm"
36 #include "pixmaps/type_vfile.xpm"
37 #include "pixmaps/type_net.xpm"
38 #include "pixmaps/type_card.xpm"
39 #include "pixmaps/type_disc.xpm"
40 #include "pixmaps/type_cdda.xpm"
41 #include "pixmaps/type_directory.xpm"
42 #include "pixmaps/type_playlist.xpm"
43 #include "pixmaps/type_node.xpm"
45 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
47 static int PlaylistChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int PlaylistNext( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int ItemChanged( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
54 vlc_value_t oval, vlc_value_t nval, void *param );
55 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
56 vlc_value_t oval, vlc_value_t nval, void *param );
58 /*************************************************************************
59 * Playlist item implementation
60 *************************************************************************/
69 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
72 i_id = _i_id; i_input_id = _i_input_id;
80 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
82 init( _i_id, _i_input_id, parent, m );
85 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
87 init( p_item->i_id, p_item->p_input->i_id, parent, m );
96 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
100 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
101 children.append( item );
103 model->endInsertRows();
106 void PLItem::remove( PLItem *removed )
108 assert( model && parentItem );
109 int i_index = parentItem->children.indexOf( removed );
110 model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
111 parentItem->children.removeAt( i_index );
112 model->endRemoveRows();
115 int PLItem::row() const
118 return parentItem->children.indexOf(const_cast<PLItem*>(this));
122 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
124 char psz_duration[MSTRTIME_MAX_SIZE];
125 assert( p_item->p_input->i_id == i_input_id );
126 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
127 if( p_item->p_input->p_meta )
129 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
131 secstotimestr( psz_duration, p_item->p_input->i_duration / 1000000 );
132 strings[2] = QString( psz_duration );
133 type = p_item->p_input->i_type;
135 if( current && p_item->p_input->p_meta &&
136 p_item->p_input->p_meta->psz_arturl &&
137 !strncmp( p_item->p_input->p_meta->psz_arturl, "file://", 7 ) )
139 model->sendArt( qfu( p_item->p_input->p_meta->psz_arturl ) );
143 /*************************************************************************
144 * Playlist model implementation
145 *************************************************************************/
147 PLModel::PLModel( playlist_t *_p_playlist,
148 playlist_item_t * p_root, int _i_depth, QObject *parent)
149 : QAbstractItemModel(parent)
152 assert( i_depth == 1 || i_depth == -1 );
153 p_playlist= _p_playlist;
154 i_items_to_append = 0;
155 b_need_update = false;
157 i_cached_input_id = -1;
158 i_popup_item = i_popup_parent = -1;
160 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
161 ADD_ICON( UNKNOWN , unknown );
162 ADD_ICON( AFILE,afile );
163 ADD_ICON( VFILE, vfile );
164 ADD_ICON( DIRECTORY, directory );
165 ADD_ICON( DISC, disc );
166 ADD_ICON( CDDA, cdda );
167 ADD_ICON( CARD, card );
168 ADD_ICON( NET, net );
169 ADD_ICON( PLAYLIST, playlist );
170 ADD_ICON( NODE, node );
184 void PLModel::addCallbacks()
186 /* Some global changes happened -> Rebuild all */
187 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
188 /* We went to the next item */
189 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
190 /* One item has been updated */
191 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
192 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
193 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
196 void PLModel::delCallbacks()
198 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
199 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
200 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
201 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
202 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
205 void PLModel::activateItem( const QModelIndex &index )
207 assert( index.isValid() );
208 PLItem *item = static_cast<PLItem*>(index.internalPointer());
211 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
212 activateItem( p_item );
215 /* Must be entered with lock */
216 void PLModel::activateItem( playlist_item_t *p_item )
218 if( !p_item ) return;
219 playlist_item_t *p_parent = p_item;
222 if( p_parent->i_id == rootItem->i_id ) break;
223 p_parent = p_parent->p_parent;
226 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
229 /****************** Base model mandatory implementations *****************/
230 QVariant PLModel::data(const QModelIndex &index, int role) const
232 assert( index.isValid() );
233 PLItem *item = static_cast<PLItem*>(index.internalPointer());
234 if( role == Qt::DisplayRole )
236 return QVariant( item->columnString( index.column() ) );
238 else if( role == Qt::DecorationRole && index.column() == 0 )
240 if( item->type >= 0 )
241 return QVariant( PLModel::icons[item->type] );
243 else if( role == Qt::FontRole )
245 if( item->current == true )
247 QFont f; f.setBold( true ); return QVariant( f );
253 bool PLModel::isCurrent( const QModelIndex &index )
255 assert( index.isValid() );
256 return static_cast<PLItem*>(index.internalPointer())->current;
259 int PLModel::itemId( const QModelIndex &index ) const
261 assert( index.isValid() );
262 return static_cast<PLItem*>(index.internalPointer())->i_id;
265 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
267 if( !index.isValid() ) return Qt::ItemIsEnabled;
268 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
271 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
274 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
275 return QVariant( rootItem->columnString( section ) );
279 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
283 if (!parent.isValid())
284 parentItem = rootItem;
286 parentItem = static_cast<PLItem*>(parent.internalPointer());
288 PLItem *childItem = parentItem->child(row);
290 return createIndex(row, column, childItem);
292 return QModelIndex();
295 /* Return the index of a given item */
296 QModelIndex PLModel::index( PLItem *item, int column ) const
298 if( !item ) return QModelIndex();
299 const PLItem *parent = item->parent();
301 return createIndex( parent->children.lastIndexOf( item ),
303 return QModelIndex();
306 QModelIndex PLModel::parent(const QModelIndex &index) const
308 if( !index.isValid() ) return QModelIndex();
310 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
311 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
312 PLItem *parentItem = childItem->parent();
313 if( !parentItem || parentItem == rootItem ) return QModelIndex();
314 if( ! parentItem->parentItem )
316 msg_Err( p_playlist, "No parent parent, trying row 0 " );
317 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
318 return createIndex( 0, 0, parentItem );
320 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
324 int PLModel::columnCount( const QModelIndex &i) const
326 if( i_depth == 1 ) return 1;
330 int PLModel::childrenCount(const QModelIndex &parent) const
332 return rowCount( parent );
335 int PLModel::rowCount(const QModelIndex &parent) const
339 if (!parent.isValid())
340 parentItem = rootItem;
342 parentItem = static_cast<PLItem*>(parent.internalPointer());
344 return parentItem->childCount();
347 /************************* General playlist status ***********************/
349 bool PLModel::hasRandom()
351 if( var_GetBool( p_playlist, "random" ) ) return true;
354 bool PLModel::hasRepeat()
356 if( var_GetBool( p_playlist, "repeat" ) ) return true;
359 bool PLModel::hasLoop()
361 if( var_GetBool( p_playlist, "loop" ) ) return true;
364 void PLModel::setLoop( bool on )
366 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
367 config_PutInt( p_playlist, "loop", on ? 1: 0 );
369 void PLModel::setRepeat( bool on )
371 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
372 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
374 void PLModel::setRandom( bool on )
376 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
377 config_PutInt( p_playlist, "random", on ? 1: 0 );
380 /************************* Lookups *****************************/
382 PLItem *PLModel::FindById( PLItem *root, int i_id )
384 return FindInner( root, i_id, false );
387 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
389 return FindInner( root, i_id, true );
392 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
393 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
395 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
397 if( ( !b_input && i_cached_id == i_id) ||
398 ( b_input && i_cached_input_id ==i_id ) )
400 return b_input ? p_cached_item_bi : p_cached_item;
403 if( !b_input && root->i_id == i_id )
408 else if( b_input && root->i_input_id == i_id )
410 ICACHE( i_id, root );
414 QList<PLItem *>::iterator it = root->children.begin();
415 while ( it != root->children.end() )
417 if( !b_input && (*it)->i_id == i_id )
419 CACHE( i_id, (*it) );
420 return p_cached_item;
422 else if( b_input && (*it)->i_input_id == i_id )
424 ICACHE( i_id, (*it) );
425 return p_cached_item_bi;
427 if( (*it)->children.size() )
429 PLItem *childFound = FindInner( (*it), i_id, b_input );
433 ICACHE( i_id, childFound )
435 CACHE( i_id, childFound )
447 /************************* Updates handling *****************************/
448 void PLModel::customEvent( QEvent *event )
450 int type = event->type();
451 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
452 type != ItemDelete_Type )
455 PLEvent *ple = static_cast<PLEvent *>(event);
457 if( type == ItemUpdate_Type )
458 ProcessInputItemUpdate( ple->i_id );
459 else if( type == ItemAppend_Type )
460 ProcessItemAppend( ple->p_add );
462 ProcessItemRemoval( ple->i_id );
465 /**** Events processing ****/
466 void PLModel::ProcessInputItemUpdate( int i_input_id )
468 if( i_input_id <= 0 ) return;
469 PLItem *item = FindByInput( rootItem, i_input_id );
471 UpdateTreeItem( item, true );
474 void PLModel::ProcessItemRemoval( int i_id )
476 if( i_id <= 0 ) return;
477 if( i_id == i_cached_id ) i_cached_id = -1;
478 i_cached_input_id = -1;
479 PLItem *item = FindById( rootItem, i_id );
481 item->remove( item );
484 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
486 playlist_item_t *p_item = NULL;
487 PLItem *newItem = NULL;
489 if( b_need_update ) return;
491 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
493 if( !nodeItem ) goto end;
495 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
496 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
497 if( i_depth == 1 && p_item->p_parent &&
498 p_item->p_parent->i_id != rootItem->i_id )
501 newItem = new PLItem( p_item, nodeItem, this );
502 nodeItem->appendChild( newItem );
503 UpdateTreeItem( p_item, newItem, true );
510 void PLModel::rebuild()
515 void PLModel::rebuild( playlist_item_t *p_root )
517 /* Remove callbacks before locking to avoid deadlocks */
519 /* Invalidate cache */
520 i_cached_id = i_cached_input_id = -1;
526 beginRemoveRows( index( rootItem, 0 ), 0,
527 rootItem->children.size() -1 );
528 qDeleteAll( rootItem->children );
529 rootItem->children.clear();
534 //if( rootItem ) delete rootItem;
535 rootItem = new PLItem( p_root, NULL, this );
536 rootItem->strings[0] = qtr("Name");
537 rootItem->strings[1] = qtr("Artist");
538 rootItem->strings[2] = qtr("Duration");
541 /* Recreate from root */
542 UpdateNodeChildren( rootItem );
543 if( p_playlist->status.p_item )
545 PLItem *currentItem = FindByInput( rootItem,
546 p_playlist->status.p_item->p_input->i_id );
549 UpdateTreeItem( p_playlist->status.p_item, currentItem,
555 /* And signal the view */
556 emit layoutChanged();
560 /* This function must be entered WITH the playlist lock */
561 void PLModel::UpdateNodeChildren( PLItem *root )
563 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
564 UpdateNodeChildren( p_node, root );
567 /* This function must be entered WITH the playlist lock */
568 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
570 for( int i = 0; i < p_node->i_children ; i++ )
572 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
573 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
574 root->appendChild( newItem, false );
575 UpdateTreeItem( newItem, false, true );
576 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
577 UpdateNodeChildren( p_node->pp_children[i], newItem );
581 /* This function must be entered WITH the playlist lock */
582 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
584 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
585 UpdateTreeItem( p_item, item, signal, force );
588 /* This function must be entered WITH the playlist lock */
589 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
590 bool signal, bool force )
592 if( !force && i_depth == 1 && p_item->p_parent &&
593 p_item->p_parent->i_id != rootItem->i_id )
595 item->update( p_item, p_item == p_playlist->status.p_item );
597 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
600 /************************* Actions ******************************/
602 void PLModel::sendArt( QString url )
604 QString arturl = url.replace( "file://",QString("" ) );
605 emit artSet( arturl );
609 * Deletion, here we have to do a ugly slow hack as we retrieve the full
610 * list of indexes to delete at once: when we delete a node and all of
611 * its children, we need to update the list.
612 * Todo: investigate whethere we can use ranges to be sure to delete all items?
614 void PLModel::doDelete( QModelIndexList selected )
616 for( int i = selected.size() -1 ; i >= 0; i-- )
618 QModelIndex index = selected[i];
619 if( index.column() != 0 ) continue;
620 PLItem *item = static_cast<PLItem*>(index.internalPointer());
623 if( item->children.size() )
624 recurseDelete( item->children, &selected );
625 doDeleteItem( item, &selected );
630 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
632 for( int i = children.size() - 1; i >= 0 ; i-- )
634 PLItem *item = children[i];
635 if( item->children.size() )
636 recurseDelete( item->children, fullList );
637 doDeleteItem( item, fullList );
641 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
643 QModelIndex deleteIndex = index( item, 0 );
644 fullList->removeAll( deleteIndex );
647 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
652 if( p_item->i_children == -1 )
653 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
655 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
656 /* And finally, remove it from the tree */
657 item->remove( item );
661 /******* Volume III: Sorting and searching ********/
662 void PLModel::sort( int column, Qt::SortOrder order )
665 playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
669 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
670 case 1: i_mode = SORT_ARTIST;break;
671 case 2: i_mode = SORT_DURATION; break;
674 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
675 order == Qt::AscendingOrder ? ORDER_NORMAL :
681 void PLModel::search( QString search_text )
683 /** \todo Fire the search with a small delay ? */
685 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,rootItem->i_id );
687 char *psz_name = search_text.toUtf8().data();
688 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
693 /*********** Popup *********/
694 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
696 assert( index.isValid() );
698 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
702 i_popup_item = p_item->i_id;
703 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
705 current_selection = list;
706 QMenu *menu = new QMenu;
707 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
708 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
709 menu->addSeparator();
710 menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
711 menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
712 menu->addSeparator();
713 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
714 if( p_item->i_children > -1 )
716 menu->addSeparator();
717 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
718 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
720 menu->popup( point );
726 void PLModel::popupDel()
728 doDelete( current_selection );
730 void PLModel::popupPlay()
733 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
734 activateItem( p_item );
738 void PLModel::popupInfo()
740 fprintf( stderr, "Popup Info is NOT implemented\n" );
743 void PLModel::popupStream()
745 fprintf( stderr, "Stream not implemented\n" );
747 void PLModel::popupSave()
749 fprintf( stderr, "Save not implemented\n" );
752 /**********************************************************************
754 **********************************************************************/
755 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
756 vlc_value_t oval, vlc_value_t nval, void *param )
758 PLModel *p_model = (PLModel *) param;
759 // p_model->b_need_update = VLC_TRUE;
763 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
764 vlc_value_t oval, vlc_value_t nval, void *param )
766 PLModel *p_model = (PLModel *) param;
767 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
768 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
769 event = new PLEvent( ItemUpdate_Type, nval.i_int );
770 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
774 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
775 vlc_value_t oval, vlc_value_t nval, void *param )
777 PLModel *p_model = (PLModel *) param;
778 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
779 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
783 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
784 vlc_value_t oval, vlc_value_t nval, void *param )
786 PLModel *p_model = (PLModel *) param;
787 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
788 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
792 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
793 vlc_value_t oval, vlc_value_t nval, void *param )
795 PLModel *p_model = (PLModel *) param;
796 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
797 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
799 if( ++p_model->i_items_to_append >= 50 )
801 // p_model->b_need_update = VLC_TRUE;
802 // return VLC_SUCCESS;
804 PLEvent *event = new PLEvent( p_add );
805 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );