]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt: playlist menu strings and icons
[vlc] / modules / gui / qt4 / components / playlist / playlist_model.cpp
1 /*****************************************************************************
2  * playlist_model.cpp : Manage playlist model
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Ilkka Ollakkka <ileoo (at) videolan dot org>
9  *          Jakob Leben <jleben@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
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>
36
37 #include "pixmaps/types/type_unknown.xpm"
38
39 #include <assert.h>
40 #include <QIcon>
41 #include <QFont>
42 #include <QMenu>
43 #include <QApplication>
44 #include <QSettings>
45
46 #include "sorting.h"
47
48 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
49
50 /*************************************************************************
51  * Playlist model implementation
52  *************************************************************************/
53
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 )
59 {
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;
65
66     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
67
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
80
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 ) );
90 }
91
92 PLModel::~PLModel()
93 {
94     delete rootItem;
95 }
96
97 Qt::DropActions PLModel::supportedDropActions() const
98 {
99     return Qt::CopyAction; /* Why not Qt::MoveAction */
100 }
101
102 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
103 {
104     Qt::ItemFlags flags = QAbstractItemModel::flags( index );
105
106     PLItem *item = index.isValid() ? getItem( index ) : rootItem;
107
108     if( canEdit() )
109     {
110         PL_LOCK;
111         playlist_item_t *plItem =
112             playlist_ItemGetById( p_playlist, item->i_id );
113
114         if ( plItem && ( plItem->i_children > -1 ) )
115             flags |= Qt::ItemIsDropEnabled;
116
117         PL_UNLOCK;
118
119     }
120     flags |= Qt::ItemIsDragEnabled;
121
122     return flags;
123 }
124
125 QStringList PLModel::mimeTypes() const
126 {
127     QStringList types;
128     types << "vlc/qt-playlist-item";
129     return types;
130 }
131
132 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
133 {
134     QMimeData *mimeData = new QMimeData();
135     QByteArray encodedData;
136     QDataStream stream( &encodedData, QIODevice::WriteOnly );
137     QModelIndexList list;
138
139     foreach( const QModelIndex &index, indexes ) {
140         if( index.isValid() && index.column() == 0 )
141             list.append(index);
142     }
143
144     qSort(list);
145
146     foreach( const QModelIndex &index, list ) {
147         PLItem *item = getItem( index );
148         stream.writeRawData( (char*) &item, sizeof( PLItem* ) );
149     }
150     mimeData->setData( "vlc/qt-playlist-item", encodedData );
151     return mimeData;
152 }
153
154 /* Drop operation */
155 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
156                            int row, int column, const QModelIndex &parent )
157 {
158     if( data->hasFormat( "vlc/qt-playlist-item" ) )
159     {
160         if( action == Qt::IgnoreAction )
161             return true;
162
163         PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
164
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 )
169         {
170             PL_UNLOCK;
171             return false;
172         }
173
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;
177         if
178         (
179             row == -1 && (
180             ( p_pl && p_parent == p_pl ) ||
181             ( p_ml && p_parent == p_ml ) )
182         )
183             copy = true;
184         PL_UNLOCK;
185
186         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
187         if( copy )
188             dropAppendCopy( encodedData, parentItem );
189         else
190             dropMove( encodedData, parentItem, row );
191     }
192     return true;
193 }
194
195 void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
196 {
197     QDataStream stream( &data, QIODevice::ReadOnly );
198
199     PL_LOCK;
200     playlist_item_t *p_parent =
201             playlist_ItemGetById( p_playlist, target->i_id );
202     while( !stream.atEnd() )
203     {
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 );
216     }
217     PL_UNLOCK;
218 }
219
220 void PLModel::dropMove( QByteArray& data, PLItem *target, int row )
221 {
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() )
228     {
229         PLItem *item;
230         stream.readRawData( (char*)&item, sizeof(PLItem*) );
231
232         /* better not try to move a node into itself: */
233         PLItem *climber = target;
234         while( climber )
235         {
236             if( climber == item ) break;
237             climber = climber->parentItem;
238         }
239         if( climber ) continue;
240
241         if( item->parentItem == target &&
242             target->children.indexOf( item ) < model_pos )
243                 model_pos--;
244
245         ids.append( item->i_id );
246         model_items.append( item );
247
248         takeItem( item );
249     }
250     int count = ids.size();
251     if( count )
252     {
253         playlist_item_t *pp_items[count];
254
255         PL_LOCK;
256         for( int i = 0; i < count; i++ )
257         {
258             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, ids[i] );
259             if( !p_item )
260             {
261                 PL_UNLOCK;
262                 return;
263             }
264             pp_items[i] = p_item;
265         }
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;
271
272         insertChildren( target, model_items, model_pos );
273     }
274 }
275
276 /* remove item with its id */
277 void PLModel::removeItem( int i_id )
278 {
279     PLItem *item = findById( rootItem, i_id );
280     removeItem( item );
281 }
282
283 void PLModel::activateItem( const QModelIndex &index )
284 {
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;
292 }
293
294 /* Must be entered with lock */
295 void PLModel::activateItem( playlist_item_t *p_item )
296 {
297     if( !p_item ) return;
298     playlist_item_t *p_parent = p_item;
299     while( p_parent )
300     {
301         if( p_parent->i_id == rootItem->i_id ) break;
302         p_parent = p_parent->p_parent;
303     }
304     if( p_parent )
305         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
306                           p_parent, p_item );
307 }
308
309 /****************** Base model mandatory implementations *****************/
310 QVariant PLModel::data( const QModelIndex &index, int role ) const
311 {
312     if( !index.isValid() ) return QVariant();
313     PLItem *item = getItem( index );
314     if( role == Qt::DisplayRole )
315     {
316         int metadata = columnToMeta( index.column() );
317         if( metadata == COLUMN_END ) return QVariant();
318
319         QString returninfo;
320         if( metadata == COLUMN_NUMBER )
321             returninfo = QString::number( index.row() + 1 );
322         else
323         {
324             char *psz = psz_column_meta( item->p_input, metadata );
325             returninfo = qfu( psz );
326             free( psz );
327         }
328         return QVariant( returninfo );
329     }
330     else if( role == Qt::DecorationRole && index.column() == 0  )
331     {
332         /* Used to segfault here because i_type wasn't always initialized */
333         return QVariant( PLModel::icons[item->p_input->i_type] );
334     }
335     else if( role == Qt::FontRole )
336     {
337         if( isCurrent( index ) )
338         {
339             QFont f; f.setBold( true ); return QVariant( f );
340         }
341     }
342     else if( role == Qt::BackgroundRole && isCurrent( index ) )
343     {
344         return QVariant( QBrush( Qt::gray ) );
345     }
346     else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
347
348     return QVariant();
349 }
350
351 bool PLModel::isCurrent( const QModelIndex &index ) const
352 {
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 );
356 }
357
358 int PLModel::itemId( const QModelIndex &index ) const
359 {
360     return getItem( index )->i_id;
361 }
362
363 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
364                               int role ) const
365 {
366     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
367         return QVariant();
368
369     int meta_col = columnToMeta( section );
370
371     if( meta_col == COLUMN_END ) return QVariant();
372
373     return QVariant( qfu( psz_column_title( meta_col ) ) );
374 }
375
376 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
377                   const
378 {
379     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
380
381     PLItem *childItem = parentItem->child( row );
382     if( childItem )
383         return createIndex( row, column, childItem );
384     else
385         return QModelIndex();
386 }
387
388 QModelIndex PLModel::index( int i_id, int c )
389 {
390   return index( findById( rootItem, i_id ), c );
391 }
392
393 /* Return the index of a given item */
394 QModelIndex PLModel::index( PLItem *item, int column ) const
395 {
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();
402 }
403
404 QModelIndex PLModel::currentIndex()
405 {
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 );
410 }
411
412 QModelIndex PLModel::parent( const QModelIndex &index ) const
413 {
414     if( !index.isValid() ) return QModelIndex();
415
416     PLItem *childItem = getItem( index );
417     if( !childItem )
418     {
419         msg_Err( p_playlist, "NULL CHILD" );
420         return QModelIndex();
421     }
422
423     PLItem *parentItem = childItem->parent();
424     if( !parentItem || parentItem == rootItem ) return QModelIndex();
425     if( !parentItem->parentItem )
426     {
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 );
430     }
431     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
432     return ind;
433 }
434
435 int PLModel::columnCount( const QModelIndex &i) const
436 {
437     return columnFromMeta( COLUMN_END );
438 }
439
440 int PLModel::rowCount( const QModelIndex &parent ) const
441 {
442     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
443     return parentItem->childCount();
444 }
445
446 QStringList PLModel::selectedURIs()
447 {
448     QStringList lst;
449     for( int i = 0; i < current_selection.size(); i++ )
450     {
451         PLItem *item = getItem( current_selection[i] );
452         if( item )
453         {
454             PL_LOCK;
455             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
456             if( p_item )
457             {
458                 char *psz = input_item_GetURI( p_item->p_input );
459                 if( psz )
460                 {
461                     lst.append( qfu(psz) );
462                     free( psz );
463                 }
464             }
465             PL_UNLOCK;
466         }
467     }
468     return lst;
469 }
470
471
472 /************************* Lookups *****************************/
473
474 PLItem *PLModel::findById( PLItem *root, int i_id )
475 {
476     return findInner( root, i_id, false );
477 }
478
479 PLItem *PLModel::findByInput( PLItem *root, int i_id )
480 {
481     PLItem *result = findInner( root, i_id, true );
482     return result;
483 }
484
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; }
487
488 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
489 {
490     if( !root ) return NULL;
491     if( ( !b_input && i_cached_id == i_id) ||
492         ( b_input && i_cached_input_id ==i_id ) )
493     {
494         return b_input ? p_cached_item_bi : p_cached_item;
495     }
496
497     if( !b_input && root->i_id == i_id )
498     {
499         CACHE( i_id, root );
500         return root;
501     }
502     else if( b_input && root->p_input->i_id == i_id )
503     {
504         ICACHE( i_id, root );
505         return root;
506     }
507
508     QList<PLItem *>::iterator it = root->children.begin();
509     while ( it != root->children.end() )
510     {
511         if( !b_input && (*it)->i_id == i_id )
512         {
513             CACHE( i_id, (*it) );
514             return p_cached_item;
515         }
516         else if( b_input && (*it)->p_input->i_id == i_id )
517         {
518             ICACHE( i_id, (*it) );
519             return p_cached_item_bi;
520         }
521         if( (*it)->children.size() )
522         {
523             PLItem *childFound = findInner( (*it), i_id, b_input );
524             if( childFound )
525             {
526                 if( b_input )
527                     ICACHE( i_id, childFound )
528                 else
529                     CACHE( i_id, childFound )
530                 return childFound;
531             }
532         }
533         it++;
534     }
535     return NULL;
536 }
537 #undef CACHE
538 #undef ICACHE
539
540 int PLModel::columnToMeta( int _column )
541 {
542     int meta = 1;
543     int column = 0;
544
545     while( column != _column && meta != COLUMN_END )
546     {
547         meta <<= 1;
548         column++;
549     }
550
551     return meta;
552 }
553
554 int PLModel::columnFromMeta( int meta_col )
555 {
556     int meta = 1;
557     int column = 0;
558
559     while( meta != meta_col && meta != COLUMN_END )
560     {
561         meta <<= 1;
562         column++;
563     }
564
565     return column;
566 }
567
568 bool PLModel::canEdit() const
569 {
570   return (
571     rootItem != NULL &&
572     (
573       rootItem->p_input == p_playlist->p_playing->p_input ||
574       (
575         p_playlist->p_media_library &&
576         rootItem->p_input == p_playlist->p_media_library->p_input
577       )
578     )
579   );
580 }
581 /************************* Updates handling *****************************/
582
583 /**** Events processing ****/
584 void PLModel::processInputItemUpdate( input_thread_t *p_input )
585 {
586     if( !p_input ) return;
587     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
588     {
589         PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
590         if( item ) emit currentChanged( index( item, 0 ) );
591     }
592     processInputItemUpdate( input_GetItem( p_input ) );
593 }
594
595 void PLModel::processInputItemUpdate( input_item_t *p_item )
596 {
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 );
601 }
602
603 void PLModel::processItemRemoval( int i_id )
604 {
605     if( i_id <= 0 ) return;
606     removeItem( i_id );
607 }
608
609 void PLModel::processItemAppend( int i_item, int i_parent )
610 {
611     playlist_item_t *p_item = NULL;
612     PLItem *newItem = NULL;
613     input_thread_t *currentInputThread;
614     int pos;
615
616     PLItem *nodeItem = findById( rootItem, i_parent );
617     if( !nodeItem ) return;
618
619     foreach( PLItem *existing, nodeItem->children )
620       if( existing->i_id == i_item ) return;
621
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;
625
626     for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
627         if( p_item->p_parent->pp_children[pos] == p_item ) break;
628
629     newItem = new PLItem( p_item, nodeItem );
630     PL_UNLOCK;
631
632     beginInsertRows( index( nodeItem, 0 ), pos, pos );
633     nodeItem->insertChild( newItem, pos );
634     endInsertRows();
635
636     return;
637 end:
638     PL_UNLOCK;
639     return;
640 }
641
642
643 void PLModel::rebuild()
644 {
645     rebuild( NULL );
646 }
647
648 void PLModel::rebuild( playlist_item_t *p_root )
649 {
650     playlist_item_t* p_item;
651
652     /* Invalidate cache */
653     i_cached_id = i_cached_input_id = -1;
654
655     if( rootItem ) rootItem->removeChildren();
656
657     PL_LOCK;
658     if( p_root )
659     {
660         delete rootItem;
661         rootItem = new PLItem( p_root );
662     }
663     assert( rootItem );
664     /* Recreate from root */
665     updateChildren( rootItem );
666     PL_UNLOCK;
667
668     /* And signal the view */
669     reset();
670
671     if( p_root ) emit rootChanged();
672 }
673
674 void PLModel::takeItem( PLItem *item )
675 {
676     assert( item );
677     PLItem *parent = item->parentItem;
678     assert( parent );
679     int i_index = parent->children.indexOf( item );
680
681     beginRemoveRows( index( parent, 0 ), i_index, i_index );
682     parent->takeChildAt( i_index );
683     endRemoveRows();
684 }
685
686 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
687 {
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++ )
693     {
694         node->children.insert( i_pos + i, items[i] );
695         items[i]->parentItem = node;
696     }
697     endInsertRows();
698 }
699
700 void PLModel::removeItem( PLItem *item )
701 {
702     if( !item ) return;
703
704     if( item->i_id == i_cached_id ) i_cached_id = -1;
705     i_cached_input_id = -1;
706
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();
713     }
714     else delete item;
715
716     if(item == rootItem)
717     {
718         rootItem = NULL;
719         rebuild( p_playlist->p_playing );
720     }
721 }
722
723 /* This function must be entered WITH the playlist lock */
724 void PLModel::updateChildren( PLItem *root )
725 {
726     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
727     updateChildren( p_node, root );
728 }
729
730 /* This function must be entered WITH the playlist lock */
731 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
732 {
733     for( int i = 0; i < p_node->i_children ; i++ )
734     {
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 );
740     }
741 }
742
743 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
744 void PLModel::updateTreeItem( PLItem *item )
745 {
746     if( !item ) return;
747     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
748 }
749
750 /************************* Actions ******************************/
751
752 /**
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?
757  */
758 void PLModel::doDelete( QModelIndexList selected )
759 {
760     if( !canEdit() ) return;
761
762     while( !selected.isEmpty() )
763     {
764         QModelIndex index = selected[0];
765         selected.removeAt( 0 );
766
767         if( index.column() != 0 ) continue;
768
769         PLItem *item = getItem( index );
770         if( item->children.size() )
771             recurseDelete( item->children, &selected );
772
773         PL_LOCK;
774         playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
775         PL_UNLOCK;
776
777         removeItem( item );
778     }
779 }
780
781 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
782 {
783     for( int i = children.size() - 1; i >= 0 ; i-- )
784     {
785         PLItem *item = children[i];
786         if( item->children.size() )
787             recurseDelete( item->children, fullList );
788         fullList->removeAll( index( item, 0 ) );
789     }
790 }
791
792 /******* Volume III: Sorting and searching ********/
793 void PLModel::sort( int column, Qt::SortOrder order )
794 {
795     sort( rootItem->i_id, column, order );
796 }
797
798 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
799 {
800     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
801
802     int meta = columnToMeta( column );
803     if( meta == COLUMN_END ) return;
804
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 )
810     {
811         beginRemoveRows( qIndex, 0, count - 1 );
812         item->removeChildren();
813         endRemoveRows( );
814     }
815
816     PL_LOCK;
817     {
818         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
819                                                         i_root_id );
820         if( p_root )
821         {
822             playlist_RecursiveNodeSort( p_playlist, p_root,
823                                         i_column_sorting( meta ),
824                                         order == Qt::AscendingOrder ?
825                                             ORDER_NORMAL : ORDER_REVERSE );
826         }
827     }
828
829     i_cached_id = i_cached_input_id = -1;
830
831     if( count )
832     {
833         beginInsertRows( qIndex, 0, count - 1 );
834         updateChildren( item );
835         endInsertRows( );
836     }
837     PL_UNLOCK;
838 }
839
840 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
841 {
842     /** \todo Fire the search with a small delay ? */
843     PL_LOCK;
844     {
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 );
850
851         if( idx.isValid() )
852         {
853             PLItem *searchRoot = getItem( idx );
854
855             beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
856             searchRoot->removeChildren();
857             endRemoveRows( );
858
859             beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
860             updateChildren( searchRoot );
861             endInsertRows();
862
863             PL_UNLOCK;
864             return;
865         }
866     }
867     PL_UNLOCK;
868     rebuild();
869 }
870
871 /*********** Popup *********/
872 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
873 {
874     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
875
876     PL_LOCK;
877     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
878     if( !p_item )
879     {
880         PL_UNLOCK;
881         return false;
882     }
883
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();
889
890     bool tree = var_InheritBool( p_intf, "playlist-tree" );
891
892     PL_UNLOCK;
893
894     current_selection = list;
895
896     QMenu menu;
897     if( i_popup_item > -1 )
898     {
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() ) );
910     }
911     if( canEdit() )
912     {
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 )
917         {
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()) );
921         }
922         else if( THEPL->p_media_library &&
923                     rootItem->i_id == THEPL->p_media_library->i_id )
924         {
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() ) );
928         }
929     }
930     if( i_popup_item > -1 )
931     {
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() ) );
939     }
940     if( !menu.isEmpty() )
941     {
942         menu.exec( point ); return true;
943     }
944     else return false;
945 }
946
947 void PLModel::popupDel()
948 {
949     doDelete( current_selection );
950 }
951
952 void PLModel::popupPlay()
953 {
954     PL_LOCK;
955     {
956         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
957                                                         i_popup_item );
958         activateItem( p_item );
959     }
960     PL_UNLOCK;
961 }
962
963 void PLModel::popupInfo()
964 {
965     PL_LOCK;
966     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
967                                                     i_popup_item );
968     if( p_item )
969     {
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;
980 }
981
982 void PLModel::popupStream()
983 {
984     QStringList mrls = selectedURIs();
985     if( !mrls.isEmpty() )
986         THEDP->streamingDialog( NULL, mrls[0], false );
987
988 }
989
990 void PLModel::popupSave()
991 {
992     QStringList mrls = selectedURIs();
993     if( !mrls.isEmpty() )
994         THEDP->streamingDialog( NULL, mrls[0] );
995 }
996
997 #include <QUrl>
998 #include <QFileInfo>
999 #include <QDesktopServices>
1000 void PLModel::popupExplore()
1001 {
1002     PL_LOCK;
1003     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1004                                                     i_popup_item );
1005     if( p_item )
1006     {
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 )
1011        {
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 );
1016
1017            if( EMPTY_STR( psz_access ) ||
1018                !strncasecmp( psz_access, "file", 4 ) ||
1019                !strncasecmp( psz_access, "dire", 4 ) )
1020            {
1021                QFileInfo info( qfu( psz_meta ) );
1022                QDesktopServices::openUrl(
1023                                QUrl::fromLocalFile( info.absolutePath() ) );
1024            }
1025            free( psz_meta );
1026        }
1027     }
1028     else
1029         PL_UNLOCK;
1030 }
1031
1032 #include <QInputDialog>
1033 void PLModel::popupAddNode()
1034 {
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 )
1044     {
1045         playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1046     }
1047     PL_UNLOCK;
1048 }
1049
1050 void PLModel::popupSortAsc()
1051 {
1052     sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1053 }
1054
1055 void PLModel::popupSortDesc()
1056 {
1057     sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );
1058 }