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