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