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 fprintf( stderr, "Updating current %i\n" );
136 fprintf( stderr, "Meta %p art %s\n", p_item->p_input->p_meta,p_item->p_input->p_meta ?p_item->p_input->p_meta->psz_arturl : "non" );
137 if( current && p_item->p_input->p_meta &&
138 p_item->p_input->p_meta->psz_arturl &&
139 !strncmp( p_item->p_input->p_meta->psz_arturl, "file://", 7 ) )
141 fprintf( stderr, "Have art %s\n", p_item->p_input->p_meta->psz_arturl );
142 model->sendArt( qfu( p_item->p_input->p_meta->psz_arturl ) );
146 /*************************************************************************
147 * Playlist model implementation
148 *************************************************************************/
150 PLModel::PLModel( playlist_t *_p_playlist,
151 playlist_item_t * p_root, int _i_depth, QObject *parent)
152 : QAbstractItemModel(parent)
155 assert( i_depth == 1 || i_depth == -1 );
156 p_playlist= _p_playlist;
157 i_items_to_append = 0;
158 b_need_update = false;
160 i_cached_input_id = -1;
161 i_popup_item = i_popup_parent = -1;
163 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
164 ADD_ICON( UNKNOWN , unknown );
165 ADD_ICON( AFILE,afile );
166 ADD_ICON( VFILE, vfile );
167 ADD_ICON( DIRECTORY, directory );
168 ADD_ICON( DISC, disc );
169 ADD_ICON( CDDA, cdda );
170 ADD_ICON( CARD, card );
171 ADD_ICON( NET, net );
172 ADD_ICON( PLAYLIST, playlist );
173 ADD_ICON( NODE, node );
187 void PLModel::addCallbacks()
189 /* Some global changes happened -> Rebuild all */
190 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
191 /* We went to the next item */
192 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
193 /* One item has been updated */
194 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
195 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
196 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
199 void PLModel::delCallbacks()
201 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
202 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
203 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
204 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
205 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
208 void PLModel::activateItem( const QModelIndex &index )
210 assert( index.isValid() );
211 PLItem *item = static_cast<PLItem*>(index.internalPointer());
214 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
215 activateItem( p_item );
218 /* Must be entered with lock */
219 void PLModel::activateItem( playlist_item_t *p_item )
221 if( !p_item ) return;
222 playlist_item_t *p_parent = p_item;
225 if( p_parent->i_id == rootItem->i_id ) break;
226 p_parent = p_parent->p_parent;
229 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
232 /****************** Base model mandatory implementations *****************/
233 QVariant PLModel::data(const QModelIndex &index, int role) const
235 assert( index.isValid() );
236 PLItem *item = static_cast<PLItem*>(index.internalPointer());
237 if( role == Qt::DisplayRole )
239 return QVariant( item->columnString( index.column() ) );
241 else if( role == Qt::DecorationRole && index.column() == 0 )
243 if( item->type >= 0 )
244 return QVariant( PLModel::icons[item->type] );
246 else if( role == Qt::FontRole )
248 if( item->current == true )
250 QFont f; f.setBold( true ); return QVariant( f );
256 bool PLModel::isCurrent( const QModelIndex &index )
258 assert( index.isValid() );
259 return static_cast<PLItem*>(index.internalPointer())->current;
262 int PLModel::itemId( const QModelIndex &index ) const
264 assert( index.isValid() );
265 return static_cast<PLItem*>(index.internalPointer())->i_id;
268 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
270 if( !index.isValid() ) return Qt::ItemIsEnabled;
271 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
274 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
277 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
278 return QVariant( rootItem->columnString( section ) );
282 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
286 if (!parent.isValid())
287 parentItem = rootItem;
289 parentItem = static_cast<PLItem*>(parent.internalPointer());
291 PLItem *childItem = parentItem->child(row);
293 return createIndex(row, column, childItem);
295 return QModelIndex();
298 /* Return the index of a given item */
299 QModelIndex PLModel::index( PLItem *item, int column ) const
301 if( !item ) return QModelIndex();
302 const PLItem *parent = item->parent();
304 return createIndex( parent->children.lastIndexOf( item ),
306 return QModelIndex();
309 QModelIndex PLModel::parent(const QModelIndex &index) const
311 if( !index.isValid() ) return QModelIndex();
313 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
314 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
315 PLItem *parentItem = childItem->parent();
316 if( !parentItem || parentItem == rootItem ) return QModelIndex();
317 if( ! parentItem->parentItem )
319 msg_Err( p_playlist, "No parent parent, trying row 0 " );
320 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
321 return createIndex( 0, 0, parentItem );
323 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
327 int PLModel::columnCount( const QModelIndex &i) const
329 if( i_depth == 1 ) return 1;
333 int PLModel::childrenCount(const QModelIndex &parent) const
335 return rowCount( parent );
338 int PLModel::rowCount(const QModelIndex &parent) const
342 if (!parent.isValid())
343 parentItem = rootItem;
345 parentItem = static_cast<PLItem*>(parent.internalPointer());
347 return parentItem->childCount();
350 /************************* General playlist status ***********************/
352 bool PLModel::hasRandom()
354 if( var_GetBool( p_playlist, "random" ) ) return true;
357 bool PLModel::hasRepeat()
359 if( var_GetBool( p_playlist, "repeat" ) ) return true;
362 bool PLModel::hasLoop()
364 if( var_GetBool( p_playlist, "loop" ) ) return true;
367 void PLModel::setLoop( bool on )
369 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
370 config_PutInt( p_playlist, "loop", on ? 1: 0 );
372 void PLModel::setRepeat( bool on )
374 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
375 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
377 void PLModel::setRandom( bool on )
379 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
380 config_PutInt( p_playlist, "random", on ? 1: 0 );
383 /************************* Lookups *****************************/
385 PLItem *PLModel::FindById( PLItem *root, int i_id )
387 return FindInner( root, i_id, false );
390 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
392 return FindInner( root, i_id, true );
395 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
396 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
398 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
400 if( ( !b_input && i_cached_id == i_id) ||
401 ( b_input && i_cached_input_id ==i_id ) )
403 return b_input ? p_cached_item_bi : p_cached_item;
406 if( !b_input && root->i_id == i_id )
411 else if( b_input && root->i_input_id == i_id )
413 ICACHE( i_id, root );
417 QList<PLItem *>::iterator it = root->children.begin();
418 while ( it != root->children.end() )
420 if( !b_input && (*it)->i_id == i_id )
422 CACHE( i_id, (*it) );
423 return p_cached_item;
425 else if( b_input && (*it)->i_input_id == i_id )
427 ICACHE( i_id, (*it) );
428 return p_cached_item_bi;
430 if( (*it)->children.size() )
432 PLItem *childFound = FindInner( (*it), i_id, b_input );
436 ICACHE( i_id, childFound )
438 CACHE( i_id, childFound )
450 /************************* Updates handling *****************************/
451 void PLModel::customEvent( QEvent *event )
453 int type = event->type();
454 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
455 type != ItemDelete_Type )
458 PLEvent *ple = static_cast<PLEvent *>(event);
460 if( type == ItemUpdate_Type )
461 ProcessInputItemUpdate( ple->i_id );
462 else if( type == ItemAppend_Type )
463 ProcessItemAppend( ple->p_add );
465 ProcessItemRemoval( ple->i_id );
468 /**** Events processing ****/
469 void PLModel::ProcessInputItemUpdate( int i_input_id )
471 if( i_input_id <= 0 ) return;
472 PLItem *item = FindByInput( rootItem, i_input_id );
474 UpdateTreeItem( item, true );
477 void PLModel::ProcessItemRemoval( int i_id )
479 if( i_id <= 0 ) return;
480 if( i_id == i_cached_id ) i_cached_id = -1;
481 i_cached_input_id = -1;
482 PLItem *item = FindById( rootItem, i_id );
484 item->remove( item );
487 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
489 playlist_item_t *p_item = NULL;
490 PLItem *newItem = NULL;
492 if( b_need_update ) return;
494 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
496 if( !nodeItem ) goto end;
498 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
499 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
500 if( i_depth == 1 && p_item->p_parent &&
501 p_item->p_parent->i_id != rootItem->i_id )
504 newItem = new PLItem( p_item, nodeItem, this );
505 nodeItem->appendChild( newItem );
506 UpdateTreeItem( p_item, newItem, true );
513 void PLModel::rebuild()
518 void PLModel::rebuild( playlist_item_t *p_root )
520 /* Remove callbacks before locking to avoid deadlocks */
522 /* Invalidate cache */
523 i_cached_id = i_cached_input_id = -1;
529 beginRemoveRows( index( rootItem, 0 ), 0,
530 rootItem->children.size() -1 );
531 qDeleteAll( rootItem->children );
532 rootItem->children.clear();
537 //if( rootItem ) delete rootItem;
538 rootItem = new PLItem( p_root, NULL, this );
539 rootItem->strings[0] = qtr("Name");
540 rootItem->strings[1] = qtr("Artist");
541 rootItem->strings[2] = qtr("Duration");
544 /* Recreate from root */
545 UpdateNodeChildren( rootItem );
546 if( p_playlist->status.p_item )
548 PLItem *currentItem = FindByInput( rootItem,
549 p_playlist->status.p_item->p_input->i_id );
552 UpdateTreeItem( p_playlist->status.p_item, currentItem,
558 /* And signal the view */
559 emit layoutChanged();
563 /* This function must be entered WITH the playlist lock */
564 void PLModel::UpdateNodeChildren( PLItem *root )
566 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
567 UpdateNodeChildren( p_node, root );
570 /* This function must be entered WITH the playlist lock */
571 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
573 for( int i = 0; i < p_node->i_children ; i++ )
575 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
576 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
577 root->appendChild( newItem, false );
578 UpdateTreeItem( newItem, false, true );
579 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
580 UpdateNodeChildren( p_node->pp_children[i], newItem );
584 /* This function must be entered WITH the playlist lock */
585 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
587 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
588 UpdateTreeItem( p_item, item, signal, force );
591 /* This function must be entered WITH the playlist lock */
592 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
593 bool signal, bool force )
595 if( !force && i_depth == 1 && p_item->p_parent &&
596 p_item->p_parent->i_id != rootItem->i_id )
598 item->update( p_item, p_item == p_playlist->status.p_item );
600 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
603 /************************* Actions ******************************/
605 void PLModel::sendArt( QString url )
607 QString arturl = url.replace( "file://",QString("" ) );
608 fprintf( stderr, "send %s\n", qta( arturl ) );
609 emit artSet( arturl );
613 * Deletion, here we have to do a ugly slow hack as we retrieve the full
614 * list of indexes to delete at once: when we delete a node and all of
615 * its children, we need to update the list.
616 * Todo: investigate whethere we can use ranges to be sure to delete all items?
618 void PLModel::doDelete( QModelIndexList selected )
620 for( int i = selected.size() -1 ; i >= 0; i-- )
622 QModelIndex index = selected[i];
623 if( index.column() != 0 ) continue;
624 PLItem *item = static_cast<PLItem*>(index.internalPointer());
627 if( item->children.size() )
628 recurseDelete( item->children, &selected );
629 doDeleteItem( item, &selected );
634 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
636 for( int i = children.size() - 1; i >= 0 ; i-- )
638 PLItem *item = children[i];
639 if( item->children.size() )
640 recurseDelete( item->children, fullList );
641 doDeleteItem( item, fullList );
645 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
647 QModelIndex deleteIndex = index( item, 0 );
648 fullList->removeAll( deleteIndex );
651 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
656 if( p_item->i_children == -1 )
657 playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
659 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
660 /* And finally, remove it from the tree */
661 item->remove( item );
665 /******* Volume III: Sorting and searching ********/
666 void PLModel::sort( int column, Qt::SortOrder order )
669 playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
673 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
674 case 1: i_mode = SORT_ARTIST;break;
675 case 2: i_mode = SORT_DURATION; break;
678 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
679 order == Qt::AscendingOrder ? ORDER_NORMAL :
685 void PLModel::search( QString search_text )
687 /** \todo Fire the search with a small delay ? */
689 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,rootItem->i_id );
691 char *psz_name = search_text.toUtf8().data();
692 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
697 /*********** Popup *********/
698 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
700 assert( index.isValid() );
702 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
706 i_popup_item = p_item->i_id;
707 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
709 current_selection = list;
710 QMenu *menu = new QMenu;
711 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
712 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
713 menu->addSeparator();
714 menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
715 menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
716 menu->addSeparator();
717 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
718 if( p_item->i_children > -1 )
720 menu->addSeparator();
721 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
722 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
724 menu->popup( point );
730 void PLModel::popupDel()
732 doDelete( current_selection );
734 void PLModel::popupPlay()
737 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
738 activateItem( p_item );
742 void PLModel::popupInfo()
744 fprintf( stderr, "Popup Info is NOT implemented\n" );
747 void PLModel::popupStream()
749 fprintf( stderr, "Stream not implemented\n" );
751 void PLModel::popupSave()
753 fprintf( stderr, "Save not implemented\n" );
756 /**********************************************************************
758 **********************************************************************/
759 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
760 vlc_value_t oval, vlc_value_t nval, void *param )
762 PLModel *p_model = (PLModel *) param;
763 // p_model->b_need_update = VLC_TRUE;
767 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
768 vlc_value_t oval, vlc_value_t nval, void *param )
770 PLModel *p_model = (PLModel *) param;
771 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
772 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
773 event = new PLEvent( ItemUpdate_Type, nval.i_int );
774 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
778 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
779 vlc_value_t oval, vlc_value_t nval, void *param )
781 PLModel *p_model = (PLModel *) param;
782 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
783 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
787 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
788 vlc_value_t oval, vlc_value_t nval, void *param )
790 PLModel *p_model = (PLModel *) param;
791 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
792 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
796 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
797 vlc_value_t oval, vlc_value_t nval, void *param )
799 PLModel *p_model = (PLModel *) param;
800 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
801 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
803 if( ++p_model->i_items_to_append >= 50 )
805 // p_model->b_need_update = VLC_TRUE;
806 // return VLC_SUCCESS;
808 PLEvent *event = new PLEvent( p_add );
809 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );