]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt: update playlist dialog on rootChanged signal from model
[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     if( p_root ) emit rootChanged();
670 }
671
672 void PLModel::takeItem( PLItem *item )
673 {
674     assert( item );
675     PLItem *parent = item->parentItem;
676     assert( parent );
677     int i_index = parent->children.indexOf( item );
678
679     beginRemoveRows( index( parent, 0 ), i_index, i_index );
680     parent->takeChildAt( i_index );
681     endRemoveRows();
682 }
683
684 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
685 {
686     assert( node );
687     int count = items.size();
688     if( !count ) return;
689     beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
690     for( int i = 0; i < count; i++ )
691     {
692         node->children.insert( i_pos + i, items[i] );
693         items[i]->parentItem = node;
694     }
695     endInsertRows();
696 }
697
698 void PLModel::removeItem( PLItem *item )
699 {
700     if( !item ) return;
701
702     if( item->i_id == i_cached_id ) i_cached_id = -1;
703     i_cached_input_id = -1;
704
705     if( item->parentItem ) {
706         int i = item->parentItem->children.indexOf( item );
707         beginRemoveRows( index( item->parentItem, 0), i, i );
708         item->parentItem->children.removeAt(i);
709         delete item;
710         endRemoveRows();
711     }
712     else delete item;
713
714     if(item == rootItem)
715     {
716         rootItem = NULL;
717         rebuild( p_playlist->p_playing );
718     }
719 }
720
721 /* This function must be entered WITH the playlist lock */
722 void PLModel::updateChildren( PLItem *root )
723 {
724     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
725     updateChildren( p_node, root );
726 }
727
728 /* This function must be entered WITH the playlist lock */
729 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
730 {
731     for( int i = 0; i < p_node->i_children ; i++ )
732     {
733         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
734         PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
735         root->appendChild( newItem );
736         if( p_node->pp_children[i]->i_children != -1 )
737             updateChildren( p_node->pp_children[i], newItem );
738     }
739 }
740
741 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
742 void PLModel::updateTreeItem( PLItem *item )
743 {
744     if( !item ) return;
745     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
746 }
747
748 /************************* Actions ******************************/
749
750 /**
751  * Deletion, here we have to do a ugly slow hack as we retrieve the full
752  * list of indexes to delete at once: when we delete a node and all of
753  * its children, we need to update the list.
754  * Todo: investigate whethere we can use ranges to be sure to delete all items?
755  */
756 void PLModel::doDelete( QModelIndexList selected )
757 {
758     if( !canEdit() ) return;
759
760     for( int i = selected.size() -1 ; i >= 0; i-- )
761     {
762         QModelIndex index = selected[i];
763         if( index.column() != 0 ) continue;
764         PLItem *item = getItem( index );
765         if( item )
766         {
767             if( item->children.size() )
768                 recurseDelete( item->children, &selected );
769             doDeleteItem( item, &selected );
770         }
771         if( i > selected.size() ) i = selected.size();
772     }
773 }
774
775 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
776 {
777     for( int i = children.size() - 1; i >= 0 ; i-- )
778     {
779         PLItem *item = children[i];
780         if( item->children.size() )
781             recurseDelete( item->children, fullList );
782         doDeleteItem( item, fullList );
783     }
784 }
785
786 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
787 {
788     QModelIndex deleteIndex = index( item, 0 );
789     fullList->removeAll( deleteIndex );
790
791     PL_LOCK;
792     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
793     if( !p_item )
794     {
795         PL_UNLOCK;
796         return;
797     }
798     playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
799     PL_UNLOCK;
800
801     /* And finally, remove it from the tree */
802     removeItem( item );
803 }
804
805 /******* Volume III: Sorting and searching ********/
806 void PLModel::sort( int column, Qt::SortOrder order )
807 {
808     sort( rootItem->i_id, column, order );
809 }
810
811 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
812 {
813     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
814
815     int meta = columnToMeta( column );
816     if( meta == COLUMN_END ) return;
817
818     PLItem *item = findById( rootItem, i_root_id );
819     if( !item ) return;
820     QModelIndex qIndex = index( item, 0 );
821     int count = item->children.size();
822     if( count )
823     {
824         beginRemoveRows( qIndex, 0, count - 1 );
825         item->removeChildren();
826         endRemoveRows( );
827     }
828
829     PL_LOCK;
830     {
831         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
832                                                         i_root_id );
833         if( p_root )
834         {
835             playlist_RecursiveNodeSort( p_playlist, p_root,
836                                         i_column_sorting( meta ),
837                                         order == Qt::AscendingOrder ?
838                                             ORDER_NORMAL : ORDER_REVERSE );
839         }
840     }
841
842     i_cached_id = i_cached_input_id = -1;
843
844     if( count )
845     {
846         beginInsertRows( qIndex, 0, count - 1 );
847         updateChildren( item );
848         endInsertRows( );
849     }
850     PL_UNLOCK;
851 }
852
853 void PLModel::search( const QString& search_text )
854 {
855     /** \todo Fire the search with a small delay ? */
856     PL_LOCK;
857     {
858         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
859                                                         rootItem->i_id );
860         assert( p_root );
861         const char *psz_name = search_text.toUtf8().data();
862         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
863     }
864     PL_UNLOCK;
865     rebuild();
866 }
867
868 /*********** Popup *********/
869 void PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
870 {
871     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
872
873     PL_LOCK;
874     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
875     if( !p_item )
876     {
877         PL_UNLOCK; return;
878     }
879     i_popup_item = index.isValid() ? p_item->i_id : -1;
880     i_popup_parent = index.isValid() ?
881         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
882         ( p_item->i_id );
883     i_popup_column = index.column();
884
885     bool tree = var_InheritBool( p_intf, "playlist-tree" );
886
887     PL_UNLOCK;
888
889     current_selection = list;
890
891     QMenu menu;
892     if( i_popup_item > -1 )
893     {
894         menu.addAction( qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
895         menu.addAction( qtr(I_POP_DEL), this, SLOT( popupDel() ) );
896         menu.addSeparator();
897         menu.addAction( qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
898         menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
899         menu.addSeparator();
900         menu.addAction( qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
901         menu.addSeparator();
902         QMenu *sort_menu = menu.addMenu( qtr( "Sort by ") +
903             qfu( psz_column_title( columnToMeta( index.column() ) ) ) );
904         sort_menu->addAction( qtr( "Ascending" ),
905             this, SLOT( popupSortAsc() ) );
906         sort_menu->addAction( qtr( "Descending" ),
907             this, SLOT( popupSortDesc() ) );
908     }
909     if( tree && canEdit() )
910         menu.addAction( qtr(I_POP_ADD), this, SLOT( popupAddNode() ) );
911     if( i_popup_item > -1 )
912     {
913         menu.addSeparator();
914         menu.addAction( qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
915     }
916     if( !menu.isEmpty() ) menu.exec( point );
917 }
918
919 void PLModel::popupDel()
920 {
921     doDelete( current_selection );
922 }
923
924 void PLModel::popupPlay()
925 {
926     PL_LOCK;
927     {
928         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
929                                                         i_popup_item );
930         activateItem( p_item );
931     }
932     PL_UNLOCK;
933 }
934
935 void PLModel::popupInfo()
936 {
937     PL_LOCK;
938     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
939                                                     i_popup_item );
940     if( p_item )
941     {
942         input_item_t* p_input = p_item->p_input;
943         vlc_gc_incref( p_input );
944         PL_UNLOCK;
945         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
946         vlc_gc_decref( p_input );
947         mid->setParent( PlaylistDialog::getInstance( p_intf ),
948                         Qt::Dialog );
949         mid->show();
950     } else
951         PL_UNLOCK;
952 }
953
954 void PLModel::popupStream()
955 {
956     QStringList mrls = selectedURIs();
957     if( !mrls.isEmpty() )
958         THEDP->streamingDialog( NULL, mrls[0], false );
959
960 }
961
962 void PLModel::popupSave()
963 {
964     QStringList mrls = selectedURIs();
965     if( !mrls.isEmpty() )
966         THEDP->streamingDialog( NULL, mrls[0] );
967 }
968
969 #include <QUrl>
970 #include <QFileInfo>
971 #include <QDesktopServices>
972 void PLModel::popupExplore()
973 {
974     PL_LOCK;
975     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
976                                                     i_popup_item );
977     if( p_item )
978     {
979        input_item_t *p_input = p_item->p_input;
980        char *psz_meta = input_item_GetURI( p_input );
981        PL_UNLOCK;
982        if( psz_meta )
983        {
984            const char *psz_access;
985            const char *psz_demux;
986            char  *psz_path;
987            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
988
989            if( EMPTY_STR( psz_access ) ||
990                !strncasecmp( psz_access, "file", 4 ) ||
991                !strncasecmp( psz_access, "dire", 4 ) )
992            {
993                QFileInfo info( qfu( psz_meta ) );
994                QDesktopServices::openUrl(
995                                QUrl::fromLocalFile( info.absolutePath() ) );
996            }
997            free( psz_meta );
998        }
999     }
1000     else
1001         PL_UNLOCK;
1002 }
1003
1004 #include <QInputDialog>
1005 void PLModel::popupAddNode()
1006 {
1007     bool ok;
1008     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1009         qtr( I_POP_ADD ), qtr( "Enter name for new node:" ),
1010         QLineEdit::Normal, QString(), &ok);
1011     if( !ok || name.isEmpty() ) return;
1012     PL_LOCK;
1013     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1014                                                     i_popup_parent );
1015     if( p_item )
1016     {
1017         playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1018     }
1019     PL_UNLOCK;
1020 }
1021
1022 void PLModel::popupSortAsc()
1023 {
1024     sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1025 }
1026
1027 void PLModel::popupSortDesc()
1028 {
1029     sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );
1030 }