]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt: playlist model cleanup
[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 /* Convenient overloaded private version of activateItem
315  * Must be entered with PL lock */
316 void PLModel::activateItem( playlist_item_t *p_item )
317 {
318     if( !p_item ) return;
319     playlist_item_t *p_parent = p_item;
320     while( p_parent )
321     {
322         if( p_parent->i_id == rootItem->id() ) break;
323         p_parent = p_parent->p_parent;
324     }
325     if( p_parent )
326         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
327                 p_parent, p_item );
328 }
329
330 /****************** Base model mandatory implementations *****************/
331 QVariant PLModel::data( const QModelIndex &index, const int role ) const
332 {
333     if( !index.isValid() ) return QVariant();
334     const PLItem *item = getItem( index );
335     if( role == Qt::DisplayRole )
336     {
337         int metadata = columnToMeta( index.column() );
338         if( metadata == COLUMN_END ) return QVariant();
339
340         QString returninfo;
341         if( metadata == COLUMN_NUMBER )
342             returninfo = QString::number( index.row() + 1 );
343         else if( metadata == COLUMN_COVER )
344         {
345             QString artUrl;
346             artUrl = InputManager::decodeArtURL( item->inputItem() );
347             if( artUrl.isEmpty() )
348             {
349                 for( int i = 0; i < item->childCount(); i++ )
350                 {
351                     artUrl = InputManager::decodeArtURL( item->child( i )->inputItem() );
352                     if( !artUrl.isEmpty() )
353                         break;
354                 }
355             }
356             return QVariant( artUrl );
357         }
358         else
359         {
360             char *psz = psz_column_meta( item->inputItem(), metadata );
361             returninfo = qfu( psz );
362             free( psz );
363         }
364         return QVariant( returninfo );
365     }
366     else if( role == Qt::DecorationRole && index.column() == 0  )
367     {
368         /* Used to segfault here because i_type wasn't always initialized */
369         return QVariant( PLModel::icons[item->inputItem()->i_type] );
370     }
371     else if( role == Qt::FontRole )
372     {
373         QFont f;
374         f.setPointSize( __MAX( f.pointSize() - 1 + i_zoom, 4 ) );
375         if( isCurrent( index ) )
376             f.setBold( true );
377         return QVariant( f );
378     }
379     else if( role == Qt::BackgroundRole && isCurrent( index ) )
380     {
381         return QVariant( QBrush( Qt::gray ) );
382     }
383     else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
384     else if( role == IsLeafNodeRole )
385     {
386         QVariant isLeaf;
387         PL_LOCK;
388         playlist_item_t *plItem =
389             playlist_ItemGetById( p_playlist, item->i_id );
390
391         if( plItem )
392             isLeaf = plItem->i_children == -1;
393
394         PL_UNLOCK;
395         return isLeaf;
396     }
397     else if( role == IsCurrentsParentNodeRole )
398     {
399         return QVariant( isParent( index, currentIndex() ) );
400     }
401     return QVariant();
402 }
403
404 /* Seek from current index toward the top and see if index is one of parent nodes */
405 bool PLModel::isParent( const QModelIndex &index, const QModelIndex &current ) const
406 {
407     if( !index.isValid() )
408         return false;
409
410     if( index == current )
411         return true;
412
413     if( !current.isValid() || !current.parent().isValid() )
414         return false;
415
416     return isParent( index, current.parent() );
417 }
418
419 bool PLModel::isCurrent( const QModelIndex &index ) const
420 {
421     return getItem( index )->inputItem() == THEMIM->currentInputItem();
422 }
423
424 int PLModel::itemId( const QModelIndex &index ) const
425 {
426     return getItem( index )->id();
427 }
428
429 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
430                               int role ) const
431 {
432     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
433         return QVariant();
434
435     int meta_col = columnToMeta( section );
436
437     if( meta_col == COLUMN_END ) return QVariant();
438
439     return QVariant( qfu( psz_column_title( meta_col ) ) );
440 }
441
442 QModelIndex PLModel::index( const int row, const int column, const QModelIndex &parent )
443                   const
444 {
445     PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
446
447     PLItem *childItem = parentItem->child( row );
448     if( childItem )
449         return createIndex( row, column, childItem );
450     else
451         return QModelIndex();
452 }
453
454 QModelIndex PLModel::index( const int i_id, const int c )
455 {
456     return index( findById( rootItem, i_id ), c );
457 }
458
459 /* Return the index of a given item */
460 QModelIndex PLModel::index( PLItem *item, int column ) const
461 {
462     if( !item ) return QModelIndex();
463     const PLItem *parent = item->parent();
464     if( parent )
465         return createIndex( parent->children.lastIndexOf( item ),
466                             column, item );
467     return QModelIndex();
468 }
469
470 QModelIndex PLModel::currentIndex() const
471 {
472     input_thread_t *p_input_thread = THEMIM->getInput();
473     if( !p_input_thread ) return QModelIndex();
474     PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
475     return index( item, 0 );
476 }
477
478 QModelIndex PLModel::parent( const QModelIndex &index ) const
479 {
480     if( !index.isValid() ) return QModelIndex();
481
482     PLItem *childItem = getItem( index );
483     if( !childItem )
484     {
485         msg_Err( p_playlist, "NULL CHILD" );
486         return QModelIndex();
487     }
488
489     PLItem *parentItem = childItem->parent();
490     if( !parentItem || parentItem == rootItem ) return QModelIndex();
491     if( !parentItem->parent() )
492     {
493         msg_Err( p_playlist, "No parent parent, trying row 0 " );
494         msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
495         return createIndex( 0, 0, parentItem );
496     }
497     return createIndex(parentItem->row(), 0, parentItem);
498 }
499
500 int PLModel::columnCount( const QModelIndex &) const
501 {
502     return columnFromMeta( COLUMN_END );
503 }
504
505 int PLModel::rowCount( const QModelIndex &parent ) const
506 {
507     const PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
508     return parentItem->childCount();
509 }
510
511 QStringList PLModel::selectedURIs()
512 {
513     QStringList lst;
514     for( int i = 0; i < current_selection.count(); i++ )
515     {
516         const PLItem *item = getItem( current_selection[i] );
517         if( item )
518         {
519             PL_LOCK;
520             playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
521             if( p_item )
522             {
523                 char *psz = input_item_GetURI( p_item->p_input );
524                 if( psz )
525                 {
526                     lst.append( qfu(psz) );
527                     free( psz );
528                 }
529             }
530             PL_UNLOCK;
531         }
532     }
533     return lst;
534 }
535
536 /************************* Lookups *****************************/
537 PLItem *PLModel::findById( PLItem *root, int i_id ) const
538 {
539     return findInner( root, i_id, false );
540 }
541
542 PLItem *PLModel::findByInput( PLItem *root, int i_id ) const
543 {
544     PLItem *result = findInner( root, i_id, true );
545     return result;
546 }
547
548 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input ) const
549 {
550     if( !root ) return NULL;
551
552     if( !b_input && root->id() == i_id )
553         return root;
554
555     else if( b_input && root->inputItem()->i_id == i_id )
556         return root;
557
558     QList<PLItem *>::iterator it = root->children.begin();
559     while ( it != root->children.end() )
560     {
561         if( !b_input && (*it)->id() == i_id )
562             return (*it);
563
564         else if( b_input && (*it)->inputItem()->i_id == i_id )
565             return (*it);
566
567         if( (*it)->childCount() )
568         {
569             PLItem *childFound = findInner( (*it), i_id, b_input );
570             if( childFound )
571                 return childFound;
572         }
573         ++it;
574     }
575     return NULL;
576 }
577
578 bool PLModel::canEdit() const
579 {
580     return (
581             rootItem != NULL &&
582             (
583              rootItem->inputItem() == p_playlist->p_playing->p_input ||
584              ( p_playlist->p_media_library &&
585               rootItem->inputItem() == p_playlist->p_media_library->p_input )
586             )
587            );
588 }
589
590 /************************* Updates handling *****************************/
591
592 /**** Events processing ****/
593 void PLModel::processInputItemUpdate( input_thread_t *p_input )
594 {
595     if( !p_input ) return;
596     if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
597     {
598         PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
599         if( item ) emit currentChanged( index( item, 0 ) );
600     }
601     processInputItemUpdate( input_GetItem( p_input ) );
602 }
603
604 void PLModel::processInputItemUpdate( input_item_t *p_item )
605 {
606     if( !p_item ||  p_item->i_id <= 0 ) return;
607     PLItem *item = findByInput( rootItem, p_item->i_id );
608     if( item )
609         updateTreeItem( item );
610 }
611
612 void PLModel::processItemRemoval( int i_id )
613 {
614     if( i_id <= 0 ) return;
615     removeItem( i_id );
616 }
617
618 void PLModel::processItemAppend( int i_item, int i_parent )
619 {
620     playlist_item_t *p_item = NULL;
621     PLItem *newItem = NULL;
622     int pos;
623
624     /* Find the Parent */
625     PLItem *nodeParentItem = findById( rootItem, i_parent );
626     if( !nodeParentItem ) return;
627
628     /* Search for an already matching children */
629     foreach( const PLItem *existing, nodeParentItem->children )
630         if( existing->i_id == i_item ) return;
631
632     /* Find the child */
633     PL_LOCK;
634     p_item = playlist_ItemGetById( p_playlist, i_item );
635     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
636     {
637         PL_UNLOCK; return;
638     }
639
640     for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
641         if( p_item->p_parent->pp_children[pos] == p_item ) break;
642
643     newItem = new PLItem( p_item, nodeParentItem );
644     PL_UNLOCK;
645
646     /* We insert the newItem (children) inside the parent */
647     beginInsertRows( index( nodeParentItem, 0 ), pos, pos );
648     nodeParentItem->insertChild( newItem, pos );
649     endInsertRows();
650
651     if( newItem->inputItem() == THEMIM->currentInputItem() )
652         emit currentChanged( index( newItem, 0 ) );
653 }
654
655 void PLModel::rebuild( playlist_item_t *p_root )
656 {
657     /* Invalidate cache */
658     i_cached_id = i_cached_input_id = -1;
659
660     if( rootItem ) rootItem->removeChildren();
661
662     PL_LOCK;
663     if( p_root ) // Can be NULL
664     {
665         delete rootItem;
666         rootItem = new PLItem( p_root );
667     }
668     assert( rootItem );
669     /* Recreate from root */
670     updateChildren( rootItem );
671     PL_UNLOCK;
672
673     /* And signal the view */
674     reset();
675
676     if( p_root ) emit rootChanged();
677 }
678
679 void PLModel::takeItem( PLItem *item )
680 {
681     assert( item );
682     PLItem *parent = item->parent();
683     assert( parent );
684     int i_index = parent->children.indexOf( item );
685
686     beginRemoveRows( index( parent, 0 ), i_index, i_index );
687     parent->takeChildAt( i_index );
688     endRemoveRows();
689 }
690
691 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
692 {
693     assert( node );
694     int count = items.count();
695     if( !count ) return;
696     printf( "Here I am\n");
697     beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
698     for( int i = 0; i < count; i++ )
699     {
700         node->children.insert( i_pos + i, items[i] );
701         items[i]->parentItem = node;
702     }
703     endInsertRows();
704 }
705
706 void PLModel::removeItem( PLItem *item )
707 {
708     if( !item ) return;
709
710     i_cached_id = -1;
711     i_cached_input_id = -1;
712
713     if( item->parent() ) {
714         int i = item->parent()->children.indexOf( item );
715         beginRemoveRows( index( item->parent(), 0), i, i );
716         item->parent()->children.removeAt(i);
717         delete item;
718         endRemoveRows();
719     }
720     else delete item;
721
722     if(item == rootItem)
723     {
724         rootItem = NULL;
725         rebuild( p_playlist->p_playing );
726     }
727 }
728
729 /* This function must be entered WITH the playlist lock */
730 void PLModel::updateChildren( PLItem *root )
731 {
732     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->id() );
733     updateChildren( p_node, root );
734 }
735
736 /* This function must be entered WITH the playlist lock */
737 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
738 {
739     for( int i = 0; i < p_node->i_children ; i++ )
740     {
741         if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
742         PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
743         root->appendChild( newItem );
744         if( p_node->pp_children[i]->i_children != -1 )
745             updateChildren( p_node->pp_children[i], newItem );
746     }
747 }
748
749 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
750 void PLModel::updateTreeItem( PLItem *item )
751 {
752     if( !item ) return;
753     emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
754 }
755
756 /************************* Actions ******************************/
757
758 /**
759  * Deletion, don't delete items childrens if item is going to be
760  * delete allready, so we remove childrens from selection-list.
761  */
762 void PLModel::doDelete( QModelIndexList selected )
763 {
764     if( !canEdit() ) return;
765
766     while( !selected.isEmpty() )
767     {
768         QModelIndex index = selected[0];
769         selected.removeAt( 0 );
770
771         if( index.column() != 0 ) continue;
772
773         PLItem *item = getItem( index );
774         if( item->childCount() )
775             recurseDelete( item->children, &selected );
776
777         PL_LOCK;
778         playlist_DeleteFromInput( p_playlist, item->inputItem(), pl_Locked );
779         PL_UNLOCK;
780
781         removeItem( item );
782     }
783 }
784
785 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
786 {
787     for( int i = children.count() - 1; i >= 0 ; i-- )
788     {
789         PLItem *item = children[i];
790         if( item->childCount() )
791             recurseDelete( item->children, fullList );
792         fullList->removeAll( index( item, 0 ) );
793     }
794 }
795
796 /******* Volume III: Sorting and searching ********/
797 void PLModel::sort( const int column, Qt::SortOrder order )
798 {
799     sort( rootItem->id(), column, order );
800 }
801
802 void PLModel::sort( const int i_root_id, const int column, Qt::SortOrder order )
803 {
804     msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
805
806     int meta = columnToMeta( column );
807     if( meta == COLUMN_END ) return;
808
809     PLItem *item = findById( rootItem, i_root_id );
810     if( !item ) return;
811     QModelIndex qIndex = index( item, 0 );
812     int count = item->childCount();
813     if( count )
814     {
815         beginRemoveRows( qIndex, 0, count - 1 );
816         item->removeChildren();
817         endRemoveRows( );
818     }
819
820     PL_LOCK;
821     {
822         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
823                                                         i_root_id );
824         if( p_root )
825         {
826             playlist_RecursiveNodeSort( p_playlist, p_root,
827                                         i_column_sorting( meta ),
828                                         order == Qt::AscendingOrder ?
829                                             ORDER_NORMAL : ORDER_REVERSE );
830         }
831     }
832
833     i_cached_id = i_cached_input_id = -1;
834
835     if( count )
836     {
837         beginInsertRows( qIndex, 0, count - 1 );
838         updateChildren( item );
839         endInsertRows( );
840     }
841     PL_UNLOCK;
842     /* if we have popup item, try to make sure that you keep that item visible */
843     if( i_popup_item > -1 )
844     {
845         PLItem *popupitem = findById( rootItem, i_popup_item );
846         if( popupitem ) emit currentChanged( index( popupitem, 0 ) );
847         /* reset i_popup_item as we don't show it as selected anymore anyway */
848         i_popup_item = -1;
849     }
850     else if( currentIndex().isValid() ) emit currentChanged( currentIndex() );
851 }
852
853 void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
854 {
855     /** \todo Fire the search with a small delay ? */
856     PL_LOCK;
857     {
858         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
859                                                         itemId( idx ) );
860         assert( p_root );
861         playlist_LiveSearchUpdate( p_playlist , p_root, qtu( search_text ),
862                                    b_recursive );
863         if( idx.isValid() )
864         {
865             PLItem *searchRoot = getItem( idx );
866
867             beginRemoveRows( idx, 0, searchRoot->childCount() - 1 );
868             searchRoot->removeChildren();
869             endRemoveRows( );
870
871             beginInsertRows( idx, 0, searchRoot->childCount() - 1 );
872             updateChildren( searchRoot ); // The PL_LOCK is needed here
873             endInsertRows();
874
875             PL_UNLOCK;
876             return;
877         }
878     }
879     PL_UNLOCK;
880     rebuild();
881 }
882
883 /*********** Popup *********/
884 bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
885 {
886     int i_id = index.isValid() ? itemId( index ) : rootItem->id();
887
888     PL_LOCK;
889     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
890     if( !p_item )
891     {
892         PL_UNLOCK;
893         return false;
894     }
895
896     i_popup_item   = index.isValid() ? p_item->i_id : -1;
897     i_popup_parent = index.isValid() ?
898         ( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
899         ( rootItem->id() );
900
901     bool tree = ( rootItem && rootItem->id() != p_playlist->p_playing->i_id ) ||
902                 var_InheritBool( p_intf, "playlist-tree" );
903
904     input_item_t *p_input = p_item->p_input;
905     vlc_gc_incref( p_input );
906     PL_UNLOCK;
907
908     /* */
909     QMenu menu;
910
911     /* Play/Stream/Info static actions */
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     /* In PL or ML, allow to add a file/folder */
929     if( canEdit() )
930     {
931         QIcon addIcon( ":/buttons/playlist/playlist_add" );
932         menu.addSeparator();
933         if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
934         if( rootItem->id() == THEPL->p_playing->i_id )
935         {
936             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
937             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
938             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
939         }
940         else if( THEPL->p_media_library &&
941                     rootItem->id() == THEPL->p_media_library->i_id )
942         {
943             menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
944             menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
945             menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
946         }
947     }
948
949     /* Item removal */
950     if( i_popup_item > -1 )
951     {
952         if( rootItem->id() != THEPL->p_playing->i_id )
953             menu.addAction( qtr( "Add to playlist"), this, SLOT( popupAddToPlaylist() ) );
954         menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
955                         qtr(I_POP_DEL), this, SLOT( popupDel() ) );
956         menu.addSeparator();
957     }
958
959     /* Playlist sorting */
960     if( !sortingMenu )
961     {
962         sortingMenu = new QMenu( qtr( "Sort by" ) );
963         sortingMapper = new QSignalMapper( this );
964         for( int i = 1, j = 1; i < COLUMN_ALBUM; i <<= 1, j++ )
965         {
966             if( i == COLUMN_NUMBER ) continue;
967             QAction *asc  = sortingMenu->addAction( qfu( psz_column_title( i ) ) + " " + qtr("Ascending") );
968             QAction *desc = sortingMenu->addAction( qfu( psz_column_title( i ) ) + " " + qtr("Descending") );
969             sortingMapper->setMapping( asc, j );
970             sortingMapper->setMapping( desc, -j );
971             CONNECT( asc, triggered(), sortingMapper, map() );
972             CONNECT( desc, triggered(), sortingMapper, map() );
973         }
974         CONNECT( sortingMapper, mapped( int ), this, popupSort( int ) );
975     }
976     menu.addMenu( sortingMenu );
977
978     /* Store the current selected item for popup*() methods */
979     current_selection = list;
980
981     /* Display and forward the result */
982     if( !menu.isEmpty() )
983     {
984         menu.exec( point ); return true;
985     }
986     else return false;
987 }
988
989 void PLModel::popupDel()
990 {
991     doDelete( current_selection );
992 }
993
994 void PLModel::popupPlay()
995 {
996     PL_LOCK;
997     {
998         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
999                                                         i_popup_item );
1000         activateItem( p_item );
1001     }
1002     PL_UNLOCK;
1003 }
1004
1005 void PLModel::popupAddToPlaylist()
1006 {
1007     playlist_Lock( THEPL );
1008
1009     foreach( QModelIndex currentIndex, current_selection )
1010     {
1011         playlist_item_t *p_item = playlist_ItemGetById( THEPL, itemId( currentIndex ) );
1012         if( !p_item ) continue;
1013
1014         playlist_NodeAddCopy( THEPL, p_item,
1015                               THEPL->p_playing,
1016                               PLAYLIST_END );
1017     }
1018     playlist_Unlock( THEPL );
1019 }
1020
1021 void PLModel::popupInfo()
1022 {
1023     PL_LOCK;
1024     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1025                                                     i_popup_item );
1026     if( p_item )
1027     {
1028         input_item_t* p_input = p_item->p_input;
1029         vlc_gc_incref( p_input );
1030         PL_UNLOCK;
1031         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
1032         vlc_gc_decref( p_input );
1033         mid->setParent( PlaylistDialog::getInstance( p_intf ),
1034                         Qt::Dialog );
1035         mid->show();
1036     } else
1037         PL_UNLOCK;
1038 }
1039
1040 void PLModel::popupStream()
1041 {
1042     QStringList mrls = selectedURIs();
1043     if( !mrls.isEmpty() )
1044         THEDP->streamingDialog( NULL, mrls[0], false );
1045 }
1046
1047 void PLModel::popupSave()
1048 {
1049     QStringList mrls = selectedURIs();
1050     if( !mrls.isEmpty() )
1051         THEDP->streamingDialog( NULL, mrls[0] );
1052 }
1053
1054 void PLModel::popupExplore()
1055 {
1056     char *uri = NULL, *path = NULL;
1057
1058     PL_LOCK;
1059     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1060     if( p_item )
1061     {
1062         input_item_t *p_input = p_item->p_input;
1063         uri = input_item_GetURI( p_input );
1064     }
1065     PL_UNLOCK;
1066
1067     if( uri != NULL )
1068     {
1069         path = make_path( uri );
1070         free( uri );
1071     }
1072     if( path == NULL )
1073         return;
1074
1075     QFileInfo info( qfu( path ) );
1076     free( path );
1077
1078     QDesktopServices::openUrl( QUrl::fromLocalFile( info.absolutePath() ) );
1079 }
1080
1081 void PLModel::popupAddNode()
1082 {
1083     bool ok;
1084     QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1085         qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
1086         QLineEdit::Normal, QString(), &ok);
1087     if( !ok || name.isEmpty() ) return;
1088
1089     PL_LOCK;
1090     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1091                                                     i_popup_parent );
1092     if( p_item )
1093         playlist_NodeCreate( p_playlist, qtu( name ), p_item, PLAYLIST_END, 0, NULL );
1094     PL_UNLOCK;
1095 }
1096
1097 void PLModel::popupSort( int column )
1098 {
1099     sort( i_popup_parent,
1100           column > 0 ? column - 1 : -column - 1,
1101           column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
1102 }
1103
1104 /******************* Drag and Drop helper class ******************/
1105 PlMimeData::~PlMimeData()
1106 {
1107     foreach( input_item_t *p_item, _inputItems )
1108         vlc_gc_decref( p_item );
1109 }
1110
1111 void PlMimeData::appendItem( input_item_t *p_item )
1112 {
1113     vlc_gc_incref( p_item );
1114     _inputItems.append( p_item );
1115 }
1116
1117 QList<input_item_t*> PlMimeData::inputItems() const
1118 {
1119     return _inputItems;
1120 }
1121
1122 QStringList PlMimeData::formats () const
1123 {
1124     QStringList fmts;
1125     fmts << "vlc/qt-input-items";
1126     return fmts;
1127 }