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