]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
fdb9f288a9e433331b25f5aa8af8384c2d446d56
[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     return getItem( index )->p_input == THEMIM->currentInputItem();
354 }
355
356 int PLModel::itemId( const QModelIndex &index ) const
357 {
358     return getItem( index )->i_id;
359 }
360
361 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
362                               int role ) const
363 {
364     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
365         return QVariant();
366
367     int meta_col = columnToMeta( section );
368
369     if( meta_col == COLUMN_END ) return QVariant();
370
371     return QVariant( qfu( psz_column_title( meta_col ) ) );
372 }
373
374 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
375                   const
376 {
377     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
378
379     PLItem *childItem = parentItem->child( row );
380     if( childItem )
381         return createIndex( row, column, childItem );
382     else
383         return QModelIndex();
384 }
385
386 QModelIndex PLModel::index( int i_id, int c )
387 {
388   return index( findById( rootItem, i_id ), c );
389 }
390
391 /* Return the index of a given item */
392 QModelIndex PLModel::index( PLItem *item, int column ) const
393 {
394     if( !item ) return QModelIndex();
395     const PLItem *parent = item->parent();
396     if( parent )
397         return createIndex( parent->children.lastIndexOf( item ),
398                             column, item );
399     return QModelIndex();
400 }
401
402 QModelIndex PLModel::currentIndex()
403 {
404     input_thread_t *p_input_thread = THEMIM->getInput();
405     if( !p_input_thread ) return QModelIndex();
406     PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
407     return index( item, 0 );
408 }
409
410 QModelIndex PLModel::parent( const QModelIndex &index ) const
411 {
412     if( !index.isValid() ) return QModelIndex();
413
414     PLItem *childItem = getItem( index );
415     if( !childItem )
416     {
417         msg_Err( p_playlist, "NULL CHILD" );
418         return QModelIndex();
419     }
420
421     PLItem *parentItem = childItem->parent();
422     if( !parentItem || parentItem == rootItem ) return QModelIndex();
423     if( !parentItem->parentItem )
424     {
425         msg_Err( p_playlist, "No parent parent, trying row 0 " );
426         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
427         return createIndex( 0, 0, parentItem );
428     }
429     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
430     return ind;
431 }
432
433 int PLModel::columnCount( const QModelIndex &i) const
434 {
435     return columnFromMeta( COLUMN_END );
436 }
437
438 int PLModel::rowCount( const QModelIndex &parent ) const
439 {
440     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
441     return parentItem->childCount();
442 }
443
444 QStringList PLModel::selectedURIs()
445 {
446     QStringList lst;
447     for( int i = 0; i < current_selection.size(); i++ )
448     {
449         PLItem *item = getItem( current_selection[i] );
450         if( item )
451         {
452             PL_LOCK;
453             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
454             if( p_item )
455             {
456                 char *psz = input_item_GetURI( p_item->p_input );
457                 if( psz )
458                 {
459                     lst.append( qfu(psz) );
460                     free( psz );
461                 }
462             }
463             PL_UNLOCK;
464         }
465     }
466     return lst;
467 }
468
469
470 /************************* Lookups *****************************/
471
472 PLItem *PLModel::findById( PLItem *root, int i_id )
473 {
474     return findInner( root, i_id, false );
475 }
476
477 PLItem *PLModel::findByInput( PLItem *root, int i_id )
478 {
479     PLItem *result = findInner( root, i_id, true );
480     return result;
481 }
482
483 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
484 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
485
486 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
487 {
488     if( !root ) return NULL;
489     if( ( !b_input && i_cached_id == i_id) ||
490         ( b_input && i_cached_input_id ==i_id ) )
491     {
492         return b_input ? p_cached_item_bi : p_cached_item;
493     }
494
495     if( !b_input && root->i_id == i_id )
496     {
497         CACHE( i_id, root );
498         return root;
499     }
500     else if( b_input && root->p_input->i_id == i_id )
501     {
502         ICACHE( i_id, root );
503         return root;
504     }
505
506     QList<PLItem *>::iterator it = root->children.begin();
507     while ( it != root->children.end() )
508     {
509         if( !b_input && (*it)->i_id == i_id )
510         {
511             CACHE( i_id, (*it) );
512             return p_cached_item;
513         }
514         else if( b_input && (*it)->p_input->i_id == i_id )
515         {
516             ICACHE( i_id, (*it) );
517             return p_cached_item_bi;
518         }
519         if( (*it)->children.size() )
520         {
521             PLItem *childFound = findInner( (*it), i_id, b_input );
522             if( childFound )
523             {
524                 if( b_input )
525                     ICACHE( i_id, childFound )
526                 else
527                     CACHE( i_id, childFound )
528                 return childFound;
529             }
530         }
531         it++;
532     }
533     return NULL;
534 }
535 #undef CACHE
536 #undef ICACHE
537
538 int PLModel::columnToMeta( int _column )
539 {
540     int meta = 1;
541     int column = 0;
542
543     while( column != _column && meta != COLUMN_END )
544     {
545         meta <<= 1;
546         column++;
547     }
548
549     return meta;
550 }
551
552 int PLModel::columnFromMeta( int meta_col )
553 {
554     int meta = 1;
555     int column = 0;
556
557     while( meta != meta_col && meta != COLUMN_END )
558     {
559         meta <<= 1;
560         column++;
561     }
562
563     return column;
564 }
565
566 bool PLModel::canEdit() const
567 {
568   return (
569     rootItem != NULL &&
570     (
571       rootItem->p_input == p_playlist->p_playing->p_input ||
572       (
573         p_playlist->p_media_library &&
574         rootItem->p_input == p_playlist->p_media_library->p_input
575       )
576     )
577   );
578 }
579 /************************* Updates handling *****************************/
580
581 /**** Events processing ****/
582 void PLModel::processInputItemUpdate( input_thread_t *p_input )
583 {
584     if( !p_input ) return;
585     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
586     {
587         PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
588         if( item ) emit currentChanged( index( item, 0 ) );
589     }
590     processInputItemUpdate( input_GetItem( p_input ) );
591 }
592
593 void PLModel::processInputItemUpdate( input_item_t *p_item )
594 {
595     if( !p_item ||  p_item->i_id <= 0 ) return;
596     PLItem *item = findByInput( rootItem, p_item->i_id );
597     if( item )
598         updateTreeItem( item );
599 }
600
601 void PLModel::processItemRemoval( int i_id )
602 {
603     if( i_id <= 0 ) return;
604     removeItem( i_id );
605 }
606
607 void PLModel::processItemAppend( int i_item, int i_parent )
608 {
609     playlist_item_t *p_item = NULL;
610     PLItem *newItem = NULL;
611     input_thread_t *currentInputThread;
612     int pos;
613
614     PLItem *nodeItem = findById( rootItem, i_parent );
615     if( !nodeItem ) return;
616
617     foreach( PLItem *existing, nodeItem->children )
618       if( existing->i_id == i_item ) return;
619
620     PL_LOCK;
621     p_item = playlist_ItemGetById( p_playlist, i_item );
622     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
623     {
624         PL_UNLOCK; return;
625     }
626
627     for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
628         if( p_item->p_parent->pp_children[pos] == p_item ) break;
629
630     newItem = new PLItem( p_item, nodeItem );
631     PL_UNLOCK;
632
633     beginInsertRows( index( nodeItem, 0 ), pos, pos );
634     nodeItem->insertChild( newItem, pos );
635     endInsertRows();
636
637     if( newItem->p_input == THEMIM->currentInputItem() )
638         emit currentChanged( index( newItem, 0 ) );
639 }
640
641
642 void PLModel::rebuild()
643 {
644     rebuild( NULL );
645 }
646
647 void PLModel::rebuild( playlist_item_t *p_root )
648 {
649     playlist_item_t* p_item;
650
651     /* Invalidate cache */
652     i_cached_id = i_cached_input_id = -1;
653
654     if( rootItem ) rootItem->removeChildren();
655
656     PL_LOCK;
657     if( p_root )
658     {
659         delete rootItem;
660         rootItem = new PLItem( p_root );
661     }
662     assert( rootItem );
663     /* Recreate from root */
664     updateChildren( rootItem );
665     PL_UNLOCK;
666
667     /* And signal the view */
668     reset();
669
670     if( p_root ) emit rootChanged();
671 }
672
673 void PLModel::takeItem( PLItem *item )
674 {
675     assert( item );
676     PLItem *parent = item->parentItem;
677     assert( parent );
678     int i_index = parent->children.indexOf( item );
679
680     beginRemoveRows( index( parent, 0 ), i_index, i_index );
681     parent->takeChildAt( i_index );
682     endRemoveRows();
683 }
684
685 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
686 {
687     assert( node );
688     int count = items.size();
689     if( !count ) return;
690     beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
691     for( int i = 0; i < count; i++ )
692     {
693         node->children.insert( i_pos + i, items[i] );
694         items[i]->parentItem = node;
695     }
696     endInsertRows();
697 }
698
699 void PLModel::removeItem( PLItem *item )
700 {
701     if( !item ) return;
702
703     if( item->i_id == i_cached_id ) i_cached_id = -1;
704     i_cached_input_id = -1;
705
706     if( item->parentItem ) {
707         int i = item->parentItem->children.indexOf( item );
708         beginRemoveRows( index( item->parentItem, 0), i, i );
709         item->parentItem->children.removeAt(i);
710         delete item;
711         endRemoveRows();
712     }
713     else delete item;
714
715     if(item == rootItem)
716     {
717         rootItem = NULL;
718         rebuild( p_playlist->p_playing );
719     }
720 }
721
722 /* This function must be entered WITH the playlist lock */
723 void PLModel::updateChildren( PLItem *root )
724 {
725     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
726     updateChildren( p_node, root );
727 }
728
729 /* This function must be entered WITH the playlist lock */
730 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
731 {
732     for( int i = 0; i < p_node->i_children ; i++ )
733     {
734         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
735         PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
736         root->appendChild( newItem );
737         if( p_node->pp_children[i]->i_children != -1 )
738             updateChildren( p_node->pp_children[i], newItem );
739     }
740 }
741
742 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
743 void PLModel::updateTreeItem( PLItem *item )
744 {
745     if( !item ) return;
746     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
747 }
748
749 /************************* Actions ******************************/
750
751 /**
752  * Deletion, here we have to do a ugly slow hack as we retrieve the full
753  * list of indexes to delete at once: when we delete a node and all of
754  * its children, we need to update the list.
755  * Todo: investigate whethere we can use ranges to be sure to delete all items?
756  */
757 void PLModel::doDelete( QModelIndexList selected )
758 {
759     if( !canEdit() ) return;
760
761     while( !selected.isEmpty() )
762     {
763         QModelIndex index = selected[0];
764         selected.removeAt( 0 );
765
766         if( index.column() != 0 ) continue;
767
768         PLItem *item = getItem( index );
769         if( item->children.size() )
770             recurseDelete( item->children, &selected );
771
772         PL_LOCK;
773         playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
774         PL_UNLOCK;
775
776         removeItem( item );
777     }
778 }
779
780 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
781 {
782     for( int i = children.size() - 1; i >= 0 ; i-- )
783     {
784         PLItem *item = children[i];
785         if( item->children.size() )
786             recurseDelete( item->children, fullList );
787         fullList->removeAll( index( item, 0 ) );
788     }
789 }
790
791 /******* Volume III: Sorting and searching ********/
792 void PLModel::sort( int column, Qt::SortOrder order )
793 {
794     sort( rootItem->i_id, column, order );
795 }
796
797 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
798 {
799     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
800
801     int meta = columnToMeta( column );
802     if( meta == COLUMN_END ) return;
803
804     PLItem *item = findById( rootItem, i_root_id );
805     if( !item ) return;
806     QModelIndex qIndex = index( item, 0 );
807     int count = item->children.size();
808     if( count )
809     {
810         beginRemoveRows( qIndex, 0, count - 1 );
811         item->removeChildren();
812         endRemoveRows( );
813     }
814
815     PL_LOCK;
816     {
817         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
818                                                         i_root_id );
819         if( p_root )
820         {
821             playlist_RecursiveNodeSort( p_playlist, p_root,
822                                         i_column_sorting( meta ),
823                                         order == Qt::AscendingOrder ?
824                                             ORDER_NORMAL : ORDER_REVERSE );
825         }
826     }
827
828     i_cached_id = i_cached_input_id = -1;
829
830     if( count )
831     {
832         beginInsertRows( qIndex, 0, count - 1 );
833         updateChildren( item );
834         endInsertRows( );
835     }
836     PL_UNLOCK;
837 }
838
839 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
840 {
841     /** \todo Fire the search with a small delay ? */
842     PL_LOCK;
843     {
844         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
845                                                         itemId( idx ) );
846         assert( p_root );
847         const char *psz_name = search_text.toUtf8().data();
848         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );
849
850         if( idx.isValid() )
851         {
852             PLItem *searchRoot = getItem( idx );
853
854             beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
855             searchRoot->removeChildren();
856             endRemoveRows( );
857
858             beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
859             updateChildren( searchRoot );
860             endInsertRows();
861
862             PL_UNLOCK;
863             return;
864         }
865     }
866     PL_UNLOCK;
867     rebuild();
868 }
869
870 /*********** Popup *********/
871 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
872 {
873     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
874
875     PL_LOCK;
876     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
877     if( !p_item )
878     {
879         PL_UNLOCK;
880         return false;
881     }
882
883     i_popup_item = index.isValid() ? p_item->i_id : -1;
884     i_popup_parent = index.isValid() ?
885         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
886         ( rootItem->i_id );
887     i_popup_column = index.column();
888
889     bool tree = var_InheritBool( p_intf, "playlist-tree" );
890
891     PL_UNLOCK;
892
893     current_selection = list;
894
895     QMenu menu;
896     if( i_popup_item > -1 )
897     {
898         menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
899         menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
900                         qtr(I_POP_DEL), this, SLOT( popupDel() ) );
901         menu.addSeparator();
902         menu.addAction( QIcon( ":/menu/stream" ),
903                         qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
904         menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
905         menu.addSeparator();
906         menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
907         menu.addAction( QIcon( ":/type/folder-grey" ),
908                         qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
909     }
910     if( canEdit() )
911     {
912         QIcon addIcon( ":/buttons/playlist/playlist_add" );
913         menu.addSeparator();
914         if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
915         if( rootItem->i_id == THEPL->p_playing->i_id )
916         {
917             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
918             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
919             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
920         }
921         else if( THEPL->p_media_library &&
922                     rootItem->i_id == THEPL->p_media_library->i_id )
923         {
924             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
925             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
926             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
927         }
928     }
929     if( i_popup_item > -1 )
930     {
931         menu.addSeparator();
932         QMenu *sort_menu = menu.addMenu( qtr( "Sort by" ) + QString(" ") +
933             qfu( psz_column_title( columnToMeta( index.column() ) ) ) );
934         sort_menu->addAction( qtr( "Ascending" ),
935             this, SLOT( popupSortAsc() ) );
936         sort_menu->addAction( qtr( "Descending" ),
937             this, SLOT( popupSortDesc() ) );
938     }
939     if( !menu.isEmpty() )
940     {
941         menu.exec( point ); return true;
942     }
943     else return false;
944 }
945
946 void PLModel::popupDel()
947 {
948     doDelete( current_selection );
949 }
950
951 void PLModel::popupPlay()
952 {
953     PL_LOCK;
954     {
955         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
956                                                         i_popup_item );
957         activateItem( p_item );
958     }
959     PL_UNLOCK;
960 }
961
962 void PLModel::popupInfo()
963 {
964     PL_LOCK;
965     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
966                                                     i_popup_item );
967     if( p_item )
968     {
969         input_item_t* p_input = p_item->p_input;
970         vlc_gc_incref( p_input );
971         PL_UNLOCK;
972         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
973         vlc_gc_decref( p_input );
974         mid->setParent( PlaylistDialog::getInstance( p_intf ),
975                         Qt::Dialog );
976         mid->show();
977     } else
978         PL_UNLOCK;
979 }
980
981 void PLModel::popupStream()
982 {
983     QStringList mrls = selectedURIs();
984     if( !mrls.isEmpty() )
985         THEDP->streamingDialog( NULL, mrls[0], false );
986
987 }
988
989 void PLModel::popupSave()
990 {
991     QStringList mrls = selectedURIs();
992     if( !mrls.isEmpty() )
993         THEDP->streamingDialog( NULL, mrls[0] );
994 }
995
996 #include <QUrl>
997 #include <QFileInfo>
998 #include <QDesktopServices>
999 void PLModel::popupExplore()
1000 {
1001     PL_LOCK;
1002     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1003                                                     i_popup_item );
1004     if( p_item )
1005     {
1006        input_item_t *p_input = p_item->p_input;
1007        char *psz_meta = input_item_GetURI( p_input );
1008        PL_UNLOCK;
1009        if( psz_meta )
1010        {
1011            const char *psz_access;
1012            const char *psz_demux;
1013            char  *psz_path;
1014            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1015
1016            if( EMPTY_STR( psz_access ) ||
1017                !strncasecmp( psz_access, "file", 4 ) ||
1018                !strncasecmp( psz_access, "dire", 4 ) )
1019            {
1020                QFileInfo info( qfu( psz_meta ) );
1021                QDesktopServices::openUrl(
1022                                QUrl::fromLocalFile( info.absolutePath() ) );
1023            }
1024            free( psz_meta );
1025        }
1026     }
1027     else
1028         PL_UNLOCK;
1029 }
1030
1031 #include <QInputDialog>
1032 void PLModel::popupAddNode()
1033 {
1034     bool ok;
1035     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1036         qtr( "Create Folder" ), qtr( "Enter name for new folder:" ),
1037         QLineEdit::Normal, QString(), &ok);
1038     if( !ok || name.isEmpty() ) return;
1039     PL_LOCK;
1040     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1041                                                     i_popup_parent );
1042     if( p_item )
1043     {
1044         playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1045     }
1046     PL_UNLOCK;
1047 }
1048
1049 void PLModel::popupSortAsc()
1050 {
1051     sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1052 }
1053
1054 void PLModel::popupSortDesc()
1055 {
1056     sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );
1057 }