1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006-2007 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 "dialogs/mediainfo.hpp"
33 #include <vlc_intf_strings.h>
35 #include "pixmaps/type_unknown.xpm"
36 #include "pixmaps/type_afile.xpm"
37 #include "pixmaps/type_vfile.xpm"
38 #include "pixmaps/type_net.xpm"
39 #include "pixmaps/type_card.xpm"
40 #include "pixmaps/type_disc.xpm"
41 #include "pixmaps/type_cdda.xpm"
42 #include "pixmaps/type_directory.xpm"
43 #include "pixmaps/type_playlist.xpm"
44 #include "pixmaps/type_node.xpm"
46 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
48 static int PlaylistChanged( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
50 static int PlaylistNext( vlc_object_t *, const char *,
51 vlc_value_t, vlc_value_t, void * );
52 static int ItemChanged( vlc_object_t *, const char *,
53 vlc_value_t, vlc_value_t, void * );
54 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
55 vlc_value_t oval, vlc_value_t nval, void *param );
56 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
57 vlc_value_t oval, vlc_value_t nval, void *param );
59 /*************************************************************************
60 * Playlist item implementation
61 *************************************************************************/
70 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
73 i_id = _i_id; i_input_id = _i_input_id;
77 if( parentItem == NULL )
79 i_showflags = config_GetInt( model->p_intf , "qt-pl-showflags" );
82 i_showflags = parentItem->i_showflags;
83 //Add empty string and update() handles data appending
84 strings.append( qtr("") );
88 void PLItem::updateview( void )
92 if( model->i_depth == 1 ) //left window for playlist etc.
94 strings.append( qtr("") );
98 for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index = i_index*2 )
100 if( i_showflags & i_index )
104 case VLC_META_ENGINE_ARTIST:
105 strings.append( qtr( VLC_META_ARTIST ) );
107 case VLC_META_ENGINE_TITLE:
108 strings.append( qtr( VLC_META_TITLE ) );
110 case VLC_META_ENGINE_DESCRIPTION:
111 strings.append( qtr( VLC_META_DESCRIPTION ) );
113 case VLC_META_ENGINE_DURATION:
114 strings.append( qtr( "Duration" ) );
116 case VLC_META_ENGINE_GENRE:
117 strings.append( qtr( VLC_META_GENRE ) );
119 case VLC_META_ENGINE_COLLECTION:
120 strings.append( qtr( VLC_META_COLLECTION ) );
122 case VLC_META_ENGINE_SEQ_NUM:
123 strings.append( qtr( VLC_META_SEQ_NUM ) );
125 case VLC_META_ENGINE_RATING:
126 strings.append( qtr( VLC_META_RATING ) );
136 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
138 init( _i_id, _i_input_id, parent, m );
141 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
143 init( p_item->i_id, p_item->p_input->i_id, parent, m );
148 qDeleteAll(children);
152 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
156 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
157 children.insert( i_pos, item );
159 model->endInsertRows();
162 void PLItem::remove( PLItem *removed )
164 assert( model && parentItem );
165 int i_index = parentItem->children.indexOf( removed );
166 model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
167 parentItem->children.removeAt( i_index );
168 model->endRemoveRows();
171 int PLItem::row() const
174 return parentItem->children.indexOf(const_cast<PLItem*>(this));
178 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
180 char psz_duration[MSTRTIME_MAX_SIZE];
181 assert( p_item->p_input->i_id == i_input_id );
183 type = p_item->p_input->i_type;
186 char *psz_arturl = input_item_GetArtURL( p_item->p_input );
187 if( ( current && psz_arturl ) &&
188 !strncmp( psz_arturl, "file://", 7 ) )
189 model->sendArt( qfu( psz_arturl ) ) ;
196 if( model->i_depth == 1 ) //left window for playlist etc.
198 strings.append( p_item->p_input->psz_name );
203 #define ADD_META( item, meta ) \
204 psz_meta = input_item_Get ## meta ( item->p_input ); \
205 strings.append( qfu( psz_meta ) ); \
208 for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index = i_index * 2 )
210 if( parentItem->i_showflags & i_index )
214 case VLC_META_ENGINE_ARTIST:
215 ADD_META( p_item, Artist );
217 case VLC_META_ENGINE_TITLE:
218 char *psz_title, *psz_name;
219 psz_title = input_item_GetTitle( p_item->p_input );
220 psz_name = input_item_GetName( p_item->p_input );
223 ADD_META( p_item, Title );
225 strings.append( qfu( psz_name ) );
230 case VLC_META_ENGINE_DESCRIPTION:
231 ADD_META( p_item, Description );
233 case VLC_META_ENGINE_DURATION:
234 secstotimestr( psz_duration, p_item->p_input->i_duration / 1000000 );
235 strings.append( QString( psz_duration ) );
237 case VLC_META_ENGINE_GENRE:
238 ADD_META( p_item, Genre );
240 case VLC_META_ENGINE_COLLECTION:
241 ADD_META( p_item, Album );
243 case VLC_META_ENGINE_SEQ_NUM:
244 ADD_META( p_item, TrackNum );
246 case VLC_META_ENGINE_RATING:
247 ADD_META( p_item, Rating );
258 /*************************************************************************
259 * Playlist model implementation
260 *************************************************************************/
262 PLModel::PLModel( playlist_t *_p_playlist, intf_thread_t *_p_intf,
263 playlist_item_t * p_root, int _i_depth, QObject *parent)
264 : QAbstractItemModel(parent)
267 assert( i_depth == 1 || i_depth == -1 );
269 p_playlist= _p_playlist;
270 i_items_to_append = 0;
271 b_need_update = false;
273 i_cached_input_id = -1;
274 i_popup_item = i_popup_parent = -1;
276 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
277 ADD_ICON( UNKNOWN , unknown );
278 ADD_ICON( AFILE,afile );
279 ADD_ICON( VFILE, vfile );
280 ADD_ICON( DIRECTORY, directory );
281 ADD_ICON( DISC, disc );
282 ADD_ICON( CDDA, cdda );
283 ADD_ICON( CARD, card );
284 ADD_ICON( NET, net );
285 ADD_ICON( PLAYLIST, playlist );
286 ADD_ICON( NODE, node );
300 Qt::DropActions PLModel::supportedDropActions() const
302 return Qt::CopyAction;
305 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
307 Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
308 if( index.isValid() )
309 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
311 return Qt::ItemIsDropEnabled | defaultFlags;
314 QStringList PLModel::mimeTypes() const
317 types << "vlc/playlist-item-id";
321 QMimeData *PLModel::mimeData(const QModelIndexList &indexes) const
323 QMimeData *mimeData = new QMimeData();
324 QByteArray encodedData;
325 QDataStream stream(&encodedData, QIODevice::WriteOnly);
327 foreach (QModelIndex index, indexes) {
328 if (index.isValid() && index.column() == 0 )
329 stream << itemId(index);
331 mimeData->setData("vlc/playlist-item-id", encodedData);
335 bool PLModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
336 int row, int column, const QModelIndex &target)
338 if ( data->hasFormat("vlc/playlist-item-id") )
340 if (action == Qt::IgnoreAction)
344 if( target.isValid() )
345 targetItem = static_cast<PLItem*>( target.internalPointer() );
347 targetItem = rootItem;
349 QByteArray encodedData = data->data("vlc/playlist-item-id");
350 QDataStream stream(&encodedData, QIODevice::ReadOnly);
352 PLItem *newParentItem;
353 while (!stream.atEnd())
360 playlist_item_t *p_target =
361 playlist_ItemGetById( p_playlist, targetItem->i_id,
363 playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
366 if( !p_target || !p_src )
371 if( p_target->i_children == -1 ) /* A leaf */
373 PLItem *parentItem = targetItem->parent();
374 assert( parentItem );
375 playlist_item_t *p_parent =
376 playlist_ItemGetById( p_playlist, parentItem->i_id,
383 for( i = 0 ; i< p_parent->i_children ; i++ )
384 if( p_parent->pp_children[i] == p_target ) break;
385 playlist_TreeMove( p_playlist, p_src, p_parent, i );
386 newParentItem = parentItem;
390 /* \todo: if we drop on a top-level node, use copy instead ? */
391 playlist_TreeMove( p_playlist, p_src, p_target, 0 );
393 newParentItem = targetItem;
395 /* Remove from source */
396 PLItem *srcItem = FindById( rootItem, p_src->i_id );
397 // We dropped on the source selector. Ask the dialog to forward
401 emit shouldRemove( p_src->i_id );
404 srcItem->remove( srcItem );
406 /* Display at new destination */
407 PLItem *newItem = new PLItem( p_src, newParentItem, this );
408 newParentItem->insertChild( newItem, i, true );
409 UpdateTreeItem( p_src, newItem, true );
410 if( p_src->i_children != -1 )
411 UpdateNodeChildren( newItem );
418 void PLModel::removeItem( int i_id )
420 PLItem *item = FindById( rootItem,i_id );
421 if( item ) item->remove( item );
424 void PLModel::addCallbacks()
426 /* Some global changes happened -> Rebuild all */
427 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
428 /* We went to the next item */
429 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
430 /* One item has been updated */
431 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
432 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
433 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
436 void PLModel::delCallbacks()
438 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
439 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
440 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
441 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
442 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
445 void PLModel::activateItem( const QModelIndex &index )
447 assert( index.isValid() );
448 PLItem *item = static_cast<PLItem*>(index.internalPointer());
451 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
453 activateItem( p_item );
456 /* Must be entered with lock */
457 void PLModel::activateItem( playlist_item_t *p_item )
459 if( !p_item ) return;
460 playlist_item_t *p_parent = p_item;
463 if( p_parent->i_id == rootItem->i_id ) break;
464 p_parent = p_parent->p_parent;
467 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, p_parent, p_item );
470 /****************** Base model mandatory implementations *****************/
471 QVariant PLModel::data(const QModelIndex &index, int role) const
473 if(!index.isValid() ) return QVariant();
474 PLItem *item = static_cast<PLItem*>(index.internalPointer());
475 if( role == Qt::DisplayRole )
477 return QVariant( item->columnString( index.column() ) );
479 else if( role == Qt::DecorationRole && index.column() == 0 )
481 if( item->type >= 0 )
482 return QVariant( PLModel::icons[item->type] );
484 else if( role == Qt::FontRole )
486 if( item->current == true )
488 QFont f; f.setBold( true ); return QVariant( f );
494 bool PLModel::isCurrent( const QModelIndex &index )
496 assert( index.isValid() );
497 return static_cast<PLItem*>(index.internalPointer())->current;
500 int PLModel::itemId( const QModelIndex &index ) const
502 assert( index.isValid() );
503 return static_cast<PLItem*>(index.internalPointer())->i_id;
506 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
509 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
510 return QVariant( rootItem->columnString( section ) );
514 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
518 if (!parent.isValid())
519 parentItem = rootItem;
521 parentItem = static_cast<PLItem*>(parent.internalPointer());
523 PLItem *childItem = parentItem->child(row);
525 return createIndex(row, column, childItem);
527 return QModelIndex();
530 /* Return the index of a given item */
531 QModelIndex PLModel::index( PLItem *item, int column ) const
533 if( !item ) return QModelIndex();
534 const PLItem *parent = item->parent();
536 return createIndex( parent->children.lastIndexOf( item ),
538 return QModelIndex();
541 QModelIndex PLModel::parent(const QModelIndex &index) const
543 if( !index.isValid() ) return QModelIndex();
545 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
546 if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
547 PLItem *parentItem = childItem->parent();
548 if( !parentItem || parentItem == rootItem ) return QModelIndex();
549 if( ! parentItem->parentItem )
551 msg_Err( p_playlist, "No parent parent, trying row 0 " );
552 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
553 return createIndex( 0, 0, parentItem );
555 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
559 int PLModel::columnCount( const QModelIndex &i) const
561 return rootItem->strings.count();
564 int PLModel::childrenCount(const QModelIndex &parent) const
566 return rowCount( parent );
569 int PLModel::rowCount(const QModelIndex &parent) const
573 if (!parent.isValid())
574 parentItem = rootItem;
576 parentItem = static_cast<PLItem*>(parent.internalPointer());
578 return parentItem->childCount();
581 /************************* General playlist status ***********************/
583 bool PLModel::hasRandom()
585 if( var_GetBool( p_playlist, "random" ) ) return true;
588 bool PLModel::hasRepeat()
590 if( var_GetBool( p_playlist, "repeat" ) ) return true;
593 bool PLModel::hasLoop()
595 if( var_GetBool( p_playlist, "loop" ) ) return true;
598 void PLModel::setLoop( bool on )
600 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
601 config_PutInt( p_playlist, "loop", on ? 1: 0 );
603 void PLModel::setRepeat( bool on )
605 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
606 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
608 void PLModel::setRandom( bool on )
610 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
611 config_PutInt( p_playlist, "random", on ? 1: 0 );
614 /************************* Lookups *****************************/
616 PLItem *PLModel::FindById( PLItem *root, int i_id )
618 return FindInner( root, i_id, false );
621 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
623 return FindInner( root, i_id, true );
626 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
627 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
629 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
631 if( ( !b_input && i_cached_id == i_id) ||
632 ( b_input && i_cached_input_id ==i_id ) )
634 return b_input ? p_cached_item_bi : p_cached_item;
637 if( !b_input && root->i_id == i_id )
642 else if( b_input && root->i_input_id == i_id )
644 ICACHE( i_id, root );
648 QList<PLItem *>::iterator it = root->children.begin();
649 while ( it != root->children.end() )
651 if( !b_input && (*it)->i_id == i_id )
653 CACHE( i_id, (*it) );
654 return p_cached_item;
656 else if( b_input && (*it)->i_input_id == i_id )
658 ICACHE( i_id, (*it) );
659 return p_cached_item_bi;
661 if( (*it)->children.size() )
663 PLItem *childFound = FindInner( (*it), i_id, b_input );
667 ICACHE( i_id, childFound )
669 CACHE( i_id, childFound )
681 /************************* Updates handling *****************************/
682 void PLModel::customEvent( QEvent *event )
684 int type = event->type();
685 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
686 type != ItemDelete_Type && type != PLUpdate_Type )
689 PLEvent *ple = static_cast<PLEvent *>(event);
691 if( type == ItemUpdate_Type )
692 ProcessInputItemUpdate( ple->i_id );
693 else if( type == ItemAppend_Type )
694 ProcessItemAppend( ple->p_add );
695 else if( type == ItemDelete_Type )
696 ProcessItemRemoval( ple->i_id );
701 /**** Events processing ****/
702 void PLModel::ProcessInputItemUpdate( int i_input_id )
704 if( i_input_id <= 0 ) return;
705 PLItem *item = FindByInput( rootItem, i_input_id );
707 UpdateTreeItem( item, true );
710 void PLModel::ProcessItemRemoval( int i_id )
712 if( i_id <= 0 ) return;
713 if( i_id == i_cached_id ) i_cached_id = -1;
714 i_cached_input_id = -1;
715 PLItem *item = FindById( rootItem, i_id );
717 item->remove( item );
720 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
722 playlist_item_t *p_item = NULL;
723 PLItem *newItem = NULL;
725 if( b_need_update ) return;
727 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
729 if( !nodeItem ) goto end;
731 p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
732 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
733 if( i_depth == 1 && p_item->p_parent &&
734 p_item->p_parent->i_id != rootItem->i_id )
737 newItem = new PLItem( p_item, nodeItem, this );
738 nodeItem->appendChild( newItem );
739 UpdateTreeItem( p_item, newItem, true );
746 void PLModel::rebuild()
751 void PLModel::rebuild( playlist_item_t *p_root )
753 /* Remove callbacks before locking to avoid deadlocks */
755 /* Invalidate cache */
756 i_cached_id = i_cached_input_id = -1;
762 if( rootItem->children.size() )
764 beginRemoveRows( index( rootItem, 0 ), 0,
765 rootItem->children.size() -1 );
766 qDeleteAll( rootItem->children );
767 rootItem->children.clear();
773 //if( rootItem ) delete rootItem;
774 rootItem = new PLItem( p_root, NULL, this );
777 /* Recreate from root */
778 UpdateNodeChildren( rootItem );
779 if( p_playlist->status.p_item )
781 PLItem *currentItem = FindByInput( rootItem,
782 p_playlist->status.p_item->p_input->i_id );
785 UpdateTreeItem( p_playlist->status.p_item, currentItem,
791 /* And signal the view */
792 emit layoutChanged();
796 /* This function must be entered WITH the playlist lock */
797 void PLModel::UpdateNodeChildren( PLItem *root )
799 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
801 UpdateNodeChildren( p_node, root );
804 /* This function must be entered WITH the playlist lock */
805 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
807 for( int i = 0; i < p_node->i_children ; i++ )
809 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
810 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
811 root->appendChild( newItem, false );
812 UpdateTreeItem( newItem, false, true );
813 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
814 UpdateNodeChildren( p_node->pp_children[i], newItem );
818 /* This function must be entered WITH the playlist lock */
819 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
821 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
823 UpdateTreeItem( p_item, item, signal, force );
826 /* This function must be entered WITH the playlist lock */
827 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
828 bool signal, bool force )
832 if( !force && i_depth == 1 && p_item->p_parent &&
833 p_item->p_parent->i_id != rootItem->i_id )
835 item->update( p_item, p_item == p_playlist->status.p_item );
837 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
840 /************************* Actions ******************************/
842 void PLModel::sendArt( QString url )
844 QString arturl = url.replace( "file://",QString("" ) );
845 emit artSet( arturl );
848 void PLModel::removeArt()
850 emit artSet( QString() );
854 * Deletion, here we have to do a ugly slow hack as we retrieve the full
855 * list of indexes to delete at once: when we delete a node and all of
856 * its children, we need to update the list.
857 * Todo: investigate whethere we can use ranges to be sure to delete all items?
859 void PLModel::doDelete( QModelIndexList selected )
861 for( int i = selected.size() -1 ; i >= 0; i-- )
863 QModelIndex index = selected[i];
864 if( index.column() != 0 ) continue;
865 PLItem *item = static_cast<PLItem*>(index.internalPointer());
868 if( item->children.size() )
869 recurseDelete( item->children, &selected );
870 doDeleteItem( item, &selected );
875 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
877 for( int i = children.size() - 1; i >= 0 ; i-- )
879 PLItem *item = children[i];
880 if( item->children.size() )
881 recurseDelete( item->children, fullList );
882 doDeleteItem( item, fullList );
886 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
888 QModelIndex deleteIndex = index( item, 0 );
889 fullList->removeAll( deleteIndex );
892 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
898 if( p_item->i_children == -1 )
899 playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
901 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
902 /* And finally, remove it from the tree */
903 item->remove( item );
907 /******* Volume III: Sorting and searching ********/
908 void PLModel::sort( int column, Qt::SortOrder order )
912 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
918 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
919 case 1: i_mode = SORT_ARTIST;break;
920 case 2: i_mode = SORT_DURATION; break;
921 default: i_mode = SORT_TITLE_NODES_FIRST; break;
924 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
925 order == Qt::AscendingOrder ?
926 ORDER_NORMAL : ORDER_REVERSE );
932 void PLModel::search( QString search_text )
934 /** \todo Fire the search with a small delay ? */
937 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
941 char *psz_name = search_text.toUtf8().data();
942 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
948 /*********** Popup *********/
949 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
951 assert( index.isValid() );
953 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
954 itemId( index ), VLC_TRUE );
957 i_popup_item = p_item->i_id;
958 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
960 current_selection = list;
961 QMenu *menu = new QMenu;
962 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
963 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
964 menu->addSeparator();
965 menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
966 menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
967 menu->addSeparator();
968 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
969 if( p_item->i_children > -1 )
971 menu->addSeparator();
972 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
973 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
975 menu->addSeparator();
977 ContextUpdateMapper = new QSignalMapper(this);
979 QMenu *selectColMenu = new QMenu( qtr("Show columns") );
981 #define ADD_META_ACTION( meta ) { \
982 QAction* option = selectColMenu->addAction( qfu(VLC_META_##meta) ); \
983 option->setCheckable( true ); \
984 option->setChecked( rootItem->i_showflags & VLC_META_ENGINE_##meta ); \
985 ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta ); \
986 CONNECT( option, triggered(), ContextUpdateMapper, map() ); \
988 CONNECT(ContextUpdateMapper, mapped( int ), this, viewchanged( int ) );
990 ADD_META_ACTION( TITLE );
991 ADD_META_ACTION( ARTIST );
992 ADD_META_ACTION( DURATION );
993 ADD_META_ACTION( COLLECTION );
994 ADD_META_ACTION( GENRE );
995 ADD_META_ACTION( SEQ_NUM );
996 ADD_META_ACTION( RATING );
997 ADD_META_ACTION( DESCRIPTION );
999 #undef ADD_META_ACTION
1000 menu->addMenu( selectColMenu );
1001 menu->popup( point );
1008 void PLModel::viewchanged( int meta )
1015 case VLC_META_ENGINE_TITLE:
1018 case VLC_META_ENGINE_DURATION:
1021 case VLC_META_ENGINE_ARTIST:
1024 case VLC_META_ENGINE_GENRE:
1027 case VLC_META_ENGINE_COPYRIGHT:
1030 case VLC_META_ENGINE_COLLECTION:
1033 case VLC_META_ENGINE_SEQ_NUM:
1036 case VLC_META_ENGINE_DESCRIPTION:
1042 emit layoutAboutToBeChanged();
1043 index = __MIN( index , rootItem->strings.count() );
1044 QModelIndex parent = createIndex( 0, 0, rootItem );
1045 if( rootItem->i_showflags & meta )
1047 beginRemoveColumns( parent , index, index+1 );
1048 rootItem->i_showflags &= ~( meta );
1049 rootItem->updateview();
1054 beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
1055 rootItem->i_showflags |= meta;
1056 rootItem->updateview();
1060 config_PutInt( p_intf, "qt-pl-showflags", rootItem->i_showflags );
1061 config_SaveConfigFile( p_intf, NULL );
1065 void PLModel::popupDel()
1067 doDelete( current_selection );
1069 void PLModel::popupPlay()
1073 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1074 i_popup_item,VLC_TRUE );
1075 activateItem( p_item );
1080 void PLModel::popupInfo()
1082 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1083 i_popup_item,VLC_TRUE );
1086 MediaInfoDialog *mid = new MediaInfoDialog( p_intf );
1087 mid->setInput( p_item->p_input );
1092 void PLModel::popupStream()
1094 fprintf( stderr, "Stream not implemented\n" );
1096 void PLModel::popupSave()
1098 fprintf( stderr, "Save not implemented\n" );
1101 /**********************************************************************
1102 * Playlist callbacks
1103 **********************************************************************/
1104 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1105 vlc_value_t oval, vlc_value_t nval, void *param )
1107 PLModel *p_model = (PLModel *) param;
1108 PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
1109 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1113 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1114 vlc_value_t oval, vlc_value_t nval, void *param )
1116 PLModel *p_model = (PLModel *) param;
1117 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
1118 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1119 event = new PLEvent( ItemUpdate_Type, nval.i_int );
1120 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1124 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1125 vlc_value_t oval, vlc_value_t nval, void *param )
1127 PLModel *p_model = (PLModel *) param;
1128 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
1129 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1133 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1134 vlc_value_t oval, vlc_value_t nval, void *param )
1136 PLModel *p_model = (PLModel *) param;
1137 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
1138 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1142 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1143 vlc_value_t oval, vlc_value_t nval, void *param )
1145 PLModel *p_model = (PLModel *) param;
1146 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
1147 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1149 if( ++p_model->i_items_to_append >= 50 )
1151 // p_model->b_need_update = VLC_TRUE;
1152 // return VLC_SUCCESS;
1154 PLEvent *event = new PLEvent( p_add );
1155 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );