]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.cpp
Improve drag&drop handling
[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             if( p_target->i_children == -1 ) /* A leaf */
255             {
256                 PLItem *parentItem = targetItem->parent();
257                 assert( parentItem );
258                 playlist_item_t *p_parent =
259                          playlist_ItemGetById( p_playlist, parentItem->i_id );
260                 if( !p_parent )
261                 {
262                     PL_UNLOCK;
263                     return false;
264                 }
265                 for( i = 0 ; i< p_parent->i_children ; i++ )
266                     if( p_parent->pp_children[i] == p_target ) break;
267                 playlist_TreeMove( p_playlist, p_src, p_parent, i );
268                 newParentItem = parentItem;
269             }
270             else
271             {
272                 /* \todo: if we drop on a top-level node, use copy instead ? */
273                 playlist_TreeMove( p_playlist, p_src, p_target, 0 );
274                 i = 0;
275                 newParentItem = targetItem;
276             }
277             /* Remove from source */
278             PLItem *srcItem = FindById( rootItem, p_src->i_id );
279             // We dropped on the source selector. Ask the dialog to forward
280             // to the main view
281             if( !srcItem )
282             {
283                 emit shouldRemove( p_src->i_id );
284             }
285             else
286                 srcItem->remove( srcItem );
287
288             /* Display at new destination */
289             PLItem *newItem = new PLItem( p_src, newParentItem, this );
290             newParentItem->insertChild( newItem, i, true );
291             UpdateTreeItem( p_src, newItem, true );
292             if( p_src->i_children != -1 )
293                 UpdateNodeChildren( newItem );
294             PL_UNLOCK;
295         }
296     }
297     return true;
298 }
299
300 void PLModel::removeItem( int i_id )
301 {
302     PLItem *item = FindById( rootItem,i_id );
303     if( item ) item->remove( item );
304 }
305
306 void PLModel::addCallbacks()
307 {
308     /* Some global changes happened -> Rebuild all */
309     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
310     /* We went to the next item */
311     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
312     /* One item has been updated */
313     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
314     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
315     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
316 }
317
318 void PLModel::delCallbacks()
319 {
320     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
321     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
322     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
323     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
324     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
325 }
326
327 void PLModel::activateItem( const QModelIndex &index )
328 {
329     assert( index.isValid() );
330     PLItem *item = static_cast<PLItem*>(index.internalPointer());
331     assert( item );
332     PL_LOCK;
333     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
334     activateItem( p_item );
335     PL_UNLOCK;
336 }
337 /* Must be entered with lock */
338 void PLModel::activateItem( playlist_item_t *p_item )
339 {
340     if( !p_item ) return;
341     playlist_item_t *p_parent = p_item;
342     while( p_parent )
343     {
344         if( p_parent->i_id == rootItem->i_id ) break;
345         p_parent = p_parent->p_parent;
346     }
347     if( p_parent )
348         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
349 }
350
351 /****************** Base model mandatory implementations *****************/
352 QVariant PLModel::data(const QModelIndex &index, int role) const
353 {
354     if(!index.isValid() ) return QVariant();
355     PLItem *item = static_cast<PLItem*>(index.internalPointer());
356     if( role == Qt::DisplayRole )
357     {
358         return QVariant( item->columnString( index.column() ) );
359     }
360     else if( role == Qt::DecorationRole && index.column() == 0  )
361     {
362         if( item->type >= 0 )
363             return QVariant( PLModel::icons[item->type] );
364     }
365     else if( role == Qt::FontRole )
366     {
367         if( item->current == true )
368         {
369             QFont f; f.setBold( true ); return QVariant( f );
370         }
371     }
372     return QVariant();
373 }
374
375 bool PLModel::isCurrent( const QModelIndex &index )
376 {
377     assert( index.isValid() );
378     return static_cast<PLItem*>(index.internalPointer())->current;
379 }
380
381 int PLModel::itemId( const QModelIndex &index ) const
382 {
383     assert( index.isValid() );
384     return static_cast<PLItem*>(index.internalPointer())->i_id;
385 }
386
387 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
388                               int role) const
389 {
390     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
391             return QVariant( rootItem->columnString( section ) );
392     return QVariant();
393 }
394
395 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
396                   const
397 {
398     PLItem *parentItem;
399     if (!parent.isValid())
400         parentItem = rootItem;
401     else
402         parentItem = static_cast<PLItem*>(parent.internalPointer());
403
404     PLItem *childItem = parentItem->child(row);
405     if (childItem)
406         return createIndex(row, column, childItem);
407     else
408         return QModelIndex();
409 }
410
411 /* Return the index of a given item */
412 QModelIndex PLModel::index( PLItem *item, int column ) const
413 {
414     if( !item ) return QModelIndex();
415     const PLItem *parent = item->parent();
416     if( parent )
417         return createIndex( parent->children.lastIndexOf( item ),
418                             column, item );
419     return QModelIndex();
420 }
421
422 QModelIndex PLModel::parent(const QModelIndex &index) const
423 {
424     if( !index.isValid() ) return QModelIndex();
425
426     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
427     if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
428     PLItem *parentItem = childItem->parent();
429     if( !parentItem || parentItem == rootItem ) return QModelIndex();
430     if( ! parentItem->parentItem )
431     {
432         msg_Err( p_playlist, "No parent parent, trying row 0 " );
433         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
434         return createIndex( 0, 0, parentItem );
435     }
436     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
437     return ind;
438 }
439
440 int PLModel::columnCount( const QModelIndex &i) const
441 {
442     if( i_depth == 1 ) return 1;
443     return 3;
444 }
445
446 int PLModel::childrenCount(const QModelIndex &parent) const
447 {
448     return rowCount( parent );
449 }
450
451 int PLModel::rowCount(const QModelIndex &parent) const
452 {
453     PLItem *parentItem;
454
455     if (!parent.isValid())
456         parentItem = rootItem;
457     else
458         parentItem = static_cast<PLItem*>(parent.internalPointer());
459
460     return parentItem->childCount();
461 }
462
463 /************************* General playlist status ***********************/
464
465 bool PLModel::hasRandom()
466 {
467     if( var_GetBool( p_playlist, "random" ) ) return true;
468     return false;
469 }
470 bool PLModel::hasRepeat()
471 {
472     if( var_GetBool( p_playlist, "repeat" ) ) return true;
473     return false;
474 }
475 bool PLModel::hasLoop()
476 {
477     if( var_GetBool( p_playlist, "loop" ) ) return true;
478     return false;
479 }
480 void PLModel::setLoop( bool on )
481 {
482     var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
483     config_PutInt( p_playlist, "loop", on ? 1: 0 );
484 }
485 void PLModel::setRepeat( bool on )
486 {
487     var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
488     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
489 }
490 void PLModel::setRandom( bool on )
491 {
492     var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
493     config_PutInt( p_playlist, "random", on ? 1: 0 );
494 }
495
496 /************************* Lookups *****************************/
497
498 PLItem *PLModel::FindById( PLItem *root, int i_id )
499 {
500     return FindInner( root, i_id, false );
501 }
502
503 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
504 {
505     return FindInner( root, i_id, true );
506 }
507
508 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
509 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
510
511 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
512 {
513     if( ( !b_input && i_cached_id == i_id) ||
514         ( b_input && i_cached_input_id ==i_id ) )
515     {
516         return b_input ? p_cached_item_bi : p_cached_item;
517     }
518
519     if( !b_input && root->i_id == i_id )
520     {
521         CACHE( i_id, root );
522         return root;
523     }
524     else if( b_input && root->i_input_id == i_id )
525     {
526         ICACHE( i_id, root );
527         return root;
528     }
529
530     QList<PLItem *>::iterator it = root->children.begin();
531     while ( it != root->children.end() )
532     {
533         if( !b_input && (*it)->i_id == i_id )
534         {
535             CACHE( i_id, (*it) );
536             return p_cached_item;
537         }
538         else if( b_input && (*it)->i_input_id == i_id )
539         {
540             ICACHE( i_id, (*it) );
541             return p_cached_item_bi;
542         }
543         if( (*it)->children.size() )
544         {
545             PLItem *childFound = FindInner( (*it), i_id, b_input );
546             if( childFound )
547             {
548                 if( b_input )
549                     ICACHE( i_id, childFound )
550                 else
551                     CACHE( i_id, childFound )
552                 return childFound;
553             }
554         }
555         it++;
556     }
557     return NULL;
558 }
559 #undef CACHE
560 #undef ICACHE
561
562
563 /************************* Updates handling *****************************/
564 void PLModel::customEvent( QEvent *event )
565 {
566     int type = event->type();
567     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
568         type != ItemDelete_Type && type != PLUpdate_Type )
569         return;
570
571     PLEvent *ple = static_cast<PLEvent *>(event);
572
573     if( type == ItemUpdate_Type )
574         ProcessInputItemUpdate( ple->i_id );
575     else if( type == ItemAppend_Type )
576         ProcessItemAppend( ple->p_add );
577     else if( type == ItemDelete_Type )
578         ProcessItemRemoval( ple->i_id );
579     else
580         rebuild();
581 }
582
583 /**** Events processing ****/
584 void PLModel::ProcessInputItemUpdate( int i_input_id )
585 {
586     if( i_input_id <= 0 ) return;
587     PLItem *item = FindByInput( rootItem, i_input_id );
588     if( item )
589         UpdateTreeItem( item, true );
590 }
591
592 void PLModel::ProcessItemRemoval( int i_id )
593 {
594     if( i_id <= 0 ) return;
595     if( i_id == i_cached_id ) i_cached_id = -1;
596     i_cached_input_id = -1;
597     PLItem *item = FindById( rootItem, i_id );
598     if( item )
599         item->remove( item );
600 }
601
602 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
603 {
604     playlist_item_t *p_item = NULL;
605     PLItem *newItem = NULL;
606     i_items_to_append--;
607     if( b_need_update ) return;
608
609     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
610     PL_LOCK;
611     if( !nodeItem ) goto end;
612
613     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
614     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
615     if( i_depth == 1 && p_item->p_parent &&
616                         p_item->p_parent->i_id != rootItem->i_id )
617         goto end;
618
619     newItem = new PLItem( p_item, nodeItem, this );
620     nodeItem->appendChild( newItem );
621     UpdateTreeItem( p_item, newItem, true );
622 end:
623     PL_UNLOCK;
624     return;
625 }
626
627
628 void PLModel::rebuild()
629 {
630     rebuild( NULL );
631 }
632
633 void PLModel::rebuild( playlist_item_t *p_root )
634 {
635     /* Remove callbacks before locking to avoid deadlocks */
636     delCallbacks();
637     /* Invalidate cache */
638     i_cached_id = i_cached_input_id = -1;
639
640     PL_LOCK;
641     /* Clear the tree */
642     if( rootItem )
643     {
644         beginRemoveRows( index( rootItem, 0 ), 0,
645                          rootItem->children.size() -1 );
646         qDeleteAll( rootItem->children );
647         rootItem->children.clear();
648         endRemoveRows();
649     }
650     if( p_root )
651     {
652         //if( rootItem ) delete rootItem;
653         rootItem = new PLItem( p_root, NULL, this );
654         rootItem->strings[0] = qtr("Name");
655         rootItem->strings[1] = qtr("Artist");
656         rootItem->strings[2] = qtr("Duration");
657     }
658     assert( rootItem );
659     /* Recreate from root */
660     UpdateNodeChildren( rootItem );
661     if( p_playlist->status.p_item )
662     {
663         PLItem *currentItem = FindByInput( rootItem,
664                                      p_playlist->status.p_item->p_input->i_id );
665         if( currentItem )
666         {
667             UpdateTreeItem( p_playlist->status.p_item, currentItem,
668                             true, false );
669         }
670     }
671     PL_UNLOCK;
672
673     /* And signal the view */
674     emit layoutChanged();
675     addCallbacks();
676 }
677
678 /* This function must be entered WITH the playlist lock */
679 void PLModel::UpdateNodeChildren( PLItem *root )
680 {
681     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
682     UpdateNodeChildren( p_node, root );
683 }
684
685 /* This function must be entered WITH the playlist lock */
686 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
687 {
688     for( int i = 0; i < p_node->i_children ; i++ )
689     {
690         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
691         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
692         root->appendChild( newItem, false );
693         UpdateTreeItem( newItem, false, true );
694         if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
695             UpdateNodeChildren( p_node->pp_children[i], newItem );
696     }
697 }
698
699 /* This function must be entered WITH the playlist lock */
700 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
701 {
702     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
703     UpdateTreeItem( p_item, item, signal, force );
704 }
705
706 /* This function must be entered WITH the playlist lock */
707 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
708                               bool signal, bool force )
709 {
710     if( !force && i_depth == 1 && p_item->p_parent &&
711                                  p_item->p_parent->i_id != rootItem->i_id )
712         return;
713     item->update( p_item, p_item == p_playlist->status.p_item );
714     if( signal )
715         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
716 }
717
718 /************************* Actions ******************************/
719
720 void PLModel::sendArt( QString url )
721 {
722     QString arturl = url.replace( "file://",QString("" ) );
723     emit artSet( arturl );
724 }
725
726 /**
727  * Deletion, here we have to do a ugly slow hack as we retrieve the full
728  * list of indexes to delete at once: when we delete a node and all of
729  * its children, we need to update the list.
730  * Todo: investigate whethere we can use ranges to be sure to delete all items?
731  */
732 void PLModel::doDelete( QModelIndexList selected )
733 {
734     for( int i = selected.size() -1 ; i >= 0; i-- )
735     {
736         QModelIndex index = selected[i];
737         if( index.column() != 0 ) continue;
738         PLItem *item = static_cast<PLItem*>(index.internalPointer());
739         if( item )
740         {
741             if( item->children.size() )
742                 recurseDelete( item->children, &selected );
743             doDeleteItem( item, &selected );
744         }
745     }
746 }
747
748 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
749 {
750     for( int i = children.size() - 1; i >= 0 ; i-- )
751     {
752         PLItem *item = children[i];
753         if( item->children.size() )
754             recurseDelete( item->children, fullList );
755         doDeleteItem( item, fullList );
756     }
757 }
758
759 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
760 {
761     QModelIndex deleteIndex = index( item, 0 );
762     fullList->removeAll( deleteIndex );
763
764     PL_LOCK;
765     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
766     if( !p_item )
767     {
768         PL_UNLOCK; return;
769     }
770     if( p_item->i_children == -1 )
771         playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
772     else
773         playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
774     /* And finally, remove it from the tree */
775     item->remove( item );
776     PL_UNLOCK;
777 }
778
779 /******* Volume III: Sorting and searching ********/
780 void PLModel::sort( int column, Qt::SortOrder order )
781 {
782     PL_LOCK;
783     playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
784     int i_mode;
785     switch( column )
786     {
787     case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
788     case 1: i_mode = SORT_ARTIST;break;
789     case 2: i_mode = SORT_DURATION; break;
790     default: i_mode = SORT_TITLE_NODES_FIRST; break;
791     }
792     if( p_root )
793         playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
794                                     order == Qt::AscendingOrder ? ORDER_NORMAL :
795                                                             ORDER_REVERSE );
796     PL_UNLOCK
797     rebuild();
798 }
799
800 void PLModel::search( QString search_text )
801 {
802     /** \todo Fire the search with a small delay ? */
803     PL_LOCK;
804     playlist_item_t *p_root = playlist_ItemGetById( p_playlist,rootItem->i_id );
805     assert( p_root );
806     char *psz_name = search_text.toUtf8().data();
807     playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
808     PL_UNLOCK;
809     rebuild();
810 }
811
812 /*********** Popup *********/
813 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
814 {
815     assert( index.isValid() );
816     PL_LOCK;
817     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
818                                                     itemId( index ) );
819     if( p_item )
820     {
821         i_popup_item = p_item->i_id;
822         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
823         PL_UNLOCK;
824         current_selection = list;
825         QMenu *menu = new QMenu;
826         menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
827         menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
828         menu->addSeparator();
829         menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
830         menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
831         menu->addSeparator();
832         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
833         if( p_item->i_children > -1 )
834         {
835             menu->addSeparator();
836             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
837             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
838         }
839         menu->popup( point );
840     }
841     else
842         PL_UNLOCK;
843 }
844
845 void PLModel::popupDel()
846 {
847     doDelete( current_selection );
848 }
849 void PLModel::popupPlay()
850 {
851     PL_LOCK;
852     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
853     activateItem( p_item );
854     PL_UNLOCK;
855 }
856
857 void PLModel::popupInfo()
858 {
859     fprintf( stderr, "Popup Info is NOT implemented\n" );
860 }
861
862 void PLModel::popupStream()
863 {
864     fprintf( stderr, "Stream not implemented\n" );
865 }
866 void PLModel::popupSave()
867 {
868     fprintf( stderr, "Save not implemented\n" );
869 }
870
871 /**********************************************************************
872  * Playlist callbacks
873  **********************************************************************/
874 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
875                             vlc_value_t oval, vlc_value_t nval, void *param )
876 {
877     PLModel *p_model = (PLModel *) param;
878     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
879     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
880     return VLC_SUCCESS;
881 }
882
883 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
884                          vlc_value_t oval, vlc_value_t nval, void *param )
885 {
886     PLModel *p_model = (PLModel *) param;
887     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
888     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
889     event = new PLEvent( ItemUpdate_Type, nval.i_int );
890     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
891     return VLC_SUCCESS;
892 }
893
894 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
895                         vlc_value_t oval, vlc_value_t nval, void *param )
896 {
897     PLModel *p_model = (PLModel *) param;
898     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
899     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
900     return VLC_SUCCESS;
901 }
902
903 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
904                         vlc_value_t oval, vlc_value_t nval, void *param )
905 {
906     PLModel *p_model = (PLModel *) param;
907     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
908     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
909     return VLC_SUCCESS;
910 }
911
912 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
913                          vlc_value_t oval, vlc_value_t nval, void *param )
914 {
915     PLModel *p_model = (PLModel *) param;
916     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
917     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
918
919     if( ++p_model->i_items_to_append >= 50 )
920     {
921 //        p_model->b_need_update = VLC_TRUE;
922 //        return VLC_SUCCESS;
923     }
924     PLEvent *event = new PLEvent(  p_add );
925     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
926     return VLC_SUCCESS;
927 }