playtree.cpp 7.18 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"

Erwan Tulou's avatar
Erwan Tulou committed
35
Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
36 37 38 39 40 41 42 43 44 45 46 47 48
{
    // Get the VLC playlist object
    m_pPlaylist = pIntf->p_sys->p_playlist;

    buildTree();
}

Playtree::~Playtree()
{
}

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

void Playtree::action( VarTree *pItem )
{
94
    playlist_Lock( m_pPlaylist );
95
    VarTree::Iterator it;
96

Erwan Tulou's avatar
Erwan Tulou committed
97
    playlist_item_t *p_item = (playlist_item_t *)pItem->getData();
98 99 100 101 102 103 104 105 106
    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 )
107
    {
108
        playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY, pl_Locked, p_parent, p_item );
109
    }
110
    playlist_Unlock( m_pPlaylist );
111 112 113 114 115
}

void Playtree::onChange()
{
    buildTree();
Clément Stenac's avatar
Clément Stenac committed
116
    tree_update descr;
117
    descr.type = tree_update::ResetAll;
Clément Stenac's avatar
Clément Stenac committed
118
    notify( &descr );
119 120
}

Clément Stenac's avatar
Clément Stenac committed
121
void Playtree::onUpdateItem( int id )
122
{
123 124
    Iterator it = findById( id );
    if( it != end() )
125
    {
126
        // Update the item
Erwan Tulou's avatar
Erwan Tulou committed
127
        playlist_item_t* pNode = (playlist_item_t*)(it->getData());
128
        UString *pName = new UString( getIntf(), pNode->p_input->psz_name );
Erwan Tulou's avatar
Erwan Tulou committed
129
        it->setString( UStringPtr( pName ) );
Erwan Tulou's avatar
Erwan Tulou committed
130 131

        tree_update descr;
132
        descr.type = tree_update::UpdateItem;
Erwan Tulou's avatar
Erwan Tulou committed
133 134 135
        descr.i_id = id;
        descr.b_active_item = false;
        notify( &descr );
136 137 138
    }
    else
    {
Clément Stenac's avatar
Clément Stenac committed
139
        msg_Warn(getIntf(), "cannot find node with id %d", id );
140
    }
Clément Stenac's avatar
Clément Stenac committed
141 142
}

143
void Playtree::onUpdateCurrent( bool b_active )
144
{
Erwan Tulou's avatar
Erwan Tulou committed
145
    for( VarTree::Iterator it = begin(); it != end(); it = getNextItem( it ) )
146
    {
Erwan Tulou's avatar
Erwan Tulou committed
147 148 149 150 151
       if( it->isPlaying() )
       {
           it->setPlaying( false );

           tree_update descr;
152
           descr.type = tree_update::UpdateItem;
Erwan Tulou's avatar
Erwan Tulou committed
153 154 155 156 157
           descr.i_id = it->getId();
           descr.b_active_item = false;
           notify( &descr );
           break;
       }
158
    }
Erwan Tulou's avatar
Erwan Tulou committed
159 160

    if( b_active )
161 162
    {
        playlist_Lock( m_pPlaylist );
163

164 165 166 167 168 169 170 171
        playlist_item_t* current = playlist_CurrentPlayingItem( m_pPlaylist );
        if( !current )
        {
            playlist_Unlock( m_pPlaylist );
            return;
        }

        Iterator it = findById( current->i_id );
172
        if( it != end() )
Erwan Tulou's avatar
Erwan Tulou committed
173
            it->setPlaying( true );
174 175

        playlist_Unlock( m_pPlaylist );
176

Erwan Tulou's avatar
Erwan Tulou committed
177
        tree_update descr;
178
        descr.type = tree_update::UpdateItem;
Erwan Tulou's avatar
Erwan Tulou committed
179 180 181 182
        descr.i_id = current->i_id;
        descr.b_active_item = true;
        notify( &descr );
    }
183 184
}

185 186 187 188 189
void Playtree::onDelete( int i_id )
{
    Iterator item = findById( i_id ) ;
    if( item != end() )
    {
190 191
        VarTree* parent = item->parent();

Erwan Tulou's avatar
Erwan Tulou committed
192
        item->setDeleted( true );
193 194

        tree_update descr;
195
        descr.type = tree_update::DeleteItem;
Erwan Tulou's avatar
Erwan Tulou committed
196
        descr.i_id = i_id;
197
        notify( &descr );
198 199 200

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

Clément Stenac's avatar
Clément Stenac committed
204 205 206 207 208
void Playtree::onAppend( playlist_add_t *p_add )
{
    Iterator node = findById( p_add->i_node );
    if( node != end() )
    {
209
        playlist_Lock( m_pPlaylist );
Erwan Tulou's avatar
Erwan Tulou committed
210 211
        playlist_item_t *p_item =
            playlist_ItemGetById( m_pPlaylist, p_add->i_item );
212
        if( !p_item )
Clément Stenac's avatar
Clément Stenac committed
213
        {
214
            playlist_Unlock( m_pPlaylist );
215
            return;
Clément Stenac's avatar
Clément Stenac committed
216
        }
Erwan Tulou's avatar
Erwan Tulou committed
217

218 219 220
        UString *pName = new UString( getIntf(),
                                      p_item->p_input->psz_name );
        node->add( p_add->i_item, UStringPtr( pName ),
Erwan Tulou's avatar
Erwan Tulou committed
221 222
                   false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
                   p_item );
223
        playlist_Unlock( m_pPlaylist );
Erwan Tulou's avatar
Erwan Tulou committed
224 225

        tree_update descr;
226
        descr.type = tree_update::AppendItem;
Erwan Tulou's avatar
Erwan Tulou committed
227 228
        descr.i_id = p_add->i_item;
        notify( &descr );
Clément Stenac's avatar
Clément Stenac committed
229
    }
230 231 232 233 234 235 236
}

void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
{
    for( int i = 0; i < pNode->i_children; i++ )
    {
        UString *pName = new UString( getIntf(),
237
                                   pNode->pp_children[i]->p_input->psz_name );
Erwan Tulou's avatar
Erwan Tulou committed
238 239 240 241 242
        rTree.add(
            pNode->pp_children[i]->i_id, UStringPtr( pName ), false,
            playlist_CurrentPlayingItem(m_pPlaylist) == pNode->pp_children[i],
            false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
            pNode->pp_children[i] );
243
        if( pNode->pp_children[i]->i_children > 0 )
244
        {
245
            buildNode( pNode->pp_children[i], rTree.back() );
246 247 248 249 250 251 252
        }
    }
}

void Playtree::buildTree()
{
    clear();
253
    playlist_Lock( m_pPlaylist );
254 255 256

    clear();

257 258
    /* TODO: Let user choose view - Stick with category ATM */

259
    /* Set the root's name */
260 261
    UString *pName = new UString( getIntf(),
                             m_pPlaylist->p_root_category->p_input->psz_name );
Erwan Tulou's avatar
Erwan Tulou committed
262
    setString( UStringPtr( pName ) );
263

264
    buildNode( m_pPlaylist->p_root_category, *this );
265

266
    playlist_Unlock( m_pPlaylist );
267
}
268