playtree.cpp 9.78 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
 *          Erwan Tulou    <erwan10@videolan.org>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25
 *****************************************************************************/

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

30
#include <vlc_common.h>
31 32

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

37 38
Playtree::Playtree( intf_thread_t *pIntf )
    : VarTree( pIntf ), m_pPlaylist( pIntf->p_sys->p_playlist )
39
{
40
    getPositionVar().addObserver( this );
41 42 43 44 45
    buildTree();
}

Playtree::~Playtree()
{
46
    getPositionVar().delObserver( this );
47 48 49 50
}

void Playtree::delSelected()
{
51
    for( Iterator it = m_children.begin(); it != m_children.end(); )
52
    {
Erwan Tulou's avatar
Erwan Tulou committed
53
        if( it->isSelected() && !it->isReadonly() )
54
        {
55 56 57 58 59
            playlist_Lock( m_pPlaylist );

            playlist_item_t *pItem =
                playlist_ItemGetById( m_pPlaylist, it->getId() );
            if( pItem )
60
            {
61 62 63 64 65 66 67 68 69
                if( pItem->i_children == -1 )
                {
                    playlist_DeleteFromInput( m_pPlaylist, pItem->p_input,
                                              pl_Locked );
                }
                else
                {
                    playlist_NodeDelete( m_pPlaylist, pItem, true, false );
                }
70
            }
71 72 73
            playlist_Unlock( m_pPlaylist );

            it = it->getNextSiblingOrUncle();
74
        }
75 76
        else
        {
77
            it = getNextItem( it );
78
        }
79
    }
80 81
}

82
void Playtree::action( VarTree *pElem )
83
{
84
    playlist_Lock( m_pPlaylist );
85

86 87 88
    playlist_item_t *pItem =
        playlist_ItemGetById( m_pPlaylist, pElem->getId() );
    if( pItem )
89
    {
90 91
        playlist_Control( m_pPlaylist, PLAYLIST_VIEWPLAY,
                          pl_Locked, pItem->p_parent, pItem );
92 93
    }

94
    playlist_Unlock( m_pPlaylist );
95 96 97 98 99
}

void Playtree::onChange()
{
    buildTree();
100
    tree_update descr( tree_update::ResetAll, end() );
Clément Stenac's avatar
Clément Stenac committed
101
    notify( &descr );
102 103
}

Clément Stenac's avatar
Clément Stenac committed
104
void Playtree::onUpdateItem( int id )
105
{
106
    Iterator it = findById( id );
107
    if( it != m_children.end() )
108
    {
109
        // Update the item
110 111 112 113 114 115 116 117 118
        playlist_Lock( m_pPlaylist );
        playlist_item_t *pNode =
            playlist_ItemGetById( m_pPlaylist, it->getId() );
        if( !pNode )
        {
            playlist_Unlock( m_pPlaylist );
            return;
        }

119
        UString *pName = getTitle( pNode->p_input );
120
        playlist_Unlock( m_pPlaylist );
Erwan Tulou's avatar
Erwan Tulou committed
121

122 123 124 125 126 127 128 129
        if( *pName != *(it->getString()) )
        {
            it->setString( UStringPtr( pName ) );

            tree_update descr(
                tree_update::ItemUpdated, IteratorVisible( it, this ) );
            notify( &descr );
        }
Erwan Tulou's avatar
Erwan Tulou committed
130 131 132 133 134
        else
        {
            delete pName;
        }

135 136 137
    }
    else
    {
138
        msg_Warn( getIntf(), "cannot find node with id %d", id );
139
    }
Clément Stenac's avatar
Clément Stenac committed
140 141
}

142
void Playtree::onUpdateCurrent( bool b_active )
143
{
Erwan Tulou's avatar
Erwan Tulou committed
144
    if( b_active )
145 146
    {
        playlist_Lock( m_pPlaylist );
147

148 149 150 151 152 153 154 155
        playlist_item_t* current = playlist_CurrentPlayingItem( m_pPlaylist );
        if( !current )
        {
            playlist_Unlock( m_pPlaylist );
            return;
        }

        Iterator it = findById( current->i_id );
156 157
        if( it != m_children.end() )
        {
Erwan Tulou's avatar
Erwan Tulou committed
158
            it->setPlaying( true );
159

160 161 162 163 164
            tree_update descr(
                tree_update::ItemUpdated, IteratorVisible( it, this ) );
            notify( &descr );
        }

165
        playlist_Unlock( m_pPlaylist );
166 167 168 169 170 171 172 173 174
    }
    else
    {
        for( Iterator it = m_children.begin(); it != m_children.end();
             it = getNextItem( it ) )
        {
            if( it->isPlaying() )
            {
                it->setPlaying( false );
175

176 177 178 179 180 181
                tree_update descr(
                    tree_update::ItemUpdated, IteratorVisible( it, this ) );
                notify( &descr );
                break;
            }
        }
Erwan Tulou's avatar
Erwan Tulou committed
182
    }
183 184
}

185 186
void Playtree::onDelete( int i_id )
{
187 188
    Iterator it = findById( i_id ) ;
    if( it != m_children.end() )
189
    {
190 191 192 193 194 195
        VarTree* parent = it->parent();
        if( parent )
        {
            tree_update descr(
                tree_update::DeletingItem, IteratorVisible( it, this ) );
            notify( &descr );
196

197 198
            parent->removeChild( it );
            m_allItems.erase( i_id );
199

200 201 202 203
            tree_update descr2(
                tree_update::ItemDeleted, end() );
            notify( &descr2 );
        }
204 205 206
    }
}

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

221 222 223 224
        int pos;
        for( pos = 0; pos < pItem->p_parent->i_children; pos++ )
            if( pItem->p_parent->pp_children[pos] == pItem ) break;

225
        UString *pName = getTitle( pItem->p_input );
226 227 228 229 230 231 232 233
        playlist_item_t* current = playlist_CurrentPlayingItem( m_pPlaylist );

        Iterator it = it_node->add(
            p_add->i_item, UStringPtr( pName ), false, pItem == current,
            false, pItem->i_flags & PLAYLIST_RO_FLAG, pos );

        m_allItems[pItem->i_id] = &*it;

234
        playlist_Unlock( m_pPlaylist );
Erwan Tulou's avatar
Erwan Tulou committed
235

236 237 238
        tree_update descr(
            tree_update::ItemInserted,
            IteratorVisible( it, this ) );
Erwan Tulou's avatar
Erwan Tulou committed
239
        notify( &descr );
Clément Stenac's avatar
Clément Stenac committed
240
    }
241 242 243 244
}

void Playtree::buildNode( playlist_item_t *pNode, VarTree &rTree )
{
245
    UString *pName = getTitle( pNode->p_input );
246 247 248 249 250 251
    Iterator it = rTree.add(
        pNode->i_id, UStringPtr( pName ), false,
        playlist_CurrentPlayingItem(m_pPlaylist) == pNode,
        false, pNode->i_flags & PLAYLIST_RO_FLAG );
    m_allItems[pNode->i_id] = &*it;

252 253
    for( int i = 0; i < pNode->i_children; i++ )
    {
254
        buildNode( pNode->pp_children[i], *it );
255 256 257 258 259 260
    }
}

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

263 264 265 266
    for( int i = 0; i < m_pPlaylist->p_root->i_children; i++ )
    {
        buildNode( m_pPlaylist->p_root->pp_children[i], *this );
    }
267

268 269
    playlist_Unlock( m_pPlaylist );
}
270

271 272 273 274 275
void Playtree::onUpdateSlider()
{
    tree_update descr( tree_update::SliderChanged, end() );
    notify( &descr );
}
276

277 278 279 280 281 282 283 284
void Playtree::insertItems( VarTree& elem, const list<string>& files, bool start )
{
    bool first = true;
    VarTree* p_elem = &elem;
    playlist_item_t* p_node = NULL;
    int i_pos = -1;

    playlist_Lock( m_pPlaylist );
285

286 287 288 289 290 291 292 293 294 295 296 297
    if( p_elem == this )
    {
        for( Iterator it = m_children.begin(); it != m_children.end(); ++it )
        {
            if( it->getId() == m_pPlaylist->p_local_category->i_id )
            {
                p_elem = &*it;
                break;
            }
        }
    }

298 299 300 301
    if( p_elem->getId() == m_pPlaylist->p_local_category->i_id )
    {
        p_node = m_pPlaylist->p_local_category;
        i_pos = 0;
302
        p_elem->setExpanded( true );
303 304 305 306 307
    }
    else if( p_elem->getId() == m_pPlaylist->p_ml_category->i_id )
    {
        p_node = m_pPlaylist->p_ml_category;
        i_pos = 0;
308
        p_elem->setExpanded( true );
309
    }
Erwan Tulou's avatar
Erwan Tulou committed
310
    else if( p_elem->size() && p_elem->isExpanded() )
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    {
        p_node = playlist_ItemGetById( m_pPlaylist, p_elem->getId() );
        i_pos = 0;
    }
    else
    {
        p_node = playlist_ItemGetById( m_pPlaylist,
                                       p_elem->parent()->getId() );
        i_pos = p_elem->getIndex();
        i_pos++;
    }

    if( !p_node )
        goto fin;

    for( list<string>::const_iterator it = files.begin();
         it != files.end(); ++it, i_pos++, first = false )
    {
329
        input_item_t *pItem;
330

331 332 333
        if( strstr( it->c_str(), "://" ) )
            pItem = input_item_New( it->c_str(), NULL );
        else
334
        {
335 336 337 338 339
            char *psz_uri = vlc_path2uri( it->c_str(), NULL );
            if( psz_uri == NULL )
                continue;
            pItem = input_item_New( psz_uri, NULL );
            free( psz_uri );
340
        }
341 342 343 344 345 346 347 348 349 350

        if( pItem == NULL)
            continue;

        int i_mode = PLAYLIST_APPEND;
        if( first && start )
            i_mode |= PLAYLIST_GO;

        playlist_NodeAddInput( m_pPlaylist, pItem, p_node,
                               i_mode, i_pos, pl_Locked );
351 352 353
    }

fin:
354
    playlist_Unlock( m_pPlaylist );
355
}
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

UString* Playtree::getTitle( input_item_t *pItem )
{
    char *psz_name = input_item_GetTitle( pItem );
    if( EMPTY_STR( psz_name ) )
    {
        free( psz_name );
        psz_name = input_item_GetName( pItem );
    }
    if( !psz_name )
        psz_name = strdup ( "" );
    UString *pTitle = new UString( getIntf(), psz_name );
    free( psz_name );
    return pTitle;
}


374 375 376 377 378 379 380 381
VarTree::Iterator Playtree::findById( int id )
{
    map<int,VarTree*>::iterator it = m_allItems.find( id );
    if( it == m_allItems.end() )
        return m_children.end();
    else
        return it->second->getSelf();
}