]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
f09a4a7abe53e0983c9722b97bd6fe5b4b523edc
[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 <vlc_intf_strings.h>
33
34 #include "pixmaps/types/type_unknown.xpm"
35
36 #include <assert.h>
37 #include <QIcon>
38 #include <QFont>
39 #include <QMenu>
40 #include <QApplication>
41 #include <QSettings>
42
43 #include "sorting.h"
44
45 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
46
47 static int PlaylistChanged( vlc_object_t *, const char *,
48                             vlc_value_t, vlc_value_t, void * );
49 static int PlaylistNext( vlc_object_t *, const char *,
50                          vlc_value_t, vlc_value_t, void * );
51 static int ItemChanged( vlc_object_t *, const char *,
52                         vlc_value_t, vlc_value_t, void * );
53 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
54                          vlc_value_t oval, vlc_value_t nval, void *param );
55 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
56                         vlc_value_t oval, vlc_value_t nval, void *param );
57
58 /*************************************************************************
59  * Playlist model implementation
60  *************************************************************************/
61
62 /*
63   This model is called two times, for the selector and the standard panel
64 */
65 PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
66                   intf_thread_t *_p_intf,   /* main Qt p_intf */
67                   playlist_item_t * p_root,
68                   /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
69                     and THEPL->p_root_category for SelectPL */
70                   int _i_depth,             /* -1 for StandPL, 1 for SelectPL */
71                   QObject *parent )         /* Basic Qt parent */
72                   : QAbstractItemModel( parent )
73 {
74     i_depth = _i_depth;
75     assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
76     p_intf            = _p_intf;
77     p_playlist        = _p_playlist;
78     i_cached_id       = -1;
79     i_cached_input_id = -1;
80     i_popup_item      = i_popup_parent = -1;
81
82     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
83
84     /* Icons initialization */
85 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
86     ADD_ICON( UNKNOWN , type_unknown_xpm );
87     ADD_ICON( FILE, ":/type_file" );
88     ADD_ICON( DIRECTORY, ":/type_directory" );
89     ADD_ICON( DISC, ":/disc" );
90     ADD_ICON( CDDA, ":/cdda" );
91     ADD_ICON( CARD, ":/capture-card" );
92     ADD_ICON( NET, ":/type_net" );
93     ADD_ICON( PLAYLIST, ":/type_playlist" );
94     ADD_ICON( NODE, ":/type_node" );
95 #undef ADD_ICON
96
97     rebuild( p_root );
98 }
99
100 PLModel::~PLModel()
101 {
102     getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
103     delCallbacks();
104     delete rootItem;
105 }
106
107 Qt::DropActions PLModel::supportedDropActions() const
108 {
109     return Qt::CopyAction; /* Why not Qt::MoveAction */
110 }
111
112 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
113 {
114     Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
115     if( index.isValid() )
116         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
117     else
118         return Qt::ItemIsDropEnabled | defaultFlags;
119 }
120
121 /* A list of model indexes are a playlist */
122 QStringList PLModel::mimeTypes() const
123 {
124     QStringList types;
125     types << "vlc/playlist-item-id";
126     return types;
127 }
128
129 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
130 {
131     QMimeData *mimeData = new QMimeData();
132     QByteArray encodedData;
133     QDataStream stream( &encodedData, QIODevice::WriteOnly );
134
135     foreach( QModelIndex index, indexes ) {
136         if( index.isValid() && index.column() == 0 )
137             stream << itemId( index );
138     }
139     mimeData->setData( "vlc/playlist-item-id", encodedData );
140     return mimeData;
141 }
142
143 /* Drop operation */
144 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
145                            int row, int column, const QModelIndex &target )
146 {
147     if( data->hasFormat( "vlc/playlist-item-id" ) )
148     {
149         if( action == Qt::IgnoreAction )
150             return true;
151
152         if( !target.isValid() )
153             /* We don't want to move on an invalid position */
154             return true;
155
156         PLItem *targetItem = static_cast<PLItem*>( target.internalPointer() );
157
158         QByteArray encodedData = data->data( "vlc/playlist-item-id" );
159         QDataStream stream( &encodedData, QIODevice::ReadOnly );
160
161         PLItem *newParentItem;
162         while( !stream.atEnd() )
163         {
164             int i;
165             int srcId;
166             stream >> srcId;
167
168             PL_LOCK;
169             playlist_item_t *p_target =
170                         playlist_ItemGetById( p_playlist, targetItem->i_id,
171                                               pl_Locked );
172             playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
173                                                            pl_Locked );
174
175             if( !p_target || !p_src )
176             {
177                 PL_UNLOCK;
178                 return false;
179             }
180             if( p_target->i_children == -1 ) /* A leaf */
181             {
182                 PLItem *parentItem = targetItem->parent();
183                 assert( parentItem );
184                 playlist_item_t *p_parent =
185                          playlist_ItemGetById( p_playlist, parentItem->i_id,
186                                                pl_Locked );
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, "playlist-current", PlaylistNext, this );
227     /* One item has been updated */
228     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
229     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
230     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
231 }
232
233 void PLModel::delCallbacks()
234 {
235     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
236     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
237     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
238     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
239     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
240 }
241
242 void PLModel::activateItem( const QModelIndex &index )
243 {
244     assert( index.isValid() );
245     PLItem *item = static_cast<PLItem*>(index.internalPointer());
246     assert( item );
247     PL_LOCK;
248     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
249                                                     pl_Locked );
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             continue;
396
397         input_item_t *p_item = input_item_GetById( p_playlist,
398                                                    item->i_input_id );
399         if( !p_item )
400             continue;
401
402         char *psz = input_item_GetURI( p_item );
403         if( !psz )
404             continue;
405         else
406         {
407             lst.append( QString( psz ) );
408             free( psz );
409         }
410         PL_UNLOCK;
411     }
412     return lst;
413 }
414
415 /************************* General playlist status ***********************/
416
417 bool PLModel::hasRandom()
418 {
419     if( var_GetBool( p_playlist, "random" ) ) return true;
420     return false;
421 }
422 bool PLModel::hasRepeat()
423 {
424     if( var_GetBool( p_playlist, "repeat" ) ) return true;
425     return false;
426 }
427 bool PLModel::hasLoop()
428 {
429     if( var_GetBool( p_playlist, "loop" ) ) return true;
430     return false;
431 }
432 void PLModel::setLoop( bool on )
433 {
434     var_SetBool( p_playlist, "loop", on ? true:false );
435     config_PutInt( p_playlist, "loop", on ? 1: 0 );
436 }
437 void PLModel::setRepeat( bool on )
438 {
439     var_SetBool( p_playlist, "repeat", on ? true:false );
440     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
441 }
442 void PLModel::setRandom( bool on )
443 {
444     var_SetBool( p_playlist, "random", on ? true:false );
445     config_PutInt( p_playlist, "random", on ? 1: 0 );
446 }
447
448 /************************* Lookups *****************************/
449
450 PLItem *PLModel::FindById( PLItem *root, int i_id )
451 {
452     return FindInner( root, i_id, false );
453 }
454
455 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
456 {
457     return FindInner( root, i_id, true );
458 }
459
460 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
461 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
462
463 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
464 {
465     if( ( !b_input && i_cached_id == i_id) ||
466         ( b_input && i_cached_input_id ==i_id ) )
467     {
468         return b_input ? p_cached_item_bi : p_cached_item;
469     }
470
471     if( !b_input && root->i_id == i_id )
472     {
473         CACHE( i_id, root );
474         return root;
475     }
476     else if( b_input && root->i_input_id == i_id )
477     {
478         ICACHE( i_id, root );
479         return root;
480     }
481
482     QList<PLItem *>::iterator it = root->children.begin();
483     while ( it != root->children.end() )
484     {
485         if( !b_input && (*it)->i_id == i_id )
486         {
487             CACHE( i_id, (*it) );
488             return p_cached_item;
489         }
490         else if( b_input && (*it)->i_input_id == i_id )
491         {
492             ICACHE( i_id, (*it) );
493             return p_cached_item_bi;
494         }
495         if( (*it)->children.size() )
496         {
497             PLItem *childFound = FindInner( (*it), i_id, b_input );
498             if( childFound )
499             {
500                 if( b_input )
501                     ICACHE( i_id, childFound )
502                 else
503                     CACHE( i_id, childFound )
504                 return childFound;
505             }
506         }
507         it++;
508     }
509     return NULL;
510 }
511 #undef CACHE
512 #undef ICACHE
513
514
515 /************************* Updates handling *****************************/
516 void PLModel::customEvent( QEvent *event )
517 {
518     int type = event->type();
519     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
520         type != ItemDelete_Type && type != PLUpdate_Type )
521         return;
522
523     PLEvent *ple = static_cast<PLEvent *>(event);
524
525     if( type == ItemUpdate_Type )
526         ProcessInputItemUpdate( ple->i_id );
527     else if( type == ItemAppend_Type )
528         ProcessItemAppend( ple->p_add );
529     else if( type == ItemDelete_Type )
530         ProcessItemRemoval( ple->i_id );
531     else
532         rebuild();
533 }
534
535 /**** Events processing ****/
536 void PLModel::ProcessInputItemUpdate( int i_input_id )
537 {
538     if( i_input_id <= 0 ) return;
539     PLItem *item = FindByInput( rootItem, i_input_id );
540     if( item )
541     {
542         QPL_LOCK;
543         UpdateTreeItem( item, true );
544         QPL_UNLOCK;
545     }
546 }
547
548 void PLModel::ProcessItemRemoval( int i_id )
549 {
550     if( i_id <= 0 ) return;
551     if( i_id == i_cached_id ) i_cached_id = -1;
552     i_cached_input_id = -1;
553
554     removeItem( i_id );
555 }
556
557 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
558 {
559     playlist_item_t *p_item = NULL;
560     PLItem *newItem = NULL;
561
562     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
563     PL_LOCK;
564     if( !nodeItem ) goto end;
565
566     p_item = playlist_ItemGetById( p_playlist, p_add->i_item, pl_Locked );
567     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
568     if( i_depth == DEPTH_SEL && p_item->p_parent &&
569                         p_item->p_parent->i_id != rootItem->i_id )
570         goto end;
571
572     newItem = new PLItem( p_item, nodeItem, this );
573     nodeItem->appendChild( newItem );
574     UpdateTreeItem( p_item, newItem, true );
575 end:
576     PL_UNLOCK;
577     return;
578 }
579
580
581 void PLModel::rebuild()
582 {
583     rebuild( NULL );
584 }
585
586 void PLModel::rebuild( playlist_item_t *p_root )
587 {
588     /* Remove callbacks before locking to avoid deadlocks */
589     delCallbacks();
590     /* Invalidate cache */
591     i_cached_id = i_cached_input_id = -1;
592
593     PL_LOCK;
594     /* Clear the tree */
595     if( rootItem )
596     {
597         if( rootItem->children.size() )
598         {
599             beginRemoveRows( index( rootItem, 0 ), 0,
600                     rootItem->children.size() -1 );
601             qDeleteAll( rootItem->children );
602             rootItem->children.clear();
603             endRemoveRows();
604         }
605     }
606     if( p_root )
607     {
608         delete rootItem;
609         rootItem = new PLItem( p_root, getSettings(), this );
610     }
611     assert( rootItem );
612     /* Recreate from root */
613     UpdateNodeChildren( rootItem );
614     if( p_playlist->status.p_item )
615     {
616         PLItem *currentItem = FindByInput( rootItem,
617                                      p_playlist->status.p_item->p_input->i_id );
618         if( currentItem )
619         {
620             UpdateTreeItem( p_playlist->status.p_item, currentItem,
621                             true, false );
622         }
623     }
624     PL_UNLOCK;
625
626     /* And signal the view */
627     emit layoutChanged();
628     addCallbacks();
629 }
630
631 /* This function must be entered WITH the playlist lock */
632 void PLModel::UpdateNodeChildren( PLItem *root )
633 {
634     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
635                                                     pl_Locked );
636     UpdateNodeChildren( p_node, root );
637 }
638
639 /* This function must be entered WITH the playlist lock */
640 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
641 {
642     for( int i = 0; i < p_node->i_children ; i++ )
643     {
644         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
645         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
646         root->appendChild( newItem, false );
647         UpdateTreeItem( newItem, false, true );
648         if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
649             UpdateNodeChildren( p_node->pp_children[i], newItem );
650     }
651 }
652
653 /* This function must be entered WITH the playlist lock */
654 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
655 {
656     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
657                                                     pl_Locked );
658     UpdateTreeItem( p_item, item, signal, force );
659 }
660
661 /* This function must be entered WITH the playlist lock */
662 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
663                               bool signal, bool force )
664 {
665     if ( !p_item )
666         return;
667     if( !force && i_depth == DEPTH_SEL && p_item->p_parent &&
668                                  p_item->p_parent->i_id != rootItem->i_id )
669         return;
670     item->update( p_item, p_item == p_playlist->status.p_item );
671     if( signal )
672         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
673 }
674
675 /************************* Actions ******************************/
676
677 /**
678  * Deletion, here we have to do a ugly slow hack as we retrieve the full
679  * list of indexes to delete at once: when we delete a node and all of
680  * its children, we need to update the list.
681  * Todo: investigate whethere we can use ranges to be sure to delete all items?
682  */
683 void PLModel::doDelete( QModelIndexList selected )
684 {
685     for( int i = selected.size() -1 ; i >= 0; i-- )
686     {
687         QModelIndex index = selected[i];
688         if( index.column() != 0 ) continue;
689         PLItem *item = static_cast<PLItem*>(index.internalPointer());
690         if( item )
691         {
692             if( item->children.size() )
693                 recurseDelete( item->children, &selected );
694             doDeleteItem( item, &selected );
695         }
696     }
697 }
698
699 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
700 {
701     for( int i = children.size() - 1; i >= 0 ; i-- )
702     {
703         PLItem *item = children[i];
704         if( item->children.size() )
705             recurseDelete( item->children, fullList );
706         doDeleteItem( item, fullList );
707     }
708 }
709
710 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
711 {
712     QModelIndex deleteIndex = index( item, 0 );
713     fullList->removeAll( deleteIndex );
714
715     PL_LOCK;
716     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
717                                                     pl_Locked );
718     if( !p_item )
719     {
720         PL_UNLOCK; return;
721     }
722     if( p_item->i_children == -1 )
723         playlist_DeleteFromInput( p_playlist, item->i_input_id, pl_Locked );
724     else
725         playlist_NodeDelete( p_playlist, p_item, true, false );
726     /* And finally, remove it from the tree */
727     item->remove( item );
728     PL_UNLOCK;
729 }
730
731 /******* Volume III: Sorting and searching ********/
732 void PLModel::sort( int column, Qt::SortOrder order )
733 {
734     int i_index = -1;
735     int i_flag = 0;
736
737     // FIXME: Disable sorting on startup by ignoring
738     // first call of sorting caused by showing dialog
739     // see: standardpanel.cpp:65
740     static bool b_first_time = true;
741     if( b_first_time )
742     {
743         b_first_time = false;
744         return;
745     }
746
747 #define CHECK_COLUMN( meta )                        \
748 {                                                   \
749     if( ( shownFlags() & meta ) )                   \
750         i_index++;                                  \
751     if( column == i_index )                         \
752     {                                               \
753         i_flag = meta;                              \
754         goto next;                                  \
755     }                                               \
756 }
757
758     CHECK_COLUMN( COLUMN_NUMBER );
759     CHECK_COLUMN( COLUMN_TITLE );
760     CHECK_COLUMN( COLUMN_DURATION );
761     CHECK_COLUMN( COLUMN_ARTIST );
762     CHECK_COLUMN( COLUMN_GENRE );
763     CHECK_COLUMN( COLUMN_ALBUM );
764     CHECK_COLUMN( COLUMN_TRACK_NUMBER );
765     CHECK_COLUMN( COLUMN_DESCRIPTION );
766
767 #undef CHECK_COLUMN
768
769 next:
770     PL_LOCK;
771     {
772         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
773                                                         rootItem->i_id,
774                                                         pl_Locked );
775         if( p_root )
776         {
777             playlist_RecursiveNodeSort( p_playlist, p_root,
778                                         i_column_sorting( i_flag ),
779                                         order == Qt::AscendingOrder ?
780                                             ORDER_NORMAL : ORDER_REVERSE );
781             p_playlist->b_reset_currently_playing = true;
782         }
783     }
784     PL_UNLOCK;
785     rebuild();
786 }
787
788 void PLModel::search( QString search_text )
789 {
790     /** \todo Fire the search with a small delay ? */
791     PL_LOCK;
792     {
793         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
794                                                         rootItem->i_id,
795                                                         pl_Locked );
796         assert( p_root );
797         char *psz_name = search_text.toUtf8().data();
798         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
799     }
800     PL_UNLOCK;
801     rebuild();
802 }
803
804 /*********** Popup *********/
805 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
806 {
807     assert( index.isValid() );
808     PL_LOCK;
809     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
810                                                     itemId( index ), pl_Locked );
811     if( p_item )
812     {
813         i_popup_item = p_item->i_id;
814         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
815         PL_UNLOCK;
816         current_selection = list;
817         QMenu *menu = new QMenu;
818         menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
819         menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
820         menu->addSeparator();
821         menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
822         menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
823         menu->addSeparator();
824         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
825         if( p_item->i_children > -1 )
826         {
827             menu->addSeparator();
828             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
829             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
830         }
831         menu->addSeparator();
832         menu->addAction( qfu( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
833         menu->popup( point );
834     }
835     else
836         PL_UNLOCK;
837 }
838
839
840 void PLModel::viewchanged( int meta )
841 {
842     assert( meta );
843     int _meta = meta;
844     if( rootItem )
845     {
846         int index=-1;
847         while( _meta )
848         {
849             index++;
850             _meta >>= 1;
851         }
852
853         /* UNUSED        emit layoutAboutToBeChanged(); */
854         index = __MIN( index, rootItem->item_col_strings.count() );
855         QModelIndex parent = createIndex( 0, 0, rootItem );
856
857         if( rootItem->i_showflags & meta )
858             /* Removing columns */
859         {
860             beginRemoveColumns( parent, index, index+1 );
861             rootItem->i_showflags &= ~( meta );
862             rootItem->updateColumnHeaders();
863             endRemoveColumns();
864         }
865         else
866         {
867             /* Adding columns */
868             beginInsertColumns( parent, index, index+1 );
869             rootItem->i_showflags |= meta;
870             rootItem->updateColumnHeaders();
871             endInsertColumns();
872         }
873         rebuild();
874     }
875 }
876
877 void PLModel::popupDel()
878 {
879     doDelete( current_selection );
880 }
881
882 void PLModel::popupPlay()
883 {
884     PL_LOCK;
885     {
886         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
887                                                         i_popup_item,
888                                                         pl_Locked );
889         activateItem( p_item );
890     }
891     PL_UNLOCK;
892 }
893
894 void PLModel::popupInfo()
895 {
896     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
897                                                     i_popup_item,
898                                                     pl_Unlocked );
899     if( p_item )
900     {
901         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_item->p_input );
902         mid->show();
903     }
904 }
905
906 void PLModel::popupStream()
907 {
908     QStringList mrls = selectedURIs();
909     if( !mrls.isEmpty() )
910         THEDP->streamingDialog( NULL, mrls[0], false );
911
912 }
913
914 void PLModel::popupSave()
915 {
916     QStringList mrls = selectedURIs();
917     if( !mrls.isEmpty() )
918         THEDP->streamingDialog( NULL, mrls[0], true );
919 }
920
921 #include <QUrl>
922 #include <QFileInfo>
923 #include <QDesktopServices>
924 void PLModel::popupExplore()
925 {
926     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
927                                                     i_popup_item,
928                                                     pl_Unlocked );
929     if( p_item )
930     {
931        input_item_t *p_input = p_item->p_input;
932        char *psz_meta = input_item_GetURI( p_input );
933        if( psz_meta )
934        {
935            const char *psz_access;
936            const char *psz_demux;
937            char  *psz_path;
938            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
939
940            if( EMPTY_STR( psz_access ) ||
941                !strncasecmp( psz_access, "file", 4 ) ||
942                !strncasecmp( psz_access, "dire", 4 ) )
943            {
944                QFileInfo info( qfu( psz_meta ) );
945                QDesktopServices::openUrl(
946                                QUrl::fromLocalFile( info.absolutePath() ) );
947            }
948            free( psz_meta );
949        }
950     }
951 }
952
953 /**********************************************************************
954  * Playlist callbacks
955  **********************************************************************/
956 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
957                             vlc_value_t oval, vlc_value_t nval, void *param )
958 {
959     PLModel *p_model = (PLModel *) param;
960     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
961     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
962     return VLC_SUCCESS;
963 }
964
965 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
966                          vlc_value_t oval, vlc_value_t nval, void *param )
967 {
968     PLModel *p_model = (PLModel *) param;
969     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
970     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
971     event = new PLEvent( ItemUpdate_Type, nval.i_int );
972     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
973     return VLC_SUCCESS;
974 }
975
976 static int ItemChanged( 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     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
981     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
982     return VLC_SUCCESS;
983 }
984
985 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
986                         vlc_value_t oval, vlc_value_t nval, void *param )
987 {
988     PLModel *p_model = (PLModel *) param;
989     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
990     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
991     return VLC_SUCCESS;
992 }
993
994 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
995                          vlc_value_t oval, vlc_value_t nval, void *param )
996 {
997     PLModel *p_model = (PLModel *) param;
998     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
999     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1000
1001     PLEvent *event = new PLEvent(  p_add );
1002     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1003     return VLC_SUCCESS;
1004 }
1005