]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt: reindent
[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 | Qt::MoveAction;
112 }
113
114 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
115 {
116     Qt::ItemFlags flags = QAbstractItemModel::flags( index );
117
118     const 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-input-items";
141     return types;
142 }
143
144 bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
145 {
146     if( !i1.isValid() || !i2.isValid() ) return false;
147     PLItem *item1 = static_cast<PLItem*>( i1.internalPointer() );
148     PLItem *item2 = static_cast<PLItem*>( i2.internalPointer() );
149     if( item1->parent() == item2->parent() ) return i1.row() < i2.row();
150     else return *item1 < *item2;
151 }
152
153 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
154 {
155     PlMimeData *plMimeData = new PlMimeData();
156     QModelIndexList list;
157
158     foreach( const QModelIndex &index, indexes ) {
159         if( index.isValid() && index.column() == 0 )
160             list.append(index);
161     }
162
163     qSort(list.begin(), list.end(), modelIndexLessThen);
164
165     PLItem *item = NULL;
166     foreach( const QModelIndex &index, list ) {
167         if( item )
168         {
169             PLItem *testee = getItem( index );
170             while( testee->parent() )
171             {
172                 if( testee->parent() == item ||
173                     testee->parent() == item->parent() ) break;
174                 testee = testee->parent();
175             }
176             if( testee->parent() == item ) continue;
177             item = getItem( index );
178         }
179         else
180             item = getItem( index );
181
182         plMimeData->appendItem( item->p_input );
183     }
184
185     return plMimeData;
186 }
187
188 /* Drop operation */
189 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
190                            int row, int column, const QModelIndex &parent )
191 {
192     bool copy = action == Qt::CopyAction;
193     if( !copy && action != Qt::MoveAction )
194         return true;
195
196     const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
197     if( plMimeData )
198     {
199         if( copy )
200             dropAppendCopy( plMimeData, getItem( parent ), row );
201         else
202             dropMove( plMimeData, getItem( parent ), row );
203     }
204     return true;
205 }
206
207 void PLModel::dropAppendCopy( const PlMimeData *plMimeData, PLItem *target, int pos )
208 {
209     PL_LOCK;
210
211     playlist_item_t *p_parent =
212             playlist_ItemGetByInput( p_playlist, target->p_input );
213     if( !p_parent ) return;
214
215     if( pos == -1 ) pos = PLAYLIST_END;
216
217     QList<input_item_t*> inputItems = plMimeData->inputItems();
218
219     foreach( input_item_t* p_input, inputItems )
220     {
221         playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
222         if( !p_item ) continue;
223         pos = playlist_NodeAddCopy( p_playlist, p_item, p_parent, pos );
224     }
225
226     PL_UNLOCK;
227 }
228
229 void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
230 {
231     QList<input_item_t*> inputItems = plMimeData->inputItems();
232     QList<PLItem*> model_items;
233     playlist_item_t *pp_items[inputItems.size()];
234
235     PL_LOCK;
236
237     playlist_item_t *p_parent =
238         playlist_ItemGetByInput( p_playlist, target->p_input );
239
240     if( !p_parent || row > p_parent->i_children )
241     {
242         PL_UNLOCK; return;
243     }
244
245     int new_pos = row == -1 ? p_parent->i_children : row;
246     int model_pos = new_pos;
247     int i = 0;
248
249     foreach( input_item_t *p_input, inputItems )
250     {
251         playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
252         if( !p_item ) continue;
253
254         PLItem *item = findByInput( rootItem, p_input->i_id );
255         if( !item ) continue;
256
257         /* Better not try to move a node into itself.
258            Abort the whole operation in that case,
259            because it is ambiguous. */
260         PLItem *climber = target;
261         while( climber )
262         {
263             if( climber == item )
264             {
265                 PL_UNLOCK; return;
266             }
267             climber = climber->parentItem;
268         }
269
270         if( item->parentItem == target &&
271             target->children.indexOf( item ) < new_pos )
272                 model_pos--;
273
274         model_items.append( item );
275         pp_items[i] = p_item;
276         i++;
277     }
278
279     if( model_items.isEmpty() )
280     {
281         PL_UNLOCK; return;
282     }
283
284     playlist_TreeMoveMany( p_playlist, i, pp_items, p_parent, new_pos );
285
286     PL_UNLOCK;
287
288     foreach( PLItem *item, model_items )
289         takeItem( item );
290
291     insertChildren( target, model_items, model_pos );
292 }
293
294 /* remove item with its id */
295 void PLModel::removeItem( int i_id )
296 {
297     PLItem *item = findById( rootItem, i_id );
298     removeItem( item );
299 }
300
301 void PLModel::activateItem( const QModelIndex &index )
302 {
303     assert( index.isValid() );
304     const PLItem *item = getItem( index );
305     assert( item );
306     PL_LOCK;
307     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
308     activateItem( p_item );
309     PL_UNLOCK;
310 }
311
312 /* Must be entered with lock */
313 void PLModel::activateItem( playlist_item_t *p_item )
314 {
315     if( !p_item ) return;
316     playlist_item_t *p_parent = p_item;
317     while( p_parent )
318     {
319         if( p_parent->i_id == rootItem->i_id ) break;
320         p_parent = p_parent->p_parent;
321     }
322     if( p_parent )
323         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
324                           p_parent, p_item );
325 }
326
327 /****************** Base model mandatory implementations *****************/
328 QVariant PLModel::data( const QModelIndex &index, const int role ) const
329 {
330     if( !index.isValid() ) return QVariant();
331     const PLItem *item = getItem( index );
332     if( role == Qt::DisplayRole )
333     {
334         int metadata = columnToMeta( index.column() );
335         if( metadata == COLUMN_END ) return QVariant();
336
337         QString returninfo;
338         if( metadata == COLUMN_NUMBER )
339             returninfo = QString::number( index.row() + 1 );
340         else
341         {
342             char *psz = psz_column_meta( item->p_input, metadata );
343             returninfo = qfu( psz );
344             free( psz );
345         }
346         return QVariant( returninfo );
347     }
348     else if( role == Qt::DecorationRole && index.column() == 0  )
349     {
350         /* Used to segfault here because i_type wasn't always initialized */
351         return QVariant( PLModel::icons[item->p_input->i_type] );
352     }
353     else if( role == Qt::FontRole )
354     {
355         QFont f;
356         f.setPointSize( f.pointSize() - 1 );
357         if( isCurrent( index ) )
358             f.setBold( true );
359         return QVariant( f );
360     }
361     else if( role == Qt::BackgroundRole && isCurrent( index ) )
362     {
363         return QVariant( QBrush( Qt::gray ) );
364     }
365     else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
366     else if( role == IsLeafNodeRole )
367     {
368         QVariant isLeaf;
369         PL_LOCK;
370         playlist_item_t *plItem =
371             playlist_ItemGetById( p_playlist, item->i_id );
372
373         if( plItem )
374             isLeaf = plItem->i_children == -1;
375
376         PL_UNLOCK;
377         return isLeaf;
378     }
379     else if( role == IsCurrentsParentNodeRole )
380     {
381         return QVariant( isParent( index, currentIndex() ) );
382     }
383     return QVariant();
384 }
385
386 /* Seek from current index toward the top and see if index is one of parent nodes */
387 bool PLModel::isParent( const QModelIndex &index, const QModelIndex &current ) const
388 {
389     if( !index.isValid() )
390         return false;
391
392     if( index == current )
393         return true;
394
395     if( !current.isValid() || !current.parent().isValid() )
396         return false;
397
398     return isParent( index, current.parent() );
399 }
400
401 bool PLModel::isCurrent( const QModelIndex &index ) const
402 {
403     return getItem( index )->p_input == THEMIM->currentInputItem();
404 }
405
406 int PLModel::itemId( const QModelIndex &index ) const
407 {
408     return getItem( index )->i_id;
409 }
410
411 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
412                               int role ) const
413 {
414     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
415         return QVariant();
416
417     int meta_col = columnToMeta( section );
418
419     if( meta_col == COLUMN_END ) return QVariant();
420
421     return QVariant( qfu( psz_column_title( meta_col ) ) );
422 }
423
424 QModelIndex PLModel::index( const int row, const int column, const QModelIndex &parent )
425                   const
426 {
427     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
428
429     PLItem *childItem = parentItem->child( row );
430     if( childItem )
431         return createIndex( row, column, childItem );
432     else
433         return QModelIndex();
434 }
435
436 QModelIndex PLModel::index( const int i_id, const int c )
437 {
438   return index( findById( rootItem, i_id ), c );
439 }
440
441 /* Return the index of a given item */
442 QModelIndex PLModel::index( PLItem *item, int column ) const
443 {
444     if( !item ) return QModelIndex();
445     const PLItem *parent = item->parent();
446     if( parent )
447         return createIndex( parent->children.lastIndexOf( item ),
448                             column, item );
449     return QModelIndex();
450 }
451
452 QModelIndex PLModel::currentIndex() const
453 {
454     input_thread_t *p_input_thread = THEMIM->getInput();
455     if( !p_input_thread ) return QModelIndex();
456     PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
457     return index( item, 0 );
458 }
459
460 QModelIndex PLModel::parent( const QModelIndex &index ) const
461 {
462     if( !index.isValid() ) return QModelIndex();
463
464     PLItem *childItem = getItem( index );
465     if( !childItem )
466     {
467         msg_Err( p_playlist, "NULL CHILD" );
468         return QModelIndex();
469     }
470
471     PLItem *parentItem = childItem->parent();
472     if( !parentItem || parentItem == rootItem ) return QModelIndex();
473     if( !parentItem->parentItem )
474     {
475         msg_Err( p_playlist, "No parent parent, trying row 0 " );
476         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
477         return createIndex( 0, 0, parentItem );
478     }
479     return createIndex(parentItem->row(), 0, parentItem);
480 }
481
482 int PLModel::columnCount( const QModelIndex &i) const
483 {
484     return columnFromMeta( COLUMN_END );
485 }
486
487 int PLModel::rowCount( const QModelIndex &parent ) const
488 {
489     const PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
490     return parentItem->childCount();
491 }
492
493 QStringList PLModel::selectedURIs()
494 {
495     QStringList lst;
496     for( int i = 0; i < current_selection.size(); i++ )
497     {
498         const PLItem *item = getItem( current_selection[i] );
499         if( item )
500         {
501             PL_LOCK;
502             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
503             if( p_item )
504             {
505                 char *psz = input_item_GetURI( p_item->p_input );
506                 if( psz )
507                 {
508                     lst.append( qfu(psz) );
509                     free( psz );
510                 }
511             }
512             PL_UNLOCK;
513         }
514     }
515     return lst;
516 }
517
518
519 /************************* Lookups *****************************/
520
521 PLItem *PLModel::findById( PLItem *root, int i_id ) const
522 {
523     return findInner( root, i_id, false );
524 }
525
526 PLItem *PLModel::findByInput( PLItem *root, int i_id ) const
527 {
528     PLItem *result = findInner( root, i_id, true );
529     return result;
530 }
531
532 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input ) const
533 {
534     if( !root ) return NULL;
535
536     if( !b_input && root->i_id == i_id )
537         return root;
538
539     else if( b_input && root->p_input->i_id == i_id )
540         return root;
541
542     QList<PLItem *>::iterator it = root->children.begin();
543     while ( it != root->children.end() )
544     {
545         if( !b_input && (*it)->i_id == i_id )
546              return (*it);
547
548         else if( b_input && (*it)->p_input->i_id == i_id )
549              return (*it);
550
551         if( (*it)->children.size() )
552         {
553             PLItem *childFound = findInner( (*it), i_id, b_input );
554             if( childFound )
555                return childFound;
556         }
557         it++;
558     }
559     return NULL;
560 }
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( const 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     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, don't delete items childrens if item is going to be
777  * delete allready, so we remove childrens from selection-list.
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( const int column, Qt::SortOrder order )
815 {
816     sort( rootItem->i_id, column, order );
817 }
818
819 void PLModel::sort( const int i_root_id, const 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     /* if we have popup item, try to make sure that you keep that item visible */
860     if( i_popup_item > -1 )
861     {
862         PLItem *popupitem = findById( rootItem, i_popup_item );
863         if( popupitem ) emit currentChanged( index( popupitem, 0 ) );
864         /* reset i_popup_item as we don't show it as selected anymore anyway */
865         i_popup_item = -1;
866     }
867     else if( currentIndex().isValid() ) emit currentChanged( currentIndex() );
868 }
869
870 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
871 {
872     /** \todo Fire the search with a small delay ? */
873     PL_LOCK;
874     {
875         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
876                                                         itemId( idx ) );
877         assert( p_root );
878         const char *psz_name = qtu( search_text );
879         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );
880
881         if( idx.isValid() )
882         {
883             PLItem *searchRoot = getItem( idx );
884
885             beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
886             searchRoot->removeChildren();
887             endRemoveRows( );
888
889             beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
890             updateChildren( searchRoot );
891             endInsertRows();
892
893             PL_UNLOCK;
894             return;
895         }
896     }
897     PL_UNLOCK;
898     rebuild();
899 }
900
901 /*********** Popup *********/
902 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
903 {
904     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
905
906     PL_LOCK;
907     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
908     if( !p_item )
909     {
910         PL_UNLOCK;
911         return false;
912     }
913
914     input_item_t *p_input = p_item->p_input;
915     vlc_gc_incref( p_input );
916
917     i_popup_item = index.isValid() ? p_item->i_id : -1;
918     i_popup_parent = index.isValid() ?
919         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
920         ( rootItem->i_id );
921     i_popup_column = index.column();
922
923     bool tree = ( rootItem && rootItem->i_id != p_playlist->p_playing->i_id ) ||
924                 var_InheritBool( p_intf, "playlist-tree" );
925
926     PL_UNLOCK;
927
928     current_selection = list;
929
930     QMenu menu;
931     if( i_popup_item > -1 )
932     {
933         menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
934         menu.addAction( QIcon( ":/menu/stream" ),
935                         qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
936         menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
937         menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
938         if( !strncasecmp( p_input->psz_uri, "file://", 7 ) )
939             menu.addAction( QIcon( ":/type/folder-grey" ),
940                             qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
941         menu.addSeparator();
942     }
943     if( canEdit() )
944     {
945         QIcon addIcon( ":/buttons/playlist/playlist_add" );
946         menu.addSeparator();
947         if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
948         if( rootItem->i_id == THEPL->p_playing->i_id )
949         {
950             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
951             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
952             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
953         }
954         else if( THEPL->p_media_library &&
955                     rootItem->i_id == THEPL->p_media_library->i_id )
956         {
957             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
958             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
959             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
960         }
961     }
962     if( i_popup_item > -1 )
963     {
964         menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
965                         qtr(I_POP_DEL), this, SLOT( popupDel() ) );
966         menu.addSeparator();
967         if( !sortingMenu )
968         {
969             sortingMenu = new QMenu( qtr( "Sort by" ) );
970             sortingMapper = new QSignalMapper( this );
971             int i, j;
972             for( i = 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
973             {
974                 if( i == COLUMN_NUMBER ) continue;
975                 QMenu *m = sortingMenu->addMenu( qfu( psz_column_title( i ) ) );
976                 QAction *asc = m->addAction( qtr("Ascending") );
977                 QAction *desc = m->addAction( qtr("Descending") );
978                 sortingMapper->setMapping( asc, j );
979                 sortingMapper->setMapping( desc, -j );
980                 CONNECT( asc, triggered(), sortingMapper, map() );
981                 CONNECT( desc, triggered(), sortingMapper, map() );
982             }
983             CONNECT( sortingMapper, mapped( int ), this, popupSort( int ) );
984         }
985         menu.addMenu( sortingMenu );
986     }
987     vlc_gc_decref( p_input );
988
989     if( !menu.isEmpty() )
990     {
991         menu.exec( point ); return true;
992     }
993     else return false;
994 }
995
996 void PLModel::popupDel()
997 {
998     doDelete( current_selection );
999 }
1000
1001 void PLModel::popupPlay()
1002 {
1003     PL_LOCK;
1004     {
1005         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1006                                                         i_popup_item );
1007         activateItem( p_item );
1008     }
1009     PL_UNLOCK;
1010 }
1011
1012 void PLModel::popupInfo()
1013 {
1014     PL_LOCK;
1015     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1016                                                     i_popup_item );
1017     if( p_item )
1018     {
1019         input_item_t* p_input = p_item->p_input;
1020         vlc_gc_incref( p_input );
1021         PL_UNLOCK;
1022         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
1023         vlc_gc_decref( p_input );
1024         mid->setParent( PlaylistDialog::getInstance( p_intf ),
1025                         Qt::Dialog );
1026         mid->show();
1027     } else
1028         PL_UNLOCK;
1029 }
1030
1031 void PLModel::popupStream()
1032 {
1033     QStringList mrls = selectedURIs();
1034     if( !mrls.isEmpty() )
1035         THEDP->streamingDialog( NULL, mrls[0], false );
1036
1037 }
1038
1039 void PLModel::popupSave()
1040 {
1041     QStringList mrls = selectedURIs();
1042     if( !mrls.isEmpty() )
1043         THEDP->streamingDialog( NULL, mrls[0] );
1044 }
1045
1046 void PLModel::popupExplore()
1047 {
1048     PL_LOCK;
1049     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1050             i_popup_item );
1051     if( p_item )
1052     {
1053         input_item_t *p_input = p_item->p_input;
1054         char *psz_meta = input_item_GetURI( p_input );
1055         PL_UNLOCK;
1056         if( psz_meta )
1057         {
1058             const char *psz_access;
1059             const char *psz_demux;
1060             char  *psz_path;
1061             input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1062
1063             if( !EMPTY_STR( psz_access ) && (
1064                    !strncasecmp( psz_access, "file", 4 ) ||
1065                    !strncasecmp( psz_access, "dire", 4 ) ))
1066             {
1067 #ifdef WIN32
1068                 /* Qt openURL doesn't know to open files that starts with a / or \ */
1069                 if( psz_path[0] == '/' || psz_path[0] == '\\'  )
1070                     psz_path++;
1071 #endif
1072
1073                 QFileInfo info( qfu( decode_URI( psz_path ) ) );
1074                 QDesktopServices::openUrl(
1075                         QUrl::fromLocalFile( info.absolutePath() ) );
1076             }
1077             free( psz_meta );
1078         }
1079     }
1080     else
1081         PL_UNLOCK;
1082 }
1083
1084 void PLModel::popupAddNode()
1085 {
1086     bool ok;
1087     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1088         qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
1089         QLineEdit::Normal, QString(), &ok);
1090     if( !ok || name.isEmpty() ) return;
1091
1092     PL_LOCK;
1093     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1094                                                     i_popup_parent );
1095     if( p_item )
1096         playlist_NodeCreate( p_playlist, qtu( name ), p_item, PLAYLIST_END, 0, NULL );
1097     PL_UNLOCK;
1098 }
1099
1100 void PLModel::popupSort( int column )
1101 {
1102     sort( i_popup_parent,
1103           column > 0 ? column - 1 : -column - 1,
1104           column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
1105 }
1106
1107 /******************* Drag and Drop helper class ******************/
1108
1109 PlMimeData::PlMimeData( )
1110 { }
1111
1112 PlMimeData::~PlMimeData()
1113 {
1114     foreach( input_item_t *p_item, _inputItems )
1115         vlc_gc_decref( p_item );
1116 }
1117
1118 void PlMimeData::appendItem( input_item_t *p_item )
1119 {
1120     vlc_gc_incref( p_item );
1121     _inputItems.append( p_item );
1122 }
1123
1124 QList<input_item_t*> PlMimeData::inputItems() const
1125 {
1126     return _inputItems;
1127 }
1128
1129 QStringList PlMimeData::formats () const
1130 {
1131     QStringList fmts;
1132     fmts << "vlc/qt-input-items";
1133     return fmts;
1134 }