]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt4 : change slightly the debug message.
[vlc] / modules / gui / qt4 / components / playlist / playlist_model.cpp
1 /*****************************************************************************
2  * playlist_model.cpp : Manage playlist model
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <assert.h>
25 #include <QIcon>
26 #include <QFont>
27 #include <QMenu>
28 #include <QApplication>
29
30 #include "qt4.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "dialogs/mediainfo.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "pixmaps/type_unknown.xpm"
36
37 #define DEPTH_PL -1
38 #define DEPTH_SEL 1
39 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
40
41 static int PlaylistChanged( vlc_object_t *, const char *,
42                             vlc_value_t, vlc_value_t, void * );
43 static int PlaylistNext( vlc_object_t *, const char *,
44                          vlc_value_t, vlc_value_t, void * );
45 static int ItemChanged( vlc_object_t *, const char *,
46                         vlc_value_t, vlc_value_t, void * );
47 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
48                          vlc_value_t oval, vlc_value_t nval, void *param );
49 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
50                         vlc_value_t oval, vlc_value_t nval, void *param );
51
52 /*************************************************************************
53  * Playlist item implementation
54  *************************************************************************/
55
56 /*
57    Playlist item is just a wrapper, an abstraction of the playlist_item
58    in order to be managed by PLModel
59
60    PLItem have a parent, and id and a input Id
61 */
62
63
64 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
65 {
66     parentItem = parent;          /* Can be NULL, but only for the rootItem */
67     i_id       = _i_id;           /* Playlist item specific id */
68     i_input_id = _i_input_id;     /* Identifier of the input */
69     model      = m;               /* PLModel (QAbsmodel) */
70     i_type     = -1;              /* Item type - Avoid segfault */
71     b_current  = false;
72
73     assert( model );
74
75     /* No parent, should be the main one */
76     if( parentItem == NULL )
77     {
78         i_showflags = config_GetInt( model->p_intf, "qt-pl-showflags" );
79         updateview();
80     }
81     else
82     {
83         i_showflags = parentItem->i_showflags;
84         //Add empty string and update() handles data appending
85         item_col_strings.append( "" );
86     }
87     msg_Dbg( model->p_intf, "PLItem created of type: %i", model->i_depth );
88 }
89
90 /*
91    Constructors
92    Call the above function init
93    So far the first constructor isn't used...
94    */
95 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
96 {
97     init( _i_id, _i_input_id, parent, m );
98 }
99
100 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
101 {
102     init( p_item->i_id, p_item->p_input->i_id, parent, m );
103 }
104
105 PLItem::~PLItem()
106 {
107     qDeleteAll( children );
108     children.clear();
109 }
110
111 /* Column manager */
112 void PLItem::updateview()
113 {
114     item_col_strings.clear();
115
116     if( model->i_depth == 1 )  /* Selector Panel */
117     {
118         item_col_strings.append( "" );
119         return;
120     }
121
122     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
123     {
124         if( i_showflags & i_index )
125         {
126             switch( i_index )
127             {
128             case VLC_META_ENGINE_ARTIST:
129                 item_col_strings.append( qtr( VLC_META_ARTIST ) );
130                 break;
131             case VLC_META_ENGINE_TITLE:
132                 item_col_strings.append( qtr( VLC_META_TITLE ) );
133                 break;
134             case VLC_META_ENGINE_DESCRIPTION:
135                 item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
136                 break;
137             case VLC_META_ENGINE_DURATION:
138                 item_col_strings.append( qtr( "Duration" ) );
139                 break;
140             case VLC_META_ENGINE_GENRE:
141                 item_col_strings.append( qtr( VLC_META_GENRE ) );
142                 break;
143             case VLC_META_ENGINE_COLLECTION:
144                 item_col_strings.append( qtr( VLC_META_COLLECTION ) );
145                 break;
146             case VLC_META_ENGINE_SEQ_NUM:
147                 item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
148                 break;
149             case VLC_META_ENGINE_RATING:
150                 item_col_strings.append( qtr( VLC_META_RATING ) );
151                 break;
152             default:
153                 break;
154             }
155         }
156     }
157 }
158
159 /* So far signal is always true.
160    Using signal false would not call PLModel... Why ?
161  */
162 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
163 {
164     if( signal )
165         model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
166     children.insert( i_pos, item );
167     if( signal )
168         model->endInsertRows();
169 }
170
171 void PLItem::remove( PLItem *removed )
172 {
173     if( model->i_depth == DEPTH_SEL || parentItem )
174     {
175         int i_index = parentItem->children.indexOf( removed );
176         model->beginRemoveRows( model->index( parentItem, 0 ),
177                                 i_index, i_index );
178         parentItem->children.removeAt( i_index );
179         model->endRemoveRows();
180     }
181 }
182
183 /* This function is used to get one's parent's row number in the model */
184 int PLItem::row() const
185 {
186     if( parentItem )
187         return parentItem->children.indexOf( const_cast<PLItem*>(this) ); // Why this ? I don't think we ever inherit PLItem
188     return 0;
189 }
190
191 /* update the PL Item, get the good names and so on */
192 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
193 {
194     char psz_duration[MSTRTIME_MAX_SIZE];
195     char *psz_meta;
196
197     assert( p_item->p_input->i_id == i_input_id );
198
199     i_type = p_item->p_input->i_type;
200     b_current = iscurrent;
201
202     item_col_strings.clear();
203
204     if( model->i_depth == 1 )  /* Selector Panel */
205     {
206         item_col_strings.append( qfu( p_item->p_input->psz_name ) );
207         return;
208     }
209
210 #define ADD_META( item, meta ) \
211     psz_meta = input_item_Get ## meta ( item->p_input ); \
212     item_col_strings.append( qfu( psz_meta ) ); \
213     free( psz_meta );
214
215     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
216     {
217         if( parentItem->i_showflags & i_index )
218         {
219             switch( i_index )
220             {
221             case VLC_META_ENGINE_ARTIST:
222                 ADD_META( p_item, Artist );
223                 break;
224             case VLC_META_ENGINE_TITLE:
225                 char *psz_title, *psz_name;
226                 psz_title = input_item_GetTitle( p_item->p_input );
227                 psz_name = input_item_GetName( p_item->p_input );
228                 if( psz_title )
229                 {
230                     ADD_META( p_item, Title );
231                 }
232                 else if( psz_name )
233                 {
234                     item_col_strings.append( qfu( psz_name ) );
235                 }
236                 free( psz_title );
237                 free( psz_name );
238                 break;
239             case VLC_META_ENGINE_DESCRIPTION:
240                 ADD_META( p_item, Description );
241                 break;
242             case VLC_META_ENGINE_DURATION:
243                 secstotimestr( psz_duration,
244                     input_item_GetDuration( p_item->p_input ) / 1000000 );
245                 item_col_strings.append( QString( psz_duration ) );
246                 break;
247             case VLC_META_ENGINE_GENRE:
248                 ADD_META( p_item, Genre );
249                 break;
250             case VLC_META_ENGINE_COLLECTION:
251                 ADD_META( p_item, Album );
252                 break;
253             case VLC_META_ENGINE_SEQ_NUM:
254                 ADD_META( p_item, TrackNum );
255                 break;
256             case VLC_META_ENGINE_RATING:
257                 ADD_META( p_item, Rating );
258             default:
259                 break;
260             }
261         }
262
263     }
264 #undef ADD_META
265 }
266
267 /*************************************************************************
268  * Playlist model implementation
269  *************************************************************************/
270
271 /*
272   This model is called two times, for the selector and the standard panel
273 */
274 PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
275                   intf_thread_t *_p_intf,   /* main Qt p_intf */
276                   playlist_item_t * p_root,
277                   /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
278                     and THEPL->p_root_category for SelectPL */
279                   int _i_depth,             /* -1 for StandPL, 1 for SelectPL */
280                   QObject *parent )         /* Basic Qt parent */
281                   : QAbstractItemModel( parent )
282 {
283     i_depth = _i_depth;
284     assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
285     p_intf            = _p_intf;
286     p_playlist        = _p_playlist;
287     i_items_to_append = 0;
288     b_need_update     = false;
289     i_cached_id       = -1;
290     i_cached_input_id = -1;
291     i_popup_item      = i_popup_parent = -1;
292
293     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
294
295     /* Icons initialization */
296 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
297     ADD_ICON( UNKNOWN , type_unknown_xpm );
298     ADD_ICON( FILE, ":/pixmaps/type_file.png" );
299     ADD_ICON( DIRECTORY, ":/pixmaps/type_directory.png" );
300     ADD_ICON( DISC, ":/pixmaps/disc_16px.png" );
301     ADD_ICON( CDDA, ":/pixmaps/cdda_16px.png" );
302     ADD_ICON( CARD, ":/pixmaps/capture-card_16px.png" );
303     ADD_ICON( NET, ":/pixmaps/type_net.png" );
304     ADD_ICON( PLAYLIST, ":/pixmaps/type_playlist.png" );
305     ADD_ICON( NODE, ":/pixmaps/type_node.png" );
306 #undef ADD_ICON
307
308     addCallbacks();
309     rebuild( p_root );
310 }
311
312 PLModel::~PLModel()
313 {
314     delCallbacks();
315     delete rootItem;
316 }
317
318 Qt::DropActions PLModel::supportedDropActions() const
319 {
320     return Qt::CopyAction; /* Why not Qt::MoveAction */
321 }
322
323 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
324 {
325     Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
326     if( index.isValid() )
327         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
328     else
329         return Qt::ItemIsDropEnabled | defaultFlags;
330 }
331
332 /* A list of model indexes are a playlist */
333 QStringList PLModel::mimeTypes() const
334 {
335     QStringList types;
336     types << "vlc/playlist-item-id";
337     return types;
338 }
339
340 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
341 {
342     QMimeData *mimeData = new QMimeData();
343     QByteArray encodedData;
344     QDataStream stream( &encodedData, QIODevice::WriteOnly );
345
346     foreach( QModelIndex index, indexes ) {
347         if( index.isValid() && index.column() == 0 )
348             stream << itemId( index );
349     }
350     mimeData->setData( "vlc/playlist-item-id", encodedData );
351     return mimeData;
352 }
353
354 /* Drop operation */
355 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
356                            int row, int column, const QModelIndex &target )
357 {
358     if( data->hasFormat( "vlc/playlist-item-id" ) )
359     {
360         if( action == Qt::IgnoreAction )
361             return true;
362
363         PLItem *targetItem;
364         if( target.isValid() )
365             targetItem = static_cast<PLItem*>( target.internalPointer() );
366         else
367             targetItem = rootItem;
368
369         QByteArray encodedData = data->data( "vlc/playlist-item-id" );
370         QDataStream stream( &encodedData, QIODevice::ReadOnly );
371
372         PLItem *newParentItem;
373         while( !stream.atEnd() )
374         {
375             int i;
376             int srcId;
377             stream >> srcId;
378
379             PL_LOCK;
380             playlist_item_t *p_target =
381                         playlist_ItemGetById( p_playlist, targetItem->i_id,
382                                               VLC_TRUE );
383             playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
384                                                            VLC_TRUE );
385
386             if( !p_target || !p_src )
387             {
388                 PL_UNLOCK;
389                 return false;
390             }
391             if( p_target->i_children == -1 ) /* A leaf */
392             {
393                 PLItem *parentItem = targetItem->parent();
394                 assert( parentItem );
395                 playlist_item_t *p_parent =
396                          playlist_ItemGetById( p_playlist, parentItem->i_id,
397                                                VLC_TRUE );
398                 if( !p_parent )
399                 {
400                     PL_UNLOCK;
401                     return false;
402                 }
403                 for( i = 0 ; i< p_parent->i_children ; i++ )
404                     if( p_parent->pp_children[i] == p_target ) break;
405                 playlist_TreeMove( p_playlist, p_src, p_parent, i );
406                 newParentItem = parentItem;
407             }
408             else
409             {
410                 /* \todo: if we drop on a top-level node, use copy instead ? */
411                 playlist_TreeMove( p_playlist, p_src, p_target, 0 );
412                 i = 0;
413                 newParentItem = targetItem;
414             }
415             /* Remove from source */
416             PLItem *srcItem = FindById( rootItem, p_src->i_id );
417             // We dropped on the source selector. Ask the dialog to forward
418             // to the main view
419             if( !srcItem )
420             {
421                 emit shouldRemove( p_src->i_id );
422             }
423             else
424                 srcItem->remove( srcItem );
425
426             /* Display at new destination */
427             PLItem *newItem = new PLItem( p_src, newParentItem, this );
428             newParentItem->insertChild( newItem, i, true );
429             UpdateTreeItem( p_src, newItem, true );
430             if( p_src->i_children != -1 )
431                 UpdateNodeChildren( newItem );
432             PL_UNLOCK;
433         }
434     }
435     return true;
436 }
437
438 /* remove item with its id */
439 void PLModel::removeItem( int i_id )
440 {
441     PLItem *item = FindById( rootItem, i_id );
442     if( item ) item->remove( item );
443 }
444
445 /* callbacks and slots */
446 void PLModel::addCallbacks()
447 {
448     /* Some global changes happened -> Rebuild all */
449     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
450     /* We went to the next item */
451     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
452     /* One item has been updated */
453     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
454     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
455     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
456 }
457
458 void PLModel::delCallbacks()
459 {
460     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
461     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
462     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
463     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
464     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
465 }
466
467 void PLModel::activateItem( const QModelIndex &index )
468 {
469     assert( index.isValid() );
470     PLItem *item = static_cast<PLItem*>(index.internalPointer());
471     assert( item );
472     PL_LOCK;
473     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
474                                                     VLC_TRUE);
475     activateItem( p_item );
476     PL_UNLOCK;
477 }
478
479 /* Must be entered with lock */
480 void PLModel::activateItem( playlist_item_t *p_item )
481 {
482     if( !p_item ) return;
483     playlist_item_t *p_parent = p_item;
484     while( p_parent )
485     {
486         if( p_parent->i_id == rootItem->i_id ) break;
487         p_parent = p_parent->p_parent;
488     }
489     if( p_parent )
490         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
491                           p_parent, p_item );
492 }
493
494 /****************** Base model mandatory implementations *****************/
495 QVariant PLModel::data( const QModelIndex &index, int role ) const
496 {
497     if( !index.isValid() ) return QVariant();
498     PLItem *item = static_cast<PLItem*>(index.internalPointer());
499     if( role == Qt::DisplayRole )
500     {
501         return QVariant( item->columnString( index.column() ) );
502     }
503     else if( role == Qt::DecorationRole && index.column() == 0  )
504     {
505         /* Use to segfault here because i_type wasn't always initialized */
506         if( item->i_type >= 0 )
507             return QVariant( PLModel::icons[item->i_type] );
508     }
509     else if( role == Qt::FontRole )
510     {
511         if( item->b_current == true )
512         {
513             QFont f; f.setBold( true ); return QVariant( f );
514         }
515     }
516     return QVariant();
517 }
518
519 bool PLModel::isCurrent( const QModelIndex &index )
520 {
521     assert( index.isValid() );
522     return static_cast<PLItem*>(index.internalPointer())->b_current;
523 }
524
525 int PLModel::itemId( const QModelIndex &index ) const
526 {
527     assert( index.isValid() );
528     return static_cast<PLItem*>(index.internalPointer())->i_id;
529 }
530
531 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
532                               int role ) const
533 {
534     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
535             return QVariant( rootItem->columnString( section ) );
536     return QVariant();
537 }
538
539 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
540                   const
541 {
542     PLItem *parentItem;
543     if( !parent.isValid() )
544         parentItem = rootItem;
545     else
546         parentItem = static_cast<PLItem*>(parent.internalPointer());
547
548     PLItem *childItem = parentItem->child( row );
549     if( childItem )
550         return createIndex( row, column, childItem );
551     else
552         return QModelIndex();
553 }
554
555 /* Return the index of a given item */
556 QModelIndex PLModel::index( PLItem *item, int column ) const
557 {
558     if( !item ) return QModelIndex();
559     const PLItem *parent = item->parent();
560     if( parent )
561         return createIndex( parent->children.lastIndexOf( item ),
562                             column, item );
563     return QModelIndex();
564 }
565
566 QModelIndex PLModel::parent( const QModelIndex &index ) const
567 {
568     if( !index.isValid() ) return QModelIndex();
569
570     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
571     if( !childItem )
572     {
573         msg_Err( p_playlist, "NULL CHILD" );
574         return QModelIndex();
575     }
576
577     PLItem *parentItem = childItem->parent();
578     if( !parentItem || parentItem == rootItem ) return QModelIndex();
579     if( !parentItem->parentItem )
580     {
581         msg_Err( p_playlist, "No parent parent, trying row 0 " );
582         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
583         return createIndex( 0, 0, parentItem );
584     }
585     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
586     return ind;
587 }
588
589 int PLModel::columnCount( const QModelIndex &i) const
590 {
591     return rootItem->item_col_strings.count();
592 }
593
594 int PLModel::childrenCount( const QModelIndex &parent ) const
595 {
596     return rowCount( parent );
597 }
598
599 int PLModel::rowCount( const QModelIndex &parent ) const
600 {
601     PLItem *parentItem;
602
603     if( !parent.isValid() )
604         parentItem = rootItem;
605     else
606         parentItem = static_cast<PLItem*>(parent.internalPointer());
607
608     return parentItem->childCount();
609 }
610
611 /************************* General playlist status ***********************/
612
613 bool PLModel::hasRandom()
614 {
615     if( var_GetBool( p_playlist, "random" ) ) return true;
616     return false;
617 }
618 bool PLModel::hasRepeat()
619 {
620     if( var_GetBool( p_playlist, "repeat" ) ) return true;
621     return false;
622 }
623 bool PLModel::hasLoop()
624 {
625     if( var_GetBool( p_playlist, "loop" ) ) return true;
626     return false;
627 }
628 void PLModel::setLoop( bool on )
629 {
630     var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
631     config_PutInt( p_playlist, "loop", on ? 1: 0 );
632 }
633 void PLModel::setRepeat( bool on )
634 {
635     var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
636     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
637 }
638 void PLModel::setRandom( bool on )
639 {
640     var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
641     config_PutInt( p_playlist, "random", on ? 1: 0 );
642 }
643
644 /************************* Lookups *****************************/
645
646 PLItem *PLModel::FindById( PLItem *root, int i_id )
647 {
648     return FindInner( root, i_id, false );
649 }
650
651 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
652 {
653     return FindInner( root, i_id, true );
654 }
655
656 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
657 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
658
659 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
660 {
661     if( ( !b_input && i_cached_id == i_id) ||
662         ( b_input && i_cached_input_id ==i_id ) )
663     {
664         return b_input ? p_cached_item_bi : p_cached_item;
665     }
666
667     if( !b_input && root->i_id == i_id )
668     {
669         CACHE( i_id, root );
670         return root;
671     }
672     else if( b_input && root->i_input_id == i_id )
673     {
674         ICACHE( i_id, root );
675         return root;
676     }
677
678     QList<PLItem *>::iterator it = root->children.begin();
679     while ( it != root->children.end() )
680     {
681         if( !b_input && (*it)->i_id == i_id )
682         {
683             CACHE( i_id, (*it) );
684             return p_cached_item;
685         }
686         else if( b_input && (*it)->i_input_id == i_id )
687         {
688             ICACHE( i_id, (*it) );
689             return p_cached_item_bi;
690         }
691         if( (*it)->children.size() )
692         {
693             PLItem *childFound = FindInner( (*it), i_id, b_input );
694             if( childFound )
695             {
696                 if( b_input )
697                     ICACHE( i_id, childFound )
698                 else
699                     CACHE( i_id, childFound )
700                 return childFound;
701             }
702         }
703         it++;
704     }
705     return NULL;
706 }
707 #undef CACHE
708 #undef ICACHE
709
710
711 /************************* Updates handling *****************************/
712 void PLModel::customEvent( QEvent *event )
713 {
714     int type = event->type();
715     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
716         type != ItemDelete_Type && type != PLUpdate_Type )
717         return;
718
719     PLEvent *ple = static_cast<PLEvent *>(event);
720
721     if( type == ItemUpdate_Type )
722         ProcessInputItemUpdate( ple->i_id );
723     else if( type == ItemAppend_Type )
724         ProcessItemAppend( ple->p_add );
725     else if( type == ItemDelete_Type )
726         ProcessItemRemoval( ple->i_id );
727     else
728         rebuild();
729 }
730
731 /**** Events processing ****/
732 void PLModel::ProcessInputItemUpdate( int i_input_id )
733 {
734     if( i_input_id <= 0 ) return;
735     PLItem *item = FindByInput( rootItem, i_input_id );
736     if( item )
737         UpdateTreeItem( item, true );
738 }
739
740 void PLModel::ProcessItemRemoval( int i_id )
741 {
742     if( i_id <= 0 ) return;
743     if( i_id == i_cached_id ) i_cached_id = -1;
744     i_cached_input_id = -1;
745
746     removeItem( i_id );
747 }
748
749 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
750 {
751     playlist_item_t *p_item = NULL;
752     PLItem *newItem = NULL;
753     i_items_to_append--;
754     if( b_need_update ) return;
755
756     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
757     PL_LOCK;
758     if( !nodeItem ) goto end;
759
760     p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
761     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
762     if( i_depth == DEPTH_SEL && p_item->p_parent &&
763                         p_item->p_parent->i_id != rootItem->i_id )
764         goto end;
765
766     newItem = new PLItem( p_item, nodeItem, this );
767     nodeItem->appendChild( newItem );
768     UpdateTreeItem( p_item, newItem, true );
769 end:
770     PL_UNLOCK;
771     return;
772 }
773
774
775 void PLModel::rebuild()
776 {
777     rebuild( NULL );
778 }
779
780 void PLModel::rebuild( playlist_item_t *p_root )
781 {
782     /* Remove callbacks before locking to avoid deadlocks */
783     delCallbacks();
784     /* Invalidate cache */
785     i_cached_id = i_cached_input_id = -1;
786
787     PL_LOCK;
788     /* Clear the tree */
789     if( rootItem )
790     {
791         if( rootItem->children.size() )
792         {
793             beginRemoveRows( index( rootItem, 0 ), 0,
794                     rootItem->children.size() -1 );
795             qDeleteAll( rootItem->children );
796             rootItem->children.clear();
797             endRemoveRows();
798         }
799     }
800     if( p_root )
801     {
802         //if( rootItem ) delete rootItem;
803         rootItem = new PLItem( p_root, NULL, this );
804     }
805     assert( rootItem );
806     /* Recreate from root */
807     UpdateNodeChildren( rootItem );
808     if( p_playlist->status.p_item )
809     {
810         PLItem *currentItem = FindByInput( rootItem,
811                                      p_playlist->status.p_item->p_input->i_id );
812         if( currentItem )
813         {
814             UpdateTreeItem( p_playlist->status.p_item, currentItem,
815                             true, false );
816         }
817     }
818     PL_UNLOCK;
819
820     /* And signal the view */
821     emit layoutChanged();
822     addCallbacks();
823 }
824
825 /* This function must be entered WITH the playlist lock */
826 void PLModel::UpdateNodeChildren( PLItem *root )
827 {
828     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
829                                                     VLC_TRUE );
830     UpdateNodeChildren( p_node, root );
831 }
832
833 /* This function must be entered WITH the playlist lock */
834 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
835 {
836     for( int i = 0; i < p_node->i_children ; i++ )
837     {
838         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
839         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
840         root->appendChild( newItem, false );
841         UpdateTreeItem( newItem, false, true );
842         if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
843             UpdateNodeChildren( p_node->pp_children[i], newItem );
844     }
845 }
846
847 /* This function must be entered WITH the playlist lock */
848 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
849 {
850     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
851                                                     VLC_TRUE );
852     UpdateTreeItem( p_item, item, signal, force );
853 }
854
855 /* This function must be entered WITH the playlist lock */
856 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
857                               bool signal, bool force )
858 {
859     if ( !p_item )
860         return;
861     if( !force && i_depth == DEPTH_SEL && p_item->p_parent &&
862                                  p_item->p_parent->i_id != rootItem->i_id )
863         return;
864     item->update( p_item, p_item == p_playlist->status.p_item );
865     if( signal )
866         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
867 }
868
869 /************************* Actions ******************************/
870
871 /**
872  * Deletion, here we have to do a ugly slow hack as we retrieve the full
873  * list of indexes to delete at once: when we delete a node and all of
874  * its children, we need to update the list.
875  * Todo: investigate whethere we can use ranges to be sure to delete all items?
876  */
877 void PLModel::doDelete( QModelIndexList selected )
878 {
879     for( int i = selected.size() -1 ; i >= 0; i-- )
880     {
881         QModelIndex index = selected[i];
882         if( index.column() != 0 ) continue;
883         PLItem *item = static_cast<PLItem*>(index.internalPointer());
884         if( item )
885         {
886             if( item->children.size() )
887                 recurseDelete( item->children, &selected );
888             doDeleteItem( item, &selected );
889         }
890     }
891 }
892
893 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
894 {
895     for( int i = children.size() - 1; i >= 0 ; i-- )
896     {
897         PLItem *item = children[i];
898         if( item->children.size() )
899             recurseDelete( item->children, fullList );
900         doDeleteItem( item, fullList );
901     }
902 }
903
904 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
905 {
906     QModelIndex deleteIndex = index( item, 0 );
907     fullList->removeAll( deleteIndex );
908
909     PL_LOCK;
910     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
911                                                     VLC_TRUE );
912     if( !p_item )
913     {
914         PL_UNLOCK; return;
915     }
916     if( p_item->i_children == -1 )
917         playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
918     else
919         playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
920     /* And finally, remove it from the tree */
921     item->remove( item );
922     PL_UNLOCK;
923 }
924
925 /******* Volume III: Sorting and searching ********/
926 void PLModel::sort( int column, Qt::SortOrder order )
927 {
928     PL_LOCK;
929     {
930         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
931                                                         rootItem->i_id,
932                                                         VLC_TRUE );
933         int i_mode;
934         switch( column )
935         {
936         case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
937         case 1: i_mode = SORT_ARTIST;break;
938         case 2: i_mode = SORT_DURATION; break;
939         default: i_mode = SORT_TITLE_NODES_FIRST; break;
940         }
941         if( p_root )
942             playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
943                                         order == Qt::AscendingOrder ?
944                                             ORDER_NORMAL : ORDER_REVERSE );
945     }
946     PL_UNLOCK;
947     rebuild();
948 }
949
950 void PLModel::search( QString search_text )
951 {
952     /** \todo Fire the search with a small delay ? */
953     PL_LOCK;
954     {
955         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
956                                                         rootItem->i_id,
957                                                         VLC_TRUE );
958         assert( p_root );
959         char *psz_name = search_text.toUtf8().data();
960         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
961     }
962     PL_UNLOCK;
963     rebuild();
964 }
965
966 /*********** Popup *********/
967 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
968 {
969     assert( index.isValid() );
970     PL_LOCK;
971     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
972                                                     itemId( index ), VLC_TRUE );
973     if( p_item )
974     {
975         i_popup_item = p_item->i_id;
976         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
977         PL_UNLOCK;
978         current_selection = list;
979         QMenu *menu = new QMenu;
980         menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
981         menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
982         menu->addSeparator();
983         menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
984         menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
985         menu->addSeparator();
986         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
987         if( p_item->i_children > -1 )
988         {
989             menu->addSeparator();
990             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
991             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
992         }
993 #ifdef WIN32
994         menu->addSeparator();
995         menu->addAction( qfu( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
996 #endif
997         menu->popup( point );
998     }
999     else
1000         PL_UNLOCK;
1001 }
1002
1003
1004 void PLModel::viewchanged( int meta )
1005 {
1006    if( rootItem )
1007    {
1008        int index=0;
1009        switch( meta )
1010        {
1011        case VLC_META_ENGINE_TITLE:
1012            index=0; break;
1013        case VLC_META_ENGINE_DURATION:
1014            index=1; break;
1015        case VLC_META_ENGINE_ARTIST:
1016            index=2; break;
1017        case VLC_META_ENGINE_GENRE:
1018            index=3; break;
1019        case VLC_META_ENGINE_COPYRIGHT:
1020            index=4; break;
1021        case VLC_META_ENGINE_COLLECTION:
1022            index=5; break;
1023        case VLC_META_ENGINE_SEQ_NUM:
1024            index=6; break;
1025        case VLC_META_ENGINE_DESCRIPTION:
1026            index=7; break;
1027        default:
1028            break;
1029        }
1030        /* UNUSED        emit layoutAboutToBeChanged(); */
1031        index = __MIN( index , rootItem->item_col_strings.count() );
1032        QModelIndex parent = createIndex( 0, 0, rootItem );
1033
1034        if( rootItem->i_showflags & meta )
1035            /* Removing columns */
1036        {
1037            beginRemoveColumns( parent, index, index+1 );
1038            rootItem->i_showflags &= ~( meta );
1039            rootItem->updateview();
1040            endRemoveColumns();
1041        }
1042        else
1043        {
1044            /* Adding columns */
1045            beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
1046            rootItem->i_showflags |= meta;
1047            rootItem->updateview();
1048            endInsertColumns();
1049        }
1050        rebuild();
1051        config_PutInt( p_intf, "qt-pl-showflags", rootItem->i_showflags );
1052        config_SaveConfigFile( p_intf, NULL );
1053    }
1054 }
1055
1056 void PLModel::popupDel()
1057 {
1058     doDelete( current_selection );
1059 }
1060 void PLModel::popupPlay()
1061 {
1062     PL_LOCK;
1063     {
1064         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1065                                                         i_popup_item,VLC_TRUE );
1066         activateItem( p_item );
1067     }
1068     PL_UNLOCK;
1069 }
1070
1071 void PLModel::popupInfo()
1072 {
1073     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1074                                                     i_popup_item,
1075                                                     VLC_TRUE );
1076     if( p_item )
1077     {
1078         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_item->p_input );
1079         mid->show();
1080     }
1081 }
1082
1083 void PLModel::popupStream()
1084 {
1085      msg_Err( p_playlist, "Stream not implemented" );
1086 }
1087
1088 void PLModel::popupSave()
1089 {
1090      msg_Err( p_playlist, "Save not implemented" );
1091 }
1092
1093 #ifdef WIN32
1094 #include <shellapi.h>
1095 void PLModel::popupExplore()
1096 {
1097     ShellExecuteW( NULL, L"explore", L"C:\\", NULL, NULL, SW_SHOWNORMAL );
1098 }
1099 #endif
1100
1101 /**********************************************************************
1102  * Playlist callbacks
1103  **********************************************************************/
1104 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1105                             vlc_value_t oval, vlc_value_t nval, void *param )
1106 {
1107     PLModel *p_model = (PLModel *) param;
1108     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
1109     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1110     return VLC_SUCCESS;
1111 }
1112
1113 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1114                          vlc_value_t oval, vlc_value_t nval, void *param )
1115 {
1116     PLModel *p_model = (PLModel *) param;
1117     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
1118     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1119     event = new PLEvent( ItemUpdate_Type, nval.i_int );
1120     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1121     return VLC_SUCCESS;
1122 }
1123
1124 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1125                         vlc_value_t oval, vlc_value_t nval, void *param )
1126 {
1127     PLModel *p_model = (PLModel *) param;
1128     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
1129     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1130     return VLC_SUCCESS;
1131 }
1132
1133 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1134                         vlc_value_t oval, vlc_value_t nval, void *param )
1135 {
1136     PLModel *p_model = (PLModel *) param;
1137     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
1138     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1139     return VLC_SUCCESS;
1140 }
1141
1142 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1143                          vlc_value_t oval, vlc_value_t nval, void *param )
1144 {
1145     PLModel *p_model = (PLModel *) param;
1146     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
1147     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1148
1149     if( ++p_model->i_items_to_append >= 50 )
1150     {
1151 //        p_model->b_need_update = VLC_TRUE;
1152 //        return VLC_SUCCESS;
1153     }
1154     PLEvent *event = new PLEvent(  p_add );
1155     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
1156     return VLC_SUCCESS;
1157 }
1158