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