Qt: kill a bug in playlist model
[vlc.git] / modules / gui / qt4 / components / playlist / playlist_model.cpp
blob2c3b8d22babd79db0594c46313456d38789147e8
1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Ilkka Ollakkka <ileoo (at) videolan dot org>
9 * Jakob Leben <jleben@videolan.org>
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.
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.
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 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
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>
37 #include "pixmaps/types/type_unknown.xpm"
39 #include <assert.h>
40 #include <QIcon>
41 #include <QFont>
42 #include <QMenu>
43 #include <QApplication>
44 #include <QSettings>
46 #include "sorting.h"
48 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
50 /*************************************************************************
51 * Playlist model implementation
52 *************************************************************************/
55 This model is called two times, for the selector and the standard panel
57 PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
58 intf_thread_t *_p_intf, /* main Qt p_intf */
59 playlist_item_t * p_root,
60 /*playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
61 and THEPL->p_root_category for SelectPL */
62 QObject *parent ) /* Basic Qt parent */
63 : QAbstractItemModel( parent )
65 p_intf = _p_intf;
66 p_playlist = _p_playlist;
67 i_cached_id = -1;
68 i_cached_input_id = -1;
69 i_popup_item = i_popup_parent = -1;
70 currentItem = NULL;
72 rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
74 /* Icons initialization */
75 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
76 ADD_ICON( UNKNOWN , type_unknown_xpm );
77 ADD_ICON( FILE, ":/type/file" );
78 ADD_ICON( DIRECTORY, ":/type/directory" );
79 ADD_ICON( DISC, ":/type/disc" );
80 ADD_ICON( CDDA, ":/type/cdda" );
81 ADD_ICON( CARD, ":/type/capture-card" );
82 ADD_ICON( NET, ":/type/net" );
83 ADD_ICON( PLAYLIST, ":/type/playlist" );
84 ADD_ICON( NODE, ":/type/node" );
85 #undef ADD_ICON
87 rebuild( p_root, true );
88 CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
89 this, processInputItemUpdate( input_item_t *) );
90 CONNECT( THEMIM, inputChanged( input_thread_t * ),
91 this, processInputItemUpdate( input_thread_t* ) );
92 CONNECT( THEMIM, playlistItemAppended( int, int ),
93 this, processItemAppend( int, int ) );
94 CONNECT( THEMIM, playlistItemRemoved( int ),
95 this, processItemRemoval( int ) );
98 PLModel::~PLModel()
100 delete rootItem;
103 Qt::DropActions PLModel::supportedDropActions() const
105 return Qt::CopyAction; /* Why not Qt::MoveAction */
108 Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
110 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
112 PLItem *item = index.isValid() ? getItem( index ) : rootItem;
114 if( canEdit() )
116 PL_LOCK;
117 playlist_item_t *plItem =
118 playlist_ItemGetById( p_playlist, item->i_id );
120 if ( plItem && ( plItem->i_children > -1 ) )
121 flags |= Qt::ItemIsDropEnabled;
123 PL_UNLOCK;
126 flags |= Qt::ItemIsDragEnabled;
128 return flags;
131 QStringList PLModel::mimeTypes() const
133 QStringList types;
134 types << "vlc/qt-playlist-item";
135 return types;
138 QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
140 QMimeData *mimeData = new QMimeData();
141 QByteArray encodedData;
142 QDataStream stream( &encodedData, QIODevice::WriteOnly );
143 QModelIndexList list;
145 foreach( const QModelIndex &index, indexes ) {
146 if( index.isValid() && index.column() == 0 )
147 list.append(index);
150 qSort(list);
152 foreach( const QModelIndex &index, list ) {
153 PLItem *item = getItem( index );
154 stream.writeRawData( (char*) &item, sizeof( PLItem* ) );
156 mimeData->setData( "vlc/qt-playlist-item", encodedData );
157 return mimeData;
160 /* Drop operation */
161 bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
162 int row, int column, const QModelIndex &parent )
164 if( data->hasFormat( "vlc/qt-playlist-item" ) )
166 if( action == Qt::IgnoreAction )
167 return true;
169 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
171 PL_LOCK;
172 playlist_item_t *p_parent =
173 playlist_ItemGetById( p_playlist, parentItem->i_id );
174 if( !p_parent || p_parent->i_children == -1 )
176 PL_UNLOCK;
177 return false;
180 bool copy = false;
181 playlist_item_t *p_pl = p_playlist->p_local_category;
182 playlist_item_t *p_ml = p_playlist->p_ml_category;
185 row == -1 && (
186 ( p_pl && p_parent->p_input == p_pl->p_input ) ||
187 ( p_ml && p_parent->p_input == p_ml->p_input ) )
189 copy = true;
190 PL_UNLOCK;
192 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
193 if( copy )
194 dropAppendCopy( encodedData, parentItem );
195 else
196 dropMove( encodedData, parentItem, row );
198 return true;
201 void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
203 QDataStream stream( &data, QIODevice::ReadOnly );
205 PL_LOCK;
206 playlist_item_t *p_parent =
207 playlist_ItemGetById( p_playlist, target->i_id );
208 while( !stream.atEnd() )
210 PLItem *item;
211 stream.readRawData( (char*)&item, sizeof(PLItem*) );
212 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
213 if( !p_item ) continue;
214 input_item_t *p_input = p_item->p_input;
215 playlist_AddExt ( p_playlist,
216 p_input->psz_uri, p_input->psz_name,
217 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
218 p_input->i_duration,
219 p_input->i_options, p_input->ppsz_options, p_input->optflagc,
220 ( p_parent == p_playlist->p_local_category ||
221 p_parent == p_playlist->p_local_onelevel ),
222 true );
224 PL_UNLOCK;
227 void PLModel::dropMove( QByteArray& data, PLItem *target, int row )
229 QDataStream stream( &data, QIODevice::ReadOnly );
230 QList<PLItem*> model_items;
231 QList<int> ids;
232 int new_pos = row == -1 ? target->children.size() : row;
233 int model_pos = new_pos;
234 while( !stream.atEnd() )
236 PLItem *item;
237 stream.readRawData( (char*)&item, sizeof(PLItem*) );
239 /* better not try to move a node into itself: */
240 PLItem *climber = target;
241 while( climber )
243 if( climber == item ) break;
244 climber = climber->parentItem;
246 if( climber ) continue;
248 if( item->parentItem == target &&
249 target->children.indexOf( item ) < model_pos )
250 model_pos--;
252 ids.append( item->i_id );
253 model_items.append( item );
255 takeItem( item );
257 int count = ids.size();
258 if( count )
260 playlist_item_t *pp_items[count];
262 PL_LOCK;
263 for( int i = 0; i < count; i++ )
265 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, ids[i] );
266 if( !p_item )
268 PL_UNLOCK;
269 return;
271 pp_items[i] = p_item;
273 playlist_item_t *p_parent =
274 playlist_ItemGetById( p_playlist, target->i_id );
275 playlist_TreeMoveMany( p_playlist, count, pp_items, p_parent,
276 new_pos );
277 PL_UNLOCK;
279 insertChildren( target, model_items, model_pos );
283 /* remove item with its id */
284 void PLModel::removeItem( int i_id )
286 PLItem *item = findById( rootItem, i_id );
287 removeItem( item );
290 void PLModel::activateItem( const QModelIndex &index )
292 assert( index.isValid() );
293 PLItem *item = getItem( index );
294 assert( item );
295 PL_LOCK;
296 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
297 activateItem( p_item );
298 PL_UNLOCK;
301 /* Must be entered with lock */
302 void PLModel::activateItem( playlist_item_t *p_item )
304 if( !p_item ) return;
305 playlist_item_t *p_parent = p_item;
306 while( p_parent )
308 if( p_parent->i_id == rootItem->i_id ) break;
309 p_parent = p_parent->p_parent;
311 if( p_parent )
312 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
313 p_parent, p_item );
316 /****************** Base model mandatory implementations *****************/
317 QVariant PLModel::data( const QModelIndex &index, int role ) const
319 if( !index.isValid() ) return QVariant();
320 PLItem *item = getItem( index );
321 if( role == Qt::DisplayRole )
323 int metadata = columnToMeta( index.column() );
324 if( metadata == COLUMN_END ) return QVariant();
326 QString returninfo;
327 if( metadata == COLUMN_NUMBER )
328 returninfo = QString::number( index.row() + 1 );
329 else
331 char *psz = psz_column_meta( item->p_input, metadata );
332 returninfo = qfu( psz );
333 free( psz );
335 return QVariant( returninfo );
337 else if( role == Qt::DecorationRole && index.column() == 0 )
339 /* Used to segfault here because i_type wasn't always initialized */
340 return QVariant( PLModel::icons[item->p_input->i_type] );
342 else if( role == Qt::FontRole )
344 if( isCurrent( index ) )
346 QFont f; f.setBold( true ); return QVariant( f );
349 else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
350 return QVariant();
353 bool PLModel::isCurrent( const QModelIndex &index ) const
355 if( !currentItem ) return false;
356 return getItem( index )->p_input == currentItem->p_input;
359 int PLModel::itemId( const QModelIndex &index ) const
361 return getItem( index )->i_id;
364 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
365 int role ) const
367 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
368 return QVariant();
370 int meta_col = columnToMeta( section );
372 if( meta_col == COLUMN_END ) return QVariant();
374 return QVariant( qfu( psz_column_title( meta_col ) ) );
377 QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
378 const
380 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
382 PLItem *childItem = parentItem->child( row );
383 if( childItem )
384 return createIndex( row, column, childItem );
385 else
386 return QModelIndex();
389 QModelIndex PLModel::index( int i_id, int c )
391 return index( findById( rootItem, i_id ), c );
394 /* Return the index of a given item */
395 QModelIndex PLModel::index( PLItem *item, int column ) const
397 if( !item ) return QModelIndex();
398 const PLItem *parent = item->parent();
399 if( parent )
400 return createIndex( parent->children.lastIndexOf( item ),
401 column, item );
402 return QModelIndex();
405 QModelIndex PLModel::parent( const QModelIndex &index ) const
407 if( !index.isValid() ) return QModelIndex();
409 PLItem *childItem = getItem( index );
410 if( !childItem )
412 msg_Err( p_playlist, "NULL CHILD" );
413 return QModelIndex();
416 PLItem *parentItem = childItem->parent();
417 if( !parentItem || parentItem == rootItem ) return QModelIndex();
418 if( !parentItem->parentItem )
420 msg_Err( p_playlist, "No parent parent, trying row 0 " );
421 msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
422 return createIndex( 0, 0, parentItem );
424 QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
425 return ind;
428 int PLModel::columnCount( const QModelIndex &i) const
430 return columnFromMeta( COLUMN_END );
433 int PLModel::rowCount( const QModelIndex &parent ) const
435 PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
436 return parentItem->childCount();
439 QStringList PLModel::selectedURIs()
441 QStringList lst;
442 for( int i = 0; i < current_selection.size(); i++ )
444 PLItem *item = getItem( current_selection[i] );
445 if( item )
447 PL_LOCK;
448 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
449 if( p_item )
451 char *psz = input_item_GetURI( p_item->p_input );
452 if( psz )
454 lst.append( qfu(psz) );
455 free( psz );
458 PL_UNLOCK;
461 return lst;
465 /************************* Lookups *****************************/
467 PLItem *PLModel::findById( PLItem *root, int i_id )
469 return findInner( root, i_id, false );
472 PLItem *PLModel::findByInput( PLItem *root, int i_id )
474 PLItem *result = findInner( root, i_id, true );
475 return result;
478 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
479 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
481 PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
483 if( !root ) return NULL;
484 if( ( !b_input && i_cached_id == i_id) ||
485 ( b_input && i_cached_input_id ==i_id ) )
487 return b_input ? p_cached_item_bi : p_cached_item;
490 if( !b_input && root->i_id == i_id )
492 CACHE( i_id, root );
493 return root;
495 else if( b_input && root->p_input->i_id == i_id )
497 ICACHE( i_id, root );
498 return root;
501 QList<PLItem *>::iterator it = root->children.begin();
502 while ( it != root->children.end() )
504 if( !b_input && (*it)->i_id == i_id )
506 CACHE( i_id, (*it) );
507 return p_cached_item;
509 else if( b_input && (*it)->p_input->i_id == i_id )
511 ICACHE( i_id, (*it) );
512 return p_cached_item_bi;
514 if( (*it)->children.size() )
516 PLItem *childFound = findInner( (*it), i_id, b_input );
517 if( childFound )
519 if( b_input )
520 ICACHE( i_id, childFound )
521 else
522 CACHE( i_id, childFound )
523 return childFound;
526 it++;
528 return NULL;
530 #undef CACHE
531 #undef ICACHE
533 int PLModel::columnToMeta( int _column ) const
535 int meta = 1;
536 int column = 0;
538 while( column != _column && meta != COLUMN_END )
540 meta <<= 1;
541 column++;
544 return meta;
547 int PLModel::columnFromMeta( int meta_col ) const
549 int meta = 1;
550 int column = 0;
552 while( meta != meta_col && meta != COLUMN_END )
554 meta <<= 1;
555 column++;
558 return column;
561 bool PLModel::canEdit() const
563 return (
564 rootItem != NULL &&
566 rootItem->p_input == p_playlist->p_local_category->p_input ||
568 p_playlist->p_ml_category &&
569 rootItem->p_input == p_playlist->p_ml_category->p_input
574 /************************* Updates handling *****************************/
576 /**** Events processing ****/
577 void PLModel::processInputItemUpdate( input_thread_t *p_input )
579 if( !p_input ) return;
580 if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
582 PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
583 currentItem = item;
584 emit currentChanged( index( item, 0 ) );
586 else
588 currentItem = NULL;
590 processInputItemUpdate( input_GetItem( p_input ) );
593 void PLModel::processInputItemUpdate( input_item_t *p_item )
595 if( !p_item || p_item->i_id <= 0 ) return;
596 PLItem *item = findByInput( rootItem, p_item->i_id );
597 if( item )
598 updateTreeItem( item );
601 void PLModel::processItemRemoval( int i_id )
603 if( i_id <= 0 ) return;
604 removeItem( i_id );
607 void PLModel::processItemAppend( int i_item, int i_parent )
609 playlist_item_t *p_item = NULL;
610 PLItem *newItem = NULL;
611 input_thread_t *currentInputThread;
612 int pos;
614 PLItem *nodeItem = findById( rootItem, i_parent );
615 if( !nodeItem ) return;
617 foreach( PLItem *existing, nodeItem->children )
618 if( existing->i_id == i_item ) return;
620 PL_LOCK;
621 p_item = playlist_ItemGetById( p_playlist, i_item );
622 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
624 for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
625 if( p_item->p_parent->pp_children[pos] == p_item ) break;
627 newItem = new PLItem( p_item, nodeItem );
628 PL_UNLOCK;
630 currentInputThread = THEMIM->getInput();
631 if( currentInputThread &&
632 newItem->p_input == input_GetItem( currentInputThread ) )
633 currentItem = newItem;
635 beginInsertRows( index( nodeItem, 0 ), pos, pos );
636 nodeItem->insertChild( newItem, pos );
637 endInsertRows();
639 if( currentItem == newItem )
640 emit currentChanged( index( newItem, 0 ) );
642 return;
643 end:
644 PL_UNLOCK;
645 return;
649 void PLModel::rebuild()
651 rebuild( NULL, false );
654 void PLModel::rebuild( playlist_item_t *p_root, bool b_first )
656 playlist_item_t* p_item;
658 /* Invalidate cache */
659 i_cached_id = i_cached_input_id = -1;
661 if( rootItem ) rootItem->removeChildren();
663 PL_LOCK;
664 if( p_root )
666 delete rootItem;
667 rootItem = new PLItem( p_root );
669 assert( rootItem );
670 /* Recreate from root */
671 updateChildren( rootItem );
672 PL_UNLOCK;
674 /* And signal the view */
675 reset();
678 void PLModel::takeItem( PLItem *item )
680 assert( item );
681 PLItem *parent = item->parentItem;
682 assert( parent );
683 int i_index = parent->children.indexOf( item );
685 beginRemoveRows( index( parent, 0 ), i_index, i_index );
686 parent->takeChildAt( i_index );
687 endRemoveRows();
690 void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
692 assert( node );
693 int count = items.size();
694 if( !count ) return;
695 beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
696 for( int i = 0; i < count; i++ )
698 node->children.insert( i_pos + i, items[i] );
699 items[i]->parentItem = node;
701 endInsertRows();
704 void PLModel::removeItem( PLItem *item )
706 if( !item ) return;
708 if( item->i_id == i_cached_id ) i_cached_id = -1;
709 i_cached_input_id = -1;
711 if( currentItem == item || rootItem == item)
713 currentItem = NULL;
714 emit currentChanged( QModelIndex() );
717 if(item == rootItem)
718 rootItem = NULL;
720 if( item->parentItem ) {
721 int i = item->parentItem->children.indexOf( item );
722 beginRemoveRows( index( item->parentItem, 0), i, i );
723 item->parentItem->children.removeAt(i);
724 delete item;
725 endRemoveRows();
727 else delete item;
731 /* This function must be entered WITH the playlist lock */
732 void PLModel::updateChildren( PLItem *root )
734 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
735 currentItem = NULL;
736 updateChildren( p_node, root );
737 emit currentChanged( index( currentItem, 0 ) );
740 /* This function must be entered WITH the playlist lock */
741 void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
743 playlist_item_t *p_item = playlist_CurrentPlayingItem(p_playlist);
744 for( int i = 0; i < p_node->i_children ; i++ )
746 if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
747 PLItem *newItem = new PLItem( p_node->pp_children[i], root );
748 root->appendChild( newItem );
749 if( p_item && newItem->p_input == p_item->p_input )
751 currentItem = newItem;
753 if( p_node->pp_children[i]->i_children != -1 )
754 updateChildren( p_node->pp_children[i], newItem );
758 /* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
759 void PLModel::updateTreeItem( PLItem *item )
761 if( !item ) return;
762 emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
765 /************************* Actions ******************************/
768 * Deletion, here we have to do a ugly slow hack as we retrieve the full
769 * list of indexes to delete at once: when we delete a node and all of
770 * its children, we need to update the list.
771 * Todo: investigate whethere we can use ranges to be sure to delete all items?
773 void PLModel::doDelete( QModelIndexList selected )
775 if( !canEdit() ) return;
777 for( int i = selected.size() -1 ; i >= 0; i-- )
779 QModelIndex index = selected[i];
780 if( index.column() != 0 ) continue;
781 PLItem *item = getItem( index );
782 if( item )
784 if( item->children.size() )
785 recurseDelete( item->children, &selected );
786 doDeleteItem( item, &selected );
788 if( i > selected.size() ) i = selected.size();
792 void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
794 for( int i = children.size() - 1; i >= 0 ; i-- )
796 PLItem *item = children[i];
797 if( item->children.size() )
798 recurseDelete( item->children, fullList );
799 doDeleteItem( item, fullList );
803 void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
805 QModelIndex deleteIndex = index( item, 0 );
806 fullList->removeAll( deleteIndex );
808 PL_LOCK;
809 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
810 if( !p_item )
812 PL_UNLOCK;
813 return;
815 playlist_DeleteFromInput( p_playlist, p_item->p_input, pl_Locked );
816 PL_UNLOCK;
818 /* And finally, remove it from the tree */
819 removeItem( item );
822 /******* Volume III: Sorting and searching ********/
823 void PLModel::sort( int column, Qt::SortOrder order )
825 sort( rootItem->i_id, column, order );
828 void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
830 msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
832 int meta = columnToMeta( column );
833 if( meta == COLUMN_END ) return;
835 PLItem *item = findById( rootItem, i_root_id );
836 if( !item ) return;
837 QModelIndex qIndex = index( item, 0 );
838 int count = item->children.size();
839 if( count )
841 beginRemoveRows( qIndex, 0, count - 1 );
842 item->removeChildren();
843 endRemoveRows( );
846 PL_LOCK;
848 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
849 i_root_id );
850 if( p_root )
852 playlist_RecursiveNodeSort( p_playlist, p_root,
853 i_column_sorting( meta ),
854 order == Qt::AscendingOrder ?
855 ORDER_NORMAL : ORDER_REVERSE );
859 i_cached_id = i_cached_input_id = -1;
861 if( count )
863 beginInsertRows( qIndex, 0, count - 1 );
864 updateChildren( item );
865 endInsertRows( );
867 PL_UNLOCK;
870 void PLModel::search( const QString& search_text )
872 /** \todo Fire the search with a small delay ? */
873 PL_LOCK;
875 playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
876 rootItem->i_id );
877 assert( p_root );
878 const char *psz_name = search_text.toUtf8().data();
879 playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
881 PL_UNLOCK;
882 rebuild();
885 /*********** Popup *********/
886 void PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
888 int i_id = index.isValid() ? itemId( index ) : rootItem->i_id;
890 PL_LOCK;
891 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
892 if( !p_item )
894 PL_UNLOCK; return;
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 ( p_item->i_id );
900 i_popup_column = index.column();
901 /* check whether we are in tree view */
902 bool tree = false;
903 playlist_item_t *p_up = p_item;
904 while( p_up )
906 if ( p_up == p_playlist->p_root_category ) tree = true;
907 p_up = p_up->p_parent;
909 PL_UNLOCK;
911 current_selection = list;
913 QMenu menu;
914 if( i_popup_item > -1 )
916 menu.addAction( qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
917 menu.addAction( qtr(I_POP_DEL), this, SLOT( popupDel() ) );
918 menu.addSeparator();
919 menu.addAction( qtr(I_POP_STREAM), this, SLOT( popupStream() ) );
920 menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
921 menu.addSeparator();
922 menu.addAction( qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
923 menu.addSeparator();
924 QMenu *sort_menu = menu.addMenu( qtr( "Sort by ") +
925 qfu( psz_column_title( columnToMeta( index.column() ) ) ) );
926 sort_menu->addAction( qtr( "Ascending" ),
927 this, SLOT( popupSortAsc() ) );
928 sort_menu->addAction( qtr( "Descending" ),
929 this, SLOT( popupSortDesc() ) );
931 if( tree && canEdit() )
932 menu.addAction( qtr(I_POP_ADD), this, SLOT( popupAddNode() ) );
933 if( i_popup_item > -1 )
935 menu.addSeparator();
936 menu.addAction( qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
938 if( !menu.isEmpty() ) menu.exec( point );
941 void PLModel::popupDel()
943 doDelete( current_selection );
946 void PLModel::popupPlay()
948 PL_LOCK;
950 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
951 i_popup_item );
952 activateItem( p_item );
954 PL_UNLOCK;
957 void PLModel::popupInfo()
959 PL_LOCK;
960 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
961 i_popup_item );
962 if( p_item )
964 input_item_t* p_input = p_item->p_input;
965 vlc_gc_incref( p_input );
966 PL_UNLOCK;
967 MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
968 vlc_gc_decref( p_input );
969 mid->setParent( PlaylistDialog::getInstance( p_intf ),
970 Qt::Dialog );
971 mid->show();
972 } else
973 PL_UNLOCK;
976 void PLModel::popupStream()
978 QStringList mrls = selectedURIs();
979 if( !mrls.isEmpty() )
980 THEDP->streamingDialog( NULL, mrls[0], false );
984 void PLModel::popupSave()
986 QStringList mrls = selectedURIs();
987 if( !mrls.isEmpty() )
988 THEDP->streamingDialog( NULL, mrls[0] );
991 #include <QUrl>
992 #include <QFileInfo>
993 #include <QDesktopServices>
994 void PLModel::popupExplore()
996 PL_LOCK;
997 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
998 i_popup_item );
999 if( p_item )
1001 input_item_t *p_input = p_item->p_input;
1002 char *psz_meta = input_item_GetURI( p_input );
1003 PL_UNLOCK;
1004 if( psz_meta )
1006 const char *psz_access;
1007 const char *psz_demux;
1008 char *psz_path;
1009 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_meta );
1011 if( EMPTY_STR( psz_access ) ||
1012 !strncasecmp( psz_access, "file", 4 ) ||
1013 !strncasecmp( psz_access, "dire", 4 ) )
1015 QFileInfo info( qfu( psz_meta ) );
1016 QDesktopServices::openUrl(
1017 QUrl::fromLocalFile( info.absolutePath() ) );
1019 free( psz_meta );
1022 else
1023 PL_UNLOCK;
1026 #include <QInputDialog>
1027 void PLModel::popupAddNode()
1029 bool ok;
1030 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
1031 qtr( I_POP_ADD ), qtr( "Enter name for new node:" ),
1032 QLineEdit::Normal, QString(), &ok);
1033 if( !ok || name.isEmpty() ) return;
1034 PL_LOCK;
1035 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
1036 i_popup_parent );
1037 if( p_item )
1039 playlist_NodeCreate( p_playlist, qtu( name ), p_item, 0, NULL );
1041 PL_UNLOCK;
1044 void PLModel::popupSortAsc()
1046 sort( i_popup_parent, i_popup_column, Qt::AscendingOrder );
1049 void PLModel::popupSortDesc()
1051 sort( i_popup_parent, i_popup_column, Qt::DescendingOrder );