+
+
+void PLModel::rebuild()
+{
+ rebuild( NULL );
+}
+
+void PLModel::rebuild( playlist_item_t *p_root )
+{
+ /* Remove callbacks before locking to avoid deadlocks */
+ delCallbacks();
+ /* Invalidate cache */
+ i_cached_id = i_cached_input_id = -1;
+
+ PL_LOCK;
+ /* Clear the tree */
+ if( rootItem )
+ {
+ beginRemoveRows( index( rootItem, 0 ), 0,
+ rootItem->children.size() -1 );
+ qDeleteAll( rootItem->children );
+ rootItem->children.clear();
+ endRemoveRows();
+ }
+ if( p_root )
+ {
+ //if( rootItem ) delete rootItem;
+ rootItem = new PLItem( p_root, NULL, this );
+ rootItem->strings[0] = qtr("Name");
+ rootItem->strings[1] = qtr("Artist");
+ rootItem->strings[2] = qtr("Duration");
+ }
+ assert( rootItem );
+ /* Recreate from root */
+ UpdateNodeChildren( rootItem );
+ if( p_playlist->status.p_item )
+ {
+ PLItem *currentItem = FindByInput( rootItem,
+ p_playlist->status.p_item->p_input->i_id );
+ if( currentItem )
+ {
+ UpdateTreeItem( p_playlist->status.p_item, currentItem,
+ true, false );
+ }
+ }
+ PL_UNLOCK;
+
+ /* And signal the view */
+ emit layoutChanged();
+ addCallbacks();
+}
+
+/* This function must be entered WITH the playlist lock */
+void PLModel::UpdateNodeChildren( PLItem *root )
+{
+ playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
+ VLC_TRUE );
+ UpdateNodeChildren( p_node, root );
+}
+
+/* This function must be entered WITH the playlist lock */
+void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
+{
+ for( int i = 0; i < p_node->i_children ; i++ )
+ {
+ if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
+ PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
+ root->appendChild( newItem, false );
+ UpdateTreeItem( newItem, false, true );
+ if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
+ UpdateNodeChildren( p_node->pp_children[i], newItem );
+ }
+}
+
+/* This function must be entered WITH the playlist lock */
+void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
+{
+ playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
+ VLC_TRUE );
+ UpdateTreeItem( p_item, item, signal, force );
+}
+
+/* This function must be entered WITH the playlist lock */
+void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
+ bool signal, bool force )
+{
+ if( !force && i_depth == 1 && p_item->p_parent &&
+ p_item->p_parent->i_id != rootItem->i_id )
+ return;
+ item->update( p_item, p_item == p_playlist->status.p_item );
+ if( signal )
+ emit dataChanged( index( item, 0 ) , index( item, 1 ) );
+}
+
+/************************* Actions ******************************/
+
+void PLModel::sendArt( QString url )
+{
+ QString arturl = url.replace( "file://",QString("" ) );
+ emit artSet( arturl );
+}
+
+void PLModel::removeArt()
+{
+ emit artSet( QString() );
+}
+
+/**
+ * Deletion, here we have to do a ugly slow hack as we retrieve the full
+ * list of indexes to delete at once: when we delete a node and all of
+ * its children, we need to update the list.
+ * Todo: investigate whethere we can use ranges to be sure to delete all items?
+ */
+void PLModel::doDelete( QModelIndexList selected )
+{
+ for( int i = selected.size() -1 ; i >= 0; i-- )
+ {
+ QModelIndex index = selected[i];
+ if( index.column() != 0 ) continue;
+ PLItem *item = static_cast<PLItem*>(index.internalPointer());
+ if( item )
+ {
+ if( item->children.size() )
+ recurseDelete( item->children, &selected );
+ doDeleteItem( item, &selected );
+ }
+ }
+}
+
+void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
+{
+ for( int i = children.size() - 1; i >= 0 ; i-- )
+ {
+ PLItem *item = children[i];
+ if( item->children.size() )
+ recurseDelete( item->children, fullList );
+ doDeleteItem( item, fullList );
+ }
+}
+
+void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
+{
+ QModelIndex deleteIndex = index( item, 0 );
+ fullList->removeAll( deleteIndex );
+
+ PL_LOCK;
+ playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
+ VLC_TRUE );
+ if( !p_item )
+ {
+ PL_UNLOCK; return;
+ }
+ if( p_item->i_children == -1 )
+ playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
+ else
+ playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
+ /* And finally, remove it from the tree */
+ item->remove( item );
+ PL_UNLOCK;
+}
+
+/******* Volume III: Sorting and searching ********/
+void PLModel::sort( int column, Qt::SortOrder order )
+{
+ PL_LOCK;
+ {
+ playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
+ rootItem->i_id,
+ VLC_TRUE );
+ int i_mode;
+ switch( column )
+ {
+ case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
+ case 1: i_mode = SORT_ARTIST;break;
+ case 2: i_mode = SORT_DURATION; break;
+ default: i_mode = SORT_TITLE_NODES_FIRST; break;
+ }
+ if( p_root )
+ playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
+ order == Qt::AscendingOrder ?
+ ORDER_NORMAL : ORDER_REVERSE );
+ }
+ PL_UNLOCK
+ rebuild();
+}
+
+void PLModel::search( QString search_text )
+{
+ /** \todo Fire the search with a small delay ? */
+ PL_LOCK;
+ {
+ playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
+ rootItem->i_id,
+ VLC_TRUE );
+ assert( p_root );
+ char *psz_name = search_text.toUtf8().data();
+ playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
+ }
+ PL_UNLOCK;
+ rebuild();
+}
+
+/*********** Popup *********/
+void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
+{
+ assert( index.isValid() );
+ PL_LOCK;
+ playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
+ itemId( index ), VLC_TRUE );
+ if( p_item )
+ {
+ i_popup_item = p_item->i_id;
+ i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
+ PL_UNLOCK;
+ current_selection = list;
+ QMenu *menu = new QMenu;
+ menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
+ menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
+ menu->addSeparator();
+ menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
+ menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
+ menu->addSeparator();
+ menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
+ if( p_item->i_children > -1 )
+ {
+ menu->addSeparator();
+ menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
+ menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
+ }
+ menu->popup( point );
+ }
+ else
+ PL_UNLOCK;
+}
+
+void PLModel::popupDel()
+{
+ doDelete( current_selection );
+}
+void PLModel::popupPlay()
+{
+ PL_LOCK;
+ {
+ playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
+ i_popup_item,VLC_TRUE );
+ activateItem( p_item );
+ }
+ PL_UNLOCK;
+}
+
+void PLModel::popupInfo()
+{
+ playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
+ i_popup_item,VLC_TRUE );
+ if( p_item )
+ {
+ MediaInfoDialog *mid = new MediaInfoDialog( p_intf );
+ mid->setInput( p_item->p_input );
+ mid->show();
+ }
+}
+
+void PLModel::popupStream()
+{
+ fprintf( stderr, "Stream not implemented\n" );
+}
+void PLModel::popupSave()
+{
+ fprintf( stderr, "Save not implemented\n" );
+}
+