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;
137 /*************************************************************************
138 * Playlist model implementation
139 *************************************************************************/
141 PLModel::PLModel( playlist_t *_p_playlist,
142 playlist_item_t * p_root, int _i_depth, QObject *parent)
143 : QAbstractItemModel(parent)
146 assert( i_depth == 1 || i_depth == -1 );
147 p_playlist= _p_playlist;
148 i_items_to_append = 0;
149 b_need_update = false;
151 i_cached_input_id = -1;
152 i_popup_item = i_popup_parent = -1;
154 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
155 ADD_ICON( UNKNOWN , unknown );
156 ADD_ICON( AFILE,afile );
157 ADD_ICON( VFILE, vfile );
158 ADD_ICON( DIRECTORY, directory );
159 ADD_ICON( DISC, disc );
160 ADD_ICON( CDDA, cdda );
161 ADD_ICON( CARD, card );
162 ADD_ICON( NET, net );
163 ADD_ICON( PLAYLIST, playlist );
164 ADD_ICON( NODE, node );
178 void PLModel::addCallbacks()
180 /* Some global changes happened -> Rebuild all */
181 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
182 /* We went to the next item */
183 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
184 /* One item has been updated */
185 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
186 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
187 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
190 void PLModel::delCallbacks()
192 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
193 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
194 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
195 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
196 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
199 void PLModel::activateItem( const QModelIndex &index )
201 assert( index.isValid() );
202 PLItem *item = static_cast<PLItem*>(index.internalPointer());
205 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
206 activateItem( p_item );
209 /* Must be entered with lock */
210 void PLModel::activateItem( playlist_item_t *p_item )
212 if( !p_item ) return;
213 playlist_item_t *p_parent = p_item;
216 if( p_parent->i_id == rootItem->i_id ) break;
217 p_parent = p_parent->p_parent;
220 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
223 /****************** Base model mandatory implementations *****************/
224 QVariant PLModel::data(const QModelIndex &index, int role) const
226 assert( index.isValid() );
227 PLItem *item = static_cast<PLItem*>(index.internalPointer());
228 if( role == Qt::DisplayRole )
230 return QVariant( item->columnString( index.column() ) );
232 else if( role == Qt::DecorationRole && index.column() == 0 )
234 if( item->type >= 0 )
235 return QVariant( PLModel::icons[item->type] );
237 else if( role == Qt::FontRole )
239 if( item->current == true )
241 QFont f; f.setBold( true ); return QVariant( f );
247 bool PLModel::isCurrent( const QModelIndex &index )
249 assert( index.isValid() );
250 return static_cast<PLItem*>(index.internalPointer())->current;
253 int PLModel::itemId( const QModelIndex &index ) const
255 assert( index.isValid() );
256 return static_cast<PLItem*>(index.internalPointer())->i_id;
259 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
261 if( !index.isValid() ) return Qt::ItemIsEnabled;
262 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
265 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
268 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
269 return QVariant( rootItem->columnString( section ) );
273 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
277 if (!parent.isValid())
278 parentItem = rootItem;
280 parentItem = static_cast<PLItem*>(parent.internalPointer());
282 PLItem *childItem = parentItem->child(row);
284 return createIndex(row, column, childItem);
286 return QModelIndex();
289 /* Return the index of a given item */
290 QModelIndex PLModel::index( PLItem *item, int column ) const
292 if( !item ) return QModelIndex();
293 const PLItem *parent = item->parent();
295 return createIndex( parent->children.lastIndexOf( item ),
297 return QModelIndex();
300 QModelIndex PLModel::parent(const QModelIndex &index) const
302 if( !index.isValid() ) return QModelIndex();
304 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
305 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
306 PLItem *parentItem = childItem->parent();
307 if( !parentItem || parentItem == rootItem ) return QModelIndex();
308 if( ! parentItem->parentItem )
310 msg_Err( p_playlist, "No parent parent, trying row 0 " );
311 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
312 return createIndex( 0, 0, parentItem );
314 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
318 int PLModel::columnCount( const QModelIndex &i) const
320 if( i_depth == 1 ) return 1;
324 int PLModel::childrenCount(const QModelIndex &parent) const
326 return rowCount( parent );
329 int PLModel::rowCount(const QModelIndex &parent) const
333 if (!parent.isValid())
334 parentItem = rootItem;
336 parentItem = static_cast<PLItem*>(parent.internalPointer());
338 return parentItem->childCount();
341 /************************* General playlist status ***********************/
343 bool PLModel::hasRandom()
345 if( var_GetBool( p_playlist, "random" ) ) return true;
348 bool PLModel::hasRepeat()
350 if( var_GetBool( p_playlist, "repeat" ) ) return true;
353 bool PLModel::hasLoop()
355 if( var_GetBool( p_playlist, "loop" ) ) return true;
358 void PLModel::setLoop( bool on )
360 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
361 config_PutInt( p_playlist, "loop", on ? 1: 0 );
363 void PLModel::setRepeat( bool on )
365 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
366 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
368 void PLModel::setRandom( bool on )
370 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
371 config_PutInt( p_playlist, "random", on ? 1: 0 );
374 /************************* Lookups *****************************/
376 PLItem *PLModel::FindById( PLItem *root, int i_id )
378 return FindInner( root, i_id, false );
381 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
383 return FindInner( root, i_id, true );
386 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
387 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
389 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
391 if( ( !b_input && i_cached_id == i_id) ||
392 ( b_input && i_cached_input_id ==i_id ) )
394 return b_input ? p_cached_item_bi : p_cached_item;
397 if( !b_input && root->i_id == i_id )
402 else if( b_input && root->i_input_id == i_id )
404 ICACHE( i_id, root );
408 QList<PLItem *>::iterator it = root->children.begin();
409 while ( it != root->children.end() )
411 if( !b_input && (*it)->i_id == i_id )
413 CACHE( i_id, (*it) );
414 return p_cached_item;
416 else if( b_input && (*it)->i_input_id == i_id )
418 ICACHE( i_id, (*it) );
419 return p_cached_item_bi;
421 if( (*it)->children.size() )
423 PLItem *childFound = FindInner( (*it), i_id, b_input );
427 ICACHE( i_id, childFound )
429 CACHE( i_id, childFound )
441 /************************* Updates handling *****************************/
442 void PLModel::customEvent( QEvent *event )
444 int type = event->type();
445 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
446 type != ItemDelete_Type )
449 PLEvent *ple = static_cast<PLEvent *>(event);
451 if( type == ItemUpdate_Type )
452 ProcessInputItemUpdate( ple->i_id );
453 else if( type == ItemAppend_Type )
454 ProcessItemAppend( ple->p_add );
456 ProcessItemRemoval( ple->i_id );
459 /**** Events processing ****/
460 void PLModel::ProcessInputItemUpdate( int i_input_id )
462 if( i_input_id <= 0 ) return;
463 PLItem *item = FindByInput( rootItem, i_input_id );
465 UpdateTreeItem( item, true );
468 void PLModel::ProcessItemRemoval( int i_id )
470 if( i_id <= 0 ) return;
471 if( i_id == i_cached_id ) i_cached_id = -1;
472 i_cached_input_id = -1;
474 PLItem *item = FindById( rootItem, i_id );
476 item->remove( item );
479 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
481 playlist_item_t *p_item = NULL;
482 PLItem *newItem = NULL;
484 if( b_need_update ) return;
486 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
488 if( !nodeItem ) goto end;
490 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
491 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
492 if( i_depth == 1 && p_item->p_parent &&
493 p_item->p_parent->i_id != rootItem->i_id )
496 newItem = new PLItem( p_item, nodeItem, this );
497 nodeItem->appendChild( newItem );
498 UpdateTreeItem( p_item, newItem, true );
505 void PLModel::rebuild()
510 void PLModel::rebuild( playlist_item_t *p_root )
512 /* Remove callbacks before locking to avoid deadlocks */
514 /* Invalidate cache */
515 i_cached_id = i_cached_input_id = -1;
521 beginRemoveRows( index( rootItem, 0 ), 0,
522 rootItem->children.size() -1 );
523 qDeleteAll( rootItem->children );
524 rootItem->children.clear();
529 //if( rootItem ) delete rootItem;
530 rootItem = new PLItem( p_root, NULL, this );
531 rootItem->strings[0] = qtr("Name");
532 rootItem->strings[1] = qtr("Artist");
533 rootItem->strings[2] = qtr("Duration");
536 /* Recreate from root */
537 UpdateNodeChildren( rootItem );
538 if( p_playlist->status.p_item )
540 PLItem *currentItem = FindByInput( rootItem,
541 p_playlist->status.p_item->p_input->i_id );
544 UpdateTreeItem( p_playlist->status.p_item, currentItem,
550 /* And signal the view */
551 emit layoutChanged();
555 /* This function must be entered WITH the playlist lock */
556 void PLModel::UpdateNodeChildren( PLItem *root )
558 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
559 UpdateNodeChildren( p_node, root );
562 /* This function must be entered WITH the playlist lock */
563 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
565 for( int i = 0; i < p_node->i_children ; i++ )
567 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
568 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
569 root->appendChild( newItem, false );
570 UpdateTreeItem( newItem, false, true );
571 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
572 UpdateNodeChildren( p_node->pp_children[i], newItem );
576 /* This function must be entered WITH the playlist lock */
577 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
579 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
580 UpdateTreeItem( p_item, item, signal, force );
583 /* This function must be entered WITH the playlist lock */
584 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
585 bool signal, bool force )
587 if( !force && i_depth == 1 && p_item->p_parent &&
588 p_item->p_parent->i_id != rootItem->i_id )
590 item->update( p_item, p_item == p_playlist->status.p_item );
592 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
595 /************************* Actions ******************************/
598 * Deletion, here we have to do a ugly slow hack as we retrieve the full
599 * list of indexes to delete at once: when we delete a node and all of
600 * its children, we need to update the list.
601 * Todo: investigate whethere we can use ranges to be sure to delete all items?
603 void PLModel::doDelete( QModelIndexList selected )
605 for( int i = selected.size() -1 ; i >= 0; i-- )
607 QModelIndex index = selected[i];
608 if( index.column() != 0 ) continue;
609 PLItem *item = static_cast<PLItem*>(index.internalPointer());
612 if( item->children.size() )
613 recurseDelete( item->children, &selected );
614 doDeleteItem( item, &selected );
619 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
621 for( int i = children.size() - 1; i >= 0 ; i-- )
623 PLItem *item = children[i];
624 if( item->children.size() )
625 recurseDelete( item->children, fullList );
626 doDeleteItem( item, fullList );
630 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
632 QModelIndex deleteIndex = index( item, 0 );
633 fullList->removeAll( deleteIndex );
636 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
641 if( p_item->i_children == -1 )
642 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
644 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
645 /* And finally, remove it from the tree */
646 item->remove( item );
650 /******* Volume III: Sorting and searching ********/
651 void PLModel::sort( int column, Qt::SortOrder order )
654 playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
658 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
659 case 1: i_mode = SORT_ARTIST;break;
660 case 2: i_mode = SORT_DURATION; break;
663 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
664 order == Qt::AscendingOrder ? ORDER_NORMAL :
670 void PLModel::search( QString search_text )
672 /** \todo Fire the search with a small delay ? */
674 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,rootItem->i_id );
676 char *psz_name = search_text.toUtf8().data();
677 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
682 /*********** Popup *********/
683 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
685 assert( index.isValid() );
687 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
691 i_popup_item = p_item->i_id;
692 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
694 current_selection = list;
695 QMenu *menu = new QMenu;
696 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
697 menu->addAction( qfu(I_POP_PREPARSE), this, SLOT( popupPreparse() ) );
698 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
699 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
700 if( p_item->i_children > -1 )
702 menu->addSeparator();
703 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
704 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
706 menu->popup( point );
712 void PLModel::popupDel()
714 doDelete( current_selection );
716 void PLModel::popupPlay()
719 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
720 activateItem( p_item );
724 /**********************************************************************
726 **********************************************************************/
727 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
728 vlc_value_t oval, vlc_value_t nval, void *param )
730 PLModel *p_model = (PLModel *) param;
731 p_model->b_need_update = VLC_TRUE;
735 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
736 vlc_value_t oval, vlc_value_t nval, void *param )
738 PLModel *p_model = (PLModel *) param;
739 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
740 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
741 event = new PLEvent( ItemUpdate_Type, nval.i_int );
742 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
746 static int ItemChanged( 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 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
751 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
755 static int ItemDeleted( 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 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
760 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
764 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
765 vlc_value_t oval, vlc_value_t nval, void *param )
767 PLModel *p_model = (PLModel *) param;
768 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
769 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
771 if( ++p_model->i_items_to_append >= 50 )
773 p_model->b_need_update = VLC_TRUE;
776 PLEvent *event = new PLEvent( p_add );
777 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );