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