playtree.cpp 7.36 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
#include <vlc_common.h>
30 31

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

35 36
Playtree::Playtree( intf_thread_t *pIntf ):
    VarTree( pIntf ), m_currentItem( NULL )
37 38 39 40
{
    // Get the VLC playlist object
    m_pPlaylist = pIntf->p_sys->p_playlist;

Clément Stenac's avatar
Clément Stenac committed
41 42
    i_items_to_append = 0;

43 44 45 46 47 48 49 50 51
    buildTree();
}

Playtree::~Playtree()
{
}

void Playtree::delSelected()
{
52
    Iterator it = begin();
53
    playlist_Lock( getIntf()->p_sys->p_playlist );
54
    for( it = begin(); it != end(); it = getNextItem( it ) )
55
    {
56
        if( it->m_selected && !it->isReadonly() )
57
        {
58
            it->cascadeDelete();
59 60 61 62 63 64 65 66 67
        }
    }
    /// \todo Do this better (handle item-deleted)
    tree_update descr;
    descr.i_type = 3;
    notify( &descr );
    it = begin();
    while( it != end() )
    {
68
        if( it->m_deleted )
69 70
        {
            VarTree::Iterator it2;
71 72 73
            playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
            if( p_item->i_children == -1 )
            {
74
                playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
75
                                          p_item->p_input, pl_Locked );
76
                it2 = getNextItem( it ) ;
77 78 79 80
            }
            else
            {
                playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
81
                                     true, false );
82
                it2 = it->getNextSiblingOrUncle();
83
            }
Erwan Tulou's avatar
Erwan Tulou committed
84 85
            it->parent()->removeChild( it );
            it = it2;
86
        }
87 88
        else
        {
89
            it = getNextItem( it );
90
        }
91
    }
92
    playlist_Unlock( getIntf()->p_sys->p_playlist );
93 94 95 96
}

void Playtree::action( VarTree *pItem )
{
97
    playlist_Lock( m_pPlaylist );
98
    VarTree::Iterator it;
99 100 101 102 103 104 105 106 107 108 109

    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 )
110
    {
111
        playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, pl_Locked, p_parent, p_item );
112
    }
113
    playlist_Unlock( m_pPlaylist );
114 115 116 117 118
}

void Playtree::onChange()
{
    buildTree();
Clément Stenac's avatar
Clément Stenac committed
119 120 121
    tree_update descr;
    descr.i_type = 1;
    notify( &descr );
122 123
}

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

144

145
void Playtree::onUpdateCurrent( bool b_active )
146
{
147
    if( !b_active )
148
    {
149
        if( !m_currentItem )
150
            return;
151

152
        Iterator it = findById( m_currentItem->i_id );
153 154
        if( it != end() )
            it->m_playing = false;
155
        m_currentItem = NULL;
156 157 158 159
    }
    else
    {
        playlist_Lock( m_pPlaylist );
160

161 162 163 164 165 166 167 168
        playlist_item_t* current = playlist_CurrentPlayingItem( m_pPlaylist );
        if( !current )
        {
            playlist_Unlock( m_pPlaylist );
            return;
        }

        Iterator it = findById( current->i_id );
169 170
        if( it != end() )
            it->m_playing = true;
171
        m_currentItem = current;
172 173 174

        playlist_Unlock( m_pPlaylist );
    }
175 176 177 178 179 180 181 182

    tree_update descr;
    descr.b_active_item = true;
    descr.i_type = 0;
    notify( &descr );
}


183 184 185 186 187 188 189
/// \todo keep a list of "recently removed" to avoid looking up if we
//  already removed it
void Playtree::onDelete( int i_id )
{
    Iterator item = findById( i_id ) ;
    if( item != end() )
    {
190 191
        VarTree* parent = item->parent();

192 193 194 195 196
        item->m_deleted = true;

        tree_update descr;
        descr.i_id = i_id;
        descr.i_type = 3;
197
        descr.b_visible = parent ? parent->m_expanded : true;
198
        notify( &descr );
199 200 201

        if( parent )
            parent->removeChild( item );
202
    }
203

204 205
}

Clément Stenac's avatar
Clément Stenac committed
206 207
void Playtree::onAppend( playlist_add_t *p_add )
{
Clément Stenac's avatar
Clément Stenac committed
208 209
    i_items_to_append --;

Clément Stenac's avatar
Clément Stenac committed
210 211 212 213 214 215
    Iterator node = findById( p_add->i_node );
    if( node != end() )
    {
        Iterator item =  findById( p_add->i_item );
        if( item == end() )
        {
216
            playlist_Lock( m_pPlaylist );
Clément Stenac's avatar
Clément Stenac committed
217
            playlist_item_t *p_item = playlist_ItemGetById(
218 219 220 221 222 223
                                        m_pPlaylist, p_add->i_item );
            if( !p_item )
            {
                playlist_Unlock( m_pPlaylist );
                return;
            }
224 225
            UString *pName = new UString( getIntf(),
                                          p_item->p_input->psz_name );
Clément Stenac's avatar
Clément Stenac committed
226
            node->add( p_add->i_item, UStringPtr( pName ),
227 228
                      false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
                      p_item );
229
            playlist_Unlock( m_pPlaylist );
Clément Stenac's avatar
Clément Stenac committed
230 231 232 233 234
        }
    }
    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
235
    descr.b_visible = node->m_expanded;
Clément Stenac's avatar
Clément Stenac committed
236 237
    descr.i_type = 2;
    notify( &descr );
238 239 240 241 242 243 244
}

void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
{
    for( int i = 0; i < pNode->i_children; i++ )
    {
        UString *pName = new UString( getIntf(),
245
                                   pNode->pp_children[i]->p_input->psz_name );
246
        rTree.add( pNode->pp_children[i]->i_id, UStringPtr( pName ),
247
                     false,
Christophe Mutricy's avatar
Christophe Mutricy committed
248
                     playlist_CurrentPlayingItem(m_pPlaylist) == pNode->pp_children[i],
249 250
                     false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
                     pNode->pp_children[i] );
251
        if( pNode->pp_children[i]->i_children > 0 )
252
        {
253
            buildNode( pNode->pp_children[i], rTree.back() );
254 255 256 257 258 259 260
        }
    }
}

void Playtree::buildTree()
{
    clear();
261
    playlist_Lock( m_pPlaylist );
262

Clément Stenac's avatar
Clément Stenac committed
263 264
    i_items_to_append = 0;

265 266
    clear();

267 268
    /* TODO: Let user choose view - Stick with category ATM */

269
    /* Set the root's name */
270 271
    UString *pName = new UString( getIntf(),
                             m_pPlaylist->p_root_category->p_input->psz_name );
272 273
    m_cString = UStringPtr( pName );

274
    buildNode( m_pPlaylist->p_root_category, *this );
275

276
    playlist_Unlock( m_pPlaylist );
277
}
278