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