selector.cpp 16.7 KB
Newer Older
1
2
3
/*****************************************************************************
 * selector.cpp : Playlist source selector
 ****************************************************************************
4
 * Copyright (C) 2006-2009 the VideoLAN team
Pierre's avatar
Pierre committed
5
 * $Id$
6
7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Jean-Baptiste Kempf
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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.
 *****************************************************************************/
24

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

zorglub's avatar
zorglub committed
29
#include "qt4.hpp"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
30
31
32
#include "components/playlist/selector.hpp"
#include "playlist_model.hpp"                /* plMimeData */
#include "input_manager.hpp"                 /* MainInputManager, for podcast */
33

34
35
#include <QInputDialog>
#include <QMessageBox>
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
36
37
38
39
40
#include <QMimeData>
#include <QDragMoveEvent>
#include <QTreeWidgetItem>
#include <QHBoxLayout>
#include <QPainter>
41
#include <QPalette>
42
43

#include <vlc_playlist.h>
44
#include <vlc_services_discovery.h>
45

jpd's avatar
jpd committed
46
47
48
49
50
51
52
53
54
55
56
void SelectorActionButton::paintEvent( QPaintEvent *event )
{
    QPainter p( this );
    QColor color = palette().color( QPalette::HighlightedText );
    color.setAlpha( 80 );
    if( underMouse() )
        p.fillRect( rect(), color );
    p.setPen( color );
    int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
    p.drawLine( rect().topLeft() + QPoint( 0, frame ),
                rect().bottomLeft() - QPoint( 0, frame ) );
57
    QFramelessButton::paintEvent( event );
jpd's avatar
jpd committed
58
59
}

60
PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
61
    : qitem(i), lblAction( NULL)
62
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
63
    layout = new QHBoxLayout( this );
64
    layout->setContentsMargins(0,0,0,0);
65
    layout->addSpacing( 3 );
66

67
    lbl = new QElidingLabel( text );
68
    layout->addWidget(lbl, 1);
69

jpd's avatar
jpd committed
70
71
    int height = qMax( 22, fontMetrics().height() + 8 );
    setMinimumHeight( height );
72
73
74
75
}

void PLSelItem::addAction( ItemAction act, const QString& tooltip )
{
76
77
78
    if( lblAction ) return; //might change later

    QIcon icon;
79
80
81
82

    switch( act )
    {
    case ADD_ACTION:
83
        icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
84
    case RM_ACTION:
85
        icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
86
87
    }

jpd's avatar
jpd committed
88
    lblAction = new SelectorActionButton();
jpd's avatar
jpd committed
89
    lblAction->setIcon( icon );
jpd's avatar
jpd committed
90
    lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
91
92
93
94
95

    if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );

    layout->addWidget( lblAction, 0 );
    lblAction->hide();
96

97
    CONNECT( lblAction, clicked(), this, triggerAction() );
98
99
100
}


101
PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
102
           : QTreeWidget( p ), p_intf(_p_intf)
103
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
104
    /* Properties */
105
    setFrameStyle( QFrame::NoFrame );
106
    setAttribute( Qt::WA_MacShowFocusRect, false );
107
    viewport()->setAutoFillBackground( false );
108
    setIconSize( QSize( 24,24 ) );
jpd's avatar
jpd committed
109
    setIndentation( 12 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
110
    setHeaderHidden( true );
111
    setRootIsDecorated( true );
112
    setAlternatingRowColors( false );
jpd's avatar
jpd committed
113

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
114
    /* drops */
115
116
117
    viewport()->setAcceptDrops(true);
    setDropIndicatorShown(true);
    invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
118

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
119
120
121
122
123
    /* Podcasts */
    podcastsParent = NULL;
    podcastsParentId = -1;

    /* Podcast connects */
124
    CONNECT( THEMIM, playlistItemAppended( int, int ),
125
             this, plItemAdded( int, int ) );
126
    CONNECT( THEMIM, playlistItemRemoved( int ),
127
             this, plItemRemoved( int ) );
128
129
    DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
              this, inputItemUpdate( input_item_t * ) );
130

131
    createItems();
132
    CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
133
             this, setSource( QTreeWidgetItem *) );
134
135
136
    CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
             this, setSource( QTreeWidgetItem *) );

137
    /* I believe this is unnecessary, seeing
138
139
140
       QStyle::SH_ItemView_ActivateItemOnSingleClick
        CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
             this, setSource( QTreeWidgetItem *) ); */
zorglub's avatar
zorglub committed
141

142
    /* select the first item */
143
//  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
144
145

#ifdef Q_WS_MAC
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
146
    setAutoFillBackground( true );
147
148
149
150
    QPalette palette;
    palette.setColor( QPalette::Window, QColor(209,215,226) );
    setPalette( palette );
#endif
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
151
    setMinimumHeight( 120 );
zorglub's avatar
zorglub committed
152
153
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
PLSelector::~PLSelector()
{
    if( podcastsParent )
    {
        int c = podcastsParent->childCount();
        for( int i = 0; i < c; i++ )
        {
            QTreeWidgetItem *item = podcastsParent->child(i);
            input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
            vlc_gc_decref( p_input );
        }
    }
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
{
    item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
    item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
    return item;
}

PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
{
    item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
/*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
    item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
    return item;
}

void PLSelector::createItems()
{
    /* PL */
186
    PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
187
188
189
190
                              THEPL->p_playing );
    pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );

    /* ML */
191
    PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
192
193
194
                              THEPL->p_media_library );
    ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );

195
    /* SQL ML */
196
    QTreeWidgetItem *sql_ml =  addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
197

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
198
    /* SD nodes */
199
200
201
202
    QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer") )->treeItem();
    QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices") )->treeItem();
    QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network") )->treeItem();
    QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet") )->treeItem();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

    /* SD subnodes */
    char **ppsz_longnames;
    int *p_categories;
    char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
    if( !ppsz_names )
        return;

    char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
    int *p_category = p_categories;
    for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
    {
        //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );

        PLSelItem *selItem;
        switch( *p_category )
        {
        case SD_CAT_INTERNET:
            {
            selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
            if( !strncmp( *ppsz_name, "podcast", 7 ) )
            {
                selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
                selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
                CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
                podcastsParent = selItem->treeItem();
            }
            }
            break;
        case SD_CAT_DEVICES:
            selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
            break;
        case SD_CAT_LAN:
            selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
            break;
        case SD_CAT_MYCOMPUTER:
            selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
            break;
        default:
            selItem = addItem( SD_TYPE, *ppsz_longname );
        }

        putSDData( selItem, *ppsz_name, *ppsz_longname );
        free( *ppsz_name );
        free( *ppsz_longname );
    }
    free( ppsz_names );
    free( ppsz_longnames );
    free( p_categories );

    if( mycomp->childCount() == 0 ) delete mycomp;
    if( devices->childCount() == 0 ) delete devices;
    if( lan->childCount() == 0 ) delete lan;
    if( internet->childCount() == 0 ) delete internet;
}

259
void PLSelector::setSource( QTreeWidgetItem *item )
zorglub's avatar
zorglub committed
260
{
261
262
263
    if( !item )
        return;

264
265
    bool b_ok;
    int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
266
    if( !b_ok || i_type == CATEGORY_TYPE )
267
        return;
268

269
    bool sd_loaded;
270
271
    if( i_type == SD_TYPE )
    {
272
        QString qs = item->data( 0, NAME_ROLE ).toString();
273
274
        sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
        if( !sd_loaded )
275
276
            playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
    }
277
    else if( i_type == SQL_ML_TYPE )
278
    {
279
        emit categoryActivated( NULL, true );
280
281
        return;
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
282

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
283
    /* */
284
285
    playlist_Lock( THEPL );
    playlist_item_t *pl_item = NULL;
286

287
288
    /* Special case for podcast */
    // FIXME: simplify
289
    if( i_type == SD_TYPE )
290
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
291
292
293
294
295
        /* Find the right item for the SD */
        pl_item = playlist_ChildSearchName( THEPL->p_root,
                      qtu( item->data(0, LONGNAME_ROLE ).toString() ) );

        /* Podcasts */
296
297
298
299
300
301
302
303
304
305
306
307
308
309
        if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
        {
            if( pl_item && !sd_loaded )
            {
                podcastsParentId = pl_item->i_id;
                for( int i=0; i < pl_item->i_children; i++ )
                    addPodcastItem( pl_item->pp_children[i] );
            }
            pl_item = NULL; //to prevent activating it
        }
    }
    else
        pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();

310
311
    playlist_Unlock( THEPL );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
312
    /* */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
313
    if( pl_item )
314
        emit categoryActivated( pl_item, false );
315
316
}

317
PLSelItem * PLSelector::addItem (
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
318
319
    SelectorItemType type, const char* str, bool drop,
    QTreeWidgetItem* parentItem )
320
321
322
{
  QTreeWidgetItem *item = parentItem ?
      new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
323

324
  PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
325
326
327
328
329
330
331
332
  setItemWidget( item, 0, selItem );
  item->setData( 0, TYPE_ROLE, (int)type );
  if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );

  return selItem;
}

PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
jpd's avatar
jpd committed
333
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
334
    vlc_gc_incref( p_item->p_input );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
335

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
336
    char *psz_name = input_item_GetName( p_item->p_input );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
337
338
339
    PLSelItem *item = addItem( PL_ITEM_TYPE,  psz_name, false, podcastsParent );
    free( psz_name );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
340
341
342
343
344
345
    item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
    item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
    item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
    item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
    CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
    return item;
jpd's avatar
jpd committed
346
347
}

348
349
350
QStringList PLSelector::mimeTypes() const
{
    QStringList types;
351
    types << "vlc/qt-input-items";
352
353
354
355
    return types;
}

bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
356
    const QMimeData * data, Qt::DropAction action )
357
358
359
360
361
{
    if( !parent ) return false;

    QVariant type = parent->data( 0, TYPE_ROLE );
    if( type == QVariant() ) return false;
362

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
363
    int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
364
    if( i_truth != IS_PL && i_truth != IS_ML ) return false;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
365

366
    bool to_pl = ( i_truth == IS_PL );
367

368
369
370
371
    const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
    if( !plMimeData ) return false;

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

373
    playlist_Lock( THEPL );
jpd's avatar
jpd committed
374

375
376
377
378
379
    foreach( input_item_t *p_input, inputItems )
    {
        playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
        if( !p_item ) continue;

jpd's avatar
jpd committed
380
381
382
        playlist_NodeAddCopy( THEPL, p_item,
                              to_pl ? THEPL->p_playing : THEPL->p_media_library,
                              PLAYLIST_END );
383
    }
384
385
386

    playlist_Unlock( THEPL );

387
388
389
    return true;
}

390
391
392
393
394
395
void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
{
    event->setDropAction( Qt::CopyAction );
    QAbstractItemView::dragMoveEvent( event );
}

396
void PLSelector::plItemAdded( int item, int parent )
397
{
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    if( parent != podcastsParentId ) return;

    playlist_Lock( THEPL );

    playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
    if( !p_item ) {
        playlist_Unlock( THEPL );
        return;
    }

    int c = podcastsParent->childCount();
    for( int i = 0; i < c; i++ )
    {
        QTreeWidgetItem *podItem = podcastsParent->child(i);
        if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
        {
          //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
          playlist_Unlock( THEPL );
          return;
        }
    }

    //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
    addPodcastItem( p_item );

    playlist_Unlock( THEPL );

    podcastsParent->setExpanded( true );
}

void PLSelector::plItemRemoved( int id )
{
    int c = podcastsParent->childCount();
    for( int i = 0; i < c; i++ )
    {
        QTreeWidgetItem *item = podcastsParent->child(i);
        if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
        {
            input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
            //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
            vlc_gc_decref( p_input );
            delete item;
            return;
        }
    }
443
}
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

void PLSelector::inputItemUpdate( input_item_t *arg )
{
    int c = podcastsParent->childCount();
    for( int i = 0; i < c; i++ )
    {
        QTreeWidgetItem *item = podcastsParent->child(i);
        input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
        if( p_input == arg )
        {
            PLSelItem *si = itemWidget( item );
            char *psz_name = input_item_GetName( p_input );
            si->setText( qfu( psz_name ) );
            free( psz_name );
            return;
        }
    }
}

void PLSelector::podcastAdd( PLSelItem* item )
{
    bool ok;
    QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
                                         qtr( "Enter URL of the podcast to subscribe to:" ),
                                         QLineEdit::Normal, QString(), &ok );
    if( !ok || url.isEmpty() ) return;

    setSource( podcastsParent ); //to load the SD in case it's not loaded

    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
        p_intf->p_libvlc, "podcast", FIND_CHILD );
    if( !p_obj ) return;

    QString request("ADD:");
478
    request += url.trimmed();
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    var_SetString( p_obj, "podcast-request", qtu( request ) );
    vlc_object_release( p_obj );
}

void PLSelector::podcastRemove( PLSelItem* item )
{
    QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
    question = question.arg( item->text() );
    QMessageBox::StandardButton res =
        QMessageBox::question( this, qtr( "Unsubscribe" ), question,
                               QMessageBox::Ok | QMessageBox::Cancel,
                               QMessageBox::Cancel );
    if( res == QMessageBox::Cancel ) return;

    input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
    if( !input ) return;

    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
        p_intf->p_libvlc, "podcast", FIND_CHILD );
    if( !p_obj ) return;

    QString request("RM:");
    char *psz_uri = input_item_GetURI( input );
    request += qfu( psz_uri );
    var_SetString( p_obj, "podcast-request", qtu( request ) );
    vlc_object_release( p_obj );
    free( psz_uri );
}

PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
{
    return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
511
512
}

513
514
515
516
517
void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
{
    if( !model()->hasChildren( index ) ) return;
    QStyleOption option;
    option.initFrom( this );
518
    option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
519
520
521
522
    style()->drawPrimitive( isExpanded( index ) ?
                            QStyle::PE_IndicatorArrowDown :
                            QStyle::PE_IndicatorArrowRight, &option, painter );
}
523
524
525
526
527
528

void PLSelector::getCurrentSelectedItem( int* type, QString *string)
{
    *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
    *string = currentItem()->data( 0, NAME_ROLE ).toString();
}