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