playlist_model.cpp 31.7 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
2
 * playlist_model.cpp : Manage playlist model
Clément Stenac's avatar
Clément Stenac committed
3
 ****************************************************************************
4
 * Copyright (C) 2006-2007 the VideoLAN team
5
 * $Id$
Clément Stenac's avatar
Clément Stenac committed
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Ilkka Ollakkka <ileoo (at) videolan dot org>
9
 *          Jakob Leben <jleben@videolan.org>
Clément Stenac's avatar
Clément Stenac 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
Clément Stenac's avatar
Clément Stenac committed
29

30
#include "qt4.hpp"
31
#include "dialogs_provider.hpp"
32
#include "components/playlist/playlist_model.hpp"
33
#include "dialogs/mediainfo.hpp"
34
#include "dialogs/playlist.hpp"
35
#include <vlc_intf_strings.h>
Clément Stenac's avatar
Clément Stenac committed
36

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
37
#include "pixmaps/types/type_unknown.xpm"
Clément Stenac's avatar
Clément Stenac committed
38

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
39 40 41 42 43 44
#include <assert.h>
#include <QIcon>
#include <QFont>
#include <QMenu>
#include <QApplication>
#include <QSettings>
45 46 47 48
#include <QUrl>
#include <QFileInfo>
#include <QDesktopServices>
#include <QInputDialog>
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
49

Rafaël Carré's avatar
Rafaël Carré committed
50 51
#include "sorting.h"

52 53 54 55 56 57
#define I_NEW_DIR \
    I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
#define I_NEW_DIR_NAME \
    I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
                     N_( "Enter name for new folder:" ) )

Clément Stenac's avatar
Clément Stenac committed
58 59
QIcon PLModel::icons[ITEM_TYPE_NUMBER];

Clément Stenac's avatar
Clément Stenac committed
60 61 62 63
/*************************************************************************
 * Playlist model implementation
 *************************************************************************/

64 65 66 67 68
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 */
                  : QAbstractItemModel( parent )
Clément Stenac's avatar
Clément Stenac committed
69
{
70 71
    p_intf            = _p_intf;
    p_playlist        = _p_playlist;
Clément Stenac's avatar
Clément Stenac committed
72 73
    i_cached_id       = -1;
    i_cached_input_id = -1;
74
    i_popup_item      = i_popup_parent = -1;
75
    sortingMenu       = NULL;
76 77

    rootItem          = NULL; /* PLItem rootItem, will be set in rebuild( ) */
Clément Stenac's avatar
Clément Stenac committed
78

79
    /* Icons initialization */
80
#define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( x )
81
    ADD_ICON( UNKNOWN , type_unknown_xpm );
82 83 84 85 86 87 88 89
    ADD_ICON( FILE, ":/type/file" );
    ADD_ICON( DIRECTORY, ":/type/directory" );
    ADD_ICON( DISC, ":/type/disc" );
    ADD_ICON( CDDA, ":/type/cdda" );
    ADD_ICON( CARD, ":/type/capture-card" );
    ADD_ICON( NET, ":/type/net" );
    ADD_ICON( PLAYLIST, ":/type/playlist" );
    ADD_ICON( NODE, ":/type/node" );
90
#undef ADD_ICON
Clément Stenac's avatar
Clément Stenac committed
91

Jakob Leben's avatar
Jakob Leben committed
92
    rebuild( p_root );
93 94 95 96
    DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
             this, processInputItemUpdate( input_item_t *) );
    DCONNECT( THEMIM, inputChanged( input_thread_t * ),
             this, processInputItemUpdate( input_thread_t* ) );
97 98 99 100
    CONNECT( THEMIM, playlistItemAppended( int, int ),
             this, processItemAppend( int, int ) );
    CONNECT( THEMIM, playlistItemRemoved( int ),
             this, processItemRemoval( int ) );
Clément Stenac's avatar
Clément Stenac committed
101 102 103 104 105
}

PLModel::~PLModel()
{
    delete rootItem;
106
    delete sortingMenu;
Clément Stenac's avatar
Clément Stenac committed
107 108
}

109 110
Qt::DropActions PLModel::supportedDropActions() const
{
111
    return Qt::CopyAction | Qt::MoveAction;
112 113
}

114
Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
115
{
116 117
    Qt::ItemFlags flags = QAbstractItemModel::flags( index );

118
    PLItem *item = index.isValid() ? getItem( index ) : rootItem;
119

120
    if( canEdit() )
121
    {
122 123 124
        PL_LOCK;
        playlist_item_t *plItem =
            playlist_ItemGetById( p_playlist, item->i_id );
125 126 127 128

        if ( plItem && ( plItem->i_children > -1 ) )
            flags |= Qt::ItemIsDropEnabled;

129
        PL_UNLOCK;
130

131
    }
132
    flags |= Qt::ItemIsDragEnabled;
133 134

    return flags;
135 136 137 138 139
}

QStringList PLModel::mimeTypes() const
{
    QStringList types;
140
    types << "vlc/qt-input-items";
141 142 143
    return types;
}

144 145 146 147 148 149 150 151 152
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() );
    if( item1->parent() == item2->parent() ) return i1.row() < i2.row();
    else return *item1 < *item2;
}

153
QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
154
{
155
    PlMimeData *plMimeData = new PlMimeData();
156
    QModelIndexList list;
157

158
    foreach( const QModelIndex &index, indexes ) {
159
        if( index.isValid() && index.column() == 0 )
160 161 162
            list.append(index);
    }

163
    qSort(list.begin(), list.end(), modelIndexLessThen);
164

165
    PLItem *item = NULL;
166
    foreach( const QModelIndex &index, list ) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        if( item )
        {
            PLItem *testee = getItem( index );
            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 );

182
        plMimeData->appendItem( item->p_input );
183
    }
184

185
    return plMimeData;
186 187
}

188
/* Drop operation */
189
bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
190
                           int row, int column, const QModelIndex &parent )
191
{
192 193 194 195
    bool copy = action == Qt::CopyAction;
    if( !copy && action != Qt::MoveAction )
        return true;

196 197
    const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
    if( plMimeData )
198
    {
199
        if( copy )
200
            dropAppendCopy( plMimeData, getItem( parent ), row );
201
        else
202
            dropMove( plMimeData, getItem( parent ), row );
203 204 205 206
    }
    return true;
}

207
void PLModel::dropAppendCopy( const PlMimeData *plMimeData, PLItem *target, int pos )
208 209
{
    PL_LOCK;
210

211
    playlist_item_t *p_parent =
212
            playlist_ItemGetByInput( p_playlist, target->p_input );
213 214
    if( !p_parent ) return;

215
    if( pos == -1 ) pos = PLAYLIST_END;
216

217
    QList<input_item_t*> inputItems = plMimeData->inputItems();
218

219
    foreach( input_item_t* p_input, inputItems )
220
    {
221
        playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
222
        if( !p_item ) continue;
223
        pos = playlist_NodeAddCopy( p_playlist, p_item, p_parent, pos );
224
    }
225

226 227 228
    PL_UNLOCK;
}

229
void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
230
{
231
    QList<input_item_t*> inputItems = plMimeData->inputItems();
232
    QList<PLItem*> model_items;
233 234 235 236 237 238 239 240 241 242 243 244 245
    playlist_item_t *pp_items[inputItems.size()];

    PL_LOCK;

    playlist_item_t *p_parent =
        playlist_ItemGetByInput( p_playlist, target->p_input );

    if( !p_parent || row > p_parent->i_children )
    {
        PL_UNLOCK; return;
    }

    int new_pos = row == -1 ? p_parent->i_children : row;
246
    int model_pos = new_pos;
247
    int i = 0;
248

249
    foreach( input_item_t *p_input, inputItems )
250
    {
251 252 253
        playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
        if( !p_item ) continue;

254
        PLItem *item = findByInput( rootItem, p_input->i_id );
255 256 257 258 259
        if( !item ) continue;

        /* Better not try to move a node into itself.
           Abort the whole operation in that case,
           because it is ambiguous. */
260 261
        PLItem *climber = target;
        while( climber )
262
        {
263 264 265 266
            if( climber == item )
            {
                PL_UNLOCK; return;
            }
267
            climber = climber->parentItem;
268
        }
269 270

        if( item->parentItem == target &&
271
            target->children.indexOf( item ) < new_pos )
272 273 274
                model_pos--;

        model_items.append( item );
275 276
        pp_items[i] = p_item;
        i++;
277
    }
278

279 280 281 282
    if( model_items.isEmpty() )
    {
        PL_UNLOCK; return;
    }
283

284
    playlist_TreeMoveMany( p_playlist, i, pp_items, p_parent, new_pos );
285

286
    PL_UNLOCK;
287 288

    foreach( PLItem *item, model_items )
289
        takeItem( item );
290 291

    insertChildren( target, model_items, model_pos );
Clément Stenac's avatar
Clément Stenac committed
292
}
293

294
/* remove item with its id */
Clément Stenac's avatar
Clément Stenac committed
295 296
void PLModel::removeItem( int i_id )
{
Jakob Leben's avatar
Jakob Leben committed
297 298
    PLItem *item = findById( rootItem, i_id );
    removeItem( item );
Clément Stenac's avatar
Clément Stenac committed
299
}
300

301 302 303
void PLModel::activateItem( const QModelIndex &index )
{
    assert( index.isValid() );
304
    PLItem *item = getItem( index );
305 306
    assert( item );
    PL_LOCK;
307
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
Clément Stenac's avatar
Clément Stenac committed
308 309 310
    activateItem( p_item );
    PL_UNLOCK;
}
311

Clément Stenac's avatar
Clément Stenac committed
312 313 314 315
/* Must be entered with lock */
void PLModel::activateItem( playlist_item_t *p_item )
{
    if( !p_item ) return;
316 317 318 319 320 321 322
    playlist_item_t *p_parent = p_item;
    while( p_parent )
    {
        if( p_parent->i_id == rootItem->i_id ) break;
        p_parent = p_parent->p_parent;
    }
    if( p_parent )
323
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
324
                          p_parent, p_item );
325
}
Clément Stenac's avatar
Clément Stenac committed
326

327
/****************** Base model mandatory implementations *****************/
328
QVariant PLModel::data( const QModelIndex &index, int role ) const
Clément Stenac's avatar
Clément Stenac committed
329
{
330
    if( !index.isValid() ) return QVariant();
331
    PLItem *item = getItem( index );
Clément Stenac's avatar
Clément Stenac committed
332 333
    if( role == Qt::DisplayRole )
    {
334
        int metadata = columnToMeta( index.column() );
335
        if( metadata == COLUMN_END ) return QVariant();
336 337 338 339 340 341 342

        QString returninfo;
        if( metadata == COLUMN_NUMBER )
            returninfo = QString::number( index.row() + 1 );
        else
        {
            char *psz = psz_column_meta( item->p_input, metadata );
343
            returninfo = qfu( psz );
344 345 346
            free( psz );
        }
        return QVariant( returninfo );
Clément Stenac's avatar
Clément Stenac committed
347 348 349
    }
    else if( role == Qt::DecorationRole && index.column() == 0  )
    {
350 351
        /* Used to segfault here because i_type wasn't always initialized */
        return QVariant( PLModel::icons[item->p_input->i_type] );
Clément Stenac's avatar
Clément Stenac committed
352
    }
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
353 354
    else if( role == Qt::FontRole )
    {
355
        if( isCurrent( index ) )
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
356 357 358 359
        {
            QFont f; f.setBold( true ); return QVariant( f );
        }
    }
Jakob Leben's avatar
Jakob Leben committed
360 361 362 363
    else if( role == Qt::BackgroundRole && isCurrent( index ) )
    {
        return QVariant( QBrush( Qt::gray ) );
    }
364
    else if( role == IsCurrentRole ) return QVariant( isCurrent( index ) );
365 366 367 368 369 370
    else if( role == IsLeafNodeRole )
    {
        QVariant isLeaf;
        PL_LOCK;
        playlist_item_t *plItem =
            playlist_ItemGetById( p_playlist, item->i_id );
Jakob Leben's avatar
Jakob Leben committed
371

372 373 374 375 376 377
        if( plItem )
            isLeaf = plItem->i_children == -1;

        PL_UNLOCK;
        return isLeaf;
    }
378 379
    else if( role == IsCurrentsParentNodeRole )
    {
Ilkka Ollakka's avatar
Ilkka Ollakka committed
380
        return QVariant( isParent( index, currentIndex() ) );
381
    }
Clément Stenac's avatar
Clément Stenac committed
382
    return QVariant();
Clément Stenac's avatar
Clément Stenac committed
383 384
}

385 386 387
/* 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
{
388 389 390
    if( !index.isValid() )
        return false;

391 392 393
    if( index == current )
        return true;

394
    if( !current.isValid() || !current.parent().isValid() )
395 396 397 398 399
        return false;

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

400
bool PLModel::isCurrent( const QModelIndex &index ) const
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
401
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
402
    return getItem( index )->p_input == THEMIM->currentInputItem();
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
403 404
}

405
int PLModel::itemId( const QModelIndex &index ) const
Clément Stenac's avatar
Clément Stenac committed
406
{
407
    return getItem( index )->i_id;
408
}
Clément Stenac's avatar
Clément Stenac committed
409 410

QVariant PLModel::headerData( int section, Qt::Orientation orientation,
411
                              int role ) const
Clément Stenac's avatar
Clément Stenac committed
412
{
413 414 415
    if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
        return QVariant();

416
    int meta_col = columnToMeta( section );
417

418
    if( meta_col == COLUMN_END ) return QVariant();
419

420
    return QVariant( qfu( psz_column_title( meta_col ) ) );
Clément Stenac's avatar
Clément Stenac committed
421 422
}

423
QModelIndex PLModel::index( int row, int column, const QModelIndex &parent )
Clément Stenac's avatar
Clément Stenac committed
424 425
                  const
{
426
    PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
Clément Stenac's avatar
Clément Stenac committed
427

428 429 430
    PLItem *childItem = parentItem->child( row );
    if( childItem )
        return createIndex( row, column, childItem );
Clément Stenac's avatar
Clément Stenac committed
431 432 433 434
    else
        return QModelIndex();
}

435 436 437 438 439
QModelIndex PLModel::index( int i_id, int c )
{
  return index( findById( rootItem, i_id ), c );
}

Clément Stenac's avatar
Clément Stenac committed
440
/* Return the index of a given item */
441
QModelIndex PLModel::index( PLItem *item, int column ) const
Clément Stenac's avatar
Clément Stenac committed
442 443 444 445
{
    if( !item ) return QModelIndex();
    const PLItem *parent = item->parent();
    if( parent )
446 447
        return createIndex( parent->children.lastIndexOf( item ),
                            column, item );
Clément Stenac's avatar
Clément Stenac committed
448 449 450
    return QModelIndex();
}

Ilkka Ollakka's avatar
Ilkka Ollakka committed
451
QModelIndex PLModel::currentIndex() const
Ilkka Ollakka's avatar
Ilkka Ollakka committed
452
{
Ilkka Ollakka's avatar
Ilkka Ollakka committed
453 454 455 456
    input_thread_t *p_input_thread = THEMIM->getInput();
    if( !p_input_thread ) return QModelIndex();
    PLItem *item = findByInput( rootItem, input_GetItem( p_input_thread )->i_id );
    return index( item, 0 );
457 458
}

459
QModelIndex PLModel::parent( const QModelIndex &index ) const
Clément Stenac's avatar
Clément Stenac committed
460
{
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
461
    if( !index.isValid() ) return QModelIndex();
Clément Stenac's avatar
Clément Stenac committed
462

463
    PLItem *childItem = getItem( index );
464 465 466 467 468 469
    if( !childItem )
    {
        msg_Err( p_playlist, "NULL CHILD" );
        return QModelIndex();
    }

Clément Stenac's avatar
Clément Stenac committed
470
    PLItem *parentItem = childItem->parent();
Clément Stenac's avatar
Clément Stenac committed
471
    if( !parentItem || parentItem == rootItem ) return QModelIndex();
472
    if( !parentItem->parentItem )
473
    {
Clément Stenac's avatar
Clément Stenac committed
474 475
        msg_Err( p_playlist, "No parent parent, trying row 0 " );
        msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
476 477
        return createIndex( 0, 0, parentItem );
    }
Ilkka Ollakka's avatar
Ilkka Ollakka committed
478
    return createIndex(parentItem->row(), 0, parentItem);
Clément Stenac's avatar
Clément Stenac committed
479 480
}

481
int PLModel::columnCount( const QModelIndex &i) const
Clément Stenac's avatar
Clément Stenac committed
482
{
483
    return columnFromMeta( COLUMN_END );
Clément Stenac's avatar
Clément Stenac committed
484 485
}

486
int PLModel::rowCount( const QModelIndex &parent ) const
Clément Stenac's avatar
Clément Stenac committed
487
{
488
    PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
Clément Stenac's avatar
Clément Stenac committed
489 490 491
    return parentItem->childCount();
}

492 493 494 495 496
QStringList PLModel::selectedURIs()
{
    QStringList lst;
    for( int i = 0; i < current_selection.size(); i++ )
    {
497
        PLItem *item = getItem( current_selection[i] );
498
        if( item )
499
        {
500
            PL_LOCK;
501 502 503 504 505 506
            playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
            if( p_item )
            {
                char *psz = input_item_GetURI( p_item->p_input );
                if( psz )
                {
507
                    lst.append( qfu(psz) );
508 509 510
                    free( psz );
                }
            }
511
            PL_UNLOCK;
512 513 514 515 516
        }
    }
    return lst;
}

Clément Stenac's avatar
Clément Stenac committed
517

Clément Stenac's avatar
Clément Stenac committed
518 519
/************************* Lookups *****************************/

Jakob Leben's avatar
Jakob Leben committed
520
PLItem *PLModel::findById( PLItem *root, int i_id )
Clément Stenac's avatar
Clément Stenac committed
521
{
Jakob Leben's avatar
Jakob Leben committed
522
    return findInner( root, i_id, false );
Clément Stenac's avatar
Clément Stenac committed
523 524
}

Jakob Leben's avatar
Jakob Leben committed
525
PLItem *PLModel::findByInput( PLItem *root, int i_id )
Clément Stenac's avatar
Clément Stenac committed
526
{
Jakob Leben's avatar
Jakob Leben committed
527
    PLItem *result = findInner( root, i_id, true );
528
    return result;
Clément Stenac's avatar
Clément Stenac committed
529 530
}

531 532
#define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
#define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
Clément Stenac's avatar
Clément Stenac committed
533

Jakob Leben's avatar
Jakob Leben committed
534
PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input )
Clément Stenac's avatar
Clément Stenac committed
535
{
536
    if( !root ) return NULL;
537
    if( ( !b_input && i_cached_id == i_id) ||
Clément Stenac's avatar
Clément Stenac committed
538
        ( b_input && i_cached_input_id ==i_id ) )
539
    {
Clément Stenac's avatar
Clément Stenac committed
540
        return b_input ? p_cached_item_bi : p_cached_item;
541
    }
Clément Stenac's avatar
Clément Stenac committed
542 543 544 545 546 547

    if( !b_input && root->i_id == i_id )
    {
        CACHE( i_id, root );
        return root;
    }
548
    else if( b_input && root->p_input->i_id == i_id )
Clément Stenac's avatar
Clément Stenac committed
549 550 551 552 553 554 555 556 557 558 559 560 561
    {
        ICACHE( i_id, root );
        return root;
    }

    QList<PLItem *>::iterator it = root->children.begin();
    while ( it != root->children.end() )
    {
        if( !b_input && (*it)->i_id == i_id )
        {
            CACHE( i_id, (*it) );
            return p_cached_item;
        }
562
        else if( b_input && (*it)->p_input->i_id == i_id )
Clément Stenac's avatar
Clément Stenac committed
563 564
        {
            ICACHE( i_id, (*it) );
565
            return p_cached_item_bi;
Clément Stenac's avatar
Clément Stenac committed
566 567 568
        }
        if( (*it)->children.size() )
        {
Jakob Leben's avatar
Jakob Leben committed
569
            PLItem *childFound = findInner( (*it), i_id, b_input );
570
            if( childFound )
Clément Stenac's avatar
Clément Stenac committed
571 572
            {
                if( b_input )
573
                    ICACHE( i_id, childFound )
Clément Stenac's avatar
Clément Stenac committed
574
                else
575
                    CACHE( i_id, childFound )
Clément Stenac's avatar
Clément Stenac committed
576
                return childFound;
577
            }
Clément Stenac's avatar
Clément Stenac committed
578
        }
Clément Stenac's avatar
Clément Stenac committed
579
        it++;
Clément Stenac's avatar
Clément Stenac committed
580 581 582 583 584 585
    }
    return NULL;
}
#undef CACHE
#undef ICACHE

Jakob Leben's avatar
Jakob Leben committed
586
int PLModel::columnToMeta( int _column )
587
{
588
    int meta = 1;
589
    int column = 0;
590

591
    while( column != _column && meta != COLUMN_END )
592
    {
593
        meta <<= 1;
594
        column++;
595 596 597 598 599
    }

    return meta;
}

Jakob Leben's avatar
Jakob Leben committed
600
int PLModel::columnFromMeta( int meta_col )
601 602
{
    int meta = 1;
603
    int column = 0;
604

605
    while( meta != meta_col && meta != COLUMN_END )
606 607
    {
        meta <<= 1;
608
        column++;
609 610
    }

611
    return column;
612
}
Clément Stenac's avatar
Clément Stenac committed
613

614 615 616 617 618
bool PLModel::canEdit() const
{
  return (
    rootItem != NULL &&
    (
619
      rootItem->p_input == p_playlist->p_playing->p_input ||
620
      (
621 622
        p_playlist->p_media_library &&
        rootItem->p_input == p_playlist->p_media_library->p_input
623 624 625 626
      )
    )
  );
}
Clément Stenac's avatar
Clément Stenac committed
627 628
/************************* Updates handling *****************************/

Clément Stenac's avatar
Clément Stenac committed
629
/**** Events processing ****/
Jakob Leben's avatar
Jakob Leben committed
630
void PLModel::processInputItemUpdate( input_thread_t *p_input )
631 632
{
    if( !p_input ) return;
633 634
    if( p_input && !( p_input->b_dead || !vlc_object_alive( p_input ) ) )
    {
Jakob Leben's avatar
Jakob Leben committed
635
        PLItem *item = findByInput( rootItem, input_GetItem( p_input )->i_id );
636
        if( item ) emit currentChanged( index( item, 0 ) );
637
    }
638
    processInputItemUpdate( input_GetItem( p_input ) );
639
}
640

Jakob Leben's avatar
Jakob Leben committed
641
void PLModel::processInputItemUpdate( input_item_t *p_item )
Clément Stenac's avatar
Clément Stenac committed
642
{
643
    if( !p_item ||  p_item->i_id <= 0 ) return;
Jakob Leben's avatar
Jakob Leben committed
644
    PLItem *item = findByInput( rootItem, p_item->i_id );
645
    if( item )
646
        updateTreeItem( item );
Clément Stenac's avatar
Clément Stenac committed
647 648
}

Jakob Leben's avatar
Jakob Leben committed
649
void PLModel::processItemRemoval( int i_id )
Clément Stenac's avatar
Clément Stenac committed
650
{
Clément Stenac's avatar
Clément Stenac committed
651
    if( i_id <= 0 ) return;
652
    removeItem( i_id );
Clément Stenac's avatar
Clément Stenac committed
653 654
}

655
void PLModel::processItemAppend( int i_item, int i_parent )
Clément Stenac's avatar
Clément Stenac committed
656 657
{
    playlist_item_t *p_item = NULL;
Clément Stenac's avatar
Clément Stenac committed
658
    PLItem *newItem = NULL;
659
    input_thread_t *currentInputThread;
660
    int pos;
Clément Stenac's avatar
Clément Stenac committed
661

662
    PLItem *nodeItem = findById( rootItem, i_parent );
663
    if( !nodeItem ) return;
Clément Stenac's avatar
Clément Stenac committed
664

Jakob Leben's avatar
Jakob Leben committed
665
    foreach( PLItem *existing, nodeItem->children )
666
      if( existing->i_id == i_item ) return;
667

668
    PL_LOCK;
669
    p_item = playlist_ItemGetById( p_playlist, i_item );
670 671 672 673
    if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG )
    {
        PL_UNLOCK; return;
    }
674

675 676 677
    for( pos = 0; pos < p_item->p_parent->i_children; pos++ )
        if( p_item->p_parent->pp_children[pos] == p_item ) break;

678
    newItem = new PLItem( p_item, nodeItem );
679 680
    PL_UNLOCK;

681 682
    beginInsertRows( index( nodeItem, 0 ), pos, pos );
    nodeItem->insertChild( newItem, pos );
683
    endInsertRows();
684

685 686
    if( newItem->p_input == THEMIM->currentInputItem() )
        emit currentChanged( index( newItem, 0 ) );
Clément Stenac's avatar
Clément Stenac committed
687
}
Clément Stenac's avatar
Clément Stenac committed
688

Clément Stenac's avatar
Clément Stenac committed
689 690 691

void PLModel::rebuild()
{
Jakob Leben's avatar
Jakob Leben committed
692
    rebuild( NULL );
Clément Stenac's avatar
Clément Stenac committed
693 694
}

Jakob Leben's avatar
Jakob Leben committed
695
void PLModel::rebuild( playlist_item_t *p_root )
Clément Stenac's avatar
Clément Stenac committed
696
{
Christophe Mutricy's avatar
Christophe Mutricy committed
697
    playlist_item_t* p_item;
698

Clément Stenac's avatar
Clément Stenac committed
699 700 701
    /* Invalidate cache */
    i_cached_id = i_cached_input_id = -1;

702
    if( rootItem ) rootItem->removeChildren();
703

704
    PL_LOCK;
Clément Stenac's avatar
Clément Stenac committed
705 706
    if( p_root )
    {
Rafaël Carré's avatar
Rafaël Carré committed
707
        delete rootItem;
708
        rootItem = new PLItem( p_root );
Clément Stenac's avatar
Clément Stenac committed
709 710
    }
    assert( rootItem );
Clément Stenac's avatar
Clément Stenac committed
711
    /* Recreate from root */
Jakob Leben's avatar
Jakob Leben committed
712
    updateChildren( rootItem );
Clément Stenac's avatar
Qt4:  
Clément Stenac committed
713
    PL_UNLOCK;
Clément Stenac's avatar
Clément Stenac committed
714 715

    /* And signal the view */
716
    reset();
717 718

    if( p_root ) emit rootChanged();
Clément Stenac's avatar
Clément Stenac committed
719 720
}

Jakob Leben's avatar
Jakob Leben committed
721
void PLModel::takeItem( PLItem *item )
722 723 724 725 726 727 728 729 730 731 732
{
    assert( item );
    PLItem *parent = item->parentItem;
    assert( parent );
    int i_index = parent->children.indexOf( item );

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

Jakob Leben's avatar
Jakob Leben committed
733
void PLModel::insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos )
734 735 736 737 738 739 740 741 742 743 744 745 746
{
    assert( node );
    int count = items.size();
    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();
}

Jakob Leben's avatar
Jakob Leben committed
747
void PLModel::removeItem( PLItem *item )
748 749
{
    if( !item ) return;
750

751
    i_cached_id = -1;
752 753 754 755 756 757 758 759 760
    i_cached_input_id = -1;

    if( item->parentItem ) {
        int i = item->parentItem->children.indexOf( item );
        beginRemoveRows( index( item->parentItem, 0), i, i );
        item->parentItem->children.removeAt(i);
        delete item;
        endRemoveRows();
    }
761 762
    else delete item;

763 764 765 766 767
    if(item == rootItem)
    {
        rootItem = NULL;
        rebuild( p_playlist->p_playing );
    }
768 769
}

Clément Stenac's avatar
Qt4:  
Clément Stenac committed
770
/* This function must be entered WITH the playlist lock */
Jakob Leben's avatar
Jakob Leben committed
771
void PLModel::updateChildren( PLItem *root )
Clément Stenac's avatar
Clément Stenac committed
772
{
773
    playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
Jakob Leben's avatar
Jakob Leben committed
774
    updateChildren( p_node, root );
Clément Stenac's avatar
Clément Stenac committed
775 776
}

Clément Stenac's avatar
Qt4:  
Clément Stenac committed
777
/* This function must be entered WITH the playlist lock */
Jakob Leben's avatar
Jakob Leben committed
778
void PLModel::updateChildren( playlist_item_t *p_node, PLItem *root )
Clément Stenac's avatar
Clément Stenac committed
779 780 781
{
    for( int i = 0; i < p_node->i_children ; i++ )
    {
Clément Stenac's avatar
Clément Stenac committed
782
        if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
783 784
        PLItem *newItem =  new PLItem( p_node->pp_children[i], root );
        root->appendChild( newItem );
785
        if( p_node->pp_children[i]->i_children != -1 )
Jakob Leben's avatar
Jakob Leben committed
786
            updateChildren( p_node->pp_children[i], newItem );
Clément Stenac's avatar
Clément Stenac committed
787 788 789
    }
}

790
/* Function doesn't need playlist-lock, as we don't touch playlist_item_t stuff here*/
791
void PLModel::updateTreeItem( PLItem *item )
Clément Stenac's avatar
Clément Stenac committed
792
{
793
    if( !item ) return;
794
    emit dataChanged( index( item, 0 ) , index( item, columnCount( QModelIndex() ) ) );
Clément Stenac's avatar
Clément Stenac committed
795
}
796

797 798 799
/************************* Actions ******************************/

/**
800 801
 * Deletion, don't delete items childrens if item is going to be
 * delete allready, so we remove childrens from selection-list.
802 803 804
 */
void PLModel::doDelete( QModelIndexList selected )
{
805 806
    if( !canEdit() ) return;

807
    while( !selected.isEmpty() )
808
    {
809 810 811
        QModelIndex index = selected[0];
        selected.removeAt( 0 );

812
        if( index.column() != 0 ) continue;
813

814
        PLItem *item = getItem( index );
815 816
        if( item->children.size() )
            recurseDelete( item->children, &selected );
817 818 819 820 821 822

        PL_LOCK;
        playlist_DeleteFromInput( p_playlist, item->p_input, pl_Locked );
        PL_UNLOCK;

        removeItem( item );
823 824 825
    }
}

826
void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList )
827 828 829 830 831 832
{
    for( int i = children.size() - 1; i >= 0 ; i-- )
    {
        PLItem *item = children[i];
        if( item->children.size() )
            recurseDelete( item->children, fullList );
833
        fullList->removeAll( index( item, 0 ) );
834 835 836
    }
}

Clément Stenac's avatar
Clément Stenac committed
837 838
/******* Volume III: Sorting and searching ********/
void PLModel::sort( int column, Qt::SortOrder order )
839 840 841 842 843
{
    sort( rootItem->i_id, column, order );
}

void PLModel::sort( int i_root_id, int column, Qt::SortOrder order )
Clément Stenac's avatar
Clément Stenac committed
844
{
845 846
    msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );

847 848
    int meta = columnToMeta( column );
    if( meta == COLUMN_END ) return;
849

Jakob Leben's avatar
Jakob Leben committed
850
    PLItem *item = findById( rootItem, i_root_id );
851 852 853 854 855 856
    if( !item ) return;
    QModelIndex qIndex = index( item, 0 );
    int count = item->children.size();
    if( count )
    {
        beginRemoveRows( qIndex, 0, count - 1 );
857
        item->removeChildren();
858 859
        endRemoveRows( );
    }
860

Clément Stenac's avatar
Clément Stenac committed
861 862
    PL_LOCK;
    {
863
        playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
864
                                                        i_root_id );
865
        if( p_root )
866
        {
Rafaël Carré's avatar
Rafaël Carré committed
867
            playlist_RecursiveNodeSort( p_playlist, p_root,
868
                                        i_column_sorting( meta ),
869 870
                                        order == Qt::AscendingOrder ?
                                            ORDER_NORMAL : ORDER_REVERSE );
871
        }
Clément Stenac's avatar
Clément Stenac committed
872
    }
873 874 875

    i_cached_id = i_cached_input_id = -1;

876 877 878
    if( count )
    {
        beginInsertRows( qIndex, 0, count - 1 );
Jakob Leben's avatar
Jakob Leben committed
879
        updateChildren( item );
880 881
        endInsertRows( );
    }
882
    PL_UNLOCK;
883 884 885 886 887 888 889 890 891
    /* if we have popup item, try to make sure that you keep that item visible */
    if( i_popup_item > -1 )
    {
        PLItem *popupitem = findById( rootItem, i_popup_item );
        if( popupitem ) emit currentChanged( index( popupitem, 0 ) );
        /* reset i_popup_item as we don't show it as selected anymore anyway */
        i_popup_item = -1;
    }
    else if( currentIndex().isValid() ) emit currentChanged( currentIndex() );
Clément Stenac's avatar
Clément Stenac committed
892 893
}

894
void PLModel::search( const QString& search_text, const QModelIndex & idx, bool b_recursive )
Clément Stenac's avatar
Clément Stenac committed
895 896 897
{
    /** \todo Fire the search with a small delay ? */
    PL_LOCK;
898 899
    {
        playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
900
                                                        itemId( idx ) );
901
        assert( p_root );
902
        const char *psz_name = qtu( search_text );
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
        playlist_LiveSearchUpdate( p_playlist , p_root, psz_name, b_recursive );

        if( idx.isValid() )
        {
            PLItem *searchRoot = getItem( idx );

            beginRemoveRows( idx, 0, searchRoot->children.size() - 1 );
            searchRoot->removeChildren();
            endRemoveRows( );

            beginInsertRows( idx, 0, searchRoot->children.size() - 1 );
            updateChildren( searchRoot );
            endInsertRows();

            PL_UNLOCK;
            return;
        }
920
    }
Clément Stenac's avatar
Clément Stenac committed
921 922 923 924
    PL_UNLOCK;
    rebuild();
}