playtree.cpp 5.1 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 21 22 23 24 25 26 27 28 29 30
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#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 37 38 39 40 41 42 43
{
    // Get the VLC playlist object
    m_pPlaylist = pIntf->p_sys->p_playlist;

    // Try to guess the current charset
    char *pCharset;
    vlc_current_charset( &pCharset );
    iconvHandle = vlc_iconv_open( "UTF-8", pCharset );
    msg_Dbg( pIntf, "Using character encoding: %s", pCharset );
    free( pCharset );

44
    if( iconvHandle == (vlc_iconv_t) - 1 )
45 46 47 48 49 50 51 52 53
    {
        msg_Warn( pIntf, "Unable to do requested conversion" );
    }

    buildTree();
}

Playtree::~Playtree()
{
54
    if( iconvHandle != (vlc_iconv_t) - 1 ) vlc_iconv_close( iconvHandle );
55 56 57 58 59
    // TODO : check that everything is destroyed
}

void Playtree::delSelected()
{
60
    Iterator it;
61
    for (it = begin(); it != end() ; it = getNextVisibleItem( it ) )
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    {
        if( (*it).m_selected )
        {
            playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
            if( p_item->i_children == -1 )
            {
                playlist_LockDelete( getIntf()->p_sys->p_playlist,
                                     p_item->input.i_id );
            }
            else
            {
                vlc_mutex_lock( &getIntf()->p_sys->p_playlist->object_lock );
                playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
                                     VLC_TRUE, VLC_FALSE );
                vlc_mutex_unlock( &getIntf()->p_sys->p_playlist->object_lock );
            }
        }
    }
80
    buildTree();
81
    notify( 1 );
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

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();
109
    notify( 1 );
110 111
}

112
void Playtree::onUpdate( int id )
113
{
114 115
    Iterator it = findById( id );
    if( it != end() )
116
    {
117 118 119 120 121 122 123 124 125 126 127
        // 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;
    }
    else
    {
        msg_Warn(getIntf(), "Cannot find node with id %d", id );
    }
    // TODO update only the right node
128
    notify( 0 );
129 130 131 132 133 134 135 136 137
}

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 ),
138
                     false,
139 140 141
                     m_pPlaylist->status.p_item == pNode->pp_children[i],
                     true, pNode->pp_children[i] );
        if( pNode->pp_children[i]->i_children )
142
        {
143
            buildNode( pNode->pp_children[i], rTree.back() );
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        }
    }
}

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

    playlist_view_t *p_view;
    p_view = playlist_ViewFind( m_pPlaylist, VIEW_CATEGORY );
    /* TODO : let the user chose the view type */

    clear();
    /* XXX : do we need Playlist::clear() instead of VarTree::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 );
    checkParents( NULL );
}
169