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