Qt: playlist menu strings and icons
[vlc.git] / modules / gui / qt4 / components / playlist / playlist_model.cpp
blob29595620a18ad28a16a723e9b0a13d28127590a9
1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Ilkka Ollakkka <ileoo (at) videolan dot org>
9 * Jakob Leben <jleben@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "qt4.hpp"
31 #include "dialogs_provider.hpp"
32 #include "components/playlist/playlist_model.hpp"
33 #include "dialogs/mediainfo.hpp"
34 #include "dialogs/playlist.hpp"
35 #include <vlc_intf_strings.h>
37 #include "pixmaps/types/type_unknown.xpm"
39 #include <assert.h>
40 #include <QIcon>
41 #include <QFont>
42 #include <QMenu>
43 #include <QApplication>
44 #include <QSettings>
46 #include "sorting.h"
48 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
50 /*************************************************************************
51 * Playlist model implementation
52 *************************************************************************/
54 PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
55 intf_thread_t *_p_intf, /* main Qt p_intf */
56 playlist_item_t * p_root,
57 QObject *parent ) /* Basic Qt parent */
58 : QAbstractItemModel( parent )
60 p_intf = _p_intf;
61 p_playlist = _p_playlist;
62 i_cached_id = -1;
63 i_cached_input_id = -1;
64 i_popup_item = i_popup_parent = -1;
66 rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
68 /* Icons initialization */
69 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
70 ADD_ICON( UNKNOWN , type_unknown_xpm );
71 ADD_ICON( FILE, ":/type/file" );
72 ADD_ICON( DIRECTORY, ":/type/directory" );
73 ADD_ICON( DISC, ":/type/disc" );
74 ADD_ICON( CDDA, ":/type/cdda" );
75 ADD_ICON( CARD, ":/type/capture-card" );
76 ADD_ICON( NET, ":/type/net" );
77 ADD_ICON( PLAYLIST, ":/type/playlist" );
78 ADD_ICON( NODE, ":/type/node" );
79 #undef ADD_ICON
81 rebuild( p_root );
82 CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
83 this, processInputItemUpdate( input_item_t *) );
84 CONNECT( THEMIM, inputChanged( input_thread_t * ),
85 this, processInputItemUpdate( input_thread_t* ) );
86 CONNECT( THEMIM, playlistItemAppended( int, int ),
87 this, processItemAppend( int, int ) );
88 CONNECT( THEMIM, playlistItemRemoved( int ),
89 this, processItemRemoval( int ) );
92 PLModel::~PLModel()
94 delete rootItem;
97 Qt::DropActions PLModel::supportedDropActions() const
99 return Qt::CopyAction; /* Why not Qt::MoveAction */
102 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
104 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
106 PLItem *item = index.isValid() ? getItem( index ) : rootItem;
108 if( canEdit() )
110 PL_LOCK;
111 playlist_item_t *plItem =
112 playlist_ItemGetById( p_playlist, item->i_id );
114 if ( plItem && ( plItem->i_children > -1 ) )
115 flags |= Qt::ItemIsDropEnabled;
117 PL_UNLOCK;
120 flags |= Qt::ItemIsDragEnabled;
122 return flags;
125 QStringList PLModel::mimeTypes() const
127 QStringList types;
128 types << "vlc/qt-playlist-item";
129 return types;
132 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
134 QMimeData *mimeData = new QMimeData();
135 QByteArray encodedData;
136 QDataStream stream( &encodedData, QIODevice::WriteOnly );
137 QModelIndexList list;
139 foreach( const QModelIndex &index, indexes ) {
140 if( index.isValid() && index.column() == 0 )
141 list.append(index);
144 qSort(list);
146 foreach( const QModelIndex &index, list ) {
147 PLItem *item = getItem( index );
148 stream.writeRawData( (char*) &item, sizeof( PLItem* ) );
150 mimeData->setData( "vlc/qt-playlist-item", encodedData );
151 return mimeData;
154 /* Drop operation */
155 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
156 int row, int column, const QModelIndex &parent )
158 if( data->hasFormat( "vlc/qt-playlist-item" ) )
160 if( action == Qt::IgnoreAction )
161 return true;
163 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
165 PL_LOCK;
166 playlist_item_t *p_parent =
167 playlist_ItemGetById( p_playlist, parentItem->i_id );
168 if( !p_parent || p_parent->i_children == -1 )
170 PL_UNLOCK;
171 return false;
174 bool copy = false;
175 playlist_item_t *p_pl = p_playlist->p_playing;
176 playlist_item_t *p_ml = p_playlist->p_media_library;
179 row == -1 && (
180 ( p_pl && p_parent == p_pl ) ||
181 ( p_ml && p_parent == p_ml ) )
183 copy = true;
184 PL_UNLOCK;
186 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
187 if( copy )
188 dropAppendCopy( encodedData, parentItem );
189 else
190 dropMove( encodedData, parentItem, row );
192 return true;
195 void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
197 QDataStream stream( &data, QIODevice::ReadOnly );
199 PL_LOCK;
200 playlist_item_t *p_parent =
201 playlist_ItemGetById( p_playlist, target->i_id );
202 while( !stream.atEnd() )
204 PLItem *item;
205 stream.readRawData( (char*)&item, sizeof(PLItem*) );
206 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
207 if( !p_item ) continue;
208 input_item_t *p_input = p_item->p_input;
209 playlist_AddExt ( p_playlist,
210 p_input->psz_uri, p_input->psz_name,
211 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
212 p_input->i_duration,
213 p_input->i_options, p_input->ppsz_options, p_input->optflagc,
214 p_parent == p_playlist->p_playing,
215 true );
217 PL_UNLOCK;
220 void PLModel::dropMove( QByteArray& data, PLItem *target, int row )
222 QDataStream stream( &data, QIODevice::ReadOnly );
223 QList<PLItem*> model_items;
224 QList<int> ids;
225 int new_pos = row == -1 ? target->children.size() : row;
226 int model_pos = new_pos;
227 while( !stream.atEnd() )
229 PLItem *item;
230 stream.readRawData( (char*)&item, sizeof(PLItem*) );
232 /* better not try to move a node into itself: */
233 PLItem *climber = target;
234 while( climber )
236 if( climber == item ) break;
237 climber = climber->parentItem;
239 if( climber ) continue;
241 if( item->parentItem == target &&
242 target->children.indexOf( item ) < model_pos )
243 model_pos--;
245 ids.append( item->i_id );
246 model_items.append( item );
248 takeItem( item );
250 int count = ids.size();
251 if( count )
253 playlist_item_t *pp_items[count];
255 PL_LOCK;
256 for( int i = 0; i < count; i++ )
258 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, ids[i] );
259 if( !p_item )
261 PL_UNLOCK;
262 return;
264 pp_items[i] = p_item;
266 playlist_item_t *p_parent =
267 playlist_ItemGetById( p_playlist, target->i_id );
268 playlist_TreeMoveMany( p_playlist, count, pp_items, p_parent,
269 new_pos );
270 PL_UNLOCK;
272 insertChildren( target, model_items, model_pos );
276 /* remove item with its id */
277 void PLModel::removeItem( int i_id )
279 PLItem *item = findById( rootItem, i_id );
280 removeItem( item );
283 void PLModel::activateItem( const QModelIndex &index )
285 assert( index.isValid() );
286 PLItem *item = getItem( index );
287 assert( item );
288 PL_LOCK;
289 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
290 activateItem( p_item );
291 PL_UNLOCK;
294 /* Must be entered with lock */
295 void PLModel::activateItem( playlist_item_t *p_item )
297 if( !p_item ) return;
298 playlist_item_t *p_parent = p_item;
299 while( p_parent )
301 if( p_parent->i_id == rootItem->i_id ) break;
302 p_parent = p_parent->p_parent;
304 if( p_parent )
305 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
306 p_parent, p_item );
309 /****************** Base model mandatory implementations *****************/
310 QVariant PLModel::data( const QModelIndex &index, int role ) const
312 if( !index.isValid() ) return QVariant();
313 PLItem *item = getItem( index );
314 if( role == Qt::DisplayRole )
316 int metadata = columnToMeta( index.column() );
317 if( metadata == COLUMN_END ) return QVariant();
319 QString returninfo;
320 if( metadata == COLUMN_NUMBER )
321 returninfo = QString::number( index.row() + 1 );
322 else
324 char *psz = psz_column_meta( item->p_input, metadata );
325 returninfo = qfu( psz );
326 free( psz );
328 return QVariant( returninfo );
330 else if( role == Qt::DecorationRole && index.column() == 0 )
332 /* Used to segfault here because i_type wasn't always initialized */
333 return QVariant( PLModel::icons[item->p_input->i_type] );
335 else if( role == Qt::FontRole )
337 if( isCurrent( index ) )
339 QFont f; f.setBold( true ); return QVariant( f );
342 else if( role == Qt::BackgroundRole && isCurrent( index ) )
344 return QVariant( QBrush( Qt::gray ) );
346 else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
348 return QVariant();
351 bool PLModel::isCurrent( const QModelIndex &index ) const
353 input_thread_t *p_input_thread = THEMIM->getInput();
354 if( !p_input_thread ) return false;
355 return getItem( index )->p_input == input_GetItem( p_input_thread );
358 int PLModel::itemId( const QModelIndex &index ) const
360 return getItem( index )->i_id;
363 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
364 int role ) const
366 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
367 return QVariant();
369 int meta_col = columnToMeta( section );
371 if( meta_col == COLUMN_END ) return QVariant();
373 return QVariant( qfu( psz_column_title( meta_col ) ) );
376 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
377 const
379 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
381 PLItem *childItem = parentItem->child( row );
382 if( childItem )
383 return createIndex( row, column, childItem );
384 else
385 return QModelIndex();
388 QModelIndex PLModel::index( int i_id, int c )
390 return index( findById( rootItem, i_id ), c );
393 /* Return the index of a given item */
394 QModelIndex PLModel::index( PLItem *item, int column ) const
396 if( !item ) return QModelIndex();
397 const PLItem *parent = item->parent();
398 if( parent )
399 return createIndex( parent->children.lastIndexOf( item ),
400 column, item );
401 return QModelIndex();
404 QModelIndex PLModel::currentIndex()
406 input_thread_t *p_input_thread = THEMIM->getInput();
407 if( !p_input_thread ) return QModelIndex();
408 PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
409 return index( item, 0 );
412 QModelIndex PLModel::parent( const QModelIndex &index ) const
414 if( !index.isValid() ) return QModelIndex();
416 PLItem *childItem = getItem( index );
417 if( !childItem )
419 msg_Err( p_playlist, "NULL CHILD" );
420 return QModelIndex();
423 PLItem *parentItem = childItem->parent();
424 if( !parentItem || parentItem == rootItem ) return QModelIndex();
425 if( !parentItem->parentItem )
427 msg_Err( p_playlist, "No parent parent, trying row 0 " );
428 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
429 return createIndex( 0, 0, parentItem );
431 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
432 return ind;
435 int PLModel::columnCount( const QModelIndex &i) const
437 return columnFromMeta( COLUMN_END );
440 int PLModel::rowCount( const QModelIndex &parent ) const
442 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
443 return parentItem->childCount();
446 QStringList PLModel::selectedURIs()
448 QStringList lst;
449 for( int i = 0; i < current_selection.size(); i++ )
451 PLItem *item = getItem( current_selection[i] );
452 if( item )
454 PL_LOCK;
455 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
456 if( p_item )
458 char *psz = input_item_GetURI( p_item->p_input );
459 if( psz )
461 lst.append( qfu(psz) );
462 free( psz );
465 PL_UNLOCK;
468 return lst;
472 /************************* Lookups *****************************/
474 PLItem *PLModel::findById( PLItem *root, int i_id )
476 return findInner( root, i_id, false );
479 PLItem *PLModel::findByInput( PLItem *root, int i_id )
481 PLItem *result = findInner( root, i_id, true );
482 return result;
485 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
486 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
488 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
490 if( !root ) return NULL;
491 if( ( !b_input && i_cached_id == i_id) ||
492 ( b_input && i_cached_input_id ==i_id ) )
494 return b_input ? p_cached_item_bi : p_cached_item;
497 if( !b_input && root->i_id == i_id )
499 CACHE( i_id, root );
500 return root;
502 else if( b_input && root->p_input->i_id == i_id )
504 ICACHE( i_id, root );
505 return root;
508 QList<PLItem *>::iterator it = root->children.begin();
509 while ( it != root->children.end() )
511 if( !b_input && (*it)->i_id == i_id )
513 CACHE( i_id, (*it) );
514 return p_cached_item;
516 else if( b_input && (*it)->p_input->i_id == i_id )
518 ICACHE( i_id, (*it) );
519 return p_cached_item_bi;
521 if( (*it)->children.size() )
523 PLItem *childFound = findInner( (*it), i_id, b_input );
524 if( childFound )
526 if( b_input )
527 ICACHE( i_id, childFound )
528 else
529 CACHE( i_id, childFound )
530 return childFound;
533 it++;
535 return NULL;
537 #undef CACHE
538 #undef ICACHE
540 int PLModel::columnToMeta( int _column )
542 int meta = 1;
543 int column = 0;
545 while( column != _column && meta != COLUMN_END )
547 meta <<= 1;
548 column++;
551 return meta;
554 int PLModel::columnFromMeta( int meta_col )
556 int meta = 1;
557 int column = 0;
559 while( meta != meta_col && meta != COLUMN_END )
561 meta <<= 1;
562 column++;
565 return column;
568 bool PLModel::canEdit() const
570 return (
571 rootItem != NULL &&
573 rootItem->p_input == p_playlist->p_playing->p_input ||
575 p_playlist->p_media_library &&
576 rootItem->p_input == p_playlist->p_media_library->p_input
581 /************************* Updates handling *****************************/
583 /**** Events processing ****/
584 void PLModel::processInputItemUpdate( input_thread_t *p_input )
586 if( !p_input ) return;
587 if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
589 PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
590 if( item ) emit currentChanged( index( item, 0 ) );
592 processInputItemUpdate( input_GetItem( p_input ) );
595 void PLModel::processInputItemUpdate( input_item_t *p_item )
597 if( !p_item || p_item->i_id <= 0 ) return;
598 PLItem *item = findByInput( rootItem, p_item->i_id );
599 if( item )
600 updateTreeItem( item );
603 void PLModel::processItemRemoval( int i_id )
605 if( i_id <= 0 ) return;
606 removeItem( i_id );
609 void PLModel::processItemAppend( int i_item, int i_parent )
611 playlist_item_t *p_item = NULL;
612 PLItem *newItem = NULL;
613 input_thread_t *currentInputThread;
614 int pos;
616 PLItem *nodeItem = findById( rootItem, i_parent );
617 if( !nodeItem ) return;
619 foreach( PLItem *existing, nodeItem->children )
620 if( existing->i_id == i_item ) return;
622 PL_LOCK;
623 p_item = playlist_ItemGetById( p_playlist, i_item );
624 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
626 for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
627 if( p_item->p_parent->pp_children[pos] == p_item ) break;
629 newItem = new PLItem( p_item, nodeItem );
630 PL_UNLOCK;
632 beginInsertRows( index( nodeItem, 0 ), pos, pos );
633 nodeItem->insertChild( newItem, pos );
634 endInsertRows();
636 return;
637 end:
638 PL_UNLOCK;
639 return;
643 void PLModel::rebuild()
645 rebuild( NULL );
648 void PLModel::rebuild( playlist_item_t *p_root )
650 playlist_item_t* p_item;
652 /* Invalidate cache */
653 i_cached_id = i_cached_input_id = -1;
655 if( rootItem ) rootItem->removeChildren();
657 PL_LOCK;
658 if( p_root )
660 delete rootItem;
661 rootItem = new PLItem( p_root );
663 assert( rootItem );
664 /* Recreate from root */
665 updateChildren( rootItem );
666 PL_UNLOCK;
668 /* And signal the view */
669 reset();
671 if( p_root ) emit rootChanged();
674 void PLModel::takeItem( PLItem *item )
676 assert( item );
677 PLItem *parent = item->parentItem;
678 assert( parent );
679 int i_index = parent->children.indexOf( item );
681 beginRemoveRows( index( parent, 0 ), i_index, i_index );
682 parent->takeChildAt( i_index );
683 endRemoveRows();
686 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
688 assert( node );
689 int count = items.size();
690 if( !count ) return;
691 beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
692 for( int i = 0; i < count; i++ )
694 node->children.insert( i_pos + i, items[i] );
695 items[i]->parentItem = node;
697 endInsertRows();
700 void PLModel::removeItem( PLItem *item )
702 if( !item ) return;
704 if( item->i_id == i_cached_id ) i_cached_id = -1;
705 i_cached_input_id = -1;
707 if( item->parentItem ) {
708 int i = item->parentItem->children.indexOf( item );
709 beginRemoveRows( index( item->parentItem, 0), i, i );
710 item->parentItem->children.removeAt(i);
711 delete item;
712 endRemoveRows();
714 else delete item;
716 if(item == rootItem)
718 rootItem = NULL;
719 rebuild( p_playlist->p_playing );
723 /* This function must be entered WITH the playlist lock */
724 void PLModel::updateChildren( PLItem *root )
726 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
727 updateChildren( p_node, root );
730 /* This function must be entered WITH the playlist lock */
731 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
733 for( int i = 0; i < p_node->i_children ; i++ )
735 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
736 PLItem *newItem = new PLItem( p_node->pp_children[i], root );
737 root->appendChild( newItem );
738 if( p_node->pp_children[i]->i_children != -1 )
739 updateChildren( p_node->pp_children[i], newItem );
743 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
744 void PLModel::updateTreeItem( PLItem *item )
746 if( !item ) return;
747 emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
750 /************************* Actions ******************************/
753 * Deletion, here we have to do a ugly slow hack as we retrieve the full
754 * list of indexes to delete at once: when we delete a node and all of
755 * its children, we need to update the list.
756 * Todo: investigate whethere we can use ranges to be sure to delete all items?
758 void PLModel::doDelete( QModelIndexList selected )
760 if( !canEdit() ) return;
762 while( !selected.isEmpty() )
764 QModelIndex index = selected[0];
765 selected.removeAt( 0 );
767 if( index.column() != 0 ) continue;
769 PLItem *item = getItem( index );
770 if( item->children.size() )
771 recurseDelete( item->children, &selected );
773 PL_LOCK;
774 playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
775 PL_UNLOCK;
777 removeItem( item );
781 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
783 for( int i = children.size() - 1; i >= 0 ; i-- )
785 PLItem *item = children[i];
786 if( item->children.size() )
787 recurseDelete( item->children, fullList );
788 fullList->removeAll( index( item, 0 ) );
792 /******* Volume III: Sorting and searching ********/
793 void PLModel::sort( int column, Qt::SortOrder order )
795 sort( rootItem->i_id, column, order );
798 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
800 msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
802 int meta = columnToMeta( column );
803 if( meta == COLUMN_END ) return;
805 PLItem *item = findById( rootItem, i_root_id );
806 if( !item ) return;
807 QModelIndex qIndex = index( item, 0 );
808 int count = item->children.size();
809 if( count )
811 beginRemoveRows( qIndex, 0, count - 1 );
812 item->removeChildren();
813 endRemoveRows( );
816 PL_LOCK;
818 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
819 i_root_id );
820 if( p_root )
822 playlist_RecursiveNodeSort( p_playlist, p_root,
823 i_column_sorting( meta ),
824 order == Qt::AscendingOrder ?
825 ORDER_NORMAL : ORDER_REVERSE );
829 i_cached_id = i_cached_input_id = -1;
831 if( count )
833 beginInsertRows( qIndex, 0, count - 1 );
834 updateChildren( item );
835 endInsertRows( );
837 PL_UNLOCK;
840 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
842 /** \todo Fire the search with a small delay ? */
843 PL_LOCK;
845 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
846 itemId( idx ) );
847 assert( p_root );
848 const char *psz_name = search_text.toUtf8().data();
849 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );
851 if( idx.isValid() )
853 PLItem *searchRoot = getItem( idx );
855 beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
856 searchRoot->removeChildren();
857 endRemoveRows( );
859 beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
860 updateChildren( searchRoot );
861 endInsertRows();
863 PL_UNLOCK;
864 return;
867 PL_UNLOCK;
868 rebuild();
871 /*********** Popup *********/
872 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
874 int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
876 PL_LOCK;
877 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
878 if( !p_item )
880 PL_UNLOCK;
881 return false;
884 i_popup_item = index.isValid() ? p_item->i_id : -1;
885 i_popup_parent = index.isValid() ?
886 ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
887 ( rootItem->i_id );
888 i_popup_column = index.column();
890 bool tree = var_InheritBool( p_intf, "playlist-tree" );
892 PL_UNLOCK;
894 current_selection = list;
896 QMenu menu;
897 if( i_popup_item > -1 )
899 menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
900 menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
901 qtr(I_POP_DEL), this, SLOT( popupDel() ) );
902 menu.addSeparator();
903 menu.addAction( QIcon( ":/menu/stream" ),
904 qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
905 menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
906 menu.addSeparator();
907 menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
908 menu.addAction( QIcon( ":/type/folder-grey" ),
909 qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
911 if( canEdit() )
913 QIcon addIcon( ":/buttons/playlist/playlist_add" );
914 menu.addSeparator();
915 if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
916 if( rootItem->i_id == THEPL->p_playing->i_id )
918 menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
919 menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
920 menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
922 else if( THEPL->p_media_library &&
923 rootItem->i_id == THEPL->p_media_library->i_id )
925 menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
926 menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
927 menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
930 if( i_popup_item > -1 )
932 menu.addSeparator();
933 QMenu *sort_menu = menu.addMenu( qtr( "Sort by" ) + QString(" ") +
934 qfu( psz_column_title( columnToMeta( index.column() ) ) ) );
935 sort_menu->addAction( qtr( "Ascending" ),
936 this, SLOT( popupSortAsc() ) );
937 sort_menu->addAction( qtr( "Descending" ),
938 this, SLOT( popupSortDesc() ) );
940 if( !menu.isEmpty() )
942 menu.exec( point ); return true;
944 else return false;
947 void PLModel::popupDel()
949 doDelete( current_selection );
952 void PLModel::popupPlay()
954 PL_LOCK;
956 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
957 i_popup_item );
958 activateItem( p_item );
960 PL_UNLOCK;
963 void PLModel::popupInfo()
965 PL_LOCK;
966 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
967 i_popup_item );
968 if( p_item )
970 input_item_t* p_input = p_item->p_input;
971 vlc_gc_incref( p_input );
972 PL_UNLOCK;
973 MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
974 vlc_gc_decref( p_input );
975 mid->setParent( PlaylistDialog::getInstance( p_intf ),
976 Qt::Dialog );
977 mid->show();
978 } else
979 PL_UNLOCK;
982 void PLModel::popupStream()
984 QStringList mrls = selectedURIs();
985 if( !mrls.isEmpty() )
986 THEDP->streamingDialog( NULL, mrls[0], false );
990 void PLModel::popupSave()
992 QStringList mrls = selectedURIs();
993 if( !mrls.isEmpty() )
994 THEDP->streamingDialog( NULL, mrls[0] );
997 #include <QUrl>
998 #include <QFileInfo>
999 #include <QDesktopServices>
1000 void PLModel::popupExplore()
1002 PL_LOCK;
1003 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1004 i_popup_item );
1005 if( p_item )
1007 input_item_t *p_input = p_item->p_input;
1008 char *psz_meta = input_item_GetURI( p_input );
1009 PL_UNLOCK;
1010 if( psz_meta )
1012 const char *psz_access;
1013 const char *psz_demux;
1014 char *psz_path;
1015 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1017 if( EMPTY_STR( psz_access ) ||
1018 !strncasecmp( psz_access, "file", 4 ) ||
1019 !strncasecmp( psz_access, "dire", 4 ) )
1021 QFileInfo info( qfu( psz_meta ) );
1022 QDesktopServices::openUrl(
1023 QUrl::fromLocalFile( info.absolutePath() ) );
1025 free( psz_meta );
1028 else
1029 PL_UNLOCK;
1032 #include <QInputDialog>
1033 void PLModel::popupAddNode()
1035 bool ok;
1036 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1037 qtr( "Create Folder" ), qtr( "Enter name for new folder:" ),
1038 QLineEdit::Normal, QString(), &ok);
1039 if( !ok || name.isEmpty() ) return;
1040 PL_LOCK;
1041 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1042 i_popup_parent );
1043 if( p_item )
1045 playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1047 PL_UNLOCK;
1050 void PLModel::popupSortAsc()
1052 sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1055 void PLModel::popupSortDesc()
1057 sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );