]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt4: don't cache current
[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  *          Ilkka Ollakkka <ileoo (at) videolan dot org>
9  *          Jakob Leben <jleben@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31 #include "dialogs_provider.hpp"
32 #include "components/playlist/playlist_model.hpp"
33 #include "dialogs/mediainfo.hpp"
34 #include "dialogs/playlist.hpp"
35 #include <vlc_intf_strings.h>
36
37 #include "pixmaps/types/type_unknown.xpm"
38
39 #include <assert.h>
40 #include <QIcon>
41 #include <QFont>
42 #include <QMenu>
43 #include <QApplication>
44 #include <QSettings>
45 #include <QUrl>
46 #include <QFileInfo>
47 #include <QDesktopServices>
48 #include <QInputDialog>
49
50 #include "sorting.h"
51
52 #define I_NEW_DIR \
53     I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
54 #define I_NEW_DIR_NAME \
55     I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
56                      N_( "Enter name for new folder:" ) )
57
58 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
59
60 /*************************************************************************
61  * Playlist model implementation
62  *************************************************************************/
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                   QObject *parent )         /* Basic Qt parent */
68                   : QAbstractItemModel( parent )
69 {
70     p_intf            = _p_intf;
71     p_playlist        = _p_playlist;
72     i_cached_id       = -1;
73     i_cached_input_id = -1;
74     i_popup_item      = i_popup_parent = -1;
75     sortingMenu       = NULL;
76
77     rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
78
79     /* Icons initialization */
80 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
81     ADD_ICON( UNKNOWN , type_unknown_xpm );
82     ADD_ICON( FILE, ":/type/file" );
83     ADD_ICON( DIRECTORY, ":/type/directory" );
84     ADD_ICON( DISC, ":/type/disc" );
85     ADD_ICON( CDDA, ":/type/cdda" );
86     ADD_ICON( CARD, ":/type/capture-card" );
87     ADD_ICON( NET, ":/type/net" );
88     ADD_ICON( PLAYLIST, ":/type/playlist" );
89     ADD_ICON( NODE, ":/type/node" );
90 #undef ADD_ICON
91
92     rebuild( p_root );
93     DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
94              this, processInputItemUpdate( input_item_t *) );
95     DCONNECT( THEMIM, inputChanged( input_thread_t * ),
96              this, processInputItemUpdate( input_thread_t* ) );
97     CONNECT( THEMIM, playlistItemAppended( int, int ),
98              this, processItemAppend( int, int ) );
99     CONNECT( THEMIM, playlistItemRemoved( int ),
100              this, processItemRemoval( int ) );
101 }
102
103 PLModel::~PLModel()
104 {
105     delete rootItem;
106     delete sortingMenu;
107 }
108
109 Qt::DropActions PLModel::supportedDropActions() const
110 {
111     return Qt::CopyAction | Qt::MoveAction;
112 }
113
114 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
115 {
116     Qt::ItemFlags flags = QAbstractItemModel::flags( index );
117
118     PLItem *item = index.isValid() ? getItem( index ) : rootItem;
119
120     if( canEdit() )
121     {
122         PL_LOCK;
123         playlist_item_t *plItem =
124             playlist_ItemGetById( p_playlist, item->i_id );
125
126         if ( plItem && ( plItem->i_children > -1 ) )
127             flags |= Qt::ItemIsDropEnabled;
128
129         PL_UNLOCK;
130
131     }
132     flags |= Qt::ItemIsDragEnabled;
133
134     return flags;
135 }
136
137 QStringList PLModel::mimeTypes() const
138 {
139     QStringList types;
140     types << "vlc/qt-input-items";
141     return types;
142 }
143
144 bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
145 {
146     if( !i1.isValid() || !i2.isValid() ) return false;
147     PLItem *item1 = static_cast<PLItem*>( i1.internalPointer() );
148     PLItem *item2 = static_cast<PLItem*>( i2.internalPointer() );
149     if( item1->parent() == item2->parent() ) return i1.row() < i2.row();
150     else return *item1 < *item2;
151 }
152
153 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
154 {
155     PlMimeData *plMimeData = new PlMimeData();
156     QModelIndexList list;
157
158     foreach( const QModelIndex &index, indexes ) {
159         if( index.isValid() && index.column() == 0 )
160             list.append(index);
161     }
162
163     qSort(list.begin(), list.end(), modelIndexLessThen);
164
165     PLItem *item = NULL;
166     foreach( const QModelIndex &index, list ) {
167         if( item )
168         {
169             PLItem *testee = getItem( index );
170             while( testee->parent() )
171             {
172                 if( testee->parent() == item ||
173                     testee->parent() == item->parent() ) break;
174                 testee = testee->parent();
175             }
176             if( testee->parent() == item ) continue;
177             item = getItem( index );
178         }
179         else
180             item = getItem( index );
181
182         plMimeData->appendItem( item->p_input );
183     }
184
185     return plMimeData;
186 }
187
188 /* Drop operation */
189 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
190                            int row, int column, const QModelIndex &parent )
191 {
192     bool copy = action == Qt::CopyAction;
193     if( !copy && action != Qt::MoveAction )
194         return true;
195
196     const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
197     if( plMimeData )
198     {
199         if( copy )
200             dropAppendCopy( plMimeData, getItem( parent ), row );
201         else
202             dropMove( plMimeData, getItem( parent ), row );
203     }
204     return true;
205 }
206
207 void PLModel::dropAppendCopy( const PlMimeData *plMimeData, PLItem *target, int pos )
208 {
209     PL_LOCK;
210
211     playlist_item_t *p_parent =
212             playlist_ItemGetByInput( p_playlist, target->p_input );
213     if( !p_parent ) return;
214
215     if( pos == -1 ) pos = PLAYLIST_END;
216
217     QList<input_item_t*> inputItems = plMimeData->inputItems();
218
219     foreach( input_item_t* p_input, inputItems )
220     {
221         playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
222         if( !p_item ) continue;
223         pos = playlist_NodeAddCopy( p_playlist, p_item, p_parent, pos );
224     }
225
226     PL_UNLOCK;
227 }
228
229 void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
230 {
231     QList<input_item_t*> inputItems = plMimeData->inputItems();
232     QList<PLItem*> model_items;
233     playlist_item_t *pp_items[inputItems.size()];
234
235     PL_LOCK;
236
237     playlist_item_t *p_parent =
238         playlist_ItemGetByInput( p_playlist, target->p_input );
239
240     if( !p_parent || row > p_parent->i_children )
241     {
242         PL_UNLOCK; return;
243     }
244
245     int new_pos = row == -1 ? p_parent->i_children : row;
246     int model_pos = new_pos;
247     int i = 0;
248
249     foreach( input_item_t *p_input, inputItems )
250     {
251         playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
252         if( !p_item ) continue;
253
254         PLItem *item = findByInput( rootItem, p_input->i_id );
255         if( !item ) continue;
256
257         /* Better not try to move a node into itself.
258            Abort the whole operation in that case,
259            because it is ambiguous. */
260         PLItem *climber = target;
261         while( climber )
262         {
263             if( climber == item )
264             {
265                 PL_UNLOCK; return;
266             }
267             climber = climber->parentItem;
268         }
269
270         if( item->parentItem == target &&
271             target->children.indexOf( item ) < new_pos )
272                 model_pos--;
273
274         model_items.append( item );
275         pp_items[i] = p_item;
276         i++;
277     }
278
279     if( model_items.isEmpty() )
280     {
281         PL_UNLOCK; return;
282     }
283
284     playlist_TreeMoveMany( p_playlist, i, pp_items, p_parent, new_pos );
285
286     PL_UNLOCK;
287
288     foreach( PLItem *item, model_items )
289         takeItem( item );
290
291     insertChildren( target, model_items, model_pos );
292 }
293
294 /* remove item with its id */
295 void PLModel::removeItem( int i_id )
296 {
297     PLItem *item = findById( rootItem, i_id );
298     removeItem( item );
299 }
300
301 void PLModel::activateItem( const QModelIndex &index )
302 {
303     assert( index.isValid() );
304     PLItem *item = getItem( index );
305     assert( item );
306     PL_LOCK;
307     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
308     activateItem( p_item );
309     PL_UNLOCK;
310 }
311
312 /* Must be entered with lock */
313 void PLModel::activateItem( playlist_item_t *p_item )
314 {
315     if( !p_item ) return;
316     playlist_item_t *p_parent = p_item;
317     while( p_parent )
318     {
319         if( p_parent->i_id == rootItem->i_id ) break;
320         p_parent = p_parent->p_parent;
321     }
322     if( p_parent )
323         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
324                           p_parent, p_item );
325 }
326
327 /****************** Base model mandatory implementations *****************/
328 QVariant PLModel::data( const QModelIndex &index, int role ) const
329 {
330     if( !index.isValid() ) return QVariant();
331     PLItem *item = getItem( index );
332     if( role == Qt::DisplayRole )
333     {
334         int metadata = columnToMeta( index.column() );
335         if( metadata == COLUMN_END ) return QVariant();
336
337         QString returninfo;
338         if( metadata == COLUMN_NUMBER )
339             returninfo = QString::number( index.row() + 1 );
340         else
341         {
342             char *psz = psz_column_meta( item->p_input, metadata );
343             returninfo = qfu( psz );
344             free( psz );
345         }
346         return QVariant( returninfo );
347     }
348     else if( role == Qt::DecorationRole && index.column() == 0  )
349     {
350         /* Used to segfault here because i_type wasn't always initialized */
351         return QVariant( PLModel::icons[item->p_input->i_type] );
352     }
353     else if( role == Qt::FontRole )
354     {
355         if( isCurrent( index ) )
356         {
357             QFont f; f.setBold( true ); return QVariant( f );
358         }
359     }
360     else if( role == Qt::BackgroundRole && isCurrent( index ) )
361     {
362         return QVariant( QBrush( Qt::gray ) );
363     }
364     else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
365     else if( role == IsLeafNodeRole )
366     {
367         QVariant isLeaf;
368         PL_LOCK;
369         playlist_item_t *plItem =
370             playlist_ItemGetById( p_playlist, item->i_id );
371
372         if( plItem )
373             isLeaf = plItem->i_children == -1;
374
375         PL_UNLOCK;
376         return isLeaf;
377     }
378     else if( role == IsCurrentsParentNodeRole )
379     {
380         return QVariant( isParent( index, currentIndex() ) );
381     }
382     return QVariant();
383 }
384
385 /* Seek from current index toward the top and see if index is one of parent nodes */
386 bool PLModel::isParent( const QModelIndex &index, const QModelIndex &current ) const
387 {
388     if( !index.isValid() )
389         return false;
390
391     if( index == current )
392         return true;
393
394     if( !current.isValid() || !current.parent().isValid() )
395         return false;
396
397     return isParent( index, current.parent() );
398 }
399
400 bool PLModel::isCurrent( const QModelIndex &index ) const
401 {
402     return getItem( index )->p_input == THEMIM->currentInputItem();
403 }
404
405 int PLModel::itemId( const QModelIndex &index ) const
406 {
407     return getItem( index )->i_id;
408 }
409
410 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
411                               int role ) const
412 {
413     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
414         return QVariant();
415
416     int meta_col = columnToMeta( section );
417
418     if( meta_col == COLUMN_END ) return QVariant();
419
420     return QVariant( qfu( psz_column_title( meta_col ) ) );
421 }
422
423 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
424                   const
425 {
426     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
427
428     PLItem *childItem = parentItem->child( row );
429     if( childItem )
430         return createIndex( row, column, childItem );
431     else
432         return QModelIndex();
433 }
434
435 QModelIndex PLModel::index( int i_id, int c )
436 {
437   return index( findById( rootItem, i_id ), c );
438 }
439
440 /* Return the index of a given item */
441 QModelIndex PLModel::index( PLItem *item, int column ) const
442 {
443     if( !item ) return QModelIndex();
444     const PLItem *parent = item->parent();
445     if( parent )
446         return createIndex( parent->children.lastIndexOf( item ),
447                             column, item );
448     return QModelIndex();
449 }
450
451 QModelIndex PLModel::currentIndex() const
452 {
453     input_thread_t *p_input_thread = THEMIM->getInput();
454     if( !p_input_thread ) return QModelIndex();
455     PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
456     return index( item, 0 );
457 }
458
459 QModelIndex PLModel::parent( const QModelIndex &index ) const
460 {
461     if( !index.isValid() ) return QModelIndex();
462
463     PLItem *childItem = getItem( index );
464     if( !childItem )
465     {
466         msg_Err( p_playlist, "NULL CHILD" );
467         return QModelIndex();
468     }
469
470     PLItem *parentItem = childItem->parent();
471     if( !parentItem || parentItem == rootItem ) return QModelIndex();
472     if( !parentItem->parentItem )
473     {
474         msg_Err( p_playlist, "No parent parent, trying row 0 " );
475         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
476         return createIndex( 0, 0, parentItem );
477     }
478     return createIndex(parentItem->row(), 0, parentItem);
479 }
480
481 int PLModel::columnCount( const QModelIndex &i) const
482 {
483     return columnFromMeta( COLUMN_END );
484 }
485
486 int PLModel::rowCount( const QModelIndex &parent ) const
487 {
488     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
489     return parentItem->childCount();
490 }
491
492 QStringList PLModel::selectedURIs()
493 {
494     QStringList lst;
495     for( int i = 0; i < current_selection.size(); i++ )
496     {
497         PLItem *item = getItem( current_selection[i] );
498         if( item )
499         {
500             PL_LOCK;
501             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
502             if( p_item )
503             {
504                 char *psz = input_item_GetURI( p_item->p_input );
505                 if( psz )
506                 {
507                     lst.append( qfu(psz) );
508                     free( psz );
509                 }
510             }
511             PL_UNLOCK;
512         }
513     }
514     return lst;
515 }
516
517
518 /************************* Lookups *****************************/
519
520 PLItem *PLModel::findById( PLItem *root, int i_id )
521 {
522     return findInner( root, i_id, false );
523 }
524
525 PLItem *PLModel::findByInput( PLItem *root, int i_id )
526 {
527     PLItem *result = findInner( root, i_id, true );
528     return result;
529 }
530
531 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
532 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
533
534 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
535 {
536     if( !root ) return NULL;
537     if( ( !b_input && i_cached_id == i_id) ||
538         ( b_input && i_cached_input_id ==i_id ) )
539     {
540         return b_input ? p_cached_item_bi : p_cached_item;
541     }
542
543     if( !b_input && root->i_id == i_id )
544     {
545         CACHE( i_id, root );
546         return root;
547     }
548     else if( b_input && root->p_input->i_id == i_id )
549     {
550         ICACHE( i_id, root );
551         return root;
552     }
553
554     QList<PLItem *>::iterator it = root->children.begin();
555     while ( it != root->children.end() )
556     {
557         if( !b_input && (*it)->i_id == i_id )
558         {
559             CACHE( i_id, (*it) );
560             return p_cached_item;
561         }
562         else if( b_input && (*it)->p_input->i_id == i_id )
563         {
564             ICACHE( i_id, (*it) );
565             return p_cached_item_bi;
566         }
567         if( (*it)->children.size() )
568         {
569             PLItem *childFound = findInner( (*it), i_id, b_input );
570             if( childFound )
571             {
572                 if( b_input )
573                     ICACHE( i_id, childFound )
574                 else
575                     CACHE( i_id, childFound )
576                 return childFound;
577             }
578         }
579         it++;
580     }
581     return NULL;
582 }
583 #undef CACHE
584 #undef ICACHE
585
586 int PLModel::columnToMeta( int _column )
587 {
588     int meta = 1;
589     int column = 0;
590
591     while( column != _column && meta != COLUMN_END )
592     {
593         meta <<= 1;
594         column++;
595     }
596
597     return meta;
598 }
599
600 int PLModel::columnFromMeta( int meta_col )
601 {
602     int meta = 1;
603     int column = 0;
604
605     while( meta != meta_col && meta != COLUMN_END )
606     {
607         meta <<= 1;
608         column++;
609     }
610
611     return column;
612 }
613
614 bool PLModel::canEdit() const
615 {
616   return (
617     rootItem != NULL &&
618     (
619       rootItem->p_input == p_playlist->p_playing->p_input ||
620       (
621         p_playlist->p_media_library &&
622         rootItem->p_input == p_playlist->p_media_library->p_input
623       )
624     )
625   );
626 }
627 /************************* Updates handling *****************************/
628
629 /**** Events processing ****/
630 void PLModel::processInputItemUpdate( input_thread_t *p_input )
631 {
632     if( !p_input ) return;
633     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
634     {
635         PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
636         if( item ) emit currentChanged( index( item, 0 ) );
637     }
638     processInputItemUpdate( input_GetItem( p_input ) );
639 }
640
641 void PLModel::processInputItemUpdate( input_item_t *p_item )
642 {
643     if( !p_item ||  p_item->i_id <= 0 ) return;
644     PLItem *item = findByInput( rootItem, p_item->i_id );
645     if( item )
646         updateTreeItem( item );
647 }
648
649 void PLModel::processItemRemoval( int i_id )
650 {
651     if( i_id <= 0 ) return;
652     removeItem( i_id );
653 }
654
655 void PLModel::processItemAppend( int i_item, int i_parent )
656 {
657     playlist_item_t *p_item = NULL;
658     PLItem *newItem = NULL;
659     input_thread_t *currentInputThread;
660     int pos;
661
662     PLItem *nodeItem = findById( rootItem, i_parent );
663     if( !nodeItem ) return;
664
665     foreach( PLItem *existing, nodeItem->children )
666       if( existing->i_id == i_item ) return;
667
668     PL_LOCK;
669     p_item = playlist_ItemGetById( p_playlist, i_item );
670     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
671     {
672         PL_UNLOCK; return;
673     }
674
675     for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
676         if( p_item->p_parent->pp_children[pos] == p_item ) break;
677
678     newItem = new PLItem( p_item, nodeItem );
679     PL_UNLOCK;
680
681     beginInsertRows( index( nodeItem, 0 ), pos, pos );
682     nodeItem->insertChild( newItem, pos );
683     endInsertRows();
684
685     if( newItem->p_input == THEMIM->currentInputItem() )
686         emit currentChanged( index( newItem, 0 ) );
687 }
688
689
690 void PLModel::rebuild()
691 {
692     rebuild( NULL );
693 }
694
695 void PLModel::rebuild( playlist_item_t *p_root )
696 {
697     playlist_item_t* p_item;
698
699     /* Invalidate cache */
700     i_cached_id = i_cached_input_id = -1;
701
702     if( rootItem ) rootItem->removeChildren();
703
704     PL_LOCK;
705     if( p_root )
706     {
707         delete rootItem;
708         rootItem = new PLItem( p_root );
709     }
710     assert( rootItem );
711     /* Recreate from root */
712     updateChildren( rootItem );
713     PL_UNLOCK;
714
715     /* And signal the view */
716     reset();
717
718     if( p_root ) emit rootChanged();
719 }
720
721 void PLModel::takeItem( PLItem *item )
722 {
723     assert( item );
724     PLItem *parent = item->parentItem;
725     assert( parent );
726     int i_index = parent->children.indexOf( item );
727
728     beginRemoveRows( index( parent, 0 ), i_index, i_index );
729     parent->takeChildAt( i_index );
730     endRemoveRows();
731 }
732
733 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
734 {
735     assert( node );
736     int count = items.size();
737     if( !count ) return;
738     beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
739     for( int i = 0; i < count; i++ )
740     {
741         node->children.insert( i_pos + i, items[i] );
742         items[i]->parentItem = node;
743     }
744     endInsertRows();
745 }
746
747 void PLModel::removeItem( PLItem *item )
748 {
749     if( !item ) return;
750
751     i_cached_id = -1;
752     i_cached_input_id = -1;
753
754     if( item->parentItem ) {
755         int i = item->parentItem->children.indexOf( item );
756         beginRemoveRows( index( item->parentItem, 0), i, i );
757         item->parentItem->children.removeAt(i);
758         delete item;
759         endRemoveRows();
760     }
761     else delete item;
762
763     if(item == rootItem)
764     {
765         rootItem = NULL;
766         rebuild( p_playlist->p_playing );
767     }
768 }
769
770 /* This function must be entered WITH the playlist lock */
771 void PLModel::updateChildren( PLItem *root )
772 {
773     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
774     updateChildren( p_node, root );
775 }
776
777 /* This function must be entered WITH the playlist lock */
778 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
779 {
780     for( int i = 0; i < p_node->i_children ; i++ )
781     {
782         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
783         PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
784         root->appendChild( newItem );
785         if( p_node->pp_children[i]->i_children != -1 )
786             updateChildren( p_node->pp_children[i], newItem );
787     }
788 }
789
790 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
791 void PLModel::updateTreeItem( PLItem *item )
792 {
793     if( !item ) return;
794     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
795 }
796
797 /************************* Actions ******************************/
798
799 /**
800  * Deletion, don't delete items childrens if item is going to be
801  * delete allready, so we remove childrens from selection-list.
802  */
803 void PLModel::doDelete( QModelIndexList selected )
804 {
805     if( !canEdit() ) return;
806
807     while( !selected.isEmpty() )
808     {
809         QModelIndex index = selected[0];
810         selected.removeAt( 0 );
811
812         if( index.column() != 0 ) continue;
813
814         PLItem *item = getItem( index );
815         if( item->children.size() )
816             recurseDelete( item->children, &selected );
817
818         PL_LOCK;
819         playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
820         PL_UNLOCK;
821
822         removeItem( item );
823     }
824 }
825
826 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
827 {
828     for( int i = children.size() - 1; i >= 0 ; i-- )
829     {
830         PLItem *item = children[i];
831         if( item->children.size() )
832             recurseDelete( item->children, fullList );
833         fullList->removeAll( index( item, 0 ) );
834     }
835 }
836
837 /******* Volume III: Sorting and searching ********/
838 void PLModel::sort( int column, Qt::SortOrder order )
839 {
840     sort( rootItem->i_id, column, order );
841 }
842
843 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
844 {
845     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
846
847     int meta = columnToMeta( column );
848     if( meta == COLUMN_END ) return;
849
850     PLItem *item = findById( rootItem, i_root_id );
851     if( !item ) return;
852     QModelIndex qIndex = index( item, 0 );
853     int count = item->children.size();
854     if( count )
855     {
856         beginRemoveRows( qIndex, 0, count - 1 );
857         item->removeChildren();
858         endRemoveRows( );
859     }
860
861     PL_LOCK;
862     {
863         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
864                                                         i_root_id );
865         if( p_root )
866         {
867             playlist_RecursiveNodeSort( p_playlist, p_root,
868                                         i_column_sorting( meta ),
869                                         order == Qt::AscendingOrder ?
870                                             ORDER_NORMAL : ORDER_REVERSE );
871         }
872     }
873
874     i_cached_id = i_cached_input_id = -1;
875
876     if( count )
877     {
878         beginInsertRows( qIndex, 0, count - 1 );
879         updateChildren( item );
880         endInsertRows( );
881     }
882     PL_UNLOCK;
883     /* if we have popup item, try to make sure that you keep that item visible */
884     if( i_popup_item > -1 )
885     {
886         PLItem *popupitem = findById( rootItem, i_popup_item );
887         if( popupitem ) emit currentChanged( index( popupitem, 0 ) );
888         /* reset i_popup_item as we don't show it as selected anymore anyway */
889         i_popup_item = -1;
890     }
891     else if( currentIndex().isValid() ) emit currentChanged( currentIndex() );
892 }
893
894 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
895 {
896     /** \todo Fire the search with a small delay ? */
897     PL_LOCK;
898     {
899         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
900                                                         itemId( idx ) );
901         assert( p_root );
902         const char *psz_name = qtu( search_text );
903         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );
904
905         if( idx.isValid() )
906         {
907             PLItem *searchRoot = getItem( idx );
908
909             beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
910             searchRoot->removeChildren();
911             endRemoveRows( );
912
913             beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
914             updateChildren( searchRoot );
915             endInsertRows();
916
917             PL_UNLOCK;
918             return;
919         }
920     }
921     PL_UNLOCK;
922     rebuild();
923 }
924
925 /*********** Popup *********/
926 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
927 {
928     int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
929
930     PL_LOCK;
931     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
932     if( !p_item )
933     {
934         PL_UNLOCK;
935         return false;
936     }
937
938     i_popup_item = index.isValid() ? p_item->i_id : -1;
939     i_popup_parent = index.isValid() ?
940         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
941         ( rootItem->i_id );
942     i_popup_column = index.column();
943
944     bool tree = ( rootItem && rootItem->i_id != p_playlist->p_playing->i_id ) ||
945                 var_InheritBool( p_intf, "playlist-tree" );
946
947     PL_UNLOCK;
948
949     current_selection = list;
950
951     QMenu menu;
952     if( i_popup_item > -1 )
953     {
954         menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
955         menu.addAction( QIcon( ":/menu/stream" ),
956                         qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
957         menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
958         menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
959         menu.addAction( QIcon( ":/type/folder-grey" ),
960                         qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
961         menu.addSeparator();
962     }
963     if( canEdit() )
964     {
965         QIcon addIcon( ":/buttons/playlist/playlist_add" );
966         menu.addSeparator();
967         if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
968         if( rootItem->i_id == THEPL->p_playing->i_id )
969         {
970             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
971             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
972             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
973         }
974         else if( THEPL->p_media_library &&
975                     rootItem->i_id == THEPL->p_media_library->i_id )
976         {
977             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
978             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
979             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
980         }
981     }
982     if( i_popup_item > -1 )
983     {
984         menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
985                         qtr(I_POP_DEL), this, SLOT( popupDel() ) );
986         menu.addSeparator();
987         if( !sortingMenu )
988         {
989             sortingMenu = new QMenu( qtr( "Sort by" ) );
990             sortingMapper = new QSignalMapper( this );
991             int i, j;
992             for( i = 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
993             {
994                 if( i == COLUMN_NUMBER ) continue;
995                 QMenu *m = sortingMenu->addMenu( qfu( psz_column_title( i ) ) );
996                 QAction *asc = m->addAction( qtr("Ascending") );
997                 QAction *desc = m->addAction( qtr("Descending") );
998                 sortingMapper->setMapping( asc, j );
999                 sortingMapper->setMapping( desc, -j );
1000                 CONNECT( asc, triggered(), sortingMapper, map() );
1001                 CONNECT( desc, triggered(), sortingMapper, map() );
1002             }
1003             CONNECT( sortingMapper, mapped( int ), this, popupSort( int ) );
1004         }
1005         menu.addMenu( sortingMenu );
1006     }
1007     if( !menu.isEmpty() )
1008     {
1009         menu.exec( point ); return true;
1010     }
1011     else return false;
1012 }
1013
1014 void PLModel::popupDel()
1015 {
1016     doDelete( current_selection );
1017 }
1018
1019 void PLModel::popupPlay()
1020 {
1021     PL_LOCK;
1022     {
1023         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1024                                                         i_popup_item );
1025         activateItem( p_item );
1026     }
1027     PL_UNLOCK;
1028 }
1029
1030 void PLModel::popupInfo()
1031 {
1032     PL_LOCK;
1033     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1034                                                     i_popup_item );
1035     if( p_item )
1036     {
1037         input_item_t* p_input = p_item->p_input;
1038         vlc_gc_incref( p_input );
1039         PL_UNLOCK;
1040         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
1041         vlc_gc_decref( p_input );
1042         mid->setParent( PlaylistDialog::getInstance( p_intf ),
1043                         Qt::Dialog );
1044         mid->show();
1045     } else
1046         PL_UNLOCK;
1047 }
1048
1049 void PLModel::popupStream()
1050 {
1051     QStringList mrls = selectedURIs();
1052     if( !mrls.isEmpty() )
1053         THEDP->streamingDialog( NULL, mrls[0], false );
1054
1055 }
1056
1057 void PLModel::popupSave()
1058 {
1059     QStringList mrls = selectedURIs();
1060     if( !mrls.isEmpty() )
1061         THEDP->streamingDialog( NULL, mrls[0] );
1062 }
1063
1064 void PLModel::popupExplore()
1065 {
1066     PL_LOCK;
1067     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1068                                                     i_popup_item );
1069     if( p_item )
1070     {
1071        input_item_t *p_input = p_item->p_input;
1072        char *psz_meta = input_item_GetURI( p_input );
1073        PL_UNLOCK;
1074        if( psz_meta )
1075        {
1076            const char *psz_access;
1077            const char *psz_demux;
1078            char  *psz_path;
1079            input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1080
1081            if( !EMPTY_STR( psz_access ) && (
1082                    !strncasecmp( psz_access, "file", 4 ) ||
1083                    !strncasecmp( psz_access, "dire", 4 ) ))
1084            {
1085                QFileInfo info( qfu( decode_URI( psz_path ) ) );
1086                QDesktopServices::openUrl(
1087                                QUrl::fromLocalFile( info.absolutePath() ) );
1088            }
1089            free( psz_meta );
1090        }
1091     }
1092     else
1093         PL_UNLOCK;
1094 }
1095
1096 void PLModel::popupAddNode()
1097 {
1098     bool ok;
1099     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1100         qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
1101         QLineEdit::Normal, QString(), &ok);
1102     if( !ok || name.isEmpty() ) return;
1103     PL_LOCK;
1104     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1105                                                     i_popup_parent );
1106     if( p_item )
1107     {
1108         playlist_NodeCreate( p_playlist, qtu( name ), p_item, PLAYLIST_END, 0, NULL );
1109     }
1110     PL_UNLOCK;
1111 }
1112
1113 void PLModel::popupSort( int column )
1114 {
1115     sort( i_popup_parent,
1116           column > 0 ? column - 1 : -column - 1,
1117           column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
1118 }
1119
1120 /******************* Drag and Drop helper class ******************/
1121
1122 PlMimeData::PlMimeData( )
1123 { }
1124
1125 PlMimeData::~PlMimeData()
1126 {
1127     foreach( input_item_t *p_item, _inputItems )
1128         vlc_gc_decref( p_item );
1129 }
1130
1131 void PlMimeData::appendItem( input_item_t *p_item )
1132 {
1133     vlc_gc_incref( p_item );
1134     _inputItems.append( p_item );
1135 }
1136
1137 QList<input_item_t*> PlMimeData::inputItems() const
1138 {
1139     return _inputItems;
1140 }
1141
1142 QStringList PlMimeData::formats () const
1143 {
1144     QStringList fmts;
1145     fmts << "vlc/qt-input-items";
1146     return fmts;
1147 }