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