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