playlist_model.cpp 30.9 KB
Newer Older
zorglub's avatar
zorglub committed
1
/*****************************************************************************
zorglub's avatar
zorglub committed
2
 * playlist_model.cpp : Manage playlist model
zorglub's avatar
zorglub committed
3
 ****************************************************************************
4
 * Copyright (C) 2006-2011 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Ilkka Ollakkka <ileoo (at) videolan dot org>
9
 *          Jakob Leben <jleben@videolan.org>
zorglub's avatar
zorglub committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
25

26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
zorglub's avatar
zorglub committed
29

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
30
#include "qt.hpp"
31
#include "components/playlist/playlist_model.hpp"
32
#include "input_manager.hpp"                            /* THEMIM */
33
#include "util/qt_dirs.hpp"
34
#include "recents.hpp"                                  /* Open:: */
35

36
#include <vlc_intf_strings.h>                           /* I_DIR */
zorglub's avatar
zorglub committed
37

38
#include "sorting.h"
zorglub's avatar
zorglub committed
39

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
40 41
#include <assert.h>
#include <QFont>
42
#include <QAction>
Rafaël Carré's avatar
Rafaël Carré committed
43

zorglub's avatar
zorglub committed
44 45 46 47
/*************************************************************************
 * Playlist model implementation
 *************************************************************************/

48 49 50 51
PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
                  intf_thread_t *_p_intf,   /* main Qt p_intf */
                  playlist_item_t * p_root,
                  QObject *parent )         /* Basic Qt parent */
52
                  : VLCModel( _p_intf, parent )
zorglub's avatar
zorglub committed
53
{
54 55 56
    p_playlist        = _p_playlist;

    rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
57
    latestSearch      = QString();
zorglub's avatar
zorglub committed
58

jpd's avatar
jpd committed
59
    rebuild( p_root );
60
    DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
61
              this, processInputItemUpdate( input_item_t *) );
62
    DCONNECT( THEMIM, inputChanged( bool ),
63
              this, processInputItemUpdate( ) );
64 65 66 67
    CONNECT( THEMIM, playlistItemAppended( int, int ),
             this, processItemAppend( int, int ) );
    CONNECT( THEMIM, playlistItemRemoved( int ),
             this, processItemRemoval( int ) );
zorglub's avatar
zorglub committed
68 69 70 71 72 73 74
}

PLModel::~PLModel()
{
    delete rootItem;
}

zorglub's avatar
zorglub committed
75 76
Qt::DropActions PLModel::supportedDropActions() const
{
77
    return Qt::CopyAction | Qt::MoveAction;
zorglub's avatar
zorglub committed
78 79
}

80
Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
zorglub's avatar
zorglub committed
81
{
82 83
    Qt::ItemFlags flags = QAbstractItemModel::flags( index );

Ilkka Ollakka's avatar
Ilkka Ollakka committed
84
    const PLItem *item = index.isValid() ? getItem( index ) : rootItem;
85

86
    if( canEdit() )
87
    {
88 89
        vlc_playlist_locker pl_lock ( THEPL );

90
        playlist_item_t *plItem =
François Cartegnie's avatar
François Cartegnie committed
91
            playlist_ItemGetById( p_playlist, item->i_playlist_id );
92 93 94

        if ( plItem && ( plItem->i_children > -1 ) )
            flags |= Qt::ItemIsDropEnabled;
95
    }
96
    flags |= Qt::ItemIsDragEnabled;
97 98

    return flags;
zorglub's avatar
zorglub committed
99 100 101 102 103
}

QStringList PLModel::mimeTypes() const
{
    QStringList types;
104
    types << "vlc/qt-input-items";
zorglub's avatar
zorglub committed
105 106 107
    return types;
}

jpd's avatar
jpd committed
108 109 110 111 112
bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
{
    if( !i1.isValid() || !i2.isValid() ) return false;
    PLItem *item1 = static_cast<PLItem*>( i1.internalPointer() );
    PLItem *item2 = static_cast<PLItem*>( i2.internalPointer() );
113
    if( item1->hasSameParent( item2 ) ) return i1.row() < i2.row();
jpd's avatar
jpd committed
114 115 116
    else return *item1 < *item2;
}

117
QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
zorglub's avatar
zorglub committed
118
{
119
    PlMimeData *plMimeData = new PlMimeData();
120
    QModelIndexList list;
zorglub's avatar
zorglub committed
121

122
    foreach( const QModelIndex &index, indexes ) {
123
        if( index.isValid() && index.column() == 0 )
124 125 126
            list.append(index);
    }

jpd's avatar
jpd committed
127
    qSort(list.begin(), list.end(), modelIndexLessThen);
128

129
    AbstractPLItem *item = NULL;
130
    foreach( const QModelIndex &index, list ) {
jpd's avatar
jpd committed
131 132
        if( item )
        {
133
            AbstractPLItem *testee = getItem( index );
jpd's avatar
jpd committed
134 135 136 137 138 139 140 141 142 143 144 145
            while( testee->parent() )
            {
                if( testee->parent() == item ||
                    testee->parent() == item->parent() ) break;
                testee = testee->parent();
            }
            if( testee->parent() == item ) continue;
            item = getItem( index );
        }
        else
            item = getItem( index );

146
        plMimeData->appendItem( static_cast<PLItem*>(item)->inputItem() );
zorglub's avatar
zorglub committed
147
    }
148

149
    return plMimeData;
zorglub's avatar
zorglub committed
150 151
}

152
/* Drop operation */
153
bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
154
        int row, int, const QModelIndex &parent )
zorglub's avatar
zorglub committed
155
{
156 157 158 159
    bool copy = action == Qt::CopyAction;
    if( !copy && action != Qt::MoveAction )
        return true;

160 161
    const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
    if( plMimeData )
zorglub's avatar
zorglub committed
162
    {
163
        if( copy )
jpd's avatar
jpd committed
164
            dropAppendCopy( plMimeData, getItem( parent ), row );
165
        else
166
            dropMove( plMimeData, getItem( parent ), row );
167 168 169 170
    }
    return true;
}

jpd's avatar
jpd committed
171
void PLModel::dropAppendCopy( const PlMimeData *plMimeData, PLItem *target, int pos )
172
{
173
    vlc_playlist_locker pl_lock ( THEPL );
174

175
    playlist_item_t *p_parent =
176
        playlist_ItemGetByInput( p_playlist, target->inputItem() );
jpd's avatar
jpd committed
177 178
    if( !p_parent ) return;

jpd's avatar
jpd committed
179
    if( pos == -1 ) pos = PLAYLIST_END;
jpd's avatar
jpd committed
180

181
    QList<input_item_t*> inputItems = plMimeData->inputItems();
jpd's avatar
jpd committed
182

183
    foreach( input_item_t* p_input, inputItems )
184
    {
185
        playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
186
        if( !p_item ) continue;
jpd's avatar
jpd committed
187
        pos = playlist_NodeAddCopy( p_playlist, p_item, p_parent, pos );
188 189 190
    }
}

191
void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
192
{
193
    QList<input_item_t*> inputItems = plMimeData->inputItems();
194
    QList<PLItem*> model_items;
195 196 197 198
    playlist_item_t **pp_items;
    pp_items = (playlist_item_t **)
               calloc( inputItems.count(), sizeof( playlist_item_t* ) );
    if ( !pp_items ) return;
199

200
    int model_pos;
201 202

    {
203
        vlc_playlist_locker pl_lock ( THEPL );
204

205 206
        playlist_item_t *p_parent =
            playlist_ItemGetByInput( p_playlist, target->inputItem() );
jpd's avatar
jpd committed
207

208 209 210 211 212
        if( !p_parent || row > p_parent->i_children )
        {
            free( pp_items );
            return;
        }
213

214 215
        int new_pos = model_pos = row == -1 ? p_parent->i_children : row;
        int i = 0;
jpd's avatar
jpd committed
216

217
        foreach( input_item_t *p_input, inputItems )
zorglub's avatar
zorglub committed
218
        {
219 220 221 222 223 224 225 226 227 228 229
            playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
            if( !p_item ) continue;

            PLItem *item = findByInputId( rootItem, p_input->i_id );
            if( !item ) continue;

            /* Better not try to move a node into itself.
               Abort the whole operation in that case,
               because it is ambiguous. */
            AbstractPLItem *climber = target;
            while( climber )
230
            {
231 232 233 234 235 236
                if( climber == item )
                {
                    free( pp_items );
                    return;
                }
                climber = climber->parent();
237
            }
238

239 240 241
            if( item->parent() == target &&
                target->children.indexOf( item ) < new_pos )
                model_pos--;
242

243 244 245 246
            model_items.append( item );
            pp_items[i] = p_item;
            i++;
        }
jpd's avatar
jpd committed
247

248 249 250 251 252
        if( model_items.isEmpty() )
        {
            free( pp_items );
            return;
        }
253

254 255
        playlist_TreeMoveMany( p_playlist, i, pp_items, p_parent, new_pos );
    }
jpd's avatar
jpd committed
256 257

    foreach( PLItem *item, model_items )
258
        takeItem( item );
jpd's avatar
jpd committed
259 260

    insertChildren( target, model_items, model_pos );
261
    free( pp_items );
zorglub's avatar
zorglub committed
262
}
zorglub's avatar
zorglub committed
263

zorglub's avatar
zorglub committed
264 265 266
void PLModel::activateItem( const QModelIndex &index )
{
    assert( index.isValid() );
Ilkka Ollakka's avatar
Ilkka Ollakka committed
267
    const PLItem *item = getItem( index );
zorglub's avatar
zorglub committed
268
    assert( item );
269 270 271

    vlc_playlist_locker pl_lock( THEPL );

François Cartegnie's avatar
François Cartegnie committed
272
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_playlist_id );
zorglub's avatar
zorglub committed
273 274
    activateItem( p_item );
}
275

276 277
/* Convenient overloaded private version of activateItem
 * Must be entered with PL lock */
zorglub's avatar
zorglub committed
278 279 280
void PLModel::activateItem( playlist_item_t *p_item )
{
    if( !p_item ) return;
zorglub's avatar
zorglub committed
281 282 283
    playlist_item_t *p_parent = p_item;
    while( p_parent )
    {
François Cartegnie's avatar
François Cartegnie committed
284
        if( p_parent->i_id == rootItem->id( PLAYLIST_ID ) ) break;
zorglub's avatar
zorglub committed
285 286 287
        p_parent = p_parent->p_parent;
    }
    if( p_parent )
ivoire's avatar
ivoire committed
288
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
289
                p_parent, p_item );
zorglub's avatar
zorglub committed
290
}
zorglub's avatar
zorglub committed
291

zorglub's avatar
zorglub committed
292
/****************** Base model mandatory implementations *****************/
Ilkka Ollakka's avatar
Ilkka Ollakka committed
293
QVariant PLModel::data( const QModelIndex &index, const int role ) const
zorglub's avatar
zorglub committed
294
{
295 296 297
    if( !index.isValid() )
        return QVariant();

298 299 300
    switch( role )
    {

301 302
        case Qt::FontRole:
            return customFont;
303

304
        case Qt::DisplayRole:
305
        {
306 307 308 309 310 311 312 313 314 315 316
            PLItem *item = getItem( index );
            int metadata = columnToMeta( index.column() );
            if( metadata == COLUMN_END )
                return QVariant();

            QString returninfo;
            if( metadata == COLUMN_NUMBER )
            {
                returninfo = QString::number( index.row() + 1 );
            }
            else if( metadata == COLUMN_COVER )
317
            {
318 319 320
                QString artUrl;
                artUrl = InputManager::decodeArtURL( item->inputItem() );
                if( artUrl.isEmpty() )
321
                {
322 323 324 325 326 327
                    for( int i = 0; i < item->childCount(); i++ )
                    {
                        artUrl = InputManager::decodeArtURL( item->child( i )->inputItem() );
                        if( !artUrl.isEmpty() )
                            break;
                    }
328
                }
329
                return artUrl;
330
            }
331 332 333 334 335 336 337 338
            else
            {
                char *psz = psz_column_meta( item->inputItem(), metadata );
                returninfo = qfu( psz );
                free( psz );
            }

            return QVariant( returninfo );
339
        }
340 341

        case Qt::DecorationRole:
342
        {
343
            switch( columnToMeta(index.column()) )
344
            {
345 346 347 348 349 350 351 352 353 354 355 356
                case COLUMN_TITLE:
                {
                    PLItem *item = getItem( index );
                    /* Used to segfault here because i_type wasn't always initialized */
                    return QVariant( icons[item->inputItem()->i_type] );
                }
                case COLUMN_COVER:
                    /* !warn: changes tree item line height. Otherwise, override
                     * delegate's sizehint */
                    return getArtPixmap( index, QSize(16,16) );
                default:
                    break;
357
            }
358
            break;
359
        }
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

        case Qt::BackgroundRole:
            if( isCurrent( index ) )
                return QVariant( QBrush( Qt::gray ) );
            break;

        case CURRENT_ITEM_ROLE:
            return QVariant( isCurrent( index ) );

        case CURRENT_ITEM_CHILD_ROLE:
            return QVariant( isParent( index, currentIndex() ) );

        case LEAF_NODE_ROLE:
            return QVariant( isLeaf( index ) );

        default:
            break;
zorglub's avatar
zorglub committed
377
    }
378

zorglub's avatar
zorglub committed
379
    return QVariant();
zorglub's avatar
zorglub committed
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393
bool PLModel::setData( const QModelIndex &index, const QVariant & value, int role )
{
    switch( role )
    {
    case Qt::FontRole:
        customFont = value.value<QFont>();
        return true;
    default:
        return VLCModel::setData( index, value, role );
    }
}

394 395 396
/* Seek from current index toward the top and see if index is one of parent nodes */
bool PLModel::isParent( const QModelIndex &index, const QModelIndex &current ) const
{
397 398 399
    if( !index.isValid() )
        return false;

400 401 402
    if( index == current )
        return true;

403
    if( !current.isValid() || !current.parent().isValid() )
404 405 406 407 408
        return false;

    return isParent( index, current.parent() );
}

François Cartegnie's avatar
François Cartegnie committed
409
bool PLModel::isLeaf( const QModelIndex &index ) const
zorglub's avatar
Qt4:  
zorglub committed
410
{
François Cartegnie's avatar
François Cartegnie committed
411
    bool b_isLeaf = false;
412 413 414

    vlc_playlist_locker pl_lock ( THEPL );

François Cartegnie's avatar
François Cartegnie committed
415 416
    playlist_item_t *plItem =
        playlist_ItemGetById( p_playlist, itemId( index, PLAYLIST_ID ) );
417

François Cartegnie's avatar
François Cartegnie committed
418 419
    if( plItem )
        b_isLeaf = plItem->i_children == -1;
420

François Cartegnie's avatar
François Cartegnie committed
421
    return b_isLeaf;
422 423
}

François Cartegnie's avatar
François Cartegnie committed
424
PLItem* PLModel::getItem( const QModelIndex & index ) const
425
{
François Cartegnie's avatar
François Cartegnie committed
426 427 428
    PLItem *item = static_cast<PLItem *>( VLCModel::getItem( index ) );
    if ( item == NULL ) item = rootItem;
    return item;
429 430
}

Ilkka Ollakka's avatar
Ilkka Ollakka committed
431
QModelIndex PLModel::index( const int row, const int column, const QModelIndex &parent )
zorglub's avatar
zorglub committed
432 433
                  const
{
434
    PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
zorglub's avatar
zorglub committed
435

436
    PLItem *childItem = static_cast<PLItem*>(parentItem->child( row ));
437 438
    if( childItem )
        return createIndex( row, column, childItem );
zorglub's avatar
zorglub committed
439 440 441 442
    else
        return QModelIndex();
}

François Cartegnie's avatar
François Cartegnie committed
443
QModelIndex PLModel::indexByPLID( const int i_plid, const int c ) const
444
{
François Cartegnie's avatar
François Cartegnie committed
445 446 447 448 449 450
    return index( findByPLId( rootItem, i_plid ), c );
}

QModelIndex PLModel::indexByInputItemID( const int i_inputitem_id, const int c ) const
{
    return index( findByInputId( rootItem, i_inputitem_id ), c );
451 452
}

453 454
QModelIndex PLModel::rootIndex() const
{
François Cartegnie's avatar
François Cartegnie committed
455
    return index( findByPLId( rootItem, rootItem->id( PLAYLIST_ID ) ), 0 );
456 457 458 459
}

bool PLModel::isTree() const
{
François Cartegnie's avatar
François Cartegnie committed
460
    return ( ( rootItem && rootItem->id( PLAYLIST_ID ) != p_playlist->p_playing->i_id )
461 462 463
             || var_InheritBool( p_intf, "playlist-tree" ) );
}

zorglub's avatar
zorglub committed
464
/* Return the index of a given item */
zorglub's avatar
zorglub committed
465
QModelIndex PLModel::index( PLItem *item, int column ) const
zorglub's avatar
zorglub committed
466 467
{
    if( !item ) return QModelIndex();
468
    AbstractPLItem *parent = item->parent();
zorglub's avatar
zorglub committed
469
    if( parent )
470
        return createIndex( parent->lastIndexOf( item ),
471
                            column, item );
zorglub's avatar
zorglub committed
472 473 474
    return QModelIndex();
}

Ilkka Ollakka's avatar
Ilkka Ollakka committed
475
QModelIndex PLModel::currentIndex() const
Ilkka Ollakka's avatar
Ilkka Ollakka committed
476
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
477 478
    input_thread_t *p_input_thread = THEMIM->getInput();
    if( !p_input_thread ) return QModelIndex();
François Cartegnie's avatar
François Cartegnie committed
479
    PLItem *item = findByInputId( rootItem, input_GetItem( p_input_thread )->i_id );
Ilkka Ollakka's avatar
Ilkka Ollakka committed
480
    return index( item, 0 );
481 482
}

483
QModelIndex PLModel::parent( const QModelIndex &index ) const
zorglub's avatar
zorglub committed
484
{
zorglub's avatar
Qt4:  
zorglub committed
485
    if( !index.isValid() ) return QModelIndex();
zorglub's avatar
zorglub committed
486

487
    PLItem *childItem = getItem( index );
488 489
    if( !childItem )
    {
490
        msg_Err( p_playlist, "Item not found" );
491 492 493
        return QModelIndex();
    }

494
    PLItem *parentItem = static_cast<PLItem*>(childItem->parent());
zorglub's avatar
zorglub committed
495
    if( !parentItem || parentItem == rootItem ) return QModelIndex();
496
    if( !parentItem->parent() )
497
    {
498
        msg_Err( p_playlist, "No parent found, trying row 0. Please report this" );
499 500
        return createIndex( 0, 0, parentItem );
    }
Ilkka Ollakka's avatar
Ilkka Ollakka committed
501
    return createIndex(parentItem->row(), 0, parentItem);
zorglub's avatar
zorglub committed
502 503
}

504
int PLModel::rowCount( const QModelIndex &parent ) const
zorglub's avatar
zorglub committed
505
{
506
    PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
zorglub's avatar
zorglub committed
507 508 509 510
    return parentItem->childCount();
}

/************************* Lookups *****************************/
François Cartegnie's avatar
François Cartegnie committed
511
PLItem *PLModel::findByPLId( PLItem *root, int i_plitemid ) const
zorglub's avatar
zorglub committed
512
{
François Cartegnie's avatar
François Cartegnie committed
513
    return findInner( root, i_plitemid, false );
zorglub's avatar
zorglub committed
514 515
}

François Cartegnie's avatar
François Cartegnie committed
516
PLItem *PLModel::findByInputId( PLItem *root, int i_input_itemid ) const
zorglub's avatar
zorglub committed
517
{
François Cartegnie's avatar
François Cartegnie committed
518
    PLItem *result = findInner( root, i_input_itemid, true );
519
    return result;
zorglub's avatar
zorglub committed
520 521
}

François Cartegnie's avatar
François Cartegnie committed
522
PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_isinputid ) const
zorglub's avatar
zorglub committed
523
{
524
    if( !root ) return NULL;
zorglub's avatar
zorglub committed
525

François Cartegnie's avatar
François Cartegnie committed
526
    if( !b_isinputid && root->id( PLAYLIST_ID ) == i_id )
zorglub's avatar
zorglub committed
527
        return root;
528

François Cartegnie's avatar
François Cartegnie committed
529
    else if( b_isinputid && root->id( INPUTITEM_ID ) == i_id )
zorglub's avatar
zorglub committed
530 531
        return root;

532
    QList<AbstractPLItem *>::iterator it = root->children.begin();
zorglub's avatar
zorglub committed
533 534
    while ( it != root->children.end() )
    {
535
        PLItem *item = static_cast<PLItem *>(*it);
François Cartegnie's avatar
François Cartegnie committed
536
        if( !b_isinputid && item->id( PLAYLIST_ID ) == i_id )
537
            return item;
538

François Cartegnie's avatar
François Cartegnie committed
539
        else if( b_isinputid && item->id( INPUTITEM_ID ) == i_id )
540
            return item;
541

542
        if( item->childCount() )
zorglub's avatar
zorglub committed
543
        {
François Cartegnie's avatar
François Cartegnie committed
544
            PLItem *childFound = findInner( item, i_id, b_isinputid );
zorglub's avatar
zorglub committed
545
            if( childFound )
546
                return childFound;
zorglub's avatar
zorglub committed
547
        }
548
        ++it;
zorglub's avatar
zorglub committed
549 550 551 552
    }
    return NULL;
}

553 554
PLModel::pl_nodetype PLModel::getPLRootType() const
{
555 556 557 558
    /* can't rely on rootitem as it depends on view / rebuild() */
    AbstractPLItem *plitem = rootItem;
    while( plitem->parent() ) plitem = plitem->parent();

559 560
    const input_item_t *p_item = plitem->inputItem();
    if( p_item == p_playlist->p_playing->p_input )
561
        return ROOTTYPE_CURRENT_PLAYING;
562 563 564

    if( p_playlist->p_media_library &&
        p_item == p_playlist->p_media_library->p_input )
565
        return ROOTTYPE_MEDIA_LIBRARY;
566 567

    return ROOTTYPE_OTHER;
568 569
}

570 571
bool PLModel::canEdit() const
{
572
    return ( getPLRootType() != ROOTTYPE_OTHER );
573
}
574

zorglub's avatar
zorglub committed
575 576
/************************* Updates handling *****************************/

zorglub's avatar
zorglub committed
577
/**** Events processing ****/
578
void PLModel::processInputItemUpdate( )
579
{
580
    input_thread_t *p_input = THEMIM->getInput();
581
    if( !p_input ) return;
582

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
583 584
    PLItem *item = findByInputId( rootItem, input_GetItem( p_input )->i_id );
    if( item ) emit currentIndexChanged( index( item, 0 ) );
François Cartegnie's avatar
François Cartegnie committed
585

586
    processInputItemUpdate( input_GetItem( p_input ) );
587
}
588

jpd's avatar
jpd committed
589
void PLModel::processInputItemUpdate( input_item_t *p_item )
zorglub's avatar
zorglub committed
590
{
591
    if( !p_item ||  p_item->i_id <= 0 ) return;
François Cartegnie's avatar
François Cartegnie committed
592
    PLItem *item = findByInputId( rootItem, p_item->i_id );
zorglub's avatar
zorglub committed
593
    if( item )
594
        updateTreeItem( item );
zorglub's avatar
zorglub committed
595 596
}

François Cartegnie's avatar
François Cartegnie committed
597
void PLModel::processItemRemoval( int i_pl_itemid )
zorglub's avatar
zorglub committed
598
{
François Cartegnie's avatar
François Cartegnie committed
599 600
    if( i_pl_itemid <= 0 ) return;
    removeItem( findByPLId( rootItem, i_pl_itemid ) );
zorglub's avatar
zorglub committed
601 602
}

François Cartegnie's avatar
François Cartegnie committed
603
void PLModel::processItemAppend( int i_pl_itemid, int i_pl_itemidparent )
zorglub's avatar
zorglub committed
604 605
{
    playlist_item_t *p_item = NULL;
zorglub's avatar
zorglub committed
606
    PLItem *newItem = NULL;
jpd's avatar
jpd committed
607
    int pos;
zorglub's avatar
zorglub committed
608

609
    /* Find the Parent */
François Cartegnie's avatar
François Cartegnie committed
610
    PLItem *nodeParentItem = findByPLId( rootItem, i_pl_itemidparent );
611
    if( !nodeParentItem ) return;
zorglub's avatar
zorglub committed
612

613
    /* Search for an already matching children */
François Cartegnie's avatar
François Cartegnie committed
614 615
    foreach( AbstractPLItem *existing, nodeParentItem->children )
        if( existing->id( PLAYLIST_ID ) == i_pl_itemid ) return;
616

617
    /* Find the child */
618
    {
619
        vlc_playlist_locker pl_lock ( THEPL );
zorglub's avatar
zorglub committed
620

621 622 623
        p_item = playlist_ItemGetById( p_playlist, i_pl_itemid );
        if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
            return;
jpd's avatar
jpd committed
624

625 626 627 628 629
        for( pos = p_item->p_parent->i_children - 1; pos >= 0; pos-- )
            if( p_item->p_parent->pp_children[pos] == p_item ) break;

        newItem = new PLItem( p_item, nodeParentItem );
    }
630

631
    /* We insert the newItem (children) inside the parent */
632 633 634 635
    beginInsertRows( index( nodeParentItem, 0 ), pos, pos );
    nodeParentItem->insertChild( newItem, pos );
    endInsertRows();
    if ( newItem->inputItem() == THEMIM->currentInputItem() )
636
        emit currentIndexChanged( index( newItem, 0 ) );
637

638
    if( latestSearch.isEmpty() ) return;
François Cartegnie's avatar
François Cartegnie committed
639
    filter( latestSearch, index( rootItem, 0), false /*FIXME*/ );
zorglub's avatar
zorglub committed
640
}
zorglub's avatar
zorglub committed
641

jpd's avatar
jpd committed
642
void PLModel::rebuild( playlist_item_t *p_root )
zorglub's avatar
zorglub committed
643
{
644 645
    beginResetModel();

zorglub's avatar
zorglub committed
646
    {
647 648 649 650 651 652 653 654 655 656 657
        vlc_playlist_locker pl_lock ( THEPL );

        if( rootItem ) rootItem->clearChildren();
        if( p_root ) // Can be NULL
        {
            if ( rootItem ) delete rootItem;
            rootItem = new PLItem( p_root );
        }
        assert( rootItem );
        /* Recreate from root */
        updateChildren( rootItem );
zorglub's avatar
zorglub committed
658
    }
zorglub's avatar
zorglub committed
659 660

    /* And signal the view */
661
    endResetModel();
662
    if( p_root ) emit rootIndexChanged();
zorglub's avatar
zorglub committed
663 664
}

jpd's avatar
jpd committed
665
void PLModel::takeItem( PLItem *item )
666 667
{
    assert( item );
668
    PLItem *parent = static_cast<PLItem*>(item->parent());
669
    assert( parent );
670
    int i_index = parent->indexOf( item );
671 672 673 674 675 676

    beginRemoveRows( index( parent, 0 ), i_index, i_index );
    parent->takeChildAt( i_index );
    endRemoveRows();
}

jpd's avatar
jpd committed
677
void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
678 679
{
    assert( node );
680
    int count = items.count();
681 682 683 684 685 686 687 688 689 690
    if( !count ) return;
    beginInsertRows( index( node, 0 ), i_pos, i_pos + count - 1 );
    for( int i = 0; i < count; i++ )
    {
        node->children.insert( i_pos + i, items[i] );
        items[i]->parentItem = node;
    }
    endInsertRows();
}

jpd's avatar
jpd committed
691
void PLModel::removeItem( PLItem *item )
692 693
{
    if( !item ) return;
694

695
    if( item->parent() ) {
696 697
        int i = item->parent()->indexOf( item );
        beginRemoveRows( index( static_cast<PLItem*>(item->parent()), 0), i, i );
698
        item->parent()->children.removeAt(i);
699 700 701
        delete item;
        endRemoveRows();
    }
702 703
    else delete item;

704 705 706 707 708
    if(item == rootItem)
    {
        rootItem = NULL;
        rebuild( p_playlist->p_playing );
    }
709 710
}

zorglub's avatar
Qt4:  
zorglub committed
711
/* This function must be entered WITH the playlist lock */
jpd's avatar
jpd committed
712
void PLModel::updateChildren( PLItem *root )
zorglub's avatar
zorglub committed
713
{
François Cartegnie's avatar
François Cartegnie committed
714
    playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->id( PLAYLIST_ID ) );
jpd's avatar
jpd committed
715
    updateChildren( p_node, root );
zorglub's avatar
zorglub committed
716 717
}

zorglub's avatar
Qt4:  
zorglub committed
718
/* This function must be entered WITH the playlist lock */
jpd's avatar
jpd committed
719
void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
zorglub's avatar
zorglub committed
720 721 722
{
    for( int i = 0; i < p_node->i_children ; i++ )
    {
zorglub's avatar
zorglub committed
723
        if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
724 725
        PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
        root->appendChild( newItem );
726
        if( p_node->pp_children[i]->i_children != -1 )
jpd's avatar
jpd committed
727
            updateChildren( p_node->pp_children[i], newItem );
zorglub's avatar
zorglub committed
728 729 730
    }
}

731
/* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
732
void PLModel::updateTreeItem( PLItem *item )
zorglub's avatar
zorglub committed
733
{
jpd's avatar