playlist_model.cpp 35.2 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-2007 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Authors: Clément Stenac <zorglub@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
zorglub's avatar
zorglub committed
23
#define PLI_NAME( p ) p ? p->p_input->psz_name : "null"
zorglub's avatar
zorglub committed
24

zorglub's avatar
zorglub committed
25
#include <assert.h>
zorglub's avatar
zorglub committed
26
#include <QIcon>
zorglub's avatar
Qt4:    
zorglub committed
27
#include <QFont>
zorglub's avatar
zorglub committed
28
#include <QMenu>
zorglub's avatar
zorglub committed
29
#include <QApplication>
zorglub's avatar
zorglub committed
30
31

#include "qt4.hpp"
zorglub's avatar
zorglub committed
32
#include "playlist_model.hpp"
zorglub's avatar
zorglub committed
33
#include "dialogs/mediainfo.hpp"
34
#include <vlc_intf_strings.h>
zorglub's avatar
zorglub committed
35

zorglub's avatar
zorglub committed
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "pixmaps/type_unknown.xpm"
#include "pixmaps/type_afile.xpm"
#include "pixmaps/type_vfile.xpm"
#include "pixmaps/type_net.xpm"
#include "pixmaps/type_card.xpm"
#include "pixmaps/type_disc.xpm"
#include "pixmaps/type_cdda.xpm"
#include "pixmaps/type_directory.xpm"
#include "pixmaps/type_playlist.xpm"
#include "pixmaps/type_node.xpm"

QIcon PLModel::icons[ITEM_TYPE_NUMBER];

zorglub's avatar
zorglub committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
static int PlaylistChanged( vlc_object_t *, const char *,
                            vlc_value_t, vlc_value_t, void * );
static int PlaylistNext( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
static int ItemChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
                         vlc_value_t oval, vlc_value_t nval, void *param );
static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
                        vlc_value_t oval, vlc_value_t nval, void *param );

/*************************************************************************
 * Playlist item implementation
 *************************************************************************/

zorglub's avatar
zorglub committed
64
65
66
67
68
69
70
71
/**
 * Column strings
 *      Title
 *      Artist
 *      Duration
 */

void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
zorglub's avatar
zorglub committed
72
73
74
75
{
    parentItem = parent;
    i_id = _i_id; i_input_id = _i_input_id;
    model = m;
76
77
78
79
80
81


    if( parentItem == NULL )
    {
        i_showflags = config_GetInt( model->p_intf , "qt-pl-showflags" );
        updateview();
82
83
84
85
    } else {
        i_showflags = parentItem->i_showflags;
        //Add empty string and update() handles data appending
        strings.append( qtr("") ); 
86
87
88
89
90
91
92
    }
}

void PLItem::updateview( void )
{
    strings.clear();

93
94
95
96
97
98
    if( model->i_depth == 1 )  //left window for playlist etc.
    {
        strings.append( qtr("") );   
        return;
    }

99
    for( int i_index=1; i_index <= VLC_META_ENGINE_MB_TRM_ID; i_index = i_index*2 )
100
101
102
103
104
105
106
    {
        if( i_showflags & i_index )
        {
            switch( i_index )
            {
                case VLC_META_ENGINE_ARTIST:
                    strings.append( qtr( VLC_META_ARTIST ) );
107
                    break;
108
109
                case VLC_META_ENGINE_TITLE:
                    strings.append( qtr( VLC_META_TITLE ) );
110
                    break;
111
112
                case VLC_META_ENGINE_DESCRIPTION:
                    strings.append( qtr( VLC_META_DESCRIPTION ) );
113
                    break;
114
115
                case VLC_META_ENGINE_DURATION:
                    strings.append( qtr( "Duration" ) );
116
                    break;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
                case VLC_META_ENGINE_GENRE:
                    strings.append( qtr( VLC_META_GENRE ) );
                    break;
                case VLC_META_ENGINE_COLLECTION:
                    strings.append( qtr( VLC_META_COLLECTION ) );
                    break;
                case VLC_META_ENGINE_SEQ_NUM:
                    strings.append( qtr( VLC_META_SEQ_NUM ) );
                    break;
                case VLC_META_ENGINE_RATING:
                    strings.append( qtr( VLC_META_RATING ) );
                    break;
                default:
                    break;
            }
        }
    }
zorglub's avatar
zorglub committed
134
135
}

136

zorglub's avatar
zorglub committed
137
138
139
PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
{
    init( _i_id, _i_input_id, parent, m );
zorglub's avatar
zorglub committed
140
141
142
143
}

PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
{
zorglub's avatar
zorglub committed
144
    init( p_item->i_id, p_item->p_input->i_id, parent, m );
zorglub's avatar
zorglub committed
145
146
147
148
149
}

PLItem::~PLItem()
{
    qDeleteAll(children);
zorglub's avatar
zorglub committed
150
    children.clear();
zorglub's avatar
zorglub committed
151
152
}

zorglub's avatar
zorglub committed
153
void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
zorglub's avatar
zorglub committed
154
155
{
    assert( model );
zorglub's avatar
zorglub committed
156
157
    if( signal )
        model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
zorglub's avatar
zorglub committed
158
    children.insert( i_pos, item );
zorglub's avatar
zorglub committed
159
160
    if( signal )
        model->endInsertRows();
zorglub's avatar
zorglub committed
161
162
}

zorglub's avatar
Qt4:    
zorglub committed
163
164
165
166
167
168
169
170
171
void PLItem::remove( PLItem *removed )
{
    assert( model && parentItem );
    int i_index = parentItem->children.indexOf( removed );
    model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
    parentItem->children.removeAt( i_index );
    model->endRemoveRows();
}

zorglub's avatar
zorglub committed
172
173
int PLItem::row() const
{
174
    if( parentItem )
zorglub's avatar
zorglub committed
175
176
177
178
        return parentItem->children.indexOf(const_cast<PLItem*>(this));
    return 0;
}

zorglub's avatar
Qt4:    
zorglub committed
179
void PLItem::update( playlist_item_t *p_item, bool iscurrent )
zorglub's avatar
zorglub committed
180
{
zorglub's avatar
zorglub committed
181
    char psz_duration[MSTRTIME_MAX_SIZE];
zorglub's avatar
zorglub committed
182
    assert( p_item->p_input->i_id == i_input_id );
183

zorglub's avatar
zorglub committed
184
    type = p_item->p_input->i_type;
zorglub's avatar
Qt4:    
zorglub committed
185
    current = iscurrent;
186

187
188
189
190
    if( current && p_item->p_input->p_meta &&
        p_item->p_input->p_meta->psz_arturl &&
        !strncmp( p_item->p_input->p_meta->psz_arturl, "file://", 7 ) )
        model->sendArt( qfu( p_item->p_input->p_meta->psz_arturl ) );
191
192
    else if( current )
        model->removeArt();
193

194
195
    strings.clear();

196
197
198
199
    if( model->i_depth == 1 )  //left window for playlist etc.
    {
        strings.append( p_item->p_input->psz_name );
        return;
200
201
    }

202
203
204
205
206
207
208

#define ADD_META( meta ) {             \
   if( p_item->p_input->p_meta )       \
      strings.append( qfu( meta ) );   \
   else                                \
      strings.append( qtr( "" ) ); }

209
    for( int i_index=1; i_index <= VLC_META_ENGINE_MB_TRM_ID; i_index = i_index * 2 )
210
211
212
213
214
215
216
    {
        if( parentItem->i_showflags & i_index )
        {
            switch( i_index )
            {
                case VLC_META_ENGINE_ARTIST:
                    ADD_META( p_item->p_input->p_meta->psz_artist );
217
                    break;
218
219
220
221
222
                case VLC_META_ENGINE_TITLE:
                    if( p_item->p_input->p_meta && p_item->p_input->p_meta->psz_title )
                    {
                        ADD_META( p_item->p_input->p_meta->psz_title );
                    } else {
223
                        strings.append( qfu( p_item->p_input->psz_name ) );
224
                    }
225
                    break;
226
227
                case VLC_META_ENGINE_DESCRIPTION:
                    ADD_META( p_item->p_input->p_meta->psz_description );
228
                    break;
229
230
231
                case VLC_META_ENGINE_DURATION:
                    secstotimestr( psz_duration, p_item->p_input->i_duration / 1000000 );
                    strings.append( QString( psz_duration ) );
232
                    break;
233
234
235
236
237
238
239
240
241
242
243
244
245
                case VLC_META_ENGINE_GENRE:
                    ADD_META( p_item->p_input->p_meta->psz_genre );
                    break;
                case VLC_META_ENGINE_COLLECTION:
                    ADD_META( p_item->p_input->p_meta->psz_album );
                    break;
                case VLC_META_ENGINE_SEQ_NUM:
                    ADD_META( p_item->p_input->p_meta->psz_tracknum);
                    break;
                case VLC_META_ENGINE_RATING:
                    ADD_META( p_item->p_input->p_meta->psz_rating );
                default:
                    break;
246
            }
247
248
249
250
251
        }

    }
#undef ADD_META

zorglub's avatar
zorglub committed
252
}
zorglub's avatar
zorglub committed
253
254
255
256
257

/*************************************************************************
 * Playlist model implementation
 *************************************************************************/

zorglub's avatar
zorglub committed
258
PLModel::PLModel( playlist_t *_p_playlist, intf_thread_t *_p_intf,
zorglub's avatar
zorglub committed
259
                  playlist_item_t * p_root, int _i_depth, QObject *parent)
zorglub's avatar
zorglub committed
260
261
                                    : QAbstractItemModel(parent)
{
zorglub's avatar
zorglub committed
262
263
    i_depth = _i_depth;
    assert( i_depth == 1 || i_depth == -1 );
zorglub's avatar
zorglub committed
264
    p_intf = _p_intf;
zorglub's avatar
zorglub committed
265
    p_playlist= _p_playlist;
zorglub's avatar
zorglub committed
266
267
268
269
    i_items_to_append = 0;
    b_need_update     = false;
    i_cached_id       = -1;
    i_cached_input_id = -1;
zorglub's avatar
zorglub committed
270
    i_popup_item = i_popup_parent = -1;
zorglub's avatar
zorglub committed
271

zorglub's avatar
zorglub committed
272
273
274
275
276
277
278
279
280
281
282
283
#define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
    ADD_ICON( UNKNOWN , unknown );
    ADD_ICON( AFILE,afile );
    ADD_ICON( VFILE, vfile );
    ADD_ICON( DIRECTORY, directory );
    ADD_ICON( DISC, disc );
    ADD_ICON( CDDA, cdda );
    ADD_ICON( CARD, card );
    ADD_ICON( NET, net );
    ADD_ICON( PLAYLIST, playlist );
    ADD_ICON( NODE, node );

zorglub's avatar
zorglub committed
284
    rootItem = NULL;
zorglub's avatar
zorglub committed
285
    addCallbacks();
zorglub's avatar
zorglub committed
286
    rebuild( p_root );
zorglub's avatar
zorglub committed
287
288
}

zorglub's avatar
zorglub committed
289

zorglub's avatar
zorglub committed
290
291
292
293
294
295
PLModel::~PLModel()
{
    delCallbacks();
    delete rootItem;
}

zorglub's avatar
zorglub committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Qt::DropActions PLModel::supportedDropActions() const
{
    return Qt::CopyAction;
}

Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
    if( index.isValid() )
        return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    else
        return Qt::ItemIsDropEnabled | defaultFlags;
}

QStringList PLModel::mimeTypes() const
{
    QStringList types;
    types << "vlc/playlist-item-id";
    return types;
}

QMimeData *PLModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData *mimeData = new QMimeData();
    QByteArray encodedData;
    QDataStream stream(&encodedData, QIODevice::WriteOnly);

    foreach (QModelIndex index, indexes) {
        if (index.isValid() && index.column() == 0 )
            stream << itemId(index);
    }
    mimeData->setData("vlc/playlist-item-id", encodedData);
    return mimeData;
}

bool PLModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
                           int row, int column, const QModelIndex &target)
{
    if ( data->hasFormat("vlc/playlist-item-id") )
    {
        if (action == Qt::IgnoreAction)
            return true;

        PLItem *targetItem;
        if( target.isValid() )
            targetItem = static_cast<PLItem*>( target.internalPointer() );
        else
            targetItem = rootItem;

        QByteArray encodedData = data->data("vlc/playlist-item-id");
        QDataStream stream(&encodedData, QIODevice::ReadOnly);

        PLItem *newParentItem;
        while (!stream.atEnd())
        {
            int i;
            int srcId;
            stream >> srcId;

            PL_LOCK;
            playlist_item_t *p_target =
357
358
359
360
                        playlist_ItemGetById( p_playlist, targetItem->i_id,
                                              VLC_TRUE );
            playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
                                                           VLC_TRUE );
zorglub's avatar
zorglub committed
361
362
363
364
365
366
367
368
369
370
371

            if( !p_target || !p_src )
            {
                PL_UNLOCK;
                return false;
            }
            if( p_target->i_children == -1 ) /* A leaf */
            {
                PLItem *parentItem = targetItem->parent();
                assert( parentItem );
                playlist_item_t *p_parent =
372
373
                         playlist_ItemGetById( p_playlist, parentItem->i_id,
                                               VLC_TRUE );
zorglub's avatar
zorglub committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
                if( !p_parent )
                {
                    PL_UNLOCK;
                    return false;
                }
                for( i = 0 ; i< p_parent->i_children ; i++ )
                    if( p_parent->pp_children[i] == p_target ) break;
                playlist_TreeMove( p_playlist, p_src, p_parent, i );
                newParentItem = parentItem;
            }
            else
            {
                /* \todo: if we drop on a top-level node, use copy instead ? */
                playlist_TreeMove( p_playlist, p_src, p_target, 0 );
                i = 0;
                newParentItem = targetItem;
            }
            /* Remove from source */
zorglub's avatar
zorglub committed
392
393
394
395
396
397
398
399
400
401
            PLItem *srcItem = FindById( rootItem, p_src->i_id );
            // We dropped on the source selector. Ask the dialog to forward
            // to the main view
            if( !srcItem )
            {
                emit shouldRemove( p_src->i_id );
            }
            else
                srcItem->remove( srcItem );

zorglub's avatar
zorglub committed
402
403
404
405
406
407
408
409
410
411
            /* Display at new destination */
            PLItem *newItem = new PLItem( p_src, newParentItem, this );
            newParentItem->insertChild( newItem, i, true );
            UpdateTreeItem( p_src, newItem, true );
            if( p_src->i_children != -1 )
                UpdateNodeChildren( newItem );
            PL_UNLOCK;
        }
    }
    return true;
zorglub's avatar
zorglub committed
412
}
zorglub's avatar
zorglub committed
413

zorglub's avatar
zorglub committed
414
415
416
417
418
void PLModel::removeItem( int i_id )
{
    PLItem *item = FindById( rootItem,i_id );
    if( item ) item->remove( item );
}
zorglub's avatar
zorglub committed
419

zorglub's avatar
zorglub committed
420
421
void PLModel::addCallbacks()
{
zorglub's avatar
zorglub committed
422
423
424
425
426
427
428
429
430
431
    /* Some global changes happened -> Rebuild all */
    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
    /* We went to the next item */
    var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
    /* One item has been updated */
    var_AddCallback( p_playlist, "item-change", ItemChanged, this );
    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
}

zorglub's avatar
zorglub committed
432
void PLModel::delCallbacks()
zorglub's avatar
zorglub committed
433
434
435
436
437
438
439
440
{
    var_DelCallback( p_playlist, "item-change", ItemChanged, this );
    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
    var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
}

zorglub's avatar
zorglub committed
441
442
443
444
445
446
void PLModel::activateItem( const QModelIndex &index )
{
    assert( index.isValid() );
    PLItem *item = static_cast<PLItem*>(index.internalPointer());
    assert( item );
    PL_LOCK;
447
448
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
                                                    VLC_TRUE);
zorglub's avatar
zorglub committed
449
450
451
452
453
454
455
    activateItem( p_item );
    PL_UNLOCK;
}
/* Must be entered with lock */
void PLModel::activateItem( playlist_item_t *p_item )
{
    if( !p_item ) return;
zorglub's avatar
zorglub committed
456
457
458
459
460
461
462
    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 )
463
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, p_parent, p_item );
zorglub's avatar
zorglub committed
464
}
zorglub's avatar
zorglub committed
465

zorglub's avatar
zorglub committed
466
/****************** Base model mandatory implementations *****************/
zorglub's avatar
zorglub committed
467
468
QVariant PLModel::data(const QModelIndex &index, int role) const
{
zorglub's avatar
zorglub committed
469
    if(!index.isValid() ) return QVariant();
zorglub's avatar
zorglub committed
470
    PLItem *item = static_cast<PLItem*>(index.internalPointer());
zorglub's avatar
zorglub committed
471
472
473
474
475
476
477
478
479
    if( role == Qt::DisplayRole )
    {
        return QVariant( item->columnString( index.column() ) );
    }
    else if( role == Qt::DecorationRole && index.column() == 0  )
    {
        if( item->type >= 0 )
            return QVariant( PLModel::icons[item->type] );
    }
zorglub's avatar
Qt4:    
zorglub committed
480
481
482
483
484
485
486
    else if( role == Qt::FontRole )
    {
        if( item->current == true )
        {
            QFont f; f.setBold( true ); return QVariant( f );
        }
    }
zorglub's avatar
zorglub committed
487
    return QVariant();
zorglub's avatar
zorglub committed
488
489
}

zorglub's avatar
Qt4:    
zorglub committed
490
491
492
493
494
495
bool PLModel::isCurrent( const QModelIndex &index )
{
    assert( index.isValid() );
    return static_cast<PLItem*>(index.internalPointer())->current;
}

zorglub's avatar
zorglub committed
496
int PLModel::itemId( const QModelIndex &index ) const
zorglub's avatar
zorglub committed
497
{
zorglub's avatar
zorglub committed
498
499
500
    assert( index.isValid() );
    return static_cast<PLItem*>(index.internalPointer())->i_id;
}
zorglub's avatar
zorglub committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

QVariant PLModel::headerData( int section, Qt::Orientation orientation,
                              int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
            return QVariant( rootItem->columnString( section ) );
    return QVariant();
}

QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
                  const
{
    PLItem *parentItem;
    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<PLItem*>(parent.internalPointer());

    PLItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}

/* Return the index of a given item */
zorglub's avatar
zorglub committed
527
QModelIndex PLModel::index( PLItem *item, int column ) const
zorglub's avatar
zorglub committed
528
529
530
531
{
    if( !item ) return QModelIndex();
    const PLItem *parent = item->parent();
    if( parent )
532
533
        return createIndex( parent->children.lastIndexOf( item ),
                            column, item );
zorglub's avatar
zorglub committed
534
535
536
537
538
    return QModelIndex();
}

QModelIndex PLModel::parent(const QModelIndex &index) const
{
zorglub's avatar
Qt4:    
zorglub committed
539
    if( !index.isValid() ) return QModelIndex();
zorglub's avatar
zorglub committed
540
541

    PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
zorglub's avatar
zorglub committed
542
    if( !childItem ) { msg_Err( p_playlist, "NULL CHILD \n" ); return QModelIndex(); }
zorglub's avatar
zorglub committed
543
    PLItem *parentItem = childItem->parent();
zorglub's avatar
zorglub committed
544
    if( !parentItem || parentItem == rootItem ) return QModelIndex();
545
546
    if( ! parentItem->parentItem )
    {
zorglub's avatar
zorglub committed
547
548
        msg_Err( p_playlist, "No parent parent, trying row 0 " );
        msg_Err( p_playlist, "----- PLEASE REPORT THIS ------" );
549
550
        return createIndex( 0, 0, parentItem );
    }
zorglub's avatar
zorglub committed
551
552
    QModelIndex ind = createIndex(parentItem->row(), 0, parentItem);
    return ind;
zorglub's avatar
zorglub committed
553
554
}

zorglub's avatar
zorglub committed
555
int PLModel::columnCount( const QModelIndex &i) const
zorglub's avatar
zorglub committed
556
{
557
    return rootItem->strings.count();
zorglub's avatar
zorglub committed
558
559
}

zorglub's avatar
zorglub committed
560
int PLModel::childrenCount(const QModelIndex &parent) const
zorglub's avatar
zorglub committed
561
562
563
564
565
{
    return rowCount( parent );
}

int PLModel::rowCount(const QModelIndex &parent) const
zorglub's avatar
zorglub committed
566
567
568
569
570
571
572
573
574
575
576
{
    PLItem *parentItem;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<PLItem*>(parent.internalPointer());

    return parentItem->childCount();
}

zorglub's avatar
zorglub committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/************************* General playlist status ***********************/

bool PLModel::hasRandom()
{
    if( var_GetBool( p_playlist, "random" ) ) return true;
    return false;
}
bool PLModel::hasRepeat()
{
    if( var_GetBool( p_playlist, "repeat" ) ) return true;
    return false;
}
bool PLModel::hasLoop()
{
    if( var_GetBool( p_playlist, "loop" ) ) return true;
    return false;
}
void PLModel::setLoop( bool on )
{
    var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
zorglub's avatar
zorglub committed
597
    config_PutInt( p_playlist, "loop", on ? 1: 0 );
zorglub's avatar
zorglub committed
598
599
600
601
}
void PLModel::setRepeat( bool on )
{
    var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
zorglub's avatar
zorglub committed
602
    config_PutInt( p_playlist, "repeat", on ? 1: 0 );
zorglub's avatar
zorglub committed
603
604
605
606
}
void PLModel::setRandom( bool on )
{
    var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
zorglub's avatar
zorglub committed
607
    config_PutInt( p_playlist, "random", on ? 1: 0 );
zorglub's avatar
zorglub committed
608
609
}

zorglub's avatar
zorglub committed
610
611
612
613
614
615
616
617
618
619
620
621
/************************* Lookups *****************************/

PLItem *PLModel::FindById( PLItem *root, int i_id )
{
    return FindInner( root, i_id, false );
}

PLItem *PLModel::FindByInput( PLItem *root, int i_id )
{
    return FindInner( root, i_id, true );
}

zorglub's avatar
zorglub committed
622
623
#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; }
zorglub's avatar
zorglub committed
624
625
626

PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
{
zorglub's avatar
zorglub committed
627
    if( ( !b_input && i_cached_id == i_id) ||
zorglub's avatar
zorglub committed
628
        ( b_input && i_cached_input_id ==i_id ) )
629
    {
zorglub's avatar
zorglub committed
630
        return b_input ? p_cached_item_bi : p_cached_item;
631
    }
zorglub's avatar
zorglub committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

    if( !b_input && root->i_id == i_id )
    {
        CACHE( i_id, root );
        return root;
    }
    else if( b_input && root->i_input_id == i_id )
    {
        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;
        }
        else if( b_input && (*it)->i_input_id == i_id )
        {
            ICACHE( i_id, (*it) );
655
            return p_cached_item_bi;
zorglub's avatar
zorglub committed
656
657
658
659
        }
        if( (*it)->children.size() )
        {
            PLItem *childFound = FindInner( (*it), i_id, b_input );
zorglub's avatar
zorglub committed
660
            if( childFound )
zorglub's avatar
zorglub committed
661
662
            {
                if( b_input )
zorglub's avatar
zorglub committed
663
                    ICACHE( i_id, childFound )
zorglub's avatar
zorglub committed
664
                else
zorglub's avatar
zorglub committed
665
                    CACHE( i_id, childFound )
zorglub's avatar
zorglub committed
666
                return childFound;
zorglub's avatar
zorglub committed
667
            }
zorglub's avatar
zorglub committed
668
        }
zorglub's avatar
zorglub committed
669
        it++;
zorglub's avatar
zorglub committed
670
671
672
673
674
675
676
677
678
679
    }
    return NULL;
}
#undef CACHE
#undef ICACHE


/************************* Updates handling *****************************/
void PLModel::customEvent( QEvent *event )
{
zorglub's avatar
zorglub committed
680
681
    int type = event->type();
    if( type != ItemUpdate_Type && type != ItemAppend_Type &&
zorglub's avatar
zorglub committed
682
        type != ItemDelete_Type && type != PLUpdate_Type )
zorglub's avatar
zorglub committed
683
684
        return;

zorglub's avatar
zorglub committed
685
    PLEvent *ple = static_cast<PLEvent *>(event);
zorglub's avatar
zorglub committed
686

zorglub's avatar
zorglub committed
687
    if( type == ItemUpdate_Type )
zorglub's avatar
zorglub committed
688
        ProcessInputItemUpdate( ple->i_id );
zorglub's avatar
zorglub committed
689
    else if( type == ItemAppend_Type )
zorglub's avatar
zorglub committed
690
        ProcessItemAppend( ple->p_add );
zorglub's avatar
zorglub committed
691
    else if( type == ItemDelete_Type )
zorglub's avatar
zorglub committed
692
        ProcessItemRemoval( ple->i_id );
zorglub's avatar
zorglub committed
693
694
    else
        rebuild();
zorglub's avatar
zorglub committed
695
696
}

zorglub's avatar
zorglub committed
697
/**** Events processing ****/
zorglub's avatar
zorglub committed
698
699
void PLModel::ProcessInputItemUpdate( int i_input_id )
{
zorglub's avatar
zorglub committed
700
701
    if( i_input_id <= 0 ) return;
    PLItem *item = FindByInput( rootItem, i_input_id );
zorglub's avatar
zorglub committed
702
703
    if( item )
        UpdateTreeItem( item, true );
zorglub's avatar
zorglub committed
704
705
706
707
}

void PLModel::ProcessItemRemoval( int i_id )
{
zorglub's avatar
zorglub committed
708
    if( i_id <= 0 ) return;
zorglub's avatar
zorglub committed
709
710
    if( i_id == i_cached_id ) i_cached_id = -1;
    i_cached_input_id = -1;
zorglub's avatar
Qt4:    
zorglub committed
711
712
713
    PLItem *item = FindById( rootItem, i_id );
    if( item )
        item->remove( item );
zorglub's avatar
zorglub committed
714
715
716
717
718
}

void PLModel::ProcessItemAppend( playlist_add_t *p_add )
{
    playlist_item_t *p_item = NULL;
zorglub's avatar
zorglub committed
719
    PLItem *newItem = NULL;
zorglub's avatar
zorglub committed
720
721
722
723
    i_items_to_append--;
    if( b_need_update ) return;

    PLItem *nodeItem = FindById( rootItem, p_add->i_node );
zorglub's avatar
zorglub committed
724
    PL_LOCK;
zorglub's avatar
zorglub committed
725
726
    if( !nodeItem ) goto end;

727
    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
zorglub's avatar
zorglub committed
728
    if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
zorglub's avatar
zorglub committed
729
730
731
732
    if( i_depth == 1 && p_item->p_parent &&
                        p_item->p_parent->i_id != rootItem->i_id )
        goto end;

zorglub's avatar
zorglub committed
733
734
735
    newItem = new PLItem( p_item, nodeItem, this );
    nodeItem->appendChild( newItem );
    UpdateTreeItem( p_item, newItem, true );
zorglub's avatar
zorglub committed
736
end:
zorglub's avatar
Qt4:    
zorglub committed
737
    PL_UNLOCK;
zorglub's avatar
zorglub committed
738
739
    return;
}
zorglub's avatar
zorglub committed
740

zorglub's avatar
zorglub committed
741
742
743
744
745
746
747

void PLModel::rebuild()
{
    rebuild( NULL );
}

void PLModel::rebuild( playlist_item_t *p_root )
zorglub's avatar
zorglub committed
748
749
750
751
752
753
{
    /* Remove callbacks before locking to avoid deadlocks */
    delCallbacks();
    /* Invalidate cache */
    i_cached_id = i_cached_input_id = -1;

zorglub's avatar
Qt4:    
zorglub committed
754
    PL_LOCK;
zorglub's avatar
zorglub committed
755
    /* Clear the tree */
zorglub's avatar
zorglub committed
756
757
    if( rootItem )
    {
758
759
760
761
762
763
764
765
        if( rootItem->children.size() )
        {
            beginRemoveRows( index( rootItem, 0 ), 0,
                    rootItem->children.size() -1 );
            qDeleteAll( rootItem->children );
            rootItem->children.clear();
            endRemoveRows();
        }
zorglub's avatar
zorglub committed
766
767
768
769
770
771
772
    }
    if( p_root )
    {
        //if( rootItem ) delete rootItem;
        rootItem = new PLItem( p_root, NULL, this );
    }
    assert( rootItem );
zorglub's avatar
zorglub committed
773
774
    /* Recreate from root */
    UpdateNodeChildren( rootItem );
zorglub's avatar
zorglub committed
775
776
777
778
779
780
781
782
783
784
    if( p_playlist->status.p_item )
    {
        PLItem *currentItem = FindByInput( rootItem,
                                     p_playlist->status.p_item->p_input->i_id );
        if( currentItem )
        {
            UpdateTreeItem( p_playlist->status.p_item, currentItem,
                            true, false );
        }
    }
zorglub's avatar
Qt4:    
zorglub committed
785
    PL_UNLOCK;
zorglub's avatar
zorglub committed
786
787
788
789
790
791

    /* And signal the view */
    emit layoutChanged();
    addCallbacks();
}

zorglub's avatar
Qt4:    
zorglub committed
792
/* This function must be entered WITH the playlist lock */
zorglub's avatar
zorglub committed
793
794
void PLModel::UpdateNodeChildren( PLItem *root )
{
795
796
    playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
                                                    VLC_TRUE );
zorglub's avatar
zorglub committed
797
798
799
    UpdateNodeChildren( p_node, root );
}

zorglub's avatar
Qt4:    
zorglub committed
800
/* This function must be entered WITH the playlist lock */
zorglub's avatar
zorglub committed
801
802
803
804
void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
{
    for( int i = 0; i < p_node->i_children ; i++ )
    {
zorglub's avatar
zorglub committed
805
        if( p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG ) continue;
zorglub's avatar
zorglub committed
806
807
        PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
        root->appendChild( newItem, false );
zorglub's avatar
zorglub committed
808
809
        UpdateTreeItem( newItem, false, true );
        if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
zorglub's avatar
zorglub committed
810
811
812
813
            UpdateNodeChildren( p_node->pp_children[i], newItem );
    }
}

zorglub's avatar
Qt4:    
zorglub committed
814
/* This function must be entered WITH the playlist lock */
zorglub's avatar
zorglub committed
815
void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
zorglub's avatar
zorglub committed
816
{
817
818
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
                                                    VLC_TRUE );
zorglub's avatar
zorglub committed
819
    UpdateTreeItem( p_item, item, signal, force );
zorglub's avatar
zorglub committed
820
821
}

zorglub's avatar
Qt4:    
zorglub committed
822
/* This function must be entered WITH the playlist lock */
zorglub's avatar
zorglub committed
823
824
void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
                              bool signal, bool force )
zorglub's avatar
zorglub committed
825
{
826
827
    if ( !p_item )
        return;
zorglub's avatar
zorglub committed
828
    if( !force && i_depth == 1 && p_item->p_parent &&
zorglub's avatar
zorglub committed
829
                                 p_item->p_parent->i_id != rootItem->i_id )
zorglub's avatar
zorglub committed
830
        return;
zorglub's avatar
Qt4:    
zorglub committed
831
    item->update( p_item, p_item == p_playlist->status.p_item );
zorglub's avatar
zorglub committed
832
    if( signal )
zorglub's avatar
Qt4:    
zorglub committed
833
        emit dataChanged( index( item, 0 ) , index( item, 1 ) );
zorglub's avatar
zorglub committed
834
}
zorglub's avatar
zorglub committed
835

zorglub's avatar
zorglub committed
836
837
/************************* Actions ******************************/

838
839
840
841
842
843
void PLModel::sendArt( QString url )
{
    QString arturl = url.replace( "file://",QString("" ) );
    emit artSet( arturl );
}

844
845
846
847
848
void PLModel::removeArt()
{
    emit artSet( QString() );
}

zorglub's avatar
zorglub committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/**
 * Deletion, here we have to do a ugly slow hack as we retrieve the full
 * list of indexes to delete at once: when we delete a node and all of
 * its children, we need to update the list.
 * Todo: investigate whethere we can use ranges to be sure to delete all items?
 */
void PLModel::doDelete( QModelIndexList selected )
{
    for( int i = selected.size() -1 ; i >= 0; i-- )
    {
        QModelIndex index = selected[i];
        if( index.column() != 0 ) continue;
        PLItem *item = static_cast<PLItem*>(index.internalPointer());
        if( item )
        {
            if( item->children.size() )
                recurseDelete( item->children, &selected );
            doDeleteItem( item, &selected );
        }
    }
}

void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList)
{
    for( int i = children.size() - 1; i >= 0 ; i-- )
    {
        PLItem *item = children[i];
        if( item->children.size() )
            recurseDelete( item->children, fullList );
        doDeleteItem( item, fullList );
    }
}

void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
{
    QModelIndex deleteIndex = index( item, 0 );
    fullList->removeAll( deleteIndex );

    PL_LOCK;
888
889
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
                                                    VLC_TRUE );
zorglub's avatar
zorglub committed
890
891
892
893
894
    if( !p_item )
    {
        PL_UNLOCK; return;
    }
    if( p_item->i_children == -1 )
895
        playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
zorglub's avatar
zorglub committed
896
897
898
899
900
901
902
    else
        playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
    /* And finally, remove it from the tree */
    item->remove( item );
    PL_UNLOCK;
}

zorglub's avatar
zorglub committed
903
904
905
906
907
/******* Volume III: Sorting and searching ********/
void PLModel::sort( int column, Qt::SortOrder order )
{
    PL_LOCK;
    {
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
        playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
                                                        rootItem->i_id,
                                                        VLC_TRUE );
        int i_mode;
        switch( column )
        {
        case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
        case 1: i_mode = SORT_ARTIST;break;
        case 2: i_mode = SORT_DURATION; break;
        default: i_mode = SORT_TITLE_NODES_FIRST; break;
        }
        if( p_root )
            playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
                                        order == Qt::AscendingOrder ?
                                            ORDER_NORMAL : ORDER_REVERSE );
zorglub's avatar
zorglub committed
923
924
925
926
927
    }
    PL_UNLOCK
    rebuild();
}

zorglub's avatar
zorglub committed
928
929
930
931
void PLModel::search( QString search_text )
{
    /** \todo Fire the search with a small delay ? */
    PL_LOCK;
932
933
934
935
936
937
938
939
    {
        playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
                                                        rootItem->i_id,
                                                        VLC_TRUE );
        assert( p_root );
        char *psz_name = search_text.toUtf8().data();
        playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
    }
zorglub's avatar
zorglub committed
940
941
942
943
    PL_UNLOCK;
    rebuild();
}

zorglub's avatar
zorglub committed
944
945
946
947
948
949
/*********** Popup *********/
void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
{
    assert( index.isValid() );
    PL_LOCK;
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
950
                                                    itemId( index ), VLC_TRUE );
zorglub's avatar
zorglub committed
951
952
953
954
955
956
957
    if( p_item )
    {
        i_popup_item = p_item->i_id;
        i_popup_parent = p_item->p_parent ? p_item->p_parent->i_id : -1;
        PL_UNLOCK;
        current_selection = list;
        QMenu *menu = new QMenu;
958
959
        menu->addAction( qfu(I_POP_PLAY), this, SLOT( popupPlay() ) );
        menu->addAction( qfu(I_POP_DEL), this, SLOT( popupDel() ) );
zorglub's avatar
zorglub committed
960
961
962
963
        menu->addSeparator();
        menu->addAction( qfu(I_POP_STREAM), this, SLOT( popupStream() ) );
        menu->addAction( qfu(I_POP_SAVE), this, SLOT( popupSave() ) );
        menu->addSeparator();
964
        menu->addAction( qfu(I_POP_INFO), this, SLOT( popupInfo() ) );
zorglub's avatar
zorglub committed
965
966
967
        if( p_item->i_children > -1 )
        {
            menu->addSeparator();
968
969
            menu->addAction( qfu(I_POP_SORT), this, SLOT( popupSort() ) );
            menu->addAction( qfu(I_POP_ADD), this, SLOT( popupAdd() ) );
zorglub's avatar
zorglub committed
970
        }
971
972
973
974
        menu->addSeparator();

        ContextUpdateMapper = new QSignalMapper(this);

975
976
        QMenu *selectColMenu = new QMenu( qtr("Show columns") );

977
#define ADD_META_ACTION( meta ) { \
978
979
980
981
982
   QAction* option = selectColMenu->addAction( qfu(VLC_META_##meta) );     \
   option->setCheckable( true );                                           \
   option->setChecked( rootItem->i_showflags & VLC_META_ENGINE_##meta );   \
   ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta );      \
   CONNECT( option, triggered(), ContextUpdateMapper, map() );             \
983
984
985
986
987
988
989
990
991
992
993
994
995
   }
        CONNECT(ContextUpdateMapper, mapped( int ), this, viewchanged( int ) );

        ADD_META_ACTION( TITLE );
        ADD_META_ACTION( ARTIST );
        ADD_META_ACTION( DURATION );
        ADD_META_ACTION( COLLECTION );
        ADD_META_ACTION( GENRE );
        ADD_META_ACTION( SEQ_NUM );
        ADD_META_ACTION( RATING );
        ADD_META_ACTION( DESCRIPTION );

#undef ADD_META_ACTION
996
        menu->addMenu( selectColMenu );
zorglub's avatar
zorglub committed
997
998
999
1000
1001
1002
        menu->popup( point );
    }
    else
        PL_UNLOCK;
}

1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060

void PLModel::viewchanged( int meta )
{
   if( rootItem )
   {
       int index=0;
       switch( meta )
       {
           case VLC_META_ENGINE_TITLE:
               index=0;
               break;
           case VLC_META_ENGINE_DURATION:
               index=1;
               break;
           case VLC_META_ENGINE_ARTIST:
               index=2;
               break;
           case VLC_META_ENGINE_GENRE:
               index=3;
               break;
           case VLC_META_ENGINE_COPYRIGHT:
               index=4;
               break;
           case VLC_META_ENGINE_COLLECTION:
               index=5;
               break;
           case VLC_META_ENGINE_SEQ_NUM:
               index=6;
               break;
           case VLC_META_ENGINE_DESCRIPTION:
               index=7;
               break;
           default:
               break;
       }
       emit layoutAboutToBeChanged();
       index = __MIN( index , rootItem->strings.count() );
       QModelIndex parent = createIndex( 0, 0, rootItem );
       if( rootItem->i_showflags & meta )
       {
           beginRemoveColumns( parent , index, index+1 );
           rootItem->i_showflags &= ~( meta );
           rootItem->updateview();
           endRemoveColumns();
       }
       else
       {
           beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
           rootItem->i_showflags |= meta;
           rootItem->updateview();
           endInsertColumns();
       }
       rebuild();
       config_PutInt( p_intf, "qt-pl-showflags", rootItem->i_showflags );
       config_SaveConfigFile( p_intf, NULL );
   }
}

zorglub's avatar
zorglub committed
1061
1062
1063
1064
1065
1066
1067
void PLModel::popupDel()
{
    doDelete( current_selection );
}
void PLModel::popupPlay()
{
    PL_LOCK;
1068
1069
1070
1071
1072
    {
        playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
                                                        i_popup_item,VLC_TRUE );
        activateItem( p_item );
    }
zorglub's avatar
zorglub committed
1073
1074
1075
    PL_UNLOCK;
}

zorglub's avatar
zorglub committed
1076
1077
void PLModel::popupInfo()
{
zorglub's avatar
zorglub committed
1078
1079
1080
1081
1082
1083
1084
1085
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
                                                    i_popup_item,VLC_TRUE );
    if( p_item )
    {
        MediaInfoDialog *mid = new MediaInfoDialog( p_intf );
        mid->setInput( p_item->p_input );
        mid->show();
    }
zorglub's avatar
zorglub committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
}

void PLModel::popupStream()
{
    fprintf( stderr, "Stream not implemented\n" );
}
void PLModel::popupSave()
{
    fprintf( stderr, "Save not implemented\n" );
}

zorglub's avatar
zorglub committed
1097
/**********************************************************************
zorglub's avatar
zorglub committed
1098
 * Playlist callbacks
zorglub's avatar
zorglub committed
1099
1100
1101
1102
1103
 **********************************************************************/
static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
                            vlc_value_t oval, vlc_value_t nval, void *param )
{
    PLModel *p_model = (PLModel *) param;
zorglub's avatar
zorglub committed
1104
1105
    PLEvent *event = new PLEvent( PLUpdate_Type, 0 );
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
zorglub's avatar
zorglub committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    return VLC_SUCCESS;
}

static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
                         vlc_value_t oval, vlc_value_t nval, void *param )
{
    PLModel *p_model = (PLModel *) param;
    PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
    event = new PLEvent( ItemUpdate_Type, nval.i_int );
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
                        vlc_value_t oval, vlc_value_t nval, void *param )
{
    PLModel *p_model = (PLModel *) param;
zorglub's avatar
zorglub committed
1124
    PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
zorglub's avatar
zorglub committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
                        vlc_value_t oval, vlc_value_t nval, void *param )
{
    PLModel *p_model = (PLModel *) param;
    PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
                         vlc_value_t oval, vlc_value_t nval, void *param )
{
    PLModel *p_model = (PLModel *) param;
    playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
    memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );

    if( ++p_model->i_items_to_append >= 50 )
    {
zorglub's avatar
zorglub committed
1147
1148
//        p_model->b_need_update = VLC_TRUE;
//        return VLC_SUCCESS;
zorglub's avatar
zorglub committed
1149
1150
1151
1152
1153
    }
    PLEvent *event = new PLEvent(  p_add );
    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}