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