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