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