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