]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.cpp
Process all playlist updates
[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     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_DEL), this, SLOT( popupDel() ) );
697         menu->addSeparator();
698         menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
699         menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
700         menu->addSeparator();
701         menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
702         if( p_item->i_children > -1 )
703         {
704             menu->addSeparator();
705             menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
706             menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
707         }
708         menu->popup( point );
709     }
710     else
711         PL_UNLOCK;
712 }
713
714 void PLModel::popupDel()
715 {
716     doDelete( current_selection );
717 }
718 void PLModel::popupPlay()
719 {
720     PL_LOCK;
721     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
722     activateItem( p_item );
723     PL_UNLOCK;
724 }
725
726 void PLModel::popupInfo()
727 {
728     fprintf( stderr, "Popup Info is NOT implemented\n" );
729 }
730
731 void PLModel::popupStream()
732 {
733     fprintf( stderr, "Stream not implemented\n" );
734 }
735 void PLModel::popupSave()
736 {
737     fprintf( stderr, "Save not implemented\n" );
738 }
739
740 /**********************************************************************
741  * Playlist callbacks
742  **********************************************************************/
743 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
744                             vlc_value_t oval, vlc_value_t nval, void *param )
745 {
746     PLModel *p_model = (PLModel *) param;
747 //    p_model->b_need_update = VLC_TRUE;
748     return VLC_SUCCESS;
749 }
750
751 static int PlaylistNext( 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( ItemUpdate_Type, oval.i_int );
756     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
757     event = new PLEvent( ItemUpdate_Type, nval.i_int );
758     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
759     return VLC_SUCCESS;
760 }
761
762 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
763                         vlc_value_t oval, vlc_value_t nval, void *param )
764 {
765     PLModel *p_model = (PLModel *) param;
766     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
767     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
768     return VLC_SUCCESS;
769 }
770
771 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
772                         vlc_value_t oval, vlc_value_t nval, void *param )
773 {
774     PLModel *p_model = (PLModel *) param;
775     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
776     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
777     return VLC_SUCCESS;
778 }
779
780 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
781                          vlc_value_t oval, vlc_value_t nval, void *param )
782 {
783     PLModel *p_model = (PLModel *) param;
784     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
785     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
786
787     if( ++p_model->i_items_to_append >= 50 )
788     {
789 //        p_model->b_need_update = VLC_TRUE;
790 //        return VLC_SUCCESS;
791     }
792     PLEvent *event = new PLEvent(  p_add );
793     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
794     return VLC_SUCCESS;
795 }