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