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 "components/playlist/playlist_model.hpp"
32 #include "dialogs/mediainfo.hpp"
33 #include <vlc_intf_strings.h>
35 #include "pixmaps/type_unknown.xpm"
39 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
41 static int PlaylistChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int PlaylistNext( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int ItemChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
48 vlc_value_t oval, vlc_value_t nval, void *param );
49 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
50 vlc_value_t oval, vlc_value_t nval, void *param );
52 /*************************************************************************
53 * Playlist item implementation
54 *************************************************************************/
57 Playlist item is just a wrapper, an abstraction of the playlist_item
58 in order to be managed by PLModel
60 PLItem have a parent, and id and a input Id
64 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
66 parentItem = parent; /* Can be NULL, but only for the rootItem */
67 i_id = _i_id; /* Playlist item specific id */
68 i_input_id = _i_input_id; /* Identifier of the input */
69 model = m; /* PLModel (QAbsmodel) */
70 i_type = -1; /* Item type - Avoid segfault */
75 /* No parent, should be the main one */
76 if( parentItem == NULL )
78 i_showflags = config_GetInt( model->p_intf, "qt-pl-showflags" );
83 i_showflags = parentItem->i_showflags;
84 //Add empty string and update() handles data appending
85 item_col_strings.append( "" );
87 msg_Dbg( model->p_intf, "PLItem created of type: %i", model->i_depth );
92 Call the above function init
93 So far the first constructor isn't used...
95 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
97 init( _i_id, _i_input_id, parent, m );
100 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
102 init( p_item->i_id, p_item->p_input->i_id, parent, m );
107 qDeleteAll( children );
112 void PLItem::updateview()
114 item_col_strings.clear();
116 if( model->i_depth == 1 ) /* Selector Panel */
118 item_col_strings.append( "" );
122 for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
124 if( i_showflags & i_index )
128 case VLC_META_ENGINE_ARTIST:
129 item_col_strings.append( qtr( VLC_META_ARTIST ) );
131 case VLC_META_ENGINE_TITLE:
132 item_col_strings.append( qtr( VLC_META_TITLE ) );
134 case VLC_META_ENGINE_DESCRIPTION:
135 item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
137 case VLC_META_ENGINE_DURATION:
138 item_col_strings.append( qtr( "Duration" ) );
140 case VLC_META_ENGINE_GENRE:
141 item_col_strings.append( qtr( VLC_META_GENRE ) );
143 case VLC_META_ENGINE_COLLECTION:
144 item_col_strings.append( qtr( VLC_META_COLLECTION ) );
146 case VLC_META_ENGINE_SEQ_NUM:
147 item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
149 case VLC_META_ENGINE_RATING:
150 item_col_strings.append( qtr( VLC_META_RATING ) );
159 /* So far signal is always true.
160 Using signal false would not call PLModel... Why ?
162 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
165 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
166 children.insert( i_pos, item );
168 model->endInsertRows();
171 void PLItem::remove( PLItem *removed )
173 if( model->i_depth == DEPTH_SEL || parentItem )
175 int i_index = parentItem->children.indexOf( removed );
176 model->beginRemoveRows( model->index( parentItem, 0 ),
178 parentItem->children.removeAt( i_index );
179 model->endRemoveRows();
183 /* This function is used to get one's parent's row number in the model */
184 int PLItem::row() const
187 return parentItem->children.indexOf( const_cast<PLItem*>(this) ); // Why this ? I don't think we ever inherit PLItem
191 /* update the PL Item, get the good names and so on */
192 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
194 char psz_duration[MSTRTIME_MAX_SIZE];
197 assert( p_item->p_input->i_id == i_input_id );
199 i_type = p_item->p_input->i_type;
200 b_current = iscurrent;
202 item_col_strings.clear();
204 if( model->i_depth == 1 ) /* Selector Panel */
206 item_col_strings.append( qfu( p_item->p_input->psz_name ) );
210 #define ADD_META( item, meta ) \
211 psz_meta = input_item_Get ## meta ( item->p_input ); \
212 item_col_strings.append( qfu( psz_meta ) ); \
215 for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
217 if( parentItem->i_showflags & i_index )
221 case VLC_META_ENGINE_ARTIST:
222 ADD_META( p_item, Artist );
224 case VLC_META_ENGINE_TITLE:
225 char *psz_title, *psz_name;
226 psz_title = input_item_GetTitle( p_item->p_input );
227 psz_name = input_item_GetName( p_item->p_input );
230 ADD_META( p_item, Title );
234 item_col_strings.append( qfu( psz_name ) );
239 case VLC_META_ENGINE_DESCRIPTION:
240 ADD_META( p_item, Description );
242 case VLC_META_ENGINE_DURATION:
243 secstotimestr( psz_duration,
244 input_item_GetDuration( p_item->p_input ) / 1000000 );
245 item_col_strings.append( QString( psz_duration ) );
247 case VLC_META_ENGINE_GENRE:
248 ADD_META( p_item, Genre );
250 case VLC_META_ENGINE_COLLECTION:
251 ADD_META( p_item, Album );
253 case VLC_META_ENGINE_SEQ_NUM:
254 ADD_META( p_item, TrackNum );
256 case VLC_META_ENGINE_RATING:
257 ADD_META( p_item, Rating );
267 /*************************************************************************
268 * Playlist model implementation
269 *************************************************************************/
272 This model is called two times, for the selector and the standard panel
274 PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
275 intf_thread_t *_p_intf, /* main Qt p_intf */
276 playlist_item_t * p_root,
277 /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
278 and THEPL->p_root_category for SelectPL */
279 int _i_depth, /* -1 for StandPL, 1 for SelectPL */
280 QObject *parent ) /* Basic Qt parent */
281 : QAbstractItemModel( parent )
284 assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
286 p_playlist = _p_playlist;
287 i_items_to_append = 0;
288 b_need_update = false;
290 i_cached_input_id = -1;
291 i_popup_item = i_popup_parent = -1;
293 rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
295 /* Icons initialization */
296 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
297 ADD_ICON( UNKNOWN , type_unknown_xpm );
298 ADD_ICON( FILE, ":/pixmaps/type_file.png" );
299 ADD_ICON( DIRECTORY, ":/pixmaps/type_directory.png" );
300 ADD_ICON( DISC, ":/pixmaps/disc_16px.png" );
301 ADD_ICON( CDDA, ":/pixmaps/cdda_16px.png" );
302 ADD_ICON( CARD, ":/pixmaps/capture-card_16px.png" );
303 ADD_ICON( NET, ":/pixmaps/type_net.png" );
304 ADD_ICON( PLAYLIST, ":/pixmaps/type_playlist.png" );
305 ADD_ICON( NODE, ":/pixmaps/type_node.png" );
318 Qt::DropActions PLModel::supportedDropActions() const
320 return Qt::CopyAction; /* Why not Qt::MoveAction */
323 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
325 Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
326 if( index.isValid() )
327 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
329 return Qt::ItemIsDropEnabled | defaultFlags;
332 /* A list of model indexes are a playlist */
333 QStringList PLModel::mimeTypes() const
336 types << "vlc/playlist-item-id";
340 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
342 QMimeData *mimeData = new QMimeData();
343 QByteArray encodedData;
344 QDataStream stream( &encodedData, QIODevice::WriteOnly );
346 foreach( QModelIndex index, indexes ) {
347 if( index.isValid() && index.column() == 0 )
348 stream << itemId( index );
350 mimeData->setData( "vlc/playlist-item-id", encodedData );
355 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
356 int row, int column, const QModelIndex &target )
358 if( data->hasFormat( "vlc/playlist-item-id" ) )
360 if( action == Qt::IgnoreAction )
364 if( target.isValid() )
365 targetItem = static_cast<PLItem*>( target.internalPointer() );
367 targetItem = rootItem;
369 QByteArray encodedData = data->data( "vlc/playlist-item-id" );
370 QDataStream stream( &encodedData, QIODevice::ReadOnly );
372 PLItem *newParentItem;
373 while( !stream.atEnd() )
380 playlist_item_t *p_target =
381 playlist_ItemGetById( p_playlist, targetItem->i_id,
383 playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
386 if( !p_target || !p_src )
391 if( p_target->i_children == -1 ) /* A leaf */
393 PLItem *parentItem = targetItem->parent();
394 assert( parentItem );
395 playlist_item_t *p_parent =
396 playlist_ItemGetById( p_playlist, parentItem->i_id,
403 for( i = 0 ; i< p_parent->i_children ; i++ )
404 if( p_parent->pp_children[i] == p_target ) break;
405 playlist_TreeMove( p_playlist, p_src, p_parent, i );
406 newParentItem = parentItem;
410 /* \todo: if we drop on a top-level node, use copy instead ? */
411 playlist_TreeMove( p_playlist, p_src, p_target, 0 );
413 newParentItem = targetItem;
415 /* Remove from source */
416 PLItem *srcItem = FindById( rootItem, p_src->i_id );
417 // We dropped on the source selector. Ask the dialog to forward
421 emit shouldRemove( p_src->i_id );
424 srcItem->remove( srcItem );
426 /* Display at new destination */
427 PLItem *newItem = new PLItem( p_src, newParentItem, this );
428 newParentItem->insertChild( newItem, i, true );
429 UpdateTreeItem( p_src, newItem, true );
430 if( p_src->i_children != -1 )
431 UpdateNodeChildren( newItem );
438 /* remove item with its id */
439 void PLModel::removeItem( int i_id )
441 PLItem *item = FindById( rootItem, i_id );
442 if( item ) item->remove( item );
445 /* callbacks and slots */
446 void PLModel::addCallbacks()
448 /* Some global changes happened -> Rebuild all */
449 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
450 /* We went to the next item */
451 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
452 /* One item has been updated */
453 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
454 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
455 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
458 void PLModel::delCallbacks()
460 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
461 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
462 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
463 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
464 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
467 void PLModel::activateItem( const QModelIndex &index )
469 assert( index.isValid() );
470 PLItem *item = static_cast<PLItem*>(index.internalPointer());
473 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
475 activateItem( p_item );
479 /* Must be entered with lock */
480 void PLModel::activateItem( playlist_item_t *p_item )
482 if( !p_item ) return;
483 playlist_item_t *p_parent = p_item;
486 if( p_parent->i_id == rootItem->i_id ) break;
487 p_parent = p_parent->p_parent;
490 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
494 /****************** Base model mandatory implementations *****************/
495 QVariant PLModel::data( const QModelIndex &index, int role ) const
497 if( !index.isValid() ) return QVariant();
498 PLItem *item = static_cast<PLItem*>(index.internalPointer());
499 if( role == Qt::DisplayRole )
501 return QVariant( item->columnString( index.column() ) );
503 else if( role == Qt::DecorationRole && index.column() == 0 )
505 /* Use to segfault here because i_type wasn't always initialized */
506 if( item->i_type >= 0 )
507 return QVariant( PLModel::icons[item->i_type] );
509 else if( role == Qt::FontRole )
511 if( item->b_current == true )
513 QFont f; f.setBold( true ); return QVariant( f );
519 bool PLModel::isCurrent( const QModelIndex &index )
521 assert( index.isValid() );
522 return static_cast<PLItem*>(index.internalPointer())->b_current;
525 int PLModel::itemId( const QModelIndex &index ) const
527 assert( index.isValid() );
528 return static_cast<PLItem*>(index.internalPointer())->i_id;
531 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
534 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
535 return QVariant( rootItem->columnString( section ) );
539 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
543 if( !parent.isValid() )
544 parentItem = rootItem;
546 parentItem = static_cast<PLItem*>(parent.internalPointer());
548 PLItem *childItem = parentItem->child( row );
550 return createIndex( row, column, childItem );
552 return QModelIndex();
555 /* Return the index of a given item */
556 QModelIndex PLModel::index( PLItem *item, int column ) const
558 if( !item ) return QModelIndex();
559 const PLItem *parent = item->parent();
561 return createIndex( parent->children.lastIndexOf( item ),
563 return QModelIndex();
566 QModelIndex PLModel::parent( const QModelIndex &index ) const
568 if( !index.isValid() ) return QModelIndex();
570 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
573 msg_Err( p_playlist, "NULL CHILD" );
574 return QModelIndex();
577 PLItem *parentItem = childItem->parent();
578 if( !parentItem || parentItem == rootItem ) return QModelIndex();
579 if( !parentItem->parentItem )
581 msg_Err( p_playlist, "No parent parent, trying row 0 " );
582 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
583 return createIndex( 0, 0, parentItem );
585 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
589 int PLModel::columnCount( const QModelIndex &i) const
591 return rootItem->item_col_strings.count();
594 int PLModel::childrenCount( const QModelIndex &parent ) const
596 return rowCount( parent );
599 int PLModel::rowCount( const QModelIndex &parent ) const
603 if( !parent.isValid() )
604 parentItem = rootItem;
606 parentItem = static_cast<PLItem*>(parent.internalPointer());
608 return parentItem->childCount();
611 /************************* General playlist status ***********************/
613 bool PLModel::hasRandom()
615 if( var_GetBool( p_playlist, "random" ) ) return true;
618 bool PLModel::hasRepeat()
620 if( var_GetBool( p_playlist, "repeat" ) ) return true;
623 bool PLModel::hasLoop()
625 if( var_GetBool( p_playlist, "loop" ) ) return true;
628 void PLModel::setLoop( bool on )
630 var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
631 config_PutInt( p_playlist, "loop", on ? 1: 0 );
633 void PLModel::setRepeat( bool on )
635 var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
636 config_PutInt( p_playlist, "repeat", on ? 1: 0 );
638 void PLModel::setRandom( bool on )
640 var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
641 config_PutInt( p_playlist, "random", on ? 1: 0 );
644 /************************* Lookups *****************************/
646 PLItem *PLModel::FindById( PLItem *root, int i_id )
648 return FindInner( root, i_id, false );
651 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
653 return FindInner( root, i_id, true );
656 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
657 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
659 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
661 if( ( !b_input && i_cached_id == i_id) ||
662 ( b_input && i_cached_input_id ==i_id ) )
664 return b_input ? p_cached_item_bi : p_cached_item;
667 if( !b_input && root->i_id == i_id )
672 else if( b_input && root->i_input_id == i_id )
674 ICACHE( i_id, root );
678 QList<PLItem *>::iterator it = root->children.begin();
679 while ( it != root->children.end() )
681 if( !b_input && (*it)->i_id == i_id )
683 CACHE( i_id, (*it) );
684 return p_cached_item;
686 else if( b_input && (*it)->i_input_id == i_id )
688 ICACHE( i_id, (*it) );
689 return p_cached_item_bi;
691 if( (*it)->children.size() )
693 PLItem *childFound = FindInner( (*it), i_id, b_input );
697 ICACHE( i_id, childFound )
699 CACHE( i_id, childFound )
711 /************************* Updates handling *****************************/
712 void PLModel::customEvent( QEvent *event )
714 int type = event->type();
715 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
716 type != ItemDelete_Type && type != PLUpdate_Type )
719 PLEvent *ple = static_cast<PLEvent *>(event);
721 if( type == ItemUpdate_Type )
722 ProcessInputItemUpdate( ple->i_id );
723 else if( type == ItemAppend_Type )
724 ProcessItemAppend( ple->p_add );
725 else if( type == ItemDelete_Type )
726 ProcessItemRemoval( ple->i_id );
731 /**** Events processing ****/
732 void PLModel::ProcessInputItemUpdate( int i_input_id )
734 if( i_input_id <= 0 ) return;
735 PLItem *item = FindByInput( rootItem, i_input_id );
737 UpdateTreeItem( item, true );
740 void PLModel::ProcessItemRemoval( int i_id )
742 if( i_id <= 0 ) return;
743 if( i_id == i_cached_id ) i_cached_id = -1;
744 i_cached_input_id = -1;
749 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
751 playlist_item_t *p_item = NULL;
752 PLItem *newItem = NULL;
754 if( b_need_update ) return;
756 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
758 if( !nodeItem ) goto end;
760 p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
761 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
762 if( i_depth == DEPTH_SEL && p_item->p_parent &&
763 p_item->p_parent->i_id != rootItem->i_id )
766 newItem = new PLItem( p_item, nodeItem, this );
767 nodeItem->appendChild( newItem );
768 UpdateTreeItem( p_item, newItem, true );
775 void PLModel::rebuild()
780 void PLModel::rebuild( playlist_item_t *p_root )
782 /* Remove callbacks before locking to avoid deadlocks */
784 /* Invalidate cache */
785 i_cached_id = i_cached_input_id = -1;
791 if( rootItem->children.size() )
793 beginRemoveRows( index( rootItem, 0 ), 0,
794 rootItem->children.size() -1 );
795 qDeleteAll( rootItem->children );
796 rootItem->children.clear();
802 //if( rootItem ) delete rootItem;
803 rootItem = new PLItem( p_root, NULL, this );
806 /* Recreate from root */
807 UpdateNodeChildren( rootItem );
808 if( p_playlist->status.p_item )
810 PLItem *currentItem = FindByInput( rootItem,
811 p_playlist->status.p_item->p_input->i_id );
814 UpdateTreeItem( p_playlist->status.p_item, currentItem,
820 /* And signal the view */
821 emit layoutChanged();
825 /* This function must be entered WITH the playlist lock */
826 void PLModel::UpdateNodeChildren( PLItem *root )
828 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
830 UpdateNodeChildren( p_node, root );
833 /* This function must be entered WITH the playlist lock */
834 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
836 for( int i = 0; i < p_node->i_children ; i++ )
838 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
839 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
840 root->appendChild( newItem, false );
841 UpdateTreeItem( newItem, false, true );
842 if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
843 UpdateNodeChildren( p_node->pp_children[i], newItem );
847 /* This function must be entered WITH the playlist lock */
848 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
850 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
852 UpdateTreeItem( p_item, item, signal, force );
855 /* This function must be entered WITH the playlist lock */
856 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
857 bool signal, bool force )
861 if( !force && i_depth == DEPTH_SEL && p_item->p_parent &&
862 p_item->p_parent->i_id != rootItem->i_id )
864 item->update( p_item, p_item == p_playlist->status.p_item );
866 emit dataChanged( index( item, 0 ) , index( item, 1 ) );
869 /************************* Actions ******************************/
872 * Deletion, here we have to do a ugly slow hack as we retrieve the full
873 * list of indexes to delete at once: when we delete a node and all of
874 * its children, we need to update the list.
875 * Todo: investigate whethere we can use ranges to be sure to delete all items?
877 void PLModel::doDelete( QModelIndexList selected )
879 for( int i = selected.size() -1 ; i >= 0; i-- )
881 QModelIndex index = selected[i];
882 if( index.column() != 0 ) continue;
883 PLItem *item = static_cast<PLItem*>(index.internalPointer());
886 if( item->children.size() )
887 recurseDelete( item->children, &selected );
888 doDeleteItem( item, &selected );
893 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
895 for( int i = children.size() - 1; i >= 0 ; i-- )
897 PLItem *item = children[i];
898 if( item->children.size() )
899 recurseDelete( item->children, fullList );
900 doDeleteItem( item, fullList );
904 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
906 QModelIndex deleteIndex = index( item, 0 );
907 fullList->removeAll( deleteIndex );
910 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
916 if( p_item->i_children == -1 )
917 playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
919 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
920 /* And finally, remove it from the tree */
921 item->remove( item );
925 /******* Volume III: Sorting and searching ********/
926 void PLModel::sort( int column, Qt::SortOrder order )
930 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
936 case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
937 case 1: i_mode = SORT_ARTIST;break;
938 case 2: i_mode = SORT_DURATION; break;
939 default: i_mode = SORT_TITLE_NODES_FIRST; break;
942 playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
943 order == Qt::AscendingOrder ?
944 ORDER_NORMAL : ORDER_REVERSE );
950 void PLModel::search( QString search_text )
952 /** \todo Fire the search with a small delay ? */
955 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
959 char *psz_name = search_text.toUtf8().data();
960 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
966 /*********** Popup *********/
967 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
969 assert( index.isValid() );
971 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
972 itemId( index ), VLC_TRUE );
975 i_popup_item = p_item->i_id;
976 i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
978 current_selection = list;
979 QMenu *menu = new QMenu;
980 menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
981 menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
982 menu->addSeparator();
983 menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
984 menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
985 menu->addSeparator();
986 menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
987 if( p_item->i_children > -1 )
989 menu->addSeparator();
990 menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
991 menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
994 menu->addSeparator();
995 menu->addAction( qfu( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
997 menu->popup( point );
1004 void PLModel::viewchanged( int meta )
1011 case VLC_META_ENGINE_TITLE:
1013 case VLC_META_ENGINE_DURATION:
1015 case VLC_META_ENGINE_ARTIST:
1017 case VLC_META_ENGINE_GENRE:
1019 case VLC_META_ENGINE_COPYRIGHT:
1021 case VLC_META_ENGINE_COLLECTION:
1023 case VLC_META_ENGINE_SEQ_NUM:
1025 case VLC_META_ENGINE_DESCRIPTION:
1030 /* UNUSED emit layoutAboutToBeChanged(); */
1031 index = __MIN( index , rootItem->item_col_strings.count() );
1032 QModelIndex parent = createIndex( 0, 0, rootItem );
1034 if( rootItem->i_showflags & meta )
1035 /* Removing columns */
1037 beginRemoveColumns( parent, index, index+1 );
1038 rootItem->i_showflags &= ~( meta );
1039 rootItem->updateview();
1044 /* Adding columns */
1045 beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
1046 rootItem->i_showflags |= meta;
1047 rootItem->updateview();
1051 config_PutInt( p_intf, "qt-pl-showflags", rootItem->i_showflags );
1052 config_SaveConfigFile( p_intf, NULL );
1056 void PLModel::popupDel()
1058 doDelete( current_selection );
1060 void PLModel::popupPlay()
1064 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1065 i_popup_item,VLC_TRUE );
1066 activateItem( p_item );
1071 void PLModel::popupInfo()
1073 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1078 MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_item->p_input );
1083 void PLModel::popupStream()
1085 msg_Err( p_playlist, "Stream not implemented" );
1088 void PLModel::popupSave()
1090 msg_Err( p_playlist, "Save not implemented" );
1094 #include <shellapi.h>
1095 void PLModel::popupExplore()
1097 ShellExecuteW( NULL, L"explore", L"C:\\", NULL, NULL, SW_SHOWNORMAL );
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) );