playtree.cpp 6.68 KB
Newer Older
1 2 3
/*****************************************************************************
 * playtree.cpp
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
Clément Stenac's avatar
Clément Stenac committed
8
 *          Clément Stenac <zorglub@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29 30 31
#include <vlc/vlc.h>

#include "playtree.hpp"
Clément Stenac's avatar
Clément Stenac committed
32
#include <vlc_playlist.h>
33 34
#include "../utils/ustring.hpp"

Clément Stenac's avatar
Clément Stenac committed
35
#include "vlc_charset.h"
36

37

38
Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
39 40 41 42
{
    // Get the VLC playlist object
    m_pPlaylist = pIntf->p_sys->p_playlist;

Clément Stenac's avatar
Clément Stenac committed
43 44
    i_items_to_append = 0;

45 46 47 48 49 50 51 52 53
    buildTree();
}

Playtree::~Playtree()
{
}

void Playtree::delSelected()
{
54 55 56
    Iterator it = begin();
    vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
    for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
57
    {
58
        if( (*it).m_selected && !(*it).isReadonly() )
59
        {
60 61 62 63 64 65 66 67 68 69 70 71 72
            (*it).m_deleted = true;
        }
    }
    /// \todo Do this better (handle item-deleted)
    tree_update descr;
    descr.i_type = 3;
    notify( &descr );
    it = begin();
    while( it != end() )
    {
        if( (*it).m_deleted )
        {
            VarTree::Iterator it2;
73 74 75
            playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
            if( p_item->i_children == -1 )
            {
76
                playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
77
                                          p_item->p_input->i_id, true );
78 79 80
                it2 = getNextVisibleItem( it ) ;
                it->parent()->removeChild( it );
                it = it2;
81 82 83 84
            }
            else
            {
                playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
85
                                     true, false );
86 87 88
                it2 = getNextSibling( it );
                it->parent()->removeChild( it );
                it = it2;
89 90
            }
        }
91 92 93 94
        else
        {
            it = getNextVisibleItem( it );
        }
95
    }
96
    vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
97 98 99 100 101 102
}

void Playtree::action( VarTree *pItem )
{
    vlc_mutex_lock( &m_pPlaylist->object_lock );
    VarTree::Iterator it;
103 104 105 106 107 108 109 110 111 112 113

    playlist_item_t *p_item = (playlist_item_t *)pItem->m_pData;
    playlist_item_t *p_parent = p_item;
    while( p_parent )
    {
        if( p_parent == m_pPlaylist->p_root_category )
            break;
        p_parent = p_parent->p_parent;
    }

    if( p_parent )
114
    {
115
        playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, true, p_parent, p_item );
116 117 118 119 120 121 122
    }
    vlc_mutex_unlock( &m_pPlaylist->object_lock );
}

void Playtree::onChange()
{
    buildTree();
Clément Stenac's avatar
Clément Stenac committed
123 124 125
    tree_update descr;
    descr.i_type = 1;
    notify( &descr );
126 127
}

Clément Stenac's avatar
Clément Stenac committed
128
void Playtree::onUpdateItem( int id )
129
{
130
    Iterator it = findById( id );
131 132
    tree_update descr;
    descr.b_active_item = false;
133
    if( it != end() )
134
    {
135 136
        // Update the item
        playlist_item_t* pNode = (playlist_item_t*)(it->m_pData);
137
        UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
138 139
        it->m_cString = UStringPtr( pName );
        it->m_playing = m_pPlaylist->status.p_item == pNode;
140
        if( it->m_playing ) descr.b_active_item = true;
141 142 143
    }
    else
    {
Clément Stenac's avatar
Clément Stenac committed
144
        msg_Warn(getIntf(), "cannot find node with id %d", id );
145
    }
Clément Stenac's avatar
Clément Stenac committed
146 147 148 149
    descr.i_type = 0;
    notify( &descr );
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/// \todo keep a list of "recently removed" to avoid looking up if we
//  already removed it
void Playtree::onDelete( int i_id )
{
    tree_update descr;
    descr.i_id = i_id;
    descr.i_type = 3;
    Iterator item = findById( i_id ) ;
    if( item != end() )
    {
        if( item->parent() )
            item->parent()->removeChild( item );
        descr.b_visible = item->parent() ? true : item->parent()->m_expanded;
        notify( &descr );
    }
}

Clément Stenac's avatar
Clément Stenac committed
167 168
void Playtree::onAppend( playlist_add_t *p_add )
{
Clément Stenac's avatar
Clément Stenac committed
169 170
    i_items_to_append --;

Clément Stenac's avatar
Clément Stenac committed
171 172 173 174 175 176 177
    Iterator node = findById( p_add->i_node );
    if( node != end() )
    {
        Iterator item =  findById( p_add->i_item );
        if( item == end() )
        {
            playlist_item_t *p_item = playlist_ItemGetById(
178
                                        m_pPlaylist, p_add->i_item, true );
Clément Stenac's avatar
Clément Stenac committed
179
            if( !p_item ) return;
180 181
            UString *pName = new UString( getIntf(),
                                          p_item->p_input->psz_name );
Clément Stenac's avatar
Clément Stenac committed
182
            node->add( p_add->i_item, UStringPtr( pName ),
183 184
                      false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
                      p_item );
Clément Stenac's avatar
Clément Stenac committed
185 186 187 188 189
        }
    }
    tree_update descr;
    descr.i_id = p_add->i_item;
    descr.i_parent = p_add->i_node;
Clément Stenac's avatar
Clément Stenac committed
190
    descr.b_visible = node->m_expanded;
Clément Stenac's avatar
Clément Stenac committed
191 192
    descr.i_type = 2;
    notify( &descr );
193 194 195 196 197 198 199
}

void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
{
    for( int i = 0; i < pNode->i_children; i++ )
    {
        UString *pName = new UString( getIntf(),
200 201
                                   pNode->pp_children[i]->p_input->psz_name );
        rTree.add( pNode->pp_children[i]->p_input->i_id, UStringPtr( pName ),
202
                     false,
203
                     m_pPlaylist->status.p_item == pNode->pp_children[i],
204 205
                     false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
                     pNode->pp_children[i] );
206
        if( pNode->pp_children[i]->i_children )
207
        {
208
            buildNode( pNode->pp_children[i], rTree.back() );
209 210 211 212 213 214 215 216 217
        }
    }
}

void Playtree::buildTree()
{
    clear();
    vlc_mutex_lock( &m_pPlaylist->object_lock );

Clément Stenac's avatar
Clément Stenac committed
218 219
    i_items_to_append = 0;

220 221
    clear();

222 223
    /* TODO: Let user choose view - Stick with category ATM */

224
    /* Set the root's name */
225 226
    UString *pName = new UString( getIntf(),
                             m_pPlaylist->p_root_category->p_input->psz_name );
227 228
    m_cString = UStringPtr( pName );

229
    buildNode( m_pPlaylist->p_root_category, *this );
230 231

    vlc_mutex_unlock( &m_pPlaylist->object_lock );
232 233
//  What is it ?
//    checkParents( NULL );
234
}
235