]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.cpp
Fix a refcount issue
[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 }
361 void PLModel::setRepeat( bool on )
362 {
363     var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
364 }
365 void PLModel::setRandom( bool on )
366 {
367     var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
368 }
369
370 /************************* Lookups *****************************/
371
372 PLItem *PLModel::FindById( PLItem *root, int i_id )
373 {
374     return FindInner( root, i_id, false );
375 }
376
377 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
378 {
379     return FindInner( root, i_id, true );
380 }
381
382 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
383 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
384
385 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
386 {
387     if( ( !b_input && i_cached_id == i_id) ||
388         ( b_input && i_cached_input_id ==i_id ) )
389     {
390         return b_input ? p_cached_item_bi : p_cached_item;
391     }
392
393     if( !b_input && root->i_id == i_id )
394     {
395         CACHE( i_id, root );
396         return root;
397     }
398     else if( b_input && root->i_input_id == i_id )
399     {
400         ICACHE( i_id, root );
401         return root;
402     }
403
404     QList<PLItem *>::iterator it = root->children.begin();
405     while ( it != root->children.end() )
406     {
407         if( !b_input && (*it)->i_id == i_id )
408         {
409             CACHE( i_id, (*it) );
410             return p_cached_item;
411         }
412         else if( b_input && (*it)->i_input_id == i_id )
413         {
414             ICACHE( i_id, (*it) );
415             return p_cached_item_bi;
416         }
417         if( (*it)->children.size() )
418         {
419             PLItem *childFound = FindInner( (*it), i_id, b_input );
420             if( childFound )
421             {
422                 if( b_input )
423                     ICACHE( i_id, childFound )
424                 else
425                     CACHE( i_id, childFound )
426                 return childFound;
427             }
428         }
429         it++;
430     }
431     return NULL;
432 }
433 #undef CACHE
434 #undef ICACHE
435
436
437 /************************* Updates handling *****************************/
438 void PLModel::customEvent( QEvent *event )
439 {
440     int type = event->type();
441     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
442         type != ItemDelete_Type )
443         return;
444
445     PLEvent *ple = static_cast<PLEvent *>(event);
446
447     if( type == ItemUpdate_Type )
448         ProcessInputItemUpdate( ple->i_id );
449     else if( type == ItemAppend_Type )
450         ProcessItemAppend( ple->p_add );
451     else
452         ProcessItemRemoval( ple->i_id );
453 }
454
455 /**** Events processing ****/
456 void PLModel::ProcessInputItemUpdate( int i_input_id )
457 {
458     if( i_input_id <= 0 ) return;
459     PLItem *item = FindByInput( rootItem, i_input_id );
460     if( item )
461         UpdateTreeItem( item, true );
462 }
463
464 void PLModel::ProcessItemRemoval( int i_id )
465 {
466     if( i_id <= 0 ) return;
467     if( i_id == i_cached_id ) i_cached_id = -1;
468     i_cached_input_id = -1;
469
470     PLItem *item = FindById( rootItem, i_id );
471     if( item )
472         item->remove( item );
473 }
474
475 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
476 {
477     playlist_item_t *p_item = NULL;
478     PLItem *newItem = NULL;
479     i_items_to_append--;
480     if( b_need_update ) return;
481
482     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
483     PL_LOCK;
484     if( !nodeItem ) goto end;
485
486     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
487     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
488     if( i_depth == 1 && p_item->p_parent &&
489                         p_item->p_parent->i_id != rootItem->i_id )
490         goto end;
491
492     newItem = new PLItem( p_item, nodeItem, this );
493     nodeItem->appendChild( newItem );
494     UpdateTreeItem( p_item, newItem, true );
495 end:
496     PL_UNLOCK;
497     return;
498 }
499
500
501 void PLModel::rebuild()
502 {
503     rebuild( NULL );
504 }
505
506 void PLModel::rebuild( playlist_item_t *p_root )
507 {
508     /* Remove callbacks before locking to avoid deadlocks */
509     delCallbacks();
510     /* Invalidate cache */
511     i_cached_id = i_cached_input_id = -1;
512
513     PL_LOCK;
514     /* Clear the tree */
515     if( rootItem )
516     {
517         beginRemoveRows( index( rootItem, 0 ), 0,
518                          rootItem->children.size() -1 );
519         qDeleteAll( rootItem->children );
520         rootItem->children.clear();
521         endRemoveRows();
522     }
523     if( p_root )
524     {
525         //if( rootItem ) delete rootItem;
526         rootItem = new PLItem( p_root, NULL, this );
527         rootItem->strings[0] = qtr("Name");
528         rootItem->strings[1] = qtr("Artist");
529         rootItem->strings[2] = qtr("Duration");
530     }
531     assert( rootItem );
532     /* Recreate from root */
533     UpdateNodeChildren( rootItem );
534     if( p_playlist->status.p_item )
535     {
536         PLItem *currentItem = FindByInput( rootItem,
537                                      p_playlist->status.p_item->p_input->i_id );
538         if( currentItem )
539         {
540             UpdateTreeItem( p_playlist->status.p_item, currentItem,
541                             true, false );
542         }
543     }
544     PL_UNLOCK;
545
546     /* And signal the view */
547     emit layoutChanged();
548     addCallbacks();
549 }
550
551 /* This function must be entered WITH the playlist lock */
552 void PLModel::UpdateNodeChildren( PLItem *root )
553 {
554     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
555     UpdateNodeChildren( p_node, root );
556 }
557
558 /* This function must be entered WITH the playlist lock */
559 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
560 {
561     for( int i = 0; i < p_node->i_children ; i++ )
562     {
563         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
564         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
565         root->appendChild( newItem, false );
566         UpdateTreeItem( newItem, false, true );
567         if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
568             UpdateNodeChildren( p_node->pp_children[i], newItem );
569     }
570 }
571
572 /* This function must be entered WITH the playlist lock */
573 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
574 {
575     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
576     UpdateTreeItem( p_item, item, signal, force );
577 }
578
579 /* This function must be entered WITH the playlist lock */
580 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
581                               bool signal, bool force )
582 {
583     if( !force && i_depth == 1 && p_item->p_parent &&
584                                  p_item->p_parent->i_id != rootItem->i_id )
585         return;
586     item->update( p_item, p_item == p_playlist->status.p_item );
587     if( signal )
588         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
589 }
590
591 /************************* Actions ******************************/
592
593 /**
594  * Deletion, here we have to do a ugly slow hack as we retrieve the full
595  * list of indexes to delete at once: when we delete a node and all of
596  * its children, we need to update the list.
597  * Todo: investigate whethere we can use ranges to be sure to delete all items?
598  */
599 void PLModel::doDelete( QModelIndexList selected )
600 {
601     for( int i = selected.size() -1 ; i >= 0; i-- )
602     {
603         QModelIndex index = selected[i];
604         if( index.column() != 0 ) continue;
605         PLItem *item = static_cast<PLItem*>(index.internalPointer());
606         if( item )
607         {
608             if( item->children.size() )
609                 recurseDelete( item->children, &selected );
610             doDeleteItem( item, &selected );
611         }
612     }
613 }
614
615 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
616 {
617     for( int i = children.size() - 1; i >= 0 ; i-- )
618     {
619         PLItem *item = children[i];
620         if( item->children.size() )
621             recurseDelete( item->children, fullList );
622         doDeleteItem( item, fullList );
623     }
624 }
625
626 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
627 {
628     QModelIndex deleteIndex = index( item, 0 );
629     fullList->removeAll( deleteIndex );
630
631     PL_LOCK;
632     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
633     if( !p_item )
634     {
635         PL_UNLOCK; return;
636     }
637     if( p_item->i_children == -1 )
638         playlist_DeleteAllFromInput( p_playlist, item->i_input_id );
639     else
640         playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
641     /* And finally, remove it from the tree */
642     item->remove( item );
643     PL_UNLOCK;
644 }
645
646 /******* Volume III: Sorting and searching ********/
647 void PLModel::sort( int column, Qt::SortOrder order )
648 {
649     PL_LOCK;
650     playlist_item_t *p_root = playlist_ItemGetById( p_playlist, rootItem->i_id );
651     int i_mode;
652     switch( column )
653     {
654     case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
655     case 1: i_mode = SORT_ARTIST;break;
656     case 2: i_mode = SORT_DURATION; break;
657     }
658     if( p_root )
659         playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
660                                     order == Qt::AscendingOrder ? ORDER_NORMAL :
661                                                             ORDER_REVERSE );
662     PL_UNLOCK
663     rebuild();
664 }
665
666 void PLModel::search( QString search_text )
667 {
668     /** \todo Fire the search with a small delay ? */
669     PL_LOCK;
670     playlist_item_t *p_root = playlist_ItemGetById( p_playlist,rootItem->i_id );
671     assert( p_root );
672     char *psz_name = search_text.toUtf8().data();
673     playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
674     PL_UNLOCK;
675     rebuild();
676 }
677
678 /*********** Popup *********/
679 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
680 {
681     assert( index.isValid() );
682     PL_LOCK;
683     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
684                                                     itemId( index ) );
685     if( p_item )
686     {
687         i_popup_item = p_item->i_id;
688         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
689         PL_UNLOCK;
690         current_selection = list;
691         QMenu *menu = new QMenu;
692         menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
693         menu->addAction( qfu(I_POP_PREPARSE), this, SLOT( popupPreparse() ) );
694         menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
695         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
696         if( p_item->i_children > -1 )
697         {
698             menu->addSeparator();
699             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
700             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
701         }
702         menu->popup( point );
703     }
704     else
705         PL_UNLOCK;
706 }
707
708 void PLModel::popupDel()
709 {
710     doDelete( current_selection );
711 }
712 void PLModel::popupPlay()
713 {
714     PL_LOCK;
715     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
716     activateItem( p_item );
717     PL_UNLOCK;
718 }
719
720 /**********************************************************************
721  * Playlist callbacks
722  **********************************************************************/
723 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
724                             vlc_value_t oval, vlc_value_t nval, void *param )
725 {
726     PLModel *p_model = (PLModel *) param;
727     p_model->b_need_update = VLC_TRUE;
728     return VLC_SUCCESS;
729 }
730
731 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
732                          vlc_value_t oval, vlc_value_t nval, void *param )
733 {
734     PLModel *p_model = (PLModel *) param;
735     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
736     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
737     event = new PLEvent( ItemUpdate_Type, nval.i_int );
738     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
739     return VLC_SUCCESS;
740 }
741
742 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
743                         vlc_value_t oval, vlc_value_t nval, void *param )
744 {
745     PLModel *p_model = (PLModel *) param;
746     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
747     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
748     return VLC_SUCCESS;
749 }
750
751 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
752                         vlc_value_t oval, vlc_value_t nval, void *param )
753 {
754     PLModel *p_model = (PLModel *) param;
755     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
756     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
757     return VLC_SUCCESS;
758 }
759
760 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
761                          vlc_value_t oval, vlc_value_t nval, void *param )
762 {
763     PLModel *p_model = (PLModel *) param;
764     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
765     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
766
767     if( ++p_model->i_items_to_append >= 50 )
768     {
769         p_model->b_need_update = VLC_TRUE;
770         return VLC_SUCCESS;
771     }
772     PLEvent *event = new PLEvent(  p_add );
773     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
774     return VLC_SUCCESS;
775 }