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