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