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