playtree.cpp 7.12 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * playtree.cpp
 *****************************************************************************
 * Copyright (C) 2005 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30
 *****************************************************************************/

#include <vlc/vlc.h>

#include "playtree.hpp"
#include "../utils/ustring.hpp"

#include "charset.h"

31

32
Playtree::Playtree( intf_thread_t *pIntf ): VarTree( pIntf )
33 34 35 36
{
    // Get the VLC playlist object
    m_pPlaylist = pIntf->p_sys->p_playlist;

Clément Stenac's avatar
Clément Stenac committed
37 38
    i_items_to_append = 0;

39 40 41 42
    // Try to guess the current charset
    char *pCharset;
    vlc_current_charset( &pCharset );
    iconvHandle = vlc_iconv_open( "UTF-8", pCharset );
Clément Stenac's avatar
Clément Stenac committed
43
    msg_Dbg( pIntf, "using character encoding: %s", pCharset );
44 45
    free( pCharset );

46
    if( iconvHandle == (vlc_iconv_t) - 1 )
47
    {
Clément Stenac's avatar
Clément Stenac committed
48
        msg_Warn( pIntf, "unable to do requested conversion" );
49 50 51 52 53 54 55
    }

    buildTree();
}

Playtree::~Playtree()
{
56
    if( iconvHandle != (vlc_iconv_t) - 1 ) vlc_iconv_close( iconvHandle );
57 58 59 60
}

void Playtree::delSelected()
{
61 62 63
    Iterator it = begin();
    vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
    for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
64
    {
65
        if( (*it).m_selected && !(*it).isReadonly() )
66
        {
67 68 69 70 71 72 73 74 75 76 77 78 79
            (*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;
80 81 82
            playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
            if( p_item->i_children == -1 )
            {
83
                playlist_Delete( getIntf()->p_sys->p_playlist,
84
                                     p_item->input.i_id );
85 86 87
                it2 = getNextVisibleItem( it ) ;
                it->parent()->removeChild( it );
                it = it2;
88 89 90 91 92
            }
            else
            {
                playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
                                     VLC_TRUE, VLC_FALSE );
93 94 95
                it2 = getNextSibling( it );
                it->parent()->removeChild( it );
                it = it2;
96 97
            }
        }
98 99 100 101
        else
        {
            it = getNextVisibleItem( it );
        }
102
    }
103
    vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

void Playtree::action( VarTree *pItem )
{
    vlc_mutex_lock( &m_pPlaylist->object_lock );
    VarTree::Iterator it;
    if( pItem->size() )
    {
        it = pItem->begin();
        while( it->size() ) it = it->begin();
    }
    playlist_Control( m_pPlaylist,
                      PLAYLIST_VIEWPLAY,
                      m_pPlaylist->status.i_view,
                      pItem->size()
                          ? (playlist_item_t *)pItem->m_pData
                          : (playlist_item_t *)pItem->parent()->m_pData,
                      pItem->size()
                          ? (playlist_item_t *)it->m_pData
                          : (playlist_item_t *)pItem->m_pData
                    );
    vlc_mutex_unlock( &m_pPlaylist->object_lock );
}

void Playtree::onChange()
{
    buildTree();
Clément Stenac's avatar
Clément Stenac committed
131 132 133
    tree_update descr;
    descr.i_type = 1;
    notify( &descr );
134 135
}

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

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/// \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
175 176
void Playtree::onAppend( playlist_add_t *p_add )
{
Clément Stenac's avatar
Clément Stenac committed
177 178
    i_items_to_append --;

Clément Stenac's avatar
Clément Stenac committed
179 180 181 182 183 184 185 186 187 188 189
    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(
                                        m_pPlaylist, p_add->i_item );
            if( !p_item ) return;
            UString *pName = new UString( getIntf(), p_item->input.psz_name );
            node->add( p_add->i_item, UStringPtr( pName ),
190 191
                      false,false, false, p_item->i_flags & PLAYLIST_RO_FLAG,
                      p_item );
Clément Stenac's avatar
Clément Stenac committed
192 193 194 195 196
        }
    }
    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
197
    descr.b_visible = node->m_expanded;
Clément Stenac's avatar
Clément Stenac committed
198 199
    descr.i_type = 2;
    notify( &descr );
200 201 202 203 204 205 206 207 208
}

void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
{
    for( int i = 0; i < pNode->i_children; i++ )
    {
        UString *pName = new UString( getIntf(),
                                      pNode->pp_children[i]->input.psz_name );
        rTree.add( pNode->pp_children[i]->input.i_id, UStringPtr( pName ),
209
                     false,
210
                     m_pPlaylist->status.p_item == pNode->pp_children[i],
211 212
                     false, pNode->pp_children[i]->i_flags & PLAYLIST_RO_FLAG,
                     pNode->pp_children[i] );
213
        if( pNode->pp_children[i]->i_children )
214
        {
215
            buildNode( pNode->pp_children[i], rTree.back() );
216 217 218 219 220 221 222 223 224
        }
    }
}

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

Clément Stenac's avatar
Clément Stenac committed
225 226
    i_items_to_append = 0;

227 228
    playlist_view_t *p_view;
    p_view = playlist_ViewFind( m_pPlaylist, VIEW_CATEGORY );
229
    /// \todo let the user chose the view
230 231 232 233 234 235 236 237 238 239

    clear();

    /* Set the root's name */
    UString *pName = new UString( getIntf(), p_view->p_root->input.psz_name );
    m_cString = UStringPtr( pName );

    buildNode( p_view->p_root, *this );

    vlc_mutex_unlock( &m_pPlaylist->object_lock );
240 241
//  What is it ?
//    checkParents( NULL );
242
}
243