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>
8 * Ilkka Ollakkka <ileoo (at) videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include "dialogs_provider.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "dialogs/mediainfo.hpp"
33 #include "dialogs/playlist.hpp"
34 #include <vlc_intf_strings.h>
36 #include "pixmaps/types/type_unknown.xpm"
42 #include <QApplication>
47 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
49 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
50 vlc_value_t oval, vlc_value_t nval, void *param );
51 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
52 vlc_value_t oval, vlc_value_t nval, void *param );
54 /*************************************************************************
55 * Playlist model implementation
56 *************************************************************************/
59 This model is called two times, for the selector and the standard panel
61 PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
62 intf_thread_t *_p_intf, /* main Qt p_intf */
63 playlist_item_t * p_root,
64 /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
65 and THEPL->p_root_category for SelectPL */
66 int _i_depth, /* -1 for StandPL, 1 for SelectPL */
67 QObject *parent ) /* Basic Qt parent */
68 : QAbstractItemModel( parent )
71 assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
73 p_playlist = _p_playlist;
75 i_cached_input_id = -1;
76 i_popup_item = i_popup_parent = -1;
79 rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
81 if( i_depth == DEPTH_SEL )
85 i_showflags = getSettings()->value( "qt-pl-showflags", COLUMN_DEFAULT ).toInt();
87 i_showflags = COLUMN_DEFAULT; /* reasonable default to show something */
88 else if ( i_showflags >= COLUMN_END )
89 i_showflags = COLUMN_END - 1; /* show everything */
92 /* Icons initialization */
93 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
94 ADD_ICON( UNKNOWN , type_unknown_xpm );
95 ADD_ICON( FILE, ":/type/file" );
96 ADD_ICON( DIRECTORY, ":/type/directory" );
97 ADD_ICON( DISC, ":/type/disc" );
98 ADD_ICON( CDDA, ":/type/cdda" );
99 ADD_ICON( CARD, ":/type/capture-card" );
100 ADD_ICON( NET, ":/type/net" );
101 ADD_ICON( PLAYLIST, ":/type/playlist" );
102 ADD_ICON( NODE, ":/type/node" );
105 ContextUpdateMapper = new QSignalMapper(this);
108 CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
109 this, processInputItemUpdate( input_item_t *) );
110 CONNECT( THEMIM, inputChanged( input_thread_t * ),
111 this, processInputItemUpdate( input_thread_t* ) );
112 CONNECT( ContextUpdateMapper, mapped( int ), this, toggleColumnShown( int ) );
118 getSettings()->setValue( "qt-pl-showflags", i_showflags );
123 Qt::DropActions PLModel::supportedDropActions() const
125 return Qt::CopyAction; /* Why not Qt::MoveAction */
128 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
130 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
132 PLItem *item = index.isValid() ? getItem( index ) : rootItem;
134 input_item_t *pl_input =
135 p_playlist->p_local_category ?
136 p_playlist->p_local_category->p_input : NULL;
137 input_item_t *ml_input =
138 p_playlist->p_ml_category ?
139 p_playlist->p_ml_category->p_input : NULL;
141 if( i_depth == DEPTH_SEL )
143 if( ( pl_input && item->p_input == pl_input ) ||
144 ( ml_input && item->p_input == ml_input ) )
145 flags |= Qt::ItemIsDropEnabled;
147 else if( ( pl_input && rootItem->p_input == pl_input ) ||
148 ( ml_input && rootItem->p_input == ml_input ) )
151 playlist_item_t *plItem =
152 playlist_ItemGetById( p_playlist, item->i_id );
154 if ( plItem && ( plItem->i_children > -1 ) )
155 flags |= Qt::ItemIsDropEnabled;
160 flags |= Qt::ItemIsDragEnabled;
165 QStringList PLModel::mimeTypes() const
168 types << "vlc/qt-playlist-item";
172 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
174 QMimeData *mimeData = new QMimeData();
175 QByteArray encodedData;
176 QDataStream stream( &encodedData, QIODevice::WriteOnly );
177 QModelIndexList list;
179 foreach( const QModelIndex &index, indexes ) {
180 if( index.isValid() && index.column() == 0 )
186 foreach( const QModelIndex &index, list ) {
187 PLItem *item = getItem( index );
188 stream.writeRawData( (char*) &item, sizeof( PLItem* ) );
190 mimeData->setData( "vlc/qt-playlist-item", encodedData );
195 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
196 int row, int column, const QModelIndex &parent )
198 if( data->hasFormat( "vlc/qt-playlist-item" ) )
200 if( action == Qt::IgnoreAction )
203 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
206 playlist_item_t *p_parent =
207 playlist_ItemGetById( p_playlist, parentItem->i_id );
208 if( !p_parent || p_parent->i_children == -1 )
215 playlist_item_t *p_pl = p_playlist->p_local_category;
216 playlist_item_t *p_ml = p_playlist->p_ml_category;
220 ( p_pl && p_parent->p_input == p_pl->p_input ) ||
221 ( p_ml && p_parent->p_input == p_ml->p_input ) )
226 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
228 dropAppendCopy( encodedData, parentItem );
230 dropMove( encodedData, parentItem, row );
235 void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
237 QDataStream stream( &data, QIODevice::ReadOnly );
240 playlist_item_t *p_parent =
241 playlist_ItemGetById( p_playlist, target->i_id );
242 while( !stream.atEnd() )
245 stream.readRawData( (char*)&item, sizeof(PLItem*) );
246 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
247 if( !p_item ) continue;
248 input_item_t *p_input = p_item->p_input;
249 playlist_AddExt ( p_playlist,
250 p_input->psz_uri, p_input->psz_name,
251 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
253 p_input->i_options, p_input->ppsz_options, p_input->optflagc,
254 p_parent == p_playlist->p_local_category, true );
259 void PLModel::dropMove( QByteArray& data, PLItem *target, int row )
261 QDataStream stream( &data, QIODevice::ReadOnly );
262 QList<PLItem*> model_items;
264 int new_pos = row == -1 ? target->children.size() : row;
265 int model_pos = new_pos;
266 while( !stream.atEnd() )
269 stream.readRawData( (char*)&item, sizeof(PLItem*) );
271 /* better not try to move a node into itself: */
272 PLItem *climber = target;
275 if( climber == item ) break;
276 climber = climber->parentItem;
278 if( climber ) continue;
280 if( item->parentItem == target &&
281 target->children.indexOf( item ) < model_pos )
284 ids.append( item->i_id );
285 model_items.append( item );
289 int count = ids.size();
292 playlist_item_t *pp_items[count];
295 for( int i = 0; i < count; i++ )
297 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, ids[i] );
303 pp_items[i] = p_item;
305 playlist_item_t *p_parent =
306 playlist_ItemGetById( p_playlist, target->i_id );
307 playlist_TreeMoveMany( p_playlist, count, pp_items, p_parent,
311 insertChildren( target, model_items, model_pos );
315 /* remove item with its id */
316 void PLModel::removeItem( int i_id )
318 PLItem *item = findById( rootItem, i_id );
322 /* callbacks and slots */
323 void PLModel::addCallbacks()
325 /* One item has been updated */
326 var_AddCallback( p_playlist, "playlist-item-append", ItemAppended, this );
327 var_AddCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
330 void PLModel::delCallbacks()
332 var_DelCallback( p_playlist, "playlist-item-append", ItemAppended, this );
333 var_DelCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
336 void PLModel::activateItem( const QModelIndex &index )
338 assert( index.isValid() );
339 PLItem *item = getItem( index );
342 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
343 activateItem( p_item );
347 /* Must be entered with lock */
348 void PLModel::activateItem( playlist_item_t *p_item )
350 if( !p_item ) return;
351 playlist_item_t *p_parent = p_item;
354 if( p_parent->i_id == rootItem->i_id ) break;
355 p_parent = p_parent->p_parent;
358 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
362 /****************** Base model mandatory implementations *****************/
363 QVariant PLModel::data( const QModelIndex &index, int role ) const
365 if( !index.isValid() ) return QVariant();
366 PLItem *item = getItem( index );
367 if( role == Qt::DisplayRole )
369 if( i_depth == DEPTH_SEL )
371 vlc_mutex_lock( &item->p_input->lock );
372 QString returninfo = QString( qfu( item->p_input->psz_name ) );
373 vlc_mutex_unlock( &item->p_input->lock );
374 return QVariant(returninfo);
377 int metadata = columnToMeta( index.column(), i_showflags );
378 if( metadata == COLUMN_END ) return QVariant();
381 if( metadata == COLUMN_NUMBER )
382 returninfo = QString::number( index.row() + 1 );
385 char *psz = psz_column_meta( item->p_input, metadata );
386 returninfo = qfu( psz );
389 return QVariant( returninfo );
391 else if( role == Qt::DecorationRole && index.column() == 0 )
393 /* Use to segfault here because i_type wasn't always initialized */
394 if( item->p_input->i_type >= 0 )
395 return QVariant( PLModel::icons[item->p_input->i_type] );
397 else if( role == Qt::FontRole )
399 if( isCurrent( index ) )
401 QFont f; f.setBold( true ); return QVariant( f );
407 bool PLModel::isCurrent( const QModelIndex &index ) const
409 if( !currentItem ) return false;
410 return getItem( index )->p_input == currentItem->p_input;
413 int PLModel::itemId( const QModelIndex &index ) const
415 return getItem( index )->i_id;
418 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
421 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
424 if( i_depth == DEPTH_SEL ) return QVariant( QString("") );
426 int meta_col = columnToMeta( section, i_showflags );
428 if( meta_col == COLUMN_END ) return QVariant();
430 return QVariant( qfu( psz_column_title( meta_col ) ) );
433 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
436 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
438 PLItem *childItem = parentItem->child( row );
440 return createIndex( row, column, childItem );
442 return QModelIndex();
445 /* Return the index of a given item */
446 QModelIndex PLModel::index( PLItem *item, int column ) const
448 if( !item ) return QModelIndex();
449 const PLItem *parent = item->parent();
451 return createIndex( parent->children.lastIndexOf( item ),
453 return QModelIndex();
456 QModelIndex PLModel::parent( const QModelIndex &index ) const
458 if( !index.isValid() ) return QModelIndex();
460 PLItem *childItem = getItem( index );
463 msg_Err( p_playlist, "NULL CHILD" );
464 return QModelIndex();
467 PLItem *parentItem = childItem->parent();
468 if( !parentItem || parentItem == rootItem ) return QModelIndex();
469 if( !parentItem->parentItem )
471 msg_Err( p_playlist, "No parent parent, trying row 0 " );
472 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
473 return createIndex( 0, 0, parentItem );
475 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
479 int PLModel::columnCount( const QModelIndex &i) const
483 if( i_depth == DEPTH_SEL ) return 1;
485 while( metadata < COLUMN_END )
487 if( metadata & i_showflags )
494 int PLModel::rowCount( const QModelIndex &parent ) const
496 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
497 return parentItem->childCount();
500 QStringList PLModel::selectedURIs()
503 for( int i = 0; i < current_selection.size(); i++ )
505 PLItem *item = getItem( current_selection[i] );
509 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
512 char *psz = input_item_GetURI( p_item->p_input );
525 /************************* General playlist status ***********************/
527 bool PLModel::hasRandom()
529 return var_GetBool( p_playlist, "random" );
531 bool PLModel::hasRepeat()
533 return var_GetBool( p_playlist, "repeat" );
535 bool PLModel::hasLoop()
537 return var_GetBool( p_playlist, "loop" );
539 void PLModel::setLoop( bool on )
541 var_SetBool( p_playlist, "loop", on ? true:false );
542 config_PutInt( p_playlist, "loop", on ? 1: 0 );
544 void PLModel::setRepeat( bool on )
546 var_SetBool( p_playlist, "repeat", on ? true:false );
547 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
549 void PLModel::setRandom( bool on )
551 var_SetBool( p_playlist, "random", on ? true:false );
552 config_PutInt( p_playlist, "random", on ? 1: 0 );
555 /************************* Lookups *****************************/
557 PLItem *PLModel::findById( PLItem *root, int i_id )
559 return findInner( root, i_id, false );
562 PLItem *PLModel::findByInput( PLItem *root, int i_id )
564 PLItem *result = findInner( root, i_id, true );
568 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
569 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
571 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
573 if( ( !b_input && i_cached_id == i_id) ||
574 ( b_input && i_cached_input_id ==i_id ) )
576 return b_input ? p_cached_item_bi : p_cached_item;
579 if( !b_input && root->i_id == i_id )
584 else if( b_input && root->p_input->i_id == i_id )
586 ICACHE( i_id, root );
590 QList<PLItem *>::iterator it = root->children.begin();
591 while ( it != root->children.end() )
593 if( !b_input && (*it)->i_id == i_id )
595 CACHE( i_id, (*it) );
596 return p_cached_item;
598 else if( b_input && (*it)->p_input->i_id == i_id )
600 ICACHE( i_id, (*it) );
601 return p_cached_item_bi;
603 if( (*it)->children.size() )
605 PLItem *childFound = findInner( (*it), i_id, b_input );
609 ICACHE( i_id, childFound )
611 CACHE( i_id, childFound )
622 PLItem *PLModel::getItem( QModelIndex index )
624 assert( index.isValid() );
625 return static_cast<PLItem*>( index.internalPointer() );
629 Computes meta data column id from shown column index and shown columns flags.
630 Returns COLUMN_END in case of failure.
632 int PLModel::columnToMeta( int column, int shown_flags ) const
637 while( meta < COLUMN_END )
639 if( meta & shown_flags )
641 if( index == column )
650 Computes shown column index from meta data column id and shown columns flags.
651 meta_col must be contained in shown_flags!
653 int PLModel::columnFromMeta( int meta_col, int shown_flags ) const
655 assert( meta_col & shown_flags );
660 while( meta < COLUMN_END )
662 if( meta & shown_flags )
664 if( meta == meta_col )
672 /************************* Updates handling *****************************/
673 void PLModel::customEvent( QEvent *event )
675 int type = event->type();
676 if( type != ItemAppend_Type &&
677 type != ItemDelete_Type )
680 PLEvent *ple = static_cast<PLEvent *>(event);
682 if( type == ItemAppend_Type )
683 processItemAppend( &ple->add );
684 else if( type == ItemDelete_Type )
685 processItemRemoval( ple->i_id );
688 /**** Events processing ****/
689 void PLModel::processInputItemUpdate( input_thread_t *p_input )
691 if( !p_input ) return;
692 processInputItemUpdate( input_GetItem( p_input ) );
693 if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
695 PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
697 emit currentChanged( index( item, 0 ) );
704 void PLModel::processInputItemUpdate( input_item_t *p_item )
706 if( !p_item || p_item->i_id <= 0 ) return;
707 PLItem *item = findByInput( rootItem, p_item->i_id );
709 updateTreeItem( item, true, true);
712 void PLModel::processItemRemoval( int i_id )
714 if( i_id <= 0 ) return;
715 if( i_id == i_cached_id ) i_cached_id = -1;
716 i_cached_input_id = -1;
721 void PLModel::processItemAppend( const playlist_add_t *p_add )
723 playlist_item_t *p_item = NULL;
724 PLItem *newItem = NULL;
726 PLItem *nodeItem = findById( rootItem, p_add->i_node );
727 if( !nodeItem ) return;
730 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
731 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
732 if( i_depth == DEPTH_SEL && p_item->p_parent &&
733 p_item->p_parent->i_id != rootItem->i_id )
736 newItem = new PLItem( p_item, nodeItem );
739 beginInsertRows( index( nodeItem, 0 ), nodeItem->childCount(), nodeItem->childCount() );
740 nodeItem->appendChild( newItem );
742 updateTreeItem( newItem, true );
750 void PLModel::rebuild()
755 void PLModel::rebuild( playlist_item_t *p_root )
757 playlist_item_t* p_item;
758 /* Remove callbacks before locking to avoid deadlocks */
760 /* Invalidate cache */
761 i_cached_id = i_cached_input_id = -1;
763 if( rootItem ) rootItem->removeChildren();
769 rootItem = new PLItem( p_root );
772 /* Recreate from root */
773 updateChildren( rootItem );
774 if( (p_item = playlist_CurrentPlayingItem(p_playlist)) )
775 currentItem = findByInput( rootItem, p_item->p_input->i_id );
780 /* And signal the view */
783 emit currentChanged( index( currentItem, 0 ) );
788 void PLModel::takeItem( PLItem *item )
791 PLItem *parent = item->parentItem;
793 int i_index = parent->children.indexOf( item );
795 beginRemoveRows( index( parent, 0 ), i_index, i_index );
796 parent->takeChildAt( i_index );
800 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
803 int count = items.size();
805 beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
806 for( int i = 0; i < count; i++ )
808 node->children.insert( i_pos + i, items[i] );
809 items[i]->parentItem = node;
814 void PLModel::removeItem( PLItem *item )
817 if( currentItem == item )
820 emit currentChanged( QModelIndex() );
822 PLItem *parent = item->parentItem;
824 parent->removeChild( item );
827 /* This function must be entered WITH the playlist lock */
828 void PLModel::updateChildren( PLItem *root )
830 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
831 updateChildren( p_node, root );
834 /* This function must be entered WITH the playlist lock */
835 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
837 playlist_item_t *p_item = playlist_CurrentPlayingItem(p_playlist);
838 for( int i = 0; i < p_node->i_children ; i++ )
840 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
841 PLItem *newItem = new PLItem( p_node->pp_children[i], root );
842 root->appendChild( newItem );
843 if( p_item && newItem->p_input == p_item->p_input )
845 currentItem = newItem;
846 emit currentChanged( index( currentItem, 0 ) );
848 if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
849 updateChildren( p_node->pp_children[i], newItem );
853 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
854 void PLModel::updateTreeItem( PLItem *item, bool signal, bool force )
856 if ( !item || !item->p_input )
858 if( !force && i_depth == DEPTH_SEL && item->parentItem &&
859 item->parentItem->p_input != rootItem->p_input )
862 emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
865 /************************* Actions ******************************/
868 * Deletion, here we have to do a ugly slow hack as we retrieve the full
869 * list of indexes to delete at once: when we delete a node and all of
870 * its children, we need to update the list.
871 * Todo: investigate whethere we can use ranges to be sure to delete all items?
873 void PLModel::doDelete( QModelIndexList selected )
875 for( int i = selected.size() -1 ; i >= 0; i-- )
877 QModelIndex index = selected[i];
878 if( index.column() != 0 ) continue;
879 PLItem *item = getItem( index );
882 if( item->children.size() )
883 recurseDelete( item->children, &selected );
884 doDeleteItem( item, &selected );
886 if( i > selected.size() ) i = selected.size();
890 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
892 for( int i = children.size() - 1; i >= 0 ; i-- )
894 PLItem *item = children[i];
895 if( item->children.size() )
896 recurseDelete( item->children, fullList );
897 doDeleteItem( item, fullList );
901 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
903 QModelIndex deleteIndex = index( item, 0 );
904 fullList->removeAll( deleteIndex );
907 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
913 if( p_item->i_children == -1 )
914 playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
916 playlist_NodeDelete( p_playlist, p_item, true, false );
918 /* And finally, remove it from the tree */
919 int itemIndex = item->parentItem->children.indexOf( item );
920 beginRemoveRows( index( item->parentItem, 0), itemIndex, itemIndex );
925 /******* Volume III: Sorting and searching ********/
926 void PLModel::sort( int column, Qt::SortOrder order )
928 sort( rootItem->i_id, column, order );
931 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
937 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
939 if( ( shownFlags() & i_column ) )
941 if( column == i_index )
948 PLItem *item = findById( rootItem, i_root_id );
950 QModelIndex qIndex = index( item, 0 );
951 int count = item->children.size();
954 beginRemoveRows( qIndex, 0, count - 1 );
955 item->removeChildren();
961 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
963 if( p_root && i_flag )
965 playlist_RecursiveNodeSort( p_playlist, p_root,
966 i_column_sorting( i_flag ),
967 order == Qt::AscendingOrder ?
968 ORDER_NORMAL : ORDER_REVERSE );
973 beginInsertRows( qIndex, 0, count - 1 );
974 updateChildren( item );
980 void PLModel::search( const QString& search_text )
982 /** \todo Fire the search with a small delay ? */
985 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
988 const char *psz_name = search_text.toUtf8().data();
989 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
995 /*********** Popup *********/
996 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
998 int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
1001 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
1006 i_popup_item = index.isValid() ? p_item->i_id : -1;
1007 i_popup_parent = index.isValid() ?
1008 ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
1010 i_popup_column = index.column();
1011 /* check whether we are in tree view */
1013 playlist_item_t *p_up = p_item;
1016 if ( p_up == p_playlist->p_root_category ) tree = true;
1017 p_up = p_up->p_parent;
1021 current_selection = list;
1022 QMenu *menu = new QMenu;
1023 if( i_popup_item > -1 )
1025 menu->addAction( qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
1026 menu->addAction( qtr(I_POP_DEL), this, SLOT( popupDel() ) );
1027 menu->addSeparator();
1028 menu->addAction( qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
1029 menu->addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
1030 menu->addSeparator();
1031 menu->addAction( qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
1032 menu->addSeparator();
1033 QMenu *sort_menu = menu->addMenu( qtr( "Sort by ") +
1034 qfu( psz_column_title( columnToMeta( index.column(), i_showflags ) ) ) );
1035 sort_menu->addAction( qtr( "Ascending" ),
1036 this, SLOT( popupSortAsc() ) );
1037 sort_menu->addAction( qtr( "Descending" ),
1038 this, SLOT( popupSortDesc() ) );
1041 menu->addAction( qtr(I_POP_ADD), this, SLOT( popupAddNode() ) );
1042 if( i_popup_item > -1 )
1044 menu->addSeparator();
1045 menu->addAction( qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
1047 if( tree || i_popup_item > -1 )
1048 menu->addSeparator();
1049 QMenu *col_selector = menu->addMenu( qtr( "Visible columns" ) );
1050 makeColumnSelectMenu( col_selector );
1051 menu->popup( point );
1054 void PLModel::makeColumnSelectMenu( QMenu *menu )
1057 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
1059 QAction* option = menu->addAction(
1060 qfu( psz_column_title( i_column ) ) );
1061 option->setCheckable( true );
1062 option->setChecked( shownFlags() & i_column );
1063 ContextUpdateMapper->setMapping( option, i_column );
1064 CONNECT( option, triggered(), ContextUpdateMapper, map() );
1068 void PLModel::toggleColumnShown( int meta )
1073 if( i_showflags & meta )
1075 /* Removing columns */
1076 int index = columnFromMeta( meta, i_showflags );
1078 beginRemoveColumns( QModelIndex(), index, index );
1079 i_showflags &= ~( meta );
1080 getSettings()->setValue( "qt-pl-showflags", i_showflags );
1085 /* Adding columns */
1086 int sf = i_showflags;
1088 int index = columnFromMeta( meta, sf );
1089 beginInsertColumns( QModelIndex(), index, index );
1091 getSettings()->setValue( "qt-pl-showflags", i_showflags );
1095 emit columnsChanged( meta );
1099 void PLModel::popupDel()
1101 doDelete( current_selection );
1104 void PLModel::popupPlay()
1108 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1110 activateItem( p_item );
1115 void PLModel::popupInfo()
1118 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1122 input_item_t* p_input = p_item->p_input;
1123 vlc_gc_incref( p_input );
1125 MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
1126 vlc_gc_decref( p_input );
1127 mid->setParent( PlaylistDialog::getInstance( p_intf ),
1134 void PLModel::popupStream()
1136 QStringList mrls = selectedURIs();
1137 if( !mrls.isEmpty() )
1138 THEDP->streamingDialog( NULL, mrls[0], false );
1142 void PLModel::popupSave()
1144 QStringList mrls = selectedURIs();
1145 if( !mrls.isEmpty() )
1146 THEDP->streamingDialog( NULL, mrls[0] );
1150 #include <QFileInfo>
1151 #include <QDesktopServices>
1152 void PLModel::popupExplore()
1155 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1159 input_item_t *p_input = p_item->p_input;
1160 char *psz_meta = input_item_GetURI( p_input );
1164 const char *psz_access;
1165 const char *psz_demux;
1167 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1169 if( EMPTY_STR( psz_access ) ||
1170 !strncasecmp( psz_access, "file", 4 ) ||
1171 !strncasecmp( psz_access, "dire", 4 ) )
1173 QFileInfo info( qfu( psz_meta ) );
1174 QDesktopServices::openUrl(
1175 QUrl::fromLocalFile( info.absolutePath() ) );
1184 #include <QInputDialog>
1185 void PLModel::popupAddNode()
1188 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1189 qtr( I_POP_ADD ), qtr( "Enter name for new node:" ),
1190 QLineEdit::Normal, QString(), &ok);
1191 if( !ok || name.isEmpty() ) return;
1193 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1197 playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1202 void PLModel::popupSortAsc()
1204 sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1207 void PLModel::popupSortDesc()
1209 sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );
1211 /**********************************************************************
1212 * Playlist callbacks
1213 **********************************************************************/
1215 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1216 vlc_value_t oval, vlc_value_t nval, void *param )
1218 PLModel *p_model = (PLModel *) param;
1219 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
1220 QApplication::postEvent( p_model, event );
1224 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1225 vlc_value_t oval, vlc_value_t nval, void *param )
1227 PLModel *p_model = (PLModel *) param;
1228 const playlist_add_t *p_add = (playlist_add_t *)nval.p_address;
1229 PLEvent *event = new PLEvent( p_add );
1230 QApplication::postEvent( p_model, event );