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