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