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