]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Last patch to close #1479 (add a column with the track id)
[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     int i_index = -1;
719     int i_flag = 0;
720
721 #define CHECK_COLUMN( meta )                        \
722 {                                                   \
723     if( ( shownFlags() & VLC_META_ENGINE_##meta ) ) \
724         i_index++;                                  \
725     if( column == i_index )                         \
726     {                                               \
727         i_flag = VLC_META_ENGINE_##meta;            \
728         goto next;                                  \
729     }                                               \
730 }
731     CHECK_COLUMN( TITLE );
732     CHECK_COLUMN( DURATION );
733     CHECK_COLUMN( ARTIST );
734     CHECK_COLUMN( GENRE );
735     CHECK_COLUMN( COLLECTION );
736     CHECK_COLUMN( SEQ_NUM );
737     CHECK_COLUMN( DESCRIPTION );
738     CHECK_COLUMN( RATING );
739     CHECK_COLUMN( TRACKID );
740
741 #undef CHECK_COLUMN;
742
743 next:
744     PL_LOCK;
745     {
746         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
747                                                         rootItem->i_id,
748                                                         VLC_TRUE );
749         int i_mode;
750         switch( i_flag )
751         {
752         case VLC_META_ENGINE_TITLE:      i_mode = SORT_TITLE_NODES_FIRST;break;
753         case VLC_META_ENGINE_DURATION:   i_mode = SORT_DURATION;         break;
754         case VLC_META_ENGINE_ARTIST:     i_mode = SORT_ARTIST;           break;
755         case VLC_META_ENGINE_GENRE:      i_mode = SORT_GENRE;            break;
756         case VLC_META_ENGINE_COLLECTION: i_mode = SORT_ALBUM;            break;
757         case VLC_META_ENGINE_SEQ_NUM:    i_mode = SORT_TRACK_NUMBER;     break;
758         case VLC_META_ENGINE_DESCRIPTION:i_mode = SORT_DESCRIPTION;      break;
759         case VLC_META_ENGINE_RATING:     i_mode = SORT_RATING;           break;
760         case VLC_META_ENGINE_TRACKID:    i_mode = SORT_ID;               break;
761         default:                         i_mode = SORT_TITLE_NODES_FIRST;break;
762         }
763         if( p_root )
764         {
765             playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
766                                         order == Qt::AscendingOrder ?
767                                             ORDER_NORMAL : ORDER_REVERSE );
768             p_playlist->b_reset_currently_playing = VLC_TRUE;
769         }
770     }
771     PL_UNLOCK;
772     rebuild();
773 }
774
775 void PLModel::search( QString search_text )
776 {
777     /** \todo Fire the search with a small delay ? */
778     PL_LOCK;
779     {
780         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
781                                                         rootItem->i_id,
782                                                         VLC_TRUE );
783         assert( p_root );
784         char *psz_name = search_text.toUtf8().data();
785         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
786     }
787     PL_UNLOCK;
788     rebuild();
789 }
790
791 /*********** Popup *********/
792 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
793 {
794     assert( index.isValid() );
795     PL_LOCK;
796     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
797                                                     itemId( index ), VLC_TRUE );
798     if( p_item )
799     {
800         i_popup_item = p_item->i_id;
801         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
802         PL_UNLOCK;
803         current_selection = list;
804         QMenu *menu = new QMenu;
805         menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
806         menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
807         menu->addSeparator();
808         menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
809         menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
810         menu->addSeparator();
811         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
812         if( p_item->i_children > -1 )
813         {
814             menu->addSeparator();
815             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
816             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
817         }
818 #ifdef WIN32
819         menu->addSeparator();
820         menu->addAction( qfu( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
821 #endif
822         menu->popup( point );
823     }
824     else
825         PL_UNLOCK;
826 }
827
828
829 void PLModel::viewchanged( int meta )
830 {
831     if( rootItem )
832     {
833         int index=0;
834         switch( meta )
835         {
836         case VLC_META_ENGINE_TITLE:
837             index=0; break;
838         case VLC_META_ENGINE_DURATION:
839             index=1; break;
840         case VLC_META_ENGINE_ARTIST:
841             index=2; break;
842         case VLC_META_ENGINE_GENRE:
843             index=3; break;
844         case VLC_META_ENGINE_COPYRIGHT:
845             index=4; break;
846         case VLC_META_ENGINE_COLLECTION:
847             index=5; break;
848         case VLC_META_ENGINE_SEQ_NUM:
849             index=6; break;
850         case VLC_META_ENGINE_DESCRIPTION:
851             index=7; break;
852         case VLC_META_ENGINE_RATING:
853             index=8; break;
854         case VLC_META_ENGINE_TRACKID:
855             index=9; break;
856         default:
857             break;
858         }
859         /* UNUSED        emit layoutAboutToBeChanged(); */
860         index = __MIN( index, rootItem->item_col_strings.count() );
861         QModelIndex parent = createIndex( 0, 0, rootItem );
862
863         if( rootItem->i_showflags & meta )
864             /* Removing columns */
865         {
866             beginRemoveColumns( parent, index, index+1 );
867             rootItem->i_showflags &= ~( meta );
868             rootItem->updateColumnHeaders();
869             endRemoveColumns();
870         }
871         else
872         {
873             /* Adding columns */
874             beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
875             rootItem->i_showflags |= meta;
876             rootItem->updateColumnHeaders();
877             endInsertColumns();
878         }
879         rebuild();
880     }
881 }
882
883 void PLModel::popupDel()
884 {
885     doDelete( current_selection );
886 }
887 void PLModel::popupPlay()
888 {
889     PL_LOCK;
890     {
891         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
892                                                         i_popup_item,VLC_TRUE );
893         activateItem( p_item );
894     }
895     PL_UNLOCK;
896 }
897
898 void PLModel::popupInfo()
899 {
900     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
901                                                     i_popup_item,
902                                                     VLC_TRUE );
903     if( p_item )
904     {
905         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_item->p_input );
906         mid->show();
907     }
908 }
909
910 void PLModel::popupStream()
911 {
912      msg_Err( p_playlist, "Stream not implemented" );
913 }
914
915 void PLModel::popupSave()
916 {
917      msg_Err( p_playlist, "Save not implemented" );
918 }
919
920 #ifdef WIN32
921 #include <shellapi.h>
922 void PLModel::popupExplore()
923 {
924     ShellExecute( NULL, "explore", "C:\\", NULL, NULL, SW_SHOWNORMAL );
925 }
926 #endif
927
928 /**********************************************************************
929  * Playlist callbacks
930  **********************************************************************/
931 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
932                             vlc_value_t oval, vlc_value_t nval, void *param )
933 {
934     PLModel *p_model = (PLModel *) param;
935     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
936     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
937     return VLC_SUCCESS;
938 }
939
940 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
941                          vlc_value_t oval, vlc_value_t nval, void *param )
942 {
943     PLModel *p_model = (PLModel *) param;
944     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
945     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
946     event = new PLEvent( ItemUpdate_Type, nval.i_int );
947     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
948     return VLC_SUCCESS;
949 }
950
951 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
952                         vlc_value_t oval, vlc_value_t nval, void *param )
953 {
954     PLModel *p_model = (PLModel *) param;
955     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
956     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
957     return VLC_SUCCESS;
958 }
959
960 static int ItemDeleted( 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( ItemDelete_Type, nval.i_int );
965     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
966     return VLC_SUCCESS;
967 }
968
969 static int ItemAppended( 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     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
974     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
975
976     if( ++p_model->i_items_to_append >= 50 )
977     {
978 //        p_model->b_need_update = VLC_TRUE;
979 //        return VLC_SUCCESS;
980     }
981     PLEvent *event = new PLEvent(  p_add );
982     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
983     return VLC_SUCCESS;
984 }
985