]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt4: store input_item_t* in plitem and handle metadata in model
[vlc] / modules / gui / qt4 / components / playlist / playlist_model.cpp
1 /*****************************************************************************
2  * playlist_model.cpp : Manage playlist model
3  ****************************************************************************
4  * Copyright (C) 2006-2007 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "qt4.hpp"
29 #include "dialogs_provider.hpp"
30 #include "components/playlist/playlist_model.hpp"
31 #include "dialogs/mediainfo.hpp"
32 #include "dialogs/playlist.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "pixmaps/types/type_unknown.xpm"
36
37 #include <assert.h>
38 #include <QIcon>
39 #include <QFont>
40 #include <QMenu>
41 #include <QApplication>
42 #include <QSettings>
43
44 #include "sorting.h"
45
46 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
47
48 static int PlaylistChanged( vlc_object_t *, const char *,
49                             vlc_value_t, vlc_value_t, void * );
50 static int PlaylistNext( 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 model implementation
59  *************************************************************************/
60
61 /*
62   This model is called two times, for the selector and the standard panel
63 */
64 PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
65                   intf_thread_t *_p_intf,   /* main Qt p_intf */
66                   playlist_item_t * p_root,
67                   /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
68                     and THEPL->p_root_category for SelectPL */
69                   int _i_depth,             /* -1 for StandPL, 1 for SelectPL */
70                   QObject *parent )         /* Basic Qt parent */
71                   : QAbstractItemModel( parent )
72 {
73     i_depth = _i_depth;
74     assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
75     p_intf            = _p_intf;
76     p_playlist        = _p_playlist;
77     i_cached_id       = -1;
78     i_cached_input_id = -1;
79     i_popup_item      = i_popup_parent = -1;
80
81     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
82
83     /* Icons initialization */
84 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
85     ADD_ICON( UNKNOWN , type_unknown_xpm );
86     ADD_ICON( FILE, ":/type/file" );
87     ADD_ICON( DIRECTORY, ":/type/directory" );
88     ADD_ICON( DISC, ":/type/disc" );
89     ADD_ICON( CDDA, ":/type/cdda" );
90     ADD_ICON( CARD, ":/type/capture-card" );
91     ADD_ICON( NET, ":/type/net" );
92     ADD_ICON( PLAYLIST, ":/type/playlist" );
93     ADD_ICON( NODE, ":/type/node" );
94 #undef ADD_ICON
95
96     rebuild( p_root );
97     CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
98             this, ProcessInputItemUpdate( input_item_t *) );
99     CONNECT( THEMIM, inputChanged( input_thread_t * ),
100             this, ProcessInputItemUpdate( input_thread_t* ) );
101 }
102
103 PLModel::~PLModel()
104 {
105     if(i_depth == -1)
106         getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
107     delCallbacks();
108     delete rootItem;
109 }
110
111 Qt::DropActions PLModel::supportedDropActions() const
112 {
113     return Qt::CopyAction; /* Why not Qt::MoveAction */
114 }
115
116 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
117 {
118     Qt::ItemFlags flags = QAbstractItemModel::flags( index );
119
120     PLItem *item = index.isValid() ?
121         static_cast<PLItem*>( index.internalPointer() ) :
122         rootItem;
123
124     int pl_input_id = p_playlist->p_local_category->p_input->i_id;
125     int ml_input_id = p_playlist->p_ml_category->p_input->i_id;
126
127     if( rootItem->i_id == p_playlist->p_root_onelevel->i_id
128           || rootItem->i_id == p_playlist->p_root_category->i_id )
129     {
130         if( item->i_input_id == pl_input_id
131             || item->i_input_id == ml_input_id )
132                 flags |= Qt::ItemIsDropEnabled;
133     }
134     else
135     {
136         if ( item->b_is_node &&
137             ( rootItem->i_input_id == pl_input_id ||
138             rootItem->i_input_id == ml_input_id ) )
139                 flags |= Qt::ItemIsDropEnabled;
140         flags |= Qt::ItemIsDragEnabled;
141     }
142
143     return flags;
144 }
145
146 /* A list of model indexes are a playlist */
147 QStringList PLModel::mimeTypes() const
148 {
149     QStringList types;
150     types << "vlc/playlist-item-id";
151     return types;
152 }
153
154 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
155 {
156     QMimeData *mimeData = new QMimeData();
157     QByteArray encodedData;
158     QDataStream stream( &encodedData, QIODevice::WriteOnly );
159     QModelIndexList list;
160
161     foreach( const QModelIndex &index, indexes ) {
162         if( index.isValid() && index.column() == 0 )
163             list.append(index);
164     }
165
166     qSort(list);
167
168     foreach( const QModelIndex &index, list ) {
169         stream << itemId( index );
170     }
171     mimeData->setData( "vlc/playlist-item-id", encodedData );
172     return mimeData;
173 }
174
175 /* Drop operation */
176 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
177                            int row, int column, const QModelIndex &parent )
178 {
179     if( data->hasFormat( "vlc/playlist-item-id" ) )
180     {
181         if( action == Qt::IgnoreAction )
182             return true;
183
184         PL_LOCK;
185
186         playlist_item_t *p_parent;
187
188         if( !parent.isValid())
189         {
190             if( row > -1)
191             {
192                 // dropped into top node
193                 p_parent = playlist_ItemGetById( p_playlist, rootItem->i_id );
194             }
195             else
196             {
197                 // dropped outside any item
198                 PL_UNLOCK;
199                 return true;
200             }
201         }
202         else
203         {
204             // dropped into/onto an item (depends on (row = -1) or (row > -1))
205             p_parent = playlist_ItemGetById( p_playlist, itemId ( parent ) );
206         }
207         if( !p_parent || p_parent->i_children == -1 )
208         {
209             PL_UNLOCK;
210             return false;
211         }
212
213         QByteArray encodedData = data->data( "vlc/playlist-item-id" );
214         QDataStream stream( &encodedData, QIODevice::ReadOnly );
215
216         /* easiest way to never miss the right index to move to is to
217         track the previously moved item */
218         playlist_item_t *p_target = NULL;
219
220         while( !stream.atEnd() )
221         {
222             int src_id;
223             stream >> src_id;
224             playlist_item_t *p_src = playlist_ItemGetById( p_playlist, src_id );
225
226             if( !p_src )
227             {
228                 PL_UNLOCK;
229                 return false;
230             }
231             if( !p_target )
232             {
233                 if(row == -1)
234                 {
235                     playlist_TreeMove( p_playlist, p_src, p_parent, 0 );
236                 }
237                 else {
238                     playlist_TreeMove( p_playlist, p_src, p_parent, row );
239                 }
240             }
241             else
242             {
243                 int i;
244                 for( i = 0 ; i< p_parent->i_children ; i++ )
245                     if( p_parent->pp_children[i] == p_target ) break;
246                 playlist_TreeMove( p_playlist, p_src, p_parent, i + 1 );
247             }
248             p_target = p_src;
249         }
250         PL_UNLOCK;
251         /*TODO: That's not a good idea to rebuild the playlist */
252         rebuild();
253     }
254     return true;
255 }
256
257 /* remove item with its id */
258 void PLModel::removeItem( int i_id )
259 {
260     PLItem *item = FindById( rootItem, i_id );
261     if( item ) item->remove( item );
262 }
263
264 /* callbacks and slots */
265 void PLModel::addCallbacks()
266 {
267     /* Some global changes happened -> Rebuild all */
268     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
269     /* We went to the next item
270     var_AddCallback( p_playlist, "item-current", PlaylistNext, this );
271     */
272     /* One item has been updated */
273     var_AddCallback( p_playlist, "playlist-item-append", ItemAppended, this );
274     var_AddCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
275 }
276
277 void PLModel::delCallbacks()
278 {
279     /*
280     var_DelCallback( p_playlist, "item-current", PlaylistNext, this );
281     */
282     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
283     var_DelCallback( p_playlist, "playlist-item-append", ItemAppended, this );
284     var_DelCallback( p_playlist, "playlist-item-deleted", ItemDeleted, this );
285 }
286
287 void PLModel::activateItem( const QModelIndex &index )
288 {
289     assert( index.isValid() );
290     PLItem *item = static_cast<PLItem*>(index.internalPointer());
291     assert( item );
292     PL_LOCK;
293     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
294     activateItem( p_item );
295     PL_UNLOCK;
296 }
297
298 /* Must be entered with lock */
299 void PLModel::activateItem( playlist_item_t *p_item )
300 {
301     if( !p_item ) return;
302     playlist_item_t *p_parent = p_item;
303     while( p_parent )
304     {
305         if( p_parent->i_id == rootItem->i_id ) break;
306         p_parent = p_parent->p_parent;
307     }
308     if( p_parent )
309         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
310                           p_parent, p_item );
311 }
312
313 /****************** Base model mandatory implementations *****************/
314 QVariant PLModel::data( const QModelIndex &index, int role ) const
315 {
316     if( !index.isValid() ) return QVariant();
317     PLItem *item = static_cast<PLItem*>(index.internalPointer());
318     if( role == Qt::DisplayRole )
319     {
320         int running_index = -1;
321         int columncount = 0;
322         int metadata = 1;
323
324         if( item->model->i_depth == DEPTH_SEL )
325             return QVariant( QString( qfu( item->p_input->psz_name ) ) );
326
327         while( metadata < COLUMN_END )
328         {
329             if( item->i_showflags & metadata )
330                 running_index++;
331             if( running_index == index.column() )
332                 break;
333             metadata <<= 1;
334         }
335
336         if( running_index != index.column() ) return QVariant();
337
338         QString returninfo;
339         if( metadata == COLUMN_NUMBER )
340             returninfo = QString::number( index.row() + 1 );
341         else
342         {
343             char *psz = psz_column_meta( item->p_input, metadata );
344             returninfo = QString( qfu( psz ) );
345             free( psz );
346         }
347         return QVariant( returninfo );
348     }
349     else if( role == Qt::DecorationRole && index.column() == 0  )
350     {
351         /* Use to segfault here because i_type wasn't always initialized */
352         if( item->i_type >= 0 )
353             return QVariant( PLModel::icons[item->i_type] );
354     }
355     else if( role == Qt::FontRole )
356     {
357         if( item->b_current == true )
358         {
359             QFont f; f.setBold( true ); return QVariant( f );
360         }
361     }
362     return QVariant();
363 }
364
365 bool PLModel::isCurrent( const QModelIndex &index )
366 {
367     assert( index.isValid() );
368     return static_cast<PLItem*>(index.internalPointer())->b_current;
369 }
370
371 int PLModel::itemId( const QModelIndex &index ) const
372 {
373     assert( index.isValid() );
374     return static_cast<PLItem*>(index.internalPointer())->i_id;
375 }
376
377 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
378                               int role ) const
379 {
380     int metadata=1;
381     int running_index=-1;
382     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
383         return QVariant();
384
385     if( i_depth == DEPTH_SEL ) return QVariant( QString("") );
386
387     while( metadata < COLUMN_END )
388     {
389         if( metadata & rootItem->i_showflags )
390             running_index++;
391         if( running_index == section )
392             break;
393         metadata <<= 1;
394     }
395
396     if( running_index != section ) return QVariant();
397
398     return QVariant( qfu( psz_column_title( metadata ) ) );
399 }
400
401 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
402                   const
403 {
404     PLItem *parentItem;
405     if( !parent.isValid() )
406         parentItem = rootItem;
407     else
408         parentItem = static_cast<PLItem*>(parent.internalPointer());
409
410     PLItem *childItem = parentItem->child( row );
411     if( childItem )
412         return createIndex( row, column, childItem );
413     else
414         return QModelIndex();
415 }
416
417 /* Return the index of a given item */
418 QModelIndex PLModel::index( PLItem *item, int column ) const
419 {
420     if( !item ) return QModelIndex();
421     const PLItem *parent = item->parent();
422     if( parent )
423         return createIndex( parent->children.lastIndexOf( item ),
424                             column, item );
425     return QModelIndex();
426 }
427
428 QModelIndex PLModel::parent( const QModelIndex &index ) const
429 {
430     if( !index.isValid() ) return QModelIndex();
431
432     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
433     if( !childItem )
434     {
435         msg_Err( p_playlist, "NULL CHILD" );
436         return QModelIndex();
437     }
438
439     PLItem *parentItem = childItem->parent();
440     if( !parentItem || parentItem == rootItem ) return QModelIndex();
441     if( !parentItem->parentItem )
442     {
443         msg_Err( p_playlist, "No parent parent, trying row 0 " );
444         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
445         return createIndex( 0, 0, parentItem );
446     }
447     QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
448     return ind;
449 }
450
451 int PLModel::columnCount( const QModelIndex &i) const
452 {
453     int columnCount=0;
454     int metadata=1;
455     if( i_depth == DEPTH_SEL ) return 1;
456
457     while( metadata < COLUMN_END )
458     {
459         if( metadata & rootItem->i_showflags )
460             columnCount++;
461         metadata <<= 1;
462     }
463     return columnCount;
464 }
465
466 int PLModel::childrenCount( const QModelIndex &parent ) const
467 {
468     return rowCount( parent );
469 }
470
471 int PLModel::rowCount( const QModelIndex &parent ) const
472 {
473     PLItem *parentItem;
474
475     if( !parent.isValid() )
476         parentItem = rootItem;
477     else
478         parentItem = static_cast<PLItem*>(parent.internalPointer());
479
480     return parentItem->childCount();
481 }
482
483 QStringList PLModel::selectedURIs()
484 {
485     QStringList lst;
486     for( int i = 0; i < current_selection.size(); i++ )
487     {
488         PL_LOCK;
489         PLItem *item = static_cast<PLItem*>
490                     (current_selection[i].internalPointer());
491         if( item )
492         {
493             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
494             if( p_item )
495             {
496                 char *psz = input_item_GetURI( p_item->p_input );
497                 if( psz )
498                 {
499                     lst.append( psz );
500                     free( psz );
501                 }
502             }
503         }
504         PL_UNLOCK;
505     }
506     return lst;
507 }
508
509 /************************* General playlist status ***********************/
510
511 bool PLModel::hasRandom()
512 {
513     return var_GetBool( p_playlist, "random" );
514 }
515 bool PLModel::hasRepeat()
516 {
517     return var_GetBool( p_playlist, "repeat" );
518 }
519 bool PLModel::hasLoop()
520 {
521     return var_GetBool( p_playlist, "loop" );
522 }
523 void PLModel::setLoop( bool on )
524 {
525     var_SetBool( p_playlist, "loop", on ? true:false );
526     config_PutInt( p_playlist, "loop", on ? 1: 0 );
527 }
528 void PLModel::setRepeat( bool on )
529 {
530     var_SetBool( p_playlist, "repeat", on ? true:false );
531     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
532 }
533 void PLModel::setRandom( bool on )
534 {
535     var_SetBool( p_playlist, "random", on ? true:false );
536     config_PutInt( p_playlist, "random", on ? 1: 0 );
537 }
538
539 /************************* Lookups *****************************/
540
541 PLItem *PLModel::FindById( PLItem *root, int i_id )
542 {
543     return FindInner( root, i_id, false );
544 }
545
546 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
547 {
548     return FindInner( root, i_id, true );
549 }
550
551 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
552 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
553
554 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
555 {
556     if( ( !b_input && i_cached_id == i_id) ||
557         ( b_input && i_cached_input_id ==i_id ) )
558     {
559         return b_input ? p_cached_item_bi : p_cached_item;
560     }
561
562     if( !b_input && root->i_id == i_id )
563     {
564         CACHE( i_id, root );
565         return root;
566     }
567     else if( b_input && root->i_input_id == i_id )
568     {
569         ICACHE( i_id, root );
570         return root;
571     }
572
573     QList<PLItem *>::iterator it = root->children.begin();
574     while ( it != root->children.end() )
575     {
576         if( !b_input && (*it)->i_id == i_id )
577         {
578             CACHE( i_id, (*it) );
579             return p_cached_item;
580         }
581         else if( b_input && (*it)->i_input_id == i_id )
582         {
583             ICACHE( i_id, (*it) );
584             return p_cached_item_bi;
585         }
586         if( (*it)->children.size() )
587         {
588             PLItem *childFound = FindInner( (*it), i_id, b_input );
589             if( childFound )
590             {
591                 if( b_input )
592                     ICACHE( i_id, childFound )
593                 else
594                     CACHE( i_id, childFound )
595                 return childFound;
596             }
597         }
598         it++;
599     }
600     return NULL;
601 }
602 #undef CACHE
603 #undef ICACHE
604
605
606 /************************* Updates handling *****************************/
607 void PLModel::customEvent( QEvent *event )
608 {
609     int type = event->type();
610     if( type != ItemAppend_Type &&
611         type != ItemDelete_Type && type != PLUpdate_Type )
612         return;
613
614     PLEvent *ple = static_cast<PLEvent *>(event);
615
616     if( type == ItemAppend_Type )
617         ProcessItemAppend( &ple->add );
618     else if( type == ItemDelete_Type )
619         ProcessItemRemoval( ple->i_id );
620     else
621         rebuild();
622 }
623
624 /**** Events processing ****/
625 void PLModel::ProcessInputItemUpdate( input_thread_t *p_input )
626 {
627     if( !p_input ) return;
628     ProcessInputItemUpdate( input_GetItem( p_input ) );
629     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
630     {
631         PLItem *item = FindByInput( rootItem, input_GetItem( p_input )->i_id );
632         emit currentChanged( index( item, 0 ) );
633     }
634 }
635 void PLModel::ProcessInputItemUpdate( input_item_t *p_item )
636 {
637     if( !p_item ||  p_item->i_id <= 0 ) return;
638     PLItem *item = FindByInput( rootItem, p_item->i_id );
639     if( item )
640     {
641         QPL_LOCK;
642         UpdateTreeItem( item, true );
643         QPL_UNLOCK;
644     }
645 }
646
647 void PLModel::ProcessItemRemoval( int i_id )
648 {
649     if( i_id <= 0 ) return;
650     if( i_id == i_cached_id ) i_cached_id = -1;
651     i_cached_input_id = -1;
652
653     removeItem( i_id );
654 }
655
656 void PLModel::ProcessItemAppend( const playlist_add_t *p_add )
657 {
658     playlist_item_t *p_item = NULL;
659     PLItem *newItem = NULL;
660
661     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
662     PL_LOCK;
663     if( !nodeItem ) goto end;
664
665     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
666     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
667     if( i_depth == DEPTH_SEL && p_item->p_parent &&
668                         p_item->p_parent->i_id != rootItem->i_id )
669         goto end;
670
671     newItem = new PLItem( p_item, nodeItem, this );
672     nodeItem->appendChild( newItem );
673     UpdateTreeItem( p_item, newItem, true );
674 end:
675     PL_UNLOCK;
676     return;
677 }
678
679
680 void PLModel::rebuild()
681 {
682     rebuild( NULL );
683 }
684
685 void PLModel::rebuild( playlist_item_t *p_root )
686 {
687     playlist_item_t* p_item;
688     /* Remove callbacks before locking to avoid deadlocks */
689     delCallbacks();
690     /* Invalidate cache */
691     i_cached_id = i_cached_input_id = -1;
692
693     PL_LOCK;
694     /* Clear the tree */
695     if( rootItem )
696     {
697         if( rootItem->children.size() )
698         {
699             beginRemoveRows( index( rootItem, 0 ), 0,
700                     rootItem->children.size() -1 );
701             qDeleteAll( rootItem->children );
702             rootItem->children.clear();
703             endRemoveRows();
704         }
705     }
706     if( p_root )
707     {
708         delete rootItem;
709         rootItem = new PLItem( p_root, getSettings(), this );
710     }
711     assert( rootItem );
712     /* Recreate from root */
713     UpdateNodeChildren( rootItem );
714     if( (p_item = playlist_CurrentPlayingItem(p_playlist)) )
715     {
716         PLItem *currentItem = FindByInput( rootItem,
717                                            p_item->p_input->i_id );
718         if( currentItem )
719         {
720             UpdateTreeItem( p_item, currentItem,
721                             true, false );
722         }
723     }
724     PL_UNLOCK;
725
726     /* And signal the view */
727     emit layoutChanged();
728     addCallbacks();
729 }
730
731 /* This function must be entered WITH the playlist lock */
732 void PLModel::UpdateNodeChildren( PLItem *root )
733 {
734     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
735     UpdateNodeChildren( p_node, root );
736 }
737
738 /* This function must be entered WITH the playlist lock */
739 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
740 {
741     for( int i = 0; i < p_node->i_children ; i++ )
742     {
743         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
744         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
745         root->appendChild( newItem, false );
746         UpdateTreeItem( newItem, false, true );
747         if( i_depth == DEPTH_PL && p_node->pp_children[i]->i_children != -1 )
748             UpdateNodeChildren( p_node->pp_children[i], newItem );
749     }
750 }
751
752 /* This function must be entered WITH the playlist lock */
753 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
754 {
755     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
756     UpdateTreeItem( p_item, item, signal, force );
757 }
758
759 /* This function must be entered WITH the playlist lock */
760 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
761                               bool signal, bool force )
762 {
763     if ( !p_item )
764         return;
765     if( !force && i_depth == DEPTH_SEL && p_item->p_parent &&
766                                  p_item->p_parent->i_id != rootItem->i_id )
767         return;
768     item->update( p_item, p_item == playlist_CurrentPlayingItem( p_playlist ) );
769     if( signal )
770         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
771 }
772
773 /************************* Actions ******************************/
774
775 /**
776  * Deletion, here we have to do a ugly slow hack as we retrieve the full
777  * list of indexes to delete at once: when we delete a node and all of
778  * its children, we need to update the list.
779  * Todo: investigate whethere we can use ranges to be sure to delete all items?
780  */
781 void PLModel::doDelete( QModelIndexList selected )
782 {
783     for( int i = selected.size() -1 ; i >= 0; i-- )
784     {
785         QModelIndex index = selected[i];
786         if( index.column() != 0 ) continue;
787         PLItem *item = static_cast<PLItem*>(index.internalPointer());
788         if( item )
789         {
790             if( item->children.size() )
791                 recurseDelete( item->children, &selected );
792             doDeleteItem( item, &selected );
793         }
794         if( i > selected.size() ) i = selected.size();
795     }
796 }
797
798 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
799 {
800     for( int i = children.size() - 1; i >= 0 ; i-- )
801     {
802         PLItem *item = children[i];
803         if( item->children.size() )
804             recurseDelete( item->children, fullList );
805         doDeleteItem( item, fullList );
806     }
807 }
808
809 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
810 {
811     QModelIndex deleteIndex = index( item, 0 );
812     fullList->removeAll( deleteIndex );
813
814     PL_LOCK;
815     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
816     if( !p_item )
817     {
818         PL_UNLOCK;
819         return;
820     }
821     if( p_item->i_children == -1 )
822         playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
823     else
824         playlist_NodeDelete( p_playlist, p_item, true, false );
825     /* And finally, remove it from the tree */
826     item->remove( item );
827     PL_UNLOCK;
828 }
829
830 /******* Volume III: Sorting and searching ********/
831 void PLModel::sort( int column, Qt::SortOrder order )
832 {
833     int i_index = -1;
834     int i_flag = 0;
835
836     int i_column = 1;
837     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
838     {
839         if( ( shownFlags() & i_column ) )
840             i_index++;
841         if( column == i_index )
842         {
843             i_flag = i_column;
844             goto next;
845         }
846     }
847
848
849 next:
850     PL_LOCK;
851     {
852         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
853                                                         rootItem->i_id );
854         if( p_root && i_flag )
855         {
856             playlist_RecursiveNodeSort( p_playlist, p_root,
857                                         i_column_sorting( i_flag ),
858                                         order == Qt::AscendingOrder ?
859                                             ORDER_NORMAL : ORDER_REVERSE );
860         }
861     }
862     PL_UNLOCK;
863     rebuild();
864 }
865
866 void PLModel::search( const QString& search_text )
867 {
868     /** \todo Fire the search with a small delay ? */
869     PL_LOCK;
870     {
871         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
872                                                         rootItem->i_id );
873         assert( p_root );
874         const char *psz_name = search_text.toUtf8().data();
875         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
876     }
877     PL_UNLOCK;
878     rebuild();
879 }
880
881 /*********** Popup *********/
882 void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
883 {
884     assert( index.isValid() );
885     PL_LOCK;
886     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, itemId( index ) );
887     if( p_item )
888     {
889         i_popup_item = p_item->i_id;
890         i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
891         PL_UNLOCK;
892         current_selection = list;
893         QMenu *menu = new QMenu;
894         menu->addAction( qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
895         menu->addAction( qtr(I_POP_DEL), this, SLOT( popupDel() ) );
896         menu->addSeparator();
897         menu->addAction( qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
898         menu->addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
899         menu->addSeparator();
900         menu->addAction( qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
901         if( p_item->i_children > -1 )
902         {
903             menu->addSeparator();
904             menu->addAction( qtr(I_POP_SORT), this, SLOT( popupSort() ) );
905             menu->addAction( qtr(I_POP_ADD), this, SLOT( popupAdd() ) );
906         }
907         menu->addSeparator();
908         menu->addAction( qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
909         menu->popup( point );
910     }
911     else
912         PL_UNLOCK;
913 }
914
915
916 void PLModel::viewchanged( int meta )
917 {
918     assert( meta );
919     int _meta = meta;
920     if( rootItem )
921     {
922         int index=-1;
923         while( _meta )
924         {
925             index++;
926             _meta >>= 1;
927         }
928
929         /* UNUSED        emit layoutAboutToBeChanged(); */
930         index = __MIN( index, columnCount() );
931         QModelIndex parent = createIndex( 0, 0, rootItem );
932
933         if( rootItem->i_showflags & meta )
934             /* Removing columns */
935         {
936             beginRemoveColumns( parent, index, index+1 );
937             rootItem->i_showflags &= ~( meta );
938             getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
939             endRemoveColumns();
940         }
941         else
942         {
943             /* Adding columns */
944             beginInsertColumns( parent, index, index+1 );
945             rootItem->i_showflags |= meta;
946             getSettings()->setValue( "qt-pl-showflags", rootItem->i_showflags );
947             endInsertColumns();
948         }
949         emit columnsChanged( meta );
950         rebuild();
951     }
952 }
953
954 void PLModel::popupDel()
955 {
956     doDelete( current_selection );
957 }
958
959 void PLModel::popupPlay()
960 {
961     PL_LOCK;
962     {
963         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
964                                                         i_popup_item );
965         activateItem( p_item );
966     }
967     PL_UNLOCK;
968 }
969
970 void PLModel::popupInfo()
971 {
972     PL_LOCK;
973     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
974                                                     i_popup_item );
975     if( p_item )
976     {
977         input_item_t* p_input = p_item->p_input;
978         vlc_gc_incref( p_input );
979         PL_UNLOCK;
980         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
981         vlc_gc_decref( p_input );
982         mid->setParent( PlaylistDialog::getInstance( p_intf ),
983                         Qt::Dialog );
984         mid->show();
985     }
986 }
987
988 void PLModel::popupStream()
989 {
990     QStringList mrls = selectedURIs();
991     if( !mrls.isEmpty() )
992         THEDP->streamingDialog( NULL, mrls[0], false );
993
994 }
995
996 void PLModel::popupSave()
997 {
998     QStringList mrls = selectedURIs();
999     if( !mrls.isEmpty() )
1000         THEDP->streamingDialog( NULL, mrls[0] );
1001 }
1002
1003 #include <QUrl>
1004 #include <QFileInfo>
1005 #include <QDesktopServices>
1006 void PLModel::popupExplore()
1007 {
1008     PL_LOCK;
1009     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1010                                                     i_popup_item );
1011     if( p_item )
1012     {
1013        input_item_t *p_input = p_item->p_input;
1014        char *psz_meta = input_item_GetURI( p_input );
1015        PL_UNLOCK;
1016        if( psz_meta )
1017        {
1018            const char *psz_access;
1019            const char *psz_demux;
1020            char  *psz_path;
1021            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1022
1023            if( EMPTY_STR( psz_access ) ||
1024                !strncasecmp( psz_access, "file", 4 ) ||
1025                !strncasecmp( psz_access, "dire", 4 ) )
1026            {
1027                QFileInfo info( qfu( psz_meta ) );
1028                QDesktopServices::openUrl(
1029                                QUrl::fromLocalFile( info.absolutePath() ) );
1030            }
1031            free( psz_meta );
1032        }
1033     }
1034     else
1035         PL_UNLOCK;
1036 }
1037
1038 /**********************************************************************
1039  * Playlist callbacks
1040  **********************************************************************/
1041 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1042                             vlc_value_t oval, vlc_value_t nval, void *param )
1043 {
1044     PLModel *p_model = (PLModel *) param;
1045     PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
1046     QApplication::postEvent( p_model, event );
1047     return VLC_SUCCESS;
1048 }
1049
1050 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1051                          vlc_value_t oval, vlc_value_t nval, void *param )
1052 {
1053     PLModel *p_model = (PLModel *) param;
1054     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
1055     QApplication::postEvent( p_model, event );
1056     event = new PLEvent( ItemUpdate_Type, nval.i_int );
1057     QApplication::postEvent( p_model, event );
1058     return VLC_SUCCESS;
1059 }
1060
1061 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1062                         vlc_value_t oval, vlc_value_t nval, void *param )
1063 {
1064     PLModel *p_model = (PLModel *) param;
1065     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
1066     QApplication::postEvent( p_model, event );
1067     return VLC_SUCCESS;
1068 }
1069
1070 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1071                          vlc_value_t oval, vlc_value_t nval, void *param )
1072 {
1073     PLModel *p_model = (PLModel *) param;
1074     const playlist_add_t *p_add = (playlist_add_t *)nval.p_address;
1075     PLEvent *event = new PLEvent( p_add );
1076     QApplication::postEvent( p_model, event );
1077     return VLC_SUCCESS;
1078 }
1079