1 /*****************************************************************************
2 * playlist_model.cpp : Manage playlist model
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include <QApplication>
26 #include "playlist_model.hpp"
29 static int PlaylistChanged( vlc_object_t *, const char *,
30 vlc_value_t, vlc_value_t, void * );
31 static int PlaylistNext( vlc_object_t *, const char *,
32 vlc_value_t, vlc_value_t, void * );
33 static int ItemChanged( vlc_object_t *, const char *,
34 vlc_value_t, vlc_value_t, void * );
35 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
36 vlc_value_t oval, vlc_value_t nval, void *param );
37 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
38 vlc_value_t oval, vlc_value_t nval, void *param );
40 /*************************************************************************
41 * Playlist item implementation
42 *************************************************************************/
51 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
54 i_id = _i_id; i_input_id = _i_input_id;
61 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
63 init( _i_id, _i_input_id, parent, m );
66 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
68 init( p_item->i_id, p_item->p_input->i_id, parent, m );
76 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
80 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
81 children.append( item );
83 model->endInsertRows();
86 int PLItem::row() const
89 return parentItem->children.indexOf(const_cast<PLItem*>(this));
93 void PLItem::update( playlist_item_t *p_item )
95 assert( p_item->p_input->i_id == i_input_id );
96 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
97 if( p_item->p_input->p_meta )
99 strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
103 /*************************************************************************
104 * Playlist model implementation
105 *************************************************************************/
107 PLModel::PLModel( playlist_t *_p_playlist,
108 playlist_item_t * p_root, int _i_depth, QObject *parent)
109 : QAbstractItemModel(parent)
112 assert( i_depth == 1 || i_depth == -1 );
113 p_playlist= _p_playlist;
114 i_items_to_append = 0;
115 b_need_update = false;
117 i_cached_input_id = -1;
120 rebuildRoot( p_root );
124 void PLModel::rebuildRoot( playlist_item_t *p_root )
126 if( rootItem ) delete rootItem;
127 rootItem = new PLItem( p_root, NULL, this );
128 rootItem->strings[0] = qtr("Name");
129 rootItem->strings[1] = qtr("Artist");
138 void PLModel::addCallbacks()
140 /* Some global changes happened -> Rebuild all */
141 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
142 /* We went to the next item */
143 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
144 /* One item has been updated */
145 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
146 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
147 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
150 void PLModel::delCallbacks()
152 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
153 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
154 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
155 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
156 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
159 void PLModel::activateItem( const QModelIndex &index )
161 assert( index.isValid() );
162 PLItem *item = static_cast<PLItem*>(index.internalPointer());
165 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
166 playlist_item_t *p_parent = p_item;
169 if( p_parent->i_id == rootItem->i_id ) break;
170 p_parent = p_parent->p_parent;
174 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
179 /****************** Base model mandatory implementations *****************/
180 QVariant PLModel::data(const QModelIndex &index, int role) const
182 if ( !index.isValid() || role != Qt::DisplayRole ) return QVariant();
183 PLItem *item = static_cast<PLItem*>(index.internalPointer());
184 return QVariant( item->columnString( index.column() ) );
187 int PLModel::itemId( const QModelIndex &index ) const
189 assert( index.isValid() );
190 return static_cast<PLItem*>(index.internalPointer())->i_id;
193 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
195 if( !index.isValid() ) return Qt::ItemIsEnabled;
196 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
199 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
202 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
203 return QVariant( rootItem->columnString( section ) );
207 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
211 if (!parent.isValid())
212 parentItem = rootItem;
214 parentItem = static_cast<PLItem*>(parent.internalPointer());
216 PLItem *childItem = parentItem->child(row);
218 return createIndex(row, column, childItem);
220 return QModelIndex();
223 /* Return the index of a given item */
224 QModelIndex PLModel::index( PLItem *item, int column ) const
226 if( !item ) return QModelIndex();
227 const PLItem *parent = item->parent();
229 return createIndex( parent->children.lastIndexOf( item ), column, item );
230 return QModelIndex();
233 QModelIndex PLModel::parent(const QModelIndex &index) const
235 if (!index.isValid()) return QModelIndex();
237 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
238 PLItem *parentItem = childItem->parent();
240 if (parentItem == rootItem) return QModelIndex();
242 return createIndex(parentItem->row(), 0, parentItem);
245 int PLModel::columnCount( const QModelIndex &i) const
247 if( i_depth == 1 ) return 1;
251 int PLModel::childrenCount(const QModelIndex &parent) const
253 return rowCount( parent );
256 int PLModel::rowCount(const QModelIndex &parent) const
260 if (!parent.isValid())
261 parentItem = rootItem;
263 parentItem = static_cast<PLItem*>(parent.internalPointer());
265 return parentItem->childCount();
268 /************************* Lookups *****************************/
270 PLItem *PLModel::FindById( PLItem *root, int i_id )
272 return FindInner( root, i_id, false );
275 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
277 return FindInner( root, i_id, true );
280 #define CACHE( i, p ) i_cached_id = i; p_cached_item = p;
281 #define ICACHE( i, p ) i_cached_input_id = i; p_cached_item_bi = p;
283 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
285 if( ( !b_input && i_cached_id == i_id) ||
286 ( b_input && i_cached_input_id ==i_id ) )
288 return b_input ? p_cached_item_bi : p_cached_item;
291 if( !b_input && root->i_id == i_id )
296 else if( b_input && root->i_input_id == i_id )
298 ICACHE( i_id, root );
302 QList<PLItem *>::iterator it = root->children.begin();
303 while ( it != root->children.end() )
305 if( !b_input && (*it)->i_id == i_id )
307 CACHE( i_id, (*it) );
308 return p_cached_item;
310 else if( b_input && (*it)->i_input_id == i_id )
312 ICACHE( i_id, (*it) );
313 return p_cached_item_bi;
315 if( (*it)->children.size() )
317 PLItem *childFound = FindInner( (*it), i_id, b_input );
322 ICACHE( i_id, childFound );
326 CACHE( i_id, childFound );
339 /************************* Updates handling *****************************/
340 void PLModel::customEvent( QEvent *event )
342 int type = event->type();
343 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
344 type != ItemDelete_Type )
347 PLEvent *ple = static_cast<PLEvent *>(event);
349 if( type == ItemUpdate_Type )
350 ProcessInputItemUpdate( ple->i_id );
351 else if( type == ItemAppend_Type )
352 ProcessItemAppend( ple->p_add );
354 ProcessItemRemoval( ple->i_id );
357 /**** Events processing ****/
358 void PLModel::ProcessInputItemUpdate( int i_input_id )
360 if( i_input_id <= 0 ) return;
361 PLItem *item = FindByInput( rootItem, i_input_id );
363 UpdateTreeItem( item, true );
366 void PLModel::ProcessItemRemoval( int i_id )
368 if( i_id <= 0 ) return;
369 if( i_id == i_cached_id ) i_cached_id = -1;
370 i_cached_input_id = -1;
375 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
377 playlist_item_t *p_item = NULL;
378 PLItem *newItem = NULL;
380 if( b_need_update ) return;
382 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
383 if( !nodeItem ) goto end;
385 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
386 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
388 fprintf( stderr, "Appending item %s - parent %i (root %i)\n",
389 p_item->p_input->psz_name, p_item->p_parent->i_id,
391 if( i_depth == 1 && p_item->p_parent &&
392 p_item->p_parent->i_id != rootItem->i_id )
395 fprintf( stderr, "Still continuing\n" );
397 newItem = new PLItem( p_item, nodeItem, this );
398 nodeItem->appendChild( newItem );
399 fprintf( stderr, "duh\n" );
400 UpdateTreeItem( p_item, newItem, true );
405 void PLModel::Rebuild()
407 /* Remove callbacks before locking to avoid deadlocks */
411 /* Invalidate cache */
412 i_cached_id = i_cached_input_id = -1;
415 qDeleteAll( rootItem->children );
417 /* Recreate from root */
418 UpdateNodeChildren( rootItem );
420 /* And signal the view */
421 emit layoutChanged();
427 void PLModel::UpdateNodeChildren( PLItem *root )
429 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
430 UpdateNodeChildren( p_node, root );
433 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
435 for( int i = 0; i < p_node->i_children ; i++ )
437 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
438 root->appendChild( newItem, false );
439 UpdateTreeItem( newItem, false, true );
440 if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
441 UpdateNodeChildren( p_node->pp_children[i], newItem );
445 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
447 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
448 UpdateTreeItem( p_item, item, signal, force );
451 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
452 bool signal, bool force )
454 if( !force && i_depth == 1 && p_item->p_parent &&
455 p_item->p_parent->i_id == rootItem->i_id )
457 item->update( p_item );
463 /**********************************************************************
465 **********************************************************************/
466 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
467 vlc_value_t oval, vlc_value_t nval, void *param )
469 PLModel *p_model = (PLModel *) param;
470 p_model->b_need_update = VLC_TRUE;
474 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
475 vlc_value_t oval, vlc_value_t nval, void *param )
477 PLModel *p_model = (PLModel *) param;
478 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
479 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
480 event = new PLEvent( ItemUpdate_Type, nval.i_int );
481 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
485 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
486 vlc_value_t oval, vlc_value_t nval, void *param )
488 PLModel *p_model = (PLModel *) param;
489 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
490 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
494 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
495 vlc_value_t oval, vlc_value_t nval, void *param )
497 PLModel *p_model = (PLModel *) param;
498 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
499 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
503 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
504 vlc_value_t oval, vlc_value_t nval, void *param )
506 PLModel *p_model = (PLModel *) param;
507 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
508 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
510 if( ++p_model->i_items_to_append >= 50 )
512 p_model->b_need_update = VLC_TRUE;
515 PLEvent *event = new PLEvent( p_add );
516 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
517 fprintf( stderr, "Posted event to model\n" );