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