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