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