]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
3dfaf18b08cec96c5cbec7764895a24ed9ad3958
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "qt4.hpp"
29 #include "dialogs_provider.hpp"
30 #include "components/playlist/playlist_model.hpp"
31 #include "dialogs/mediainfo.hpp"
32 #include "dialogs/playlist.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "pixmaps/types/type_unknown.xpm"
36
37 #include <assert.h>
38 #include <QIcon>
39 #include <QFont>
40 #include <QMenu>
41 #include <QApplication>
42 #include <QSettings>
43
44 #include "sorting.h"
45
46 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
47
48 static int PlaylistChanged( vlc_object_t *, const char *,
49                             vlc_value_t, vlc_value_t, void * );
50 static int PlaylistNext( vlc_object_t *, const char *,
51                          vlc_value_t, vlc_value_t, void * );
52 static int ItemChanged( vlc_object_t *, const char *,
53                         vlc_value_t, vlc_value_t, void * );
54 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
55                          vlc_value_t oval, vlc_value_t nval, void *param );
56 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
57                         vlc_value_t oval, vlc_value_t nval, void *param );
58
59 /*************************************************************************
60  * Playlist model implementation
61  *************************************************************************/
62
63 /*
64   This model is called two times, for the selector and the standard panel
65 */
66 PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
67                   intf_thread_t *_p_intf,   /* main Qt p_intf */
68                   playlist_item_t * p_root,
69                   /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
70                     and THEPL->p_root_category for SelectPL */
71                   int _i_depth,             /* -1 for StandPL, 1 for SelectPL */
72                   QObject *parent )         /* Basic Qt parent */
73                   : QAbstractItemModel( parent )
74 {
75     i_depth = _i_depth;
76     assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
77     p_intf            = _p_intf;
78     p_playlist        = _p_playlist;
79     i_cached_id       = -1;
80     i_cached_input_id = -1;
81     i_popup_item      = i_popup_parent = -1;
82
83     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
84
85     /* Icons initialization */
86 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
87     ADD_ICON( UNKNOWN , type_unknown_xpm );
88     ADD_ICON( FILE, ":/type_file" );
89     ADD_ICON( DIRECTORY, ":/type_directory" );
90     ADD_ICON( DISC, ":/disc" );
91     ADD_ICON( CDDA, ":/cdda" );
92     ADD_ICON( CARD, ":/capture-card" );
93     ADD_ICON( NET, ":/type_net" );
94     ADD_ICON( PLAYLIST, ":/type_playlist" );
95     ADD_ICON( NODE, ":/type_node" );
96 #undef ADD_ICON
97
98     rebuild( p_root );
99     CONNECT( THEMIM->getIM(), metaChanged( int ),
100             this, ProcessInputItemUpdate( int ) );
101     CONNECT( THEMIM, inputChanged( input_thread_t * ),
102             this, ProcessInputItemUpdate( input_thread_t* ) );
103 }
104
105 PLModel::~PLModel()
106 {
107     getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
108     delCallbacks();
109     delete rootItem;
110 }
111
112 Qt::DropActions PLModel::supportedDropActions() const
113 {
114     return Qt::CopyAction; /* Why not Qt::MoveAction */
115 }
116
117 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
118 {
119     Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
120     if( index.isValid() )
121         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
122     else
123         return Qt::ItemIsDropEnabled | defaultFlags;
124 }
125
126 /* A list of model indexes are a playlist */
127 QStringList PLModel::mimeTypes() const
128 {
129     QStringList types;
130     types << "vlc/playlist-item-id";
131     return types;
132 }
133
134 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
135 {
136     QMimeData *mimeData = new QMimeData();
137     QByteArray encodedData;
138     QDataStream stream( &encodedData, QIODevice::WriteOnly );
139
140     foreach( const QModelIndex &index, indexes ) {
141         if( index.isValid() && index.column() == 0 )
142             stream << itemId( index );
143     }
144     mimeData->setData( "vlc/playlist-item-id", encodedData );
145     return mimeData;
146 }
147
148 /* Drop operation */
149 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
150                            int row, int column, const QModelIndex &target )
151 {
152     if( data->hasFormat( "vlc/playlist-item-id" ) )
153     {
154         if( action == Qt::IgnoreAction )
155             return true;
156
157         if( !target.isValid() )
158             /* We don't want to move on an invalid position */
159             return true;
160
161         PLItem *targetItem = static_cast<PLItem*>( target.internalPointer() );
162
163         QByteArray encodedData = data->data( "vlc/playlist-item-id" );
164         QDataStream stream( &encodedData, QIODevice::ReadOnly );
165
166         PLItem *newParentItem;
167         while( !stream.atEnd() )
168         {
169             int i;
170             int srcId;
171             stream >> srcId;
172
173             PL_LOCK;
174             playlist_item_t *p_target =
175                         playlist_ItemGetById( p_playlist, targetItem->i_id );
176             playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId );
177
178             if( !p_target || !p_src )
179             {
180                 PL_UNLOCK;
181                 return false;
182             }
183             if( p_target->i_children == -1 ) /* A leaf */
184             {
185                 PLItem *parentItem = targetItem->parent();
186                 assert( parentItem );
187                 playlist_item_t *p_parent =
188                          playlist_ItemGetById( p_playlist, parentItem->i_id );
189                 if( !p_parent )
190                 {
191                     PL_UNLOCK;
192                     return false;
193                 }
194                 for( i = 0 ; i< p_parent->i_children ; i++ )
195                     if( p_parent->pp_children[i] == p_target ) break;
196                 // Move the item to the element after i
197                 playlist_TreeMove( p_playlist, p_src, p_parent, i + 1 );
198                 newParentItem = parentItem;
199             }
200             else
201             {
202                 /* \todo: if we drop on a top-level node, use copy instead ? */
203                 playlist_TreeMove( p_playlist, p_src, p_target, 0 );
204                 i = 0;
205                 newParentItem = targetItem;
206             }
207             PL_UNLOCK;
208         }
209         /*TODO: That's not a good idea to rebuild the playlist */
210         rebuild();
211     }
212     return true;
213 }
214
215 /* remove item with its id */
216 void PLModel::removeItem( int i_id )
217 {
218     PLItem *item = FindById( rootItem, i_id );
219     if( item ) item->remove( item );
220 }
221
222 /* callbacks and slots */
223 void PLModel::addCallbacks()
224 {
225     /* Some global changes happened -> Rebuild all */
226     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
227     /* We went to the next item 
228     var_AddCallback( p_playlist, "item-current", PlaylistNext, this );
229     */
230     /* One item has been updated */
231     var_AddCallback( p_playlist, "playlist-item-append", ItemAppended, this );
232     var_AddCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
233 }
234
235 void PLModel::delCallbacks()
236 {
237     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
238     /*
239     var_DelCallback( p_playlist, "item-current", PlaylistNext, this );
240     */
241     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
242     var_DelCallback( p_playlist, "playlist-item-append", ItemAppended, this );
243     var_DelCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
244 }
245
246 void PLModel::activateItem( const QModelIndex &index )
247 {
248     assert( index.isValid() );
249     PLItem *item = static_cast<PLItem*>(index.internalPointer());
250     assert( item );
251     PL_LOCK;
252     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
253     activateItem( p_item );
254     PL_UNLOCK;
255 }
256
257 /* Must be entered with lock */
258 void PLModel::activateItem( playlist_item_t *p_item )
259 {
260     if( !p_item ) return;
261     playlist_item_t *p_parent = p_item;
262     while( p_parent )
263     {
264         if( p_parent->i_id == rootItem->i_id ) break;
265         p_parent = p_parent->p_parent;
266     }
267     if( p_parent )
268         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
269                           p_parent, p_item );
270 }
271
272 /****************** Base model mandatory implementations *****************/
273 QVariant PLModel::data( const QModelIndex &index, int role ) const
274 {
275     if( !index.isValid() ) return QVariant();
276     PLItem *item = static_cast<PLItem*>(index.internalPointer());
277     if( role == Qt::DisplayRole )
278     {
279         return QVariant( item->columnString( index.column() ) );
280     }
281     else if( role == Qt::DecorationRole && index.column() == 0  )
282     {
283         /* Use to segfault here because i_type wasn't always initialized */
284         if( item->i_type >= 0 )
285             return QVariant( PLModel::icons[item->i_type] );
286     }
287     else if( role == Qt::FontRole )
288     {
289         if( item->b_current == true )
290         {
291             QFont f; f.setBold( true ); return QVariant( f );
292         }
293     }
294     return QVariant();
295 }
296
297 bool PLModel::isCurrent( const QModelIndex &index )
298 {
299     assert( index.isValid() );
300     return static_cast<PLItem*>(index.internalPointer())->b_current;
301 }
302
303 int PLModel::itemId( const QModelIndex &index ) const
304 {
305     assert( index.isValid() );
306     return static_cast<PLItem*>(index.internalPointer())->i_id;
307 }
308
309 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
310                               int role ) const
311 {
312     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
313             return QVariant( rootItem->columnString( section ) );
314     return QVariant();
315 }
316
317 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
318                   const
319 {
320     PLItem *parentItem;
321     if( !parent.isValid() )
322         parentItem = rootItem;
323     else
324         parentItem = static_cast<PLItem*>(parent.internalPointer());
325
326     PLItem *childItem = parentItem->child( row );
327     if( childItem )
328         return createIndex( row, column, childItem );
329     else
330         return QModelIndex();
331 }
332
333 /* Return the index of a given item */
334 QModelIndex PLModel::index( PLItem *item, int column ) const
335 {
336     if( !item ) return QModelIndex();
337     const PLItem *parent = item->parent();
338     if( parent )
339         return createIndex( parent->children.lastIndexOf( item ),
340                             column, item );
341     return QModelIndex();
342 }
343
344 QModelIndex PLModel::parent( const QModelIndex &index ) const
345 {
346     if( !index.isValid() ) return QModelIndex();
347
348     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
349     if( !childItem )
350     {
351         msg_Err( p_playlist, "NULL CHILD" );
352         return QModelIndex();
353     }
354
355     PLItem *parentItem = childItem->parent();
356     if( !parentItem || parentItem == rootItem ) return QModelIndex();
357     if( !parentItem->parentItem )
358     {
359         msg_Err( p_playlist, "No parent parent, trying row 0 " );
360         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
361         return createIndex( 0, 0, parentItem );
362     }
363     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
364     return ind;
365 }
366
367 int PLModel::columnCount( const QModelIndex &i) const
368 {
369     return rootItem->item_col_strings.count();
370 }
371
372 int PLModel::childrenCount( const QModelIndex &parent ) const
373 {
374     return rowCount( parent );
375 }
376
377 int PLModel::rowCount( const QModelIndex &parent ) const
378 {
379     PLItem *parentItem;
380
381     if( !parent.isValid() )
382         parentItem = rootItem;
383     else
384         parentItem = static_cast<PLItem*>(parent.internalPointer());
385
386     return parentItem->childCount();
387 }
388
389 QStringList PLModel::selectedURIs()
390 {
391     QStringList lst;
392     for( int i = 0; i < current_selection.size(); i++ )
393     {
394         PL_LOCK;
395         PLItem *item = static_cast<PLItem*>
396                     (current_selection[i].internalPointer());
397         if( item )
398         {
399             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
400             if( p_item )
401             {
402                 char *psz = input_item_GetURI( p_item->p_input );
403                 if( psz )
404                 {
405                     lst.append( QString( psz ) );
406                     free( psz );
407                 }
408             }
409         }
410         PL_UNLOCK;
411     }
412     return lst;
413 }
414
415 /************************* General playlist status ***********************/
416
417 bool PLModel::hasRandom()
418 {
419     if( var_GetBool( p_playlist, "random" ) ) return true;
420     return false;
421 }
422 bool PLModel::hasRepeat()
423 {
424     if( var_GetBool( p_playlist, "repeat" ) ) return true;
425     return false;
426 }
427 bool PLModel::hasLoop()
428 {
429     if( var_GetBool( p_playlist, "loop" ) ) return true;
430     return false;
431 }
432 void PLModel::setLoop( bool on )
433 {
434     var_SetBool( p_playlist, "loop", on ? true:false );
435     config_PutInt( p_playlist, "loop", on ? 1: 0 );
436 }
437 void PLModel::setRepeat( bool on )
438 {
439     var_SetBool( p_playlist, "repeat", on ? true:false );
440     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
441 }
442 void PLModel::setRandom( bool on )
443 {
444     var_SetBool( p_playlist, "random", on ? true:false );
445     config_PutInt( p_playlist, "random", on ? 1: 0 );
446 }
447
448 /************************* Lookups *****************************/
449
450 PLItem *PLModel::FindById( PLItem *root, int i_id )
451 {
452     return FindInner( root, i_id, false );
453 }
454
455 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
456 {
457     return FindInner( root, i_id, true );
458 }
459
460 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
461 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
462
463 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
464 {
465     if( ( !b_input && i_cached_id == i_id) ||
466         ( b_input && i_cached_input_id ==i_id ) )
467     {
468         return b_input ? p_cached_item_bi : p_cached_item;
469     }
470
471     if( !b_input && root->i_id == i_id )
472     {
473         CACHE( i_id, root );
474         return root;
475     }
476     else if( b_input && root->i_input_id == i_id )
477     {
478         ICACHE( i_id, root );
479         return root;
480     }
481
482     QList<PLItem *>::iterator it = root->children.begin();
483     while ( it != root->children.end() )
484     {
485         if( !b_input && (*it)->i_id == i_id )
486         {
487             CACHE( i_id, (*it) );
488             return p_cached_item;
489         }
490         else if( b_input && (*it)->i_input_id == i_id )
491         {
492             ICACHE( i_id, (*it) );
493             return p_cached_item_bi;
494         }
495         if( (*it)->children.size() )
496         {
497             PLItem *childFound = FindInner( (*it), i_id, b_input );
498             if( childFound )
499             {
500                 if( b_input )
501                     ICACHE( i_id, childFound )
502                 else
503                     CACHE( i_id, childFound )
504                 return childFound;
505             }
506         }
507         it++;
508     }
509     return NULL;
510 }
511 #undef CACHE
512 #undef ICACHE
513
514
515 /************************* Updates handling *****************************/
516 void PLModel::customEvent( QEvent *event )
517 {
518     int type = event->type();
519     if( type != ItemAppend_Type &&
520         type != ItemDelete_Type && type != PLUpdate_Type )
521         return;
522
523     PLEvent *ple = static_cast<PLEvent *>(event);
524
525     if( type == ItemAppend_Type )
526         ProcessItemAppend( &ple->add );
527     else if( type == ItemDelete_Type )
528         ProcessItemRemoval( ple->i_id );
529     else
530         rebuild();
531 }
532
533 /**** Events processing ****/
534 void PLModel::ProcessInputItemUpdate( input_thread_t *p_input )
535 {
536     if( !p_input ) return;
537     ProcessInputItemUpdate( input_GetItem( p_input )->i_id );
538 }
539 void PLModel::ProcessInputItemUpdate( int i_input_id )
540 {
541     if( i_input_id <= 0 ) return;
542     PLItem *item = FindByInput( rootItem, i_input_id );
543     if( item )
544     {
545         QPL_LOCK;
546         UpdateTreeItem( item, true );
547         QPL_UNLOCK;
548     }
549 }
550
551 void PLModel::ProcessItemRemoval( int i_id )
552 {
553     if( i_id <= 0 ) return;
554     if( i_id == i_cached_id ) i_cached_id = -1;
555     i_cached_input_id = -1;
556
557     removeItem( i_id );
558 }
559
560 void PLModel::ProcessItemAppend( const playlist_add_t *p_add )
561 {
562     playlist_item_t *p_item = NULL;
563     PLItem *newItem = NULL;
564
565     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
566     PL_LOCK;
567     if( !nodeItem ) goto end;
568
569     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
570     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
571     if( i_depth == DEPTH_SEL && p_item->p_parent &&
572                         p_item->p_parent->i_id != rootItem->i_id )
573         goto end;
574
575     newItem = new PLItem( p_item, nodeItem, this );
576     nodeItem->appendChild( newItem );
577     UpdateTreeItem( p_item, newItem, true );
578 end:
579     PL_UNLOCK;
580     return;
581 }
582
583
584 void PLModel::rebuild()
585 {
586     rebuild( NULL );
587 }
588
589 void PLModel::rebuild( playlist_item_t *p_root )
590 {
591     playlist_item_t* p_item;
592     /* Remove callbacks before locking to avoid deadlocks */
593     delCallbacks();
594     /* Invalidate cache */
595     i_cached_id = i_cached_input_id = -1;
596
597     PL_LOCK;
598     /* Clear the tree */
599     if( rootItem )
600     {
601         if( rootItem->children.size() )
602         {
603             beginRemoveRows( index( rootItem, 0 ), 0,
604                     rootItem->children.size() -1 );
605             qDeleteAll( rootItem->children );
606             rootItem->children.clear();
607             endRemoveRows();
608         }
609     }
610     if( p_root )
611     {
612         delete rootItem;
613         rootItem = new PLItem( p_root, getSettings(), this );
614     }
615     assert( rootItem );
616     /* Recreate from root */
617     UpdateNodeChildren( rootItem );
618     if( (p_item = playlist_CurrentPlayingItem(p_playlist)) )
619     {
620         PLItem *currentItem = FindByInput( rootItem,
621                                            p_item->p_input->i_id );
622         if( currentItem )
623         {
624             UpdateTreeItem( p_item, currentItem,
625                             true, false );
626         }
627     }
628     PL_UNLOCK;
629
630     /* And signal the view */
631     emit layoutChanged();
632     addCallbacks();
633 }
634
635 /* This function must be entered WITH the playlist lock */
636 void PLModel::UpdateNodeChildren( PLItem *root )
637 {
638     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
639     UpdateNodeChildren( p_node, root );
640 }
641
642 /* This function must be entered WITH the playlist lock */
643 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
644 {
645     for( int i = 0; i < p_node->i_children ; i++ )
646     {
647         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
648         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
649         root->appendChild( newItem, false );
650         UpdateTreeItem( newItem, false, true );
651         if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
652             UpdateNodeChildren( p_node->pp_children[i], newItem );
653     }
654 }
655
656 /* This function must be entered WITH the playlist lock */
657 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
658 {
659     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
660     UpdateTreeItem( p_item, item, signal, force );
661 }
662
663 /* This function must be entered WITH the playlist lock */
664 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
665                               bool signal, bool force )
666 {
667     if ( !p_item )
668         return;
669     if( !force && i_depth == DEPTH_SEL && p_item->p_parent &&
670                                  p_item->p_parent->i_id != rootItem->i_id )
671         return;
672     item->update( p_item, p_item == playlist_CurrentPlayingItem( p_playlist ) );
673     if( signal )
674         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
675 }
676
677 /************************* Actions ******************************/
678
679 /**
680  * Deletion, here we have to do a ugly slow hack as we retrieve the full
681  * list of indexes to delete at once: when we delete a node and all of
682  * its children, we need to update the list.
683  * Todo: investigate whethere we can use ranges to be sure to delete all items?
684  */
685 void PLModel::doDelete( QModelIndexList selected )
686 {
687     for( int i = selected.size() -1 ; i >= 0; i-- )
688     {
689         QModelIndex index = selected[i];
690         if( index.column() != 0 ) continue;
691         PLItem *item = static_cast<PLItem*>(index.internalPointer());
692         if( item )
693         {
694             if( item->children.size() )
695                 recurseDelete( item->children, &selected );
696             doDeleteItem( item, &selected );
697         }
698     }
699 }
700
701 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
702 {
703     for( int i = children.size() - 1; i >= 0 ; i-- )
704     {
705         PLItem *item = children[i];
706         if( item->children.size() )
707             recurseDelete( item->children, fullList );
708         doDeleteItem( item, fullList );
709     }
710 }
711
712 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
713 {
714     QModelIndex deleteIndex = index( item, 0 );
715     fullList->removeAll( deleteIndex );
716
717     PL_LOCK;
718     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
719     if( !p_item )
720     {
721         PL_UNLOCK;
722         return;
723     }
724     if( p_item->i_children == -1 )
725         playlist_DeleteFromInput( p_playlist, item->i_input_id, pl_Locked );
726     else
727         playlist_NodeDelete( p_playlist, p_item, true, false );
728     /* And finally, remove it from the tree */
729     item->remove( item );
730     PL_UNLOCK;
731 }
732
733 /******* Volume III: Sorting and searching ********/
734 void PLModel::sort( int column, Qt::SortOrder order )
735 {
736     int i_index = -1;
737     int i_flag = 0;
738
739     int i_column = 1;
740     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
741     {
742         if( ( shownFlags() & i_column ) )
743             i_index++;
744         if( column == i_index )
745         {
746             i_flag = i_column;
747             goto next;
748         }
749     }
750
751
752 next:
753     PL_LOCK;
754     {
755         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
756                                                         rootItem->i_id );
757         if( p_root )
758         {
759             playlist_RecursiveNodeSort( p_playlist, p_root,
760                                         i_column_sorting( i_flag ),
761                                         order == Qt::AscendingOrder ?
762                                             ORDER_NORMAL : ORDER_REVERSE );
763         }
764     }
765     PL_UNLOCK;
766     rebuild();
767 }
768
769 void PLModel::search( QString search_text )
770 {
771     /** \todo Fire the search with a small delay ? */
772     PL_LOCK;
773     {
774         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
775                                                         rootItem->i_id );
776         assert( p_root );
777         char *psz_name = search_text.toUtf8().data();
778         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
779     }
780     PL_UNLOCK;
781     rebuild();
782 }
783
784 /*********** Popup *********/
785 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
786 {
787     assert( index.isValid() );
788     PL_LOCK;
789     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, itemId( index ) );
790     if( p_item )
791     {
792         i_popup_item = p_item->i_id;
793         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
794         PL_UNLOCK;
795         current_selection = list;
796         QMenu *menu = new QMenu;
797         menu->addAction( qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
798         menu->addAction( qtr(I_POP_DEL), this, SLOT( popupDel() ) );
799         menu->addSeparator();
800         menu->addAction( qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
801         menu->addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
802         menu->addSeparator();
803         menu->addAction( qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
804         if( p_item->i_children > -1 )
805         {
806             menu->addSeparator();
807             menu->addAction( qtr(I_POP_SORT), this, SLOT( popupSort() ) );
808             menu->addAction( qtr(I_POP_ADD), this, SLOT( popupAdd() ) );
809         }
810         menu->addSeparator();
811         menu->addAction( qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
812         menu->popup( point );
813     }
814     else
815         PL_UNLOCK;
816 }
817
818
819 void PLModel::viewchanged( int meta )
820 {
821     assert( meta );
822     int _meta = meta;
823     if( rootItem )
824     {
825         int index=-1;
826         while( _meta )
827         {
828             index++;
829             _meta >>= 1;
830         }
831
832         /* UNUSED        emit layoutAboutToBeChanged(); */
833         index = __MIN( index, rootItem->item_col_strings.count() );
834         QModelIndex parent = createIndex( 0, 0, rootItem );
835
836         if( rootItem->i_showflags & meta )
837             /* Removing columns */
838         {
839             beginRemoveColumns( parent, index, index+1 );
840             rootItem->i_showflags &= ~( meta );
841             getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
842             rootItem->updateColumnHeaders();
843             endRemoveColumns();
844         }
845         else
846         {
847             /* Adding columns */
848             beginInsertColumns( parent, index, index+1 );
849             rootItem->i_showflags |= meta;
850             getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
851             rootItem->updateColumnHeaders();
852             endInsertColumns();
853         }
854         rebuild();
855     }
856 }
857
858 void PLModel::popupDel()
859 {
860     doDelete( current_selection );
861 }
862
863 void PLModel::popupPlay()
864 {
865     PL_LOCK;
866     {
867         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
868                                                         i_popup_item );
869         activateItem( p_item );
870     }
871     PL_UNLOCK;
872 }
873
874 void PLModel::popupInfo()
875 {
876     PL_LOCK;
877     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
878                                                     i_popup_item );
879     if( p_item )
880     {
881         input_item_t* p_input = p_item->p_input;
882         vlc_gc_incref( p_input );
883         PL_UNLOCK;
884         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
885         vlc_gc_decref( p_input );
886         mid->setParent( PlaylistDialog::getInstance( p_intf ),
887                         Qt::Dialog );
888         mid->show();
889     }
890 }
891
892 void PLModel::popupStream()
893 {
894     QStringList mrls = selectedURIs();
895     if( !mrls.isEmpty() )
896         THEDP->streamingDialog( NULL, mrls[0], false );
897
898 }
899
900 void PLModel::popupSave()
901 {
902     QStringList mrls = selectedURIs();
903     if( !mrls.isEmpty() )
904         THEDP->streamingDialog( NULL, mrls[0] );
905 }
906
907 #include <QUrl>
908 #include <QFileInfo>
909 #include <QDesktopServices>
910 void PLModel::popupExplore()
911 {
912     PL_LOCK;
913     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
914                                                     i_popup_item );
915     if( p_item )
916     {
917        input_item_t *p_input = p_item->p_input;
918        char *psz_meta = input_item_GetURI( p_input );
919        PL_UNLOCK;
920        if( psz_meta )
921        {
922            const char *psz_access;
923            const char *psz_demux;
924            char  *psz_path;
925            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
926
927            if( EMPTY_STR( psz_access ) ||
928                !strncasecmp( psz_access, "file", 4 ) ||
929                !strncasecmp( psz_access, "dire", 4 ) )
930            {
931                QFileInfo info( qfu( psz_meta ) );
932                QDesktopServices::openUrl(
933                                QUrl::fromLocalFile( info.absolutePath() ) );
934            }
935            free( psz_meta );
936        }
937     }
938     else
939         PL_UNLOCK;
940 }
941
942 /**********************************************************************
943  * Playlist callbacks
944  **********************************************************************/
945 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
946                             vlc_value_t oval, vlc_value_t nval, void *param )
947 {
948     PLModel *p_model = (PLModel *) param;
949     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
950     QApplication::postEvent( p_model, event );
951     return VLC_SUCCESS;
952 }
953
954 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
955                          vlc_value_t oval, vlc_value_t nval, void *param )
956 {
957     PLModel *p_model = (PLModel *) param;
958     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
959     QApplication::postEvent( p_model, event );
960     event = new PLEvent( ItemUpdate_Type, nval.i_int );
961     QApplication::postEvent( p_model, event );
962     return VLC_SUCCESS;
963 }
964
965 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
966                         vlc_value_t oval, vlc_value_t nval, void *param )
967 {
968     PLModel *p_model = (PLModel *) param;
969     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
970     QApplication::postEvent( p_model, event );
971     return VLC_SUCCESS;
972 }
973
974 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
975                         vlc_value_t oval, vlc_value_t nval, void *param )
976 {
977     PLModel *p_model = (PLModel *) param;
978     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
979     QApplication::postEvent( p_model, event );
980     return VLC_SUCCESS;
981 }
982
983 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
984                          vlc_value_t oval, vlc_value_t nval, void *param )
985 {
986     PLModel *p_model = (PLModel *) param;
987     const playlist_add_t *p_add = (playlist_add_t *)nval.p_address;
988     PLEvent *event = new PLEvent( p_add );
989     QApplication::postEvent( p_model, event );
990     return VLC_SUCCESS;
991 }
992