]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt: media library is always a tree
[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 #include <QUrl>
46 #include <QFileInfo>
47 #include <QDesktopServices>
48 #include <QInputDialog>
49
50 #include "sorting.h"
51
52 #define I_NEW_DIR \
53     I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
54 #define I_NEW_DIR_NAME \
55     I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
56                      N_( "Enter name for new folder:" ) )
57
58 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
59
60 /*************************************************************************
61  * Playlist model implementation
62  *************************************************************************/
63
64 PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
65                   intf_thread_t *_p_intf,   /* main Qt p_intf */
66                   playlist_item_t * p_root,
67                   QObject *parent )         /* Basic Qt parent */
68                   : QAbstractItemModel( parent )
69 {
70     p_intf            = _p_intf;
71     p_playlist        = _p_playlist;
72     i_cached_id       = -1;
73     i_cached_input_id = -1;
74     i_popup_item      = i_popup_parent = -1;
75
76     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
77
78     /* Icons initialization */
79 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
80     ADD_ICON( UNKNOWN , type_unknown_xpm );
81     ADD_ICON( FILE, ":/type/file" );
82     ADD_ICON( DIRECTORY, ":/type/directory" );
83     ADD_ICON( DISC, ":/type/disc" );
84     ADD_ICON( CDDA, ":/type/cdda" );
85     ADD_ICON( CARD, ":/type/capture-card" );
86     ADD_ICON( NET, ":/type/net" );
87     ADD_ICON( PLAYLIST, ":/type/playlist" );
88     ADD_ICON( NODE, ":/type/node" );
89 #undef ADD_ICON
90
91     rebuild( p_root );
92     CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
93             this, processInputItemUpdate( input_item_t *) );
94     CONNECT( THEMIM, inputChanged( input_thread_t * ),
95             this, processInputItemUpdate( input_thread_t* ) );
96     CONNECT( THEMIM, playlistItemAppended( int, int ),
97              this, processItemAppend( int, int ) );
98     CONNECT( THEMIM, playlistItemRemoved( int ),
99              this, processItemRemoval( int ) );
100 }
101
102 PLModel::~PLModel()
103 {
104     delete rootItem;
105 }
106
107 Qt::DropActions PLModel::supportedDropActions() const
108 {
109     return Qt::CopyAction; /* Why not Qt::MoveAction */
110 }
111
112 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
113 {
114     Qt::ItemFlags flags = QAbstractItemModel::flags( index );
115
116     PLItem *item = index.isValid() ? getItem( index ) : rootItem;
117
118     if( canEdit() )
119     {
120         PL_LOCK;
121         playlist_item_t *plItem =
122             playlist_ItemGetById( p_playlist, item->i_id );
123
124         if ( plItem && ( plItem->i_children > -1 ) )
125             flags |= Qt::ItemIsDropEnabled;
126
127         PL_UNLOCK;
128
129     }
130     flags |= Qt::ItemIsDragEnabled;
131
132     return flags;
133 }
134
135 QStringList PLModel::mimeTypes() const
136 {
137     QStringList types;
138     types << "vlc/qt-playlist-item";
139     return types;
140 }
141
142 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
143 {
144     QMimeData *mimeData = new QMimeData();
145     QByteArray encodedData;
146     QDataStream stream( &encodedData, QIODevice::WriteOnly );
147     QModelIndexList list;
148
149     foreach( const QModelIndex &index, indexes ) {
150         if( index.isValid() && index.column() == 0 )
151             list.append(index);
152     }
153
154     qSort(list);
155
156     foreach( const QModelIndex &index, list ) {
157         PLItem *item = getItem( index );
158         stream.writeRawData( (char*) &item, sizeof( PLItem* ) );
159     }
160     mimeData->setData( "vlc/qt-playlist-item", encodedData );
161     return mimeData;
162 }
163
164 /* Drop operation */
165 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
166                            int row, int column, const QModelIndex &parent )
167 {
168     if( data->hasFormat( "vlc/qt-playlist-item" ) )
169     {
170         if( action == Qt::IgnoreAction )
171             return true;
172
173         PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
174
175         PL_LOCK;
176         playlist_item_t *p_parent =
177             playlist_ItemGetById( p_playlist, parentItem->i_id );
178         if( !p_parent || p_parent->i_children == -1 )
179         {
180             PL_UNLOCK;
181             return false;
182         }
183
184         bool copy = false;
185         playlist_item_t *p_pl = p_playlist->p_playing;
186         playlist_item_t *p_ml = p_playlist->p_media_library;
187         if
188         (
189             row == -1 && (
190             ( p_pl && p_parent == p_pl ) ||
191             ( p_ml && p_parent == p_ml ) )
192         )
193             copy = true;
194         PL_UNLOCK;
195
196         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
197         if( copy )
198             dropAppendCopy( encodedData, parentItem );
199         else
200             dropMove( encodedData, parentItem, row );
201     }
202     return true;
203 }
204
205 void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
206 {
207     QDataStream stream( &data, QIODevice::ReadOnly );
208
209     PL_LOCK;
210     playlist_item_t *p_parent =
211             playlist_ItemGetById( p_playlist, target->i_id );
212     while( !stream.atEnd() )
213     {
214         PLItem *item;
215         stream.readRawData( (char*)&item, sizeof(PLItem*) );
216         playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
217         if( !p_item ) continue;
218         input_item_t *p_input = p_item->p_input;
219         playlist_AddExt ( p_playlist,
220             p_input->psz_uri, p_input->psz_name,
221             PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
222             p_input->i_duration,
223             p_input->i_options, p_input->ppsz_options, p_input->optflagc,
224             p_parent == p_playlist->p_playing,
225             true );
226     }
227     PL_UNLOCK;
228 }
229
230 void PLModel::dropMove( QByteArray& data, PLItem *target, int row )
231 {
232     QDataStream stream( &data, QIODevice::ReadOnly );
233     QList<PLItem*> model_items;
234     QList<int> ids;
235     int new_pos = row == -1 ? target->children.size() : row;
236     int model_pos = new_pos;
237     while( !stream.atEnd() )
238     {
239         PLItem *item;
240         stream.readRawData( (char*)&item, sizeof(PLItem*) );
241
242         /* better not try to move a node into itself: */
243         PLItem *climber = target;
244         while( climber )
245         {
246             if( climber == item ) break;
247             climber = climber->parentItem;
248         }
249         if( climber ) continue;
250
251         if( item->parentItem == target &&
252             target->children.indexOf( item ) < model_pos )
253                 model_pos--;
254
255         ids.append( item->i_id );
256         model_items.append( item );
257
258         takeItem( item );
259     }
260     int count = ids.size();
261     if( count )
262     {
263         playlist_item_t *pp_items[count];
264
265         PL_LOCK;
266         for( int i = 0; i < count; i++ )
267         {
268             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, ids[i] );
269             if( !p_item )
270             {
271                 PL_UNLOCK;
272                 return;
273             }
274             pp_items[i] = p_item;
275         }
276         playlist_item_t *p_parent =
277             playlist_ItemGetById( p_playlist, target->i_id );
278         playlist_TreeMoveMany( p_playlist, count, pp_items, p_parent,
279             new_pos );
280         PL_UNLOCK;
281
282         insertChildren( target, model_items, model_pos );
283     }
284 }
285
286 /* remove item with its id */
287 void PLModel::removeItem( int i_id )
288 {
289     PLItem *item = findById( rootItem, i_id );
290     removeItem( item );
291 }
292
293 void PLModel::activateItem( const QModelIndex &index )
294 {
295     assert( index.isValid() );
296     PLItem *item = getItem( index );
297     assert( item );
298     PL_LOCK;
299     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
300     activateItem( p_item );
301     PL_UNLOCK;
302 }
303
304 /* Must be entered with lock */
305 void PLModel::activateItem( playlist_item_t *p_item )
306 {
307     if( !p_item ) return;
308     playlist_item_t *p_parent = p_item;
309     while( p_parent )
310     {
311         if( p_parent->i_id == rootItem->i_id ) break;
312         p_parent = p_parent->p_parent;
313     }
314     if( p_parent )
315         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
316                           p_parent, p_item );
317 }
318
319 /****************** Base model mandatory implementations *****************/
320 QVariant PLModel::data( const QModelIndex &index, int role ) const
321 {
322     if( !index.isValid() ) return QVariant();
323     PLItem *item = getItem( index );
324     if( role == Qt::DisplayRole )
325     {
326         int metadata = columnToMeta( index.column() );
327         if( metadata == COLUMN_END ) return QVariant();
328
329         QString returninfo;
330         if( metadata == COLUMN_NUMBER )
331             returninfo = QString::number( index.row() + 1 );
332         else
333         {
334             char *psz = psz_column_meta( item->p_input, metadata );
335             returninfo = qfu( psz );
336             free( psz );
337         }
338         return QVariant( returninfo );
339     }
340     else if( role == Qt::DecorationRole && index.column() == 0  )
341     {
342         /* Used to segfault here because i_type wasn't always initialized */
343         return QVariant( PLModel::icons[item->p_input->i_type] );
344     }
345     else if( role == Qt::FontRole )
346     {
347         if( isCurrent( index ) )
348         {
349             QFont f; f.setBold( true ); return QVariant( f );
350         }
351     }
352     else if( role == Qt::BackgroundRole && isCurrent( index ) )
353     {
354         return QVariant( QBrush( Qt::gray ) );
355     }
356     else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
357
358     return QVariant();
359 }
360
361 bool PLModel::isCurrent( const QModelIndex &index ) const
362 {
363     return getItem( index )->p_input == THEMIM->currentInputItem();
364 }
365
366 int PLModel::itemId( const QModelIndex &index ) const
367 {
368     return getItem( index )->i_id;
369 }
370
371 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
372                               int role ) const
373 {
374     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
375         return QVariant();
376
377     int meta_col = columnToMeta( section );
378
379     if( meta_col == COLUMN_END ) return QVariant();
380
381     return QVariant( qfu( psz_column_title( meta_col ) ) );
382 }
383
384 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
385                   const
386 {
387     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
388
389     PLItem *childItem = parentItem->child( row );
390     if( childItem )
391         return createIndex( row, column, childItem );
392     else
393         return QModelIndex();
394 }
395
396 QModelIndex PLModel::index( int i_id, int c )
397 {
398   return index( findById( rootItem, i_id ), c );
399 }
400
401 /* Return the index of a given item */
402 QModelIndex PLModel::index( PLItem *item, int column ) const
403 {
404     if( !item ) return QModelIndex();
405     const PLItem *parent = item->parent();
406     if( parent )
407         return createIndex( parent->children.lastIndexOf( item ),
408                             column, item );
409     return QModelIndex();
410 }
411
412 QModelIndex PLModel::currentIndex()
413 {
414     input_thread_t *p_input_thread = THEMIM->getInput();
415     if( !p_input_thread ) return QModelIndex();
416     PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
417     return index( item, 0 );
418 }
419
420 QModelIndex PLModel::parent( const QModelIndex &index ) const
421 {
422     if( !index.isValid() ) return QModelIndex();
423
424     PLItem *childItem = getItem( index );
425     if( !childItem )
426     {
427         msg_Err( p_playlist, "NULL CHILD" );
428         return QModelIndex();
429     }
430
431     PLItem *parentItem = childItem->parent();
432     if( !parentItem || parentItem == rootItem ) return QModelIndex();
433     if( !parentItem->parentItem )
434     {
435         msg_Err( p_playlist, "No parent parent, trying row 0 " );
436         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
437         return createIndex( 0, 0, parentItem );
438     }
439     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
440     return ind;
441 }
442
443 int PLModel::columnCount( const QModelIndex &i) const
444 {
445     return columnFromMeta( COLUMN_END );
446 }
447
448 int PLModel::rowCount( const QModelIndex &parent ) const
449 {
450     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
451     return parentItem->childCount();
452 }
453
454 QStringList PLModel::selectedURIs()
455 {
456     QStringList lst;
457     for( int i = 0; i < current_selection.size(); i++ )
458     {
459         PLItem *item = getItem( current_selection[i] );
460         if( item )
461         {
462             PL_LOCK;
463             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
464             if( p_item )
465             {
466                 char *psz = input_item_GetURI( p_item->p_input );
467                 if( psz )
468                 {
469                     lst.append( qfu(psz) );
470                     free( psz );
471                 }
472             }
473             PL_UNLOCK;
474         }
475     }
476     return lst;
477 }
478
479
480 /************************* Lookups *****************************/
481
482 PLItem *PLModel::findById( PLItem *root, int i_id )
483 {
484     return findInner( root, i_id, false );
485 }
486
487 PLItem *PLModel::findByInput( PLItem *root, int i_id )
488 {
489     PLItem *result = findInner( root, i_id, true );
490     return result;
491 }
492
493 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
494 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
495
496 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
497 {
498     if( !root ) return NULL;
499     if( ( !b_input && i_cached_id == i_id) ||
500         ( b_input && i_cached_input_id ==i_id ) )
501     {
502         return b_input ? p_cached_item_bi : p_cached_item;
503     }
504
505     if( !b_input && root->i_id == i_id )
506     {
507         CACHE( i_id, root );
508         return root;
509     }
510     else if( b_input && root->p_input->i_id == i_id )
511     {
512         ICACHE( i_id, root );
513         return root;
514     }
515
516     QList<PLItem *>::iterator it = root->children.begin();
517     while ( it != root->children.end() )
518     {
519         if( !b_input && (*it)->i_id == i_id )
520         {
521             CACHE( i_id, (*it) );
522             return p_cached_item;
523         }
524         else if( b_input && (*it)->p_input->i_id == i_id )
525         {
526             ICACHE( i_id, (*it) );
527             return p_cached_item_bi;
528         }
529         if( (*it)->children.size() )
530         {
531             PLItem *childFound = findInner( (*it), i_id, b_input );
532             if( childFound )
533             {
534                 if( b_input )
535                     ICACHE( i_id, childFound )
536                 else
537                     CACHE( i_id, childFound )
538                 return childFound;
539             }
540         }
541         it++;
542     }
543     return NULL;
544 }
545 #undef CACHE
546 #undef ICACHE
547
548 int PLModel::columnToMeta( int _column )
549 {
550     int meta = 1;
551     int column = 0;
552
553     while( column != _column && meta != COLUMN_END )
554     {
555         meta <<= 1;
556         column++;
557     }
558
559     return meta;
560 }
561
562 int PLModel::columnFromMeta( int meta_col )
563 {
564     int meta = 1;
565     int column = 0;
566
567     while( meta != meta_col && meta != COLUMN_END )
568     {
569         meta <<= 1;
570         column++;
571     }
572
573     return column;
574 }
575
576 bool PLModel::canEdit() const
577 {
578   return (
579     rootItem != NULL &&
580     (
581       rootItem->p_input == p_playlist->p_playing->p_input ||
582       (
583         p_playlist->p_media_library &&
584         rootItem->p_input == p_playlist->p_media_library->p_input
585       )
586     )
587   );
588 }
589 /************************* Updates handling *****************************/
590
591 /**** Events processing ****/
592 void PLModel::processInputItemUpdate( input_thread_t *p_input )
593 {
594     if( !p_input ) return;
595     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
596     {
597         PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
598         if( item ) emit currentChanged( index( item, 0 ) );
599     }
600     processInputItemUpdate( input_GetItem( p_input ) );
601 }
602
603 void PLModel::processInputItemUpdate( input_item_t *p_item )
604 {
605     if( !p_item ||  p_item->i_id <= 0 ) return;
606     PLItem *item = findByInput( rootItem, p_item->i_id );
607     if( item )
608         updateTreeItem( item );
609 }
610
611 void PLModel::processItemRemoval( int i_id )
612 {
613     if( i_id <= 0 ) return;
614     removeItem( i_id );
615 }
616
617 void PLModel::processItemAppend( int i_item, int i_parent )
618 {
619     playlist_item_t *p_item = NULL;
620     PLItem *newItem = NULL;
621     input_thread_t *currentInputThread;
622     int pos;
623
624     PLItem *nodeItem = findById( rootItem, i_parent );
625     if( !nodeItem ) return;
626
627     foreach( PLItem *existing, nodeItem->children )
628       if( existing->i_id == i_item ) return;
629
630     PL_LOCK;
631     p_item = playlist_ItemGetById( p_playlist, i_item );
632     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
633     {
634         PL_UNLOCK; return;
635     }
636
637     for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
638         if( p_item->p_parent->pp_children[pos] == p_item ) break;
639
640     newItem = new PLItem( p_item, nodeItem );
641     PL_UNLOCK;
642
643     beginInsertRows( index( nodeItem, 0 ), pos, pos );
644     nodeItem->insertChild( newItem, pos );
645     endInsertRows();
646
647     if( newItem->p_input == THEMIM->currentInputItem() )
648         emit currentChanged( index( newItem, 0 ) );
649 }
650
651
652 void PLModel::rebuild()
653 {
654     rebuild( NULL );
655 }
656
657 void PLModel::rebuild( playlist_item_t *p_root )
658 {
659     playlist_item_t* p_item;
660
661     /* Invalidate cache */
662     i_cached_id = i_cached_input_id = -1;
663
664     if( rootItem ) rootItem->removeChildren();
665
666     PL_LOCK;
667     if( p_root )
668     {
669         delete rootItem;
670         rootItem = new PLItem( p_root );
671     }
672     assert( rootItem );
673     /* Recreate from root */
674     updateChildren( rootItem );
675     PL_UNLOCK;
676
677     /* And signal the view */
678     reset();
679
680     if( p_root ) emit rootChanged();
681 }
682
683 void PLModel::takeItem( PLItem *item )
684 {
685     assert( item );
686     PLItem *parent = item->parentItem;
687     assert( parent );
688     int i_index = parent->children.indexOf( item );
689
690     beginRemoveRows( index( parent, 0 ), i_index, i_index );
691     parent->takeChildAt( i_index );
692     endRemoveRows();
693 }
694
695 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
696 {
697     assert( node );
698     int count = items.size();
699     if( !count ) return;
700     beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
701     for( int i = 0; i < count; i++ )
702     {
703         node->children.insert( i_pos + i, items[i] );
704         items[i]->parentItem = node;
705     }
706     endInsertRows();
707 }
708
709 void PLModel::removeItem( PLItem *item )
710 {
711     if( !item ) return;
712
713     if( item->i_id == i_cached_id ) i_cached_id = -1;
714     i_cached_input_id = -1;
715
716     if( item->parentItem ) {
717         int i = item->parentItem->children.indexOf( item );
718         beginRemoveRows( index( item->parentItem, 0), i, i );
719         item->parentItem->children.removeAt(i);
720         delete item;
721         endRemoveRows();
722     }
723     else delete item;
724
725     if(item == rootItem)
726     {
727         rootItem = NULL;
728         rebuild( p_playlist->p_playing );
729     }
730 }
731
732 /* This function must be entered WITH the playlist lock */
733 void PLModel::updateChildren( PLItem *root )
734 {
735     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
736     updateChildren( p_node, root );
737 }
738
739 /* This function must be entered WITH the playlist lock */
740 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
741 {
742     for( int i = 0; i < p_node->i_children ; i++ )
743     {
744         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
745         PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
746         root->appendChild( newItem );
747         if( p_node->pp_children[i]->i_children != -1 )
748             updateChildren( p_node->pp_children[i], newItem );
749     }
750 }
751
752 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
753 void PLModel::updateTreeItem( PLItem *item )
754 {
755     if( !item ) return;
756     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
757 }
758
759 /************************* Actions ******************************/
760
761 /**
762  * Deletion, here we have to do a ugly slow hack as we retrieve the full
763  * list of indexes to delete at once: when we delete a node and all of
764  * its children, we need to update the list.
765  * Todo: investigate whethere we can use ranges to be sure to delete all items?
766  */
767 void PLModel::doDelete( QModelIndexList selected )
768 {
769     if( !canEdit() ) return;
770
771     while( !selected.isEmpty() )
772     {
773         QModelIndex index = selected[0];
774         selected.removeAt( 0 );
775
776         if( index.column() != 0 ) continue;
777
778         PLItem *item = getItem( index );
779         if( item->children.size() )
780             recurseDelete( item->children, &selected );
781
782         PL_LOCK;
783         playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
784         PL_UNLOCK;
785
786         removeItem( item );
787     }
788 }
789
790 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
791 {
792     for( int i = children.size() - 1; i >= 0 ; i-- )
793     {
794         PLItem *item = children[i];
795         if( item->children.size() )
796             recurseDelete( item->children, fullList );
797         fullList->removeAll( index( item, 0 ) );
798     }
799 }
800
801 /******* Volume III: Sorting and searching ********/
802 void PLModel::sort( int column, Qt::SortOrder order )
803 {
804     sort( rootItem->i_id, column, order );
805 }
806
807 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
808 {
809     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
810
811     int meta = columnToMeta( column );
812     if( meta == COLUMN_END ) return;
813
814     PLItem *item = findById( rootItem, i_root_id );
815     if( !item ) return;
816     QModelIndex qIndex = index( item, 0 );
817     int count = item->children.size();
818     if( count )
819     {
820         beginRemoveRows( qIndex, 0, count - 1 );
821         item->removeChildren();
822         endRemoveRows( );
823     }
824
825     PL_LOCK;
826     {
827         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
828                                                         i_root_id );
829         if( p_root )
830         {
831             playlist_RecursiveNodeSort( p_playlist, p_root,
832                                         i_column_sorting( meta ),
833                                         order == Qt::AscendingOrder ?
834                                             ORDER_NORMAL : ORDER_REVERSE );
835         }
836     }
837
838     i_cached_id = i_cached_input_id = -1;
839
840     if( count )
841     {
842         beginInsertRows( qIndex, 0, count - 1 );
843         updateChildren( item );
844         endInsertRows( );
845     }
846     PL_UNLOCK;
847 }
848
849 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
850 {
851     /** \todo Fire the search with a small delay ? */
852     PL_LOCK;
853     {
854         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
855                                                         itemId( idx ) );
856         assert( p_root );
857         const char *psz_name = search_text.toUtf8().data();
858         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );
859
860         if( idx.isValid() )
861         {
862             PLItem *searchRoot = getItem( idx );
863
864             beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
865             searchRoot->removeChildren();
866             endRemoveRows( );
867
868             beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
869             updateChildren( searchRoot );
870             endInsertRows();
871
872             PL_UNLOCK;
873             return;
874         }
875     }
876     PL_UNLOCK;
877     rebuild();
878 }
879
880 /*********** Popup *********/
881 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
882 {
883     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
884
885     PL_LOCK;
886     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
887     if( !p_item )
888     {
889         PL_UNLOCK;
890         return false;
891     }
892
893     i_popup_item = index.isValid() ? p_item->i_id : -1;
894     i_popup_parent = index.isValid() ?
895         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
896         ( rootItem->i_id );
897     i_popup_column = index.column();
898
899     bool tree = ( rootItem && rootItem->i_id != p_playlist->p_playing->i_id ) ||
900                 var_InheritBool( p_intf, "playlist-tree" );
901
902     PL_UNLOCK;
903
904     current_selection = list;
905
906     QMenu menu;
907     if( i_popup_item > -1 )
908     {
909         menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
910         menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
911                         qtr(I_POP_DEL), this, SLOT( popupDel() ) );
912         menu.addSeparator();
913         menu.addAction( QIcon( ":/menu/stream" ),
914                         qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
915         menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
916         menu.addSeparator();
917         menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
918         menu.addAction( QIcon( ":/type/folder-grey" ),
919                         qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
920     }
921     if( canEdit() )
922     {
923         QIcon addIcon( ":/buttons/playlist/playlist_add" );
924         menu.addSeparator();
925         if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
926         if( rootItem->i_id == THEPL->p_playing->i_id )
927         {
928             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
929             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
930             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
931         }
932         else if( THEPL->p_media_library &&
933                     rootItem->i_id == THEPL->p_media_library->i_id )
934         {
935             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
936             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
937             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
938         }
939     }
940     if( i_popup_item > -1 )
941     {
942         menu.addSeparator();
943         QMenu *sort_menu = menu.addMenu( qtr( "Sort by" ) + QString(" ") +
944             qfu( psz_column_title( columnToMeta( index.column() ) ) ) );
945         sort_menu->addAction( qtr( "Ascending" ),
946             this, SLOT( popupSortAsc() ) );
947         sort_menu->addAction( qtr( "Descending" ),
948             this, SLOT( popupSortDesc() ) );
949     }
950     if( !menu.isEmpty() )
951     {
952         menu.exec( point ); return true;
953     }
954     else return false;
955 }
956
957 void PLModel::popupDel()
958 {
959     doDelete( current_selection );
960 }
961
962 void PLModel::popupPlay()
963 {
964     PL_LOCK;
965     {
966         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
967                                                         i_popup_item );
968         activateItem( p_item );
969     }
970     PL_UNLOCK;
971 }
972
973 void PLModel::popupInfo()
974 {
975     PL_LOCK;
976     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
977                                                     i_popup_item );
978     if( p_item )
979     {
980         input_item_t* p_input = p_item->p_input;
981         vlc_gc_incref( p_input );
982         PL_UNLOCK;
983         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
984         vlc_gc_decref( p_input );
985         mid->setParent( PlaylistDialog::getInstance( p_intf ),
986                         Qt::Dialog );
987         mid->show();
988     } else
989         PL_UNLOCK;
990 }
991
992 void PLModel::popupStream()
993 {
994     QStringList mrls = selectedURIs();
995     if( !mrls.isEmpty() )
996         THEDP->streamingDialog( NULL, mrls[0], false );
997
998 }
999
1000 void PLModel::popupSave()
1001 {
1002     QStringList mrls = selectedURIs();
1003     if( !mrls.isEmpty() )
1004         THEDP->streamingDialog( NULL, mrls[0] );
1005 }
1006
1007 void PLModel::popupExplore()
1008 {
1009     PL_LOCK;
1010     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1011                                                     i_popup_item );
1012     if( p_item )
1013     {
1014        input_item_t *p_input = p_item->p_input;
1015        char *psz_meta = input_item_GetURI( p_input );
1016        PL_UNLOCK;
1017        if( psz_meta )
1018        {
1019            const char *psz_access;
1020            const char *psz_demux;
1021            char  *psz_path;
1022            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1023
1024            if( !EMPTY_STR( psz_access ) && (
1025                    !strncasecmp( psz_access, "file", 4 ) ||
1026                    !strncasecmp( psz_access, "dire", 4 ) ))
1027            {
1028                QFileInfo info( qfu( psz_path ) );
1029                QDesktopServices::openUrl(
1030                                QUrl::fromLocalFile( info.absolutePath() ) );
1031            }
1032            free( psz_meta );
1033        }
1034     }
1035     else
1036         PL_UNLOCK;
1037 }
1038
1039 void PLModel::popupAddNode()
1040 {
1041     bool ok;
1042     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1043         qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
1044         QLineEdit::Normal, QString(), &ok);
1045     if( !ok || name.isEmpty() ) return;
1046     PL_LOCK;
1047     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1048                                                     i_popup_parent );
1049     if( p_item )
1050     {
1051         playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1052     }
1053     PL_UNLOCK;
1054 }
1055
1056 void PLModel::popupSortAsc()
1057 {
1058     sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1059 }
1060
1061 void PLModel::popupSortDesc()
1062 {
1063     sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );
1064 }