]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
ea0b5d673f546869107a2fa163efa6d2ff8104ec
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
1 /*****************************************************************************
2  * selector.cpp : Playlist source selector
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include "components/playlist/selector.hpp"
32 #include "playlist_model.hpp"
33 #include "qt4.hpp"
34 #include "dialogs_provider.hpp"
35 #include "playlist.hpp"
36 #include "util/customwidgets.hpp"
37
38 #include <QVBoxLayout>
39 #include <QHeaderView>
40 #include <QMimeData>
41 #include <QInputDialog>
42 #include <QMessageBox>
43
44 #include <vlc_playlist.h>
45 #include <vlc_services_discovery.h>
46
47 void SelectorActionButton::paintEvent( QPaintEvent *event )
48 {
49     QPainter p( this );
50     QColor color = palette().color( QPalette::HighlightedText );
51     color.setAlpha( 80 );
52     if( underMouse() )
53         p.fillRect( rect(), color );
54     p.setPen( color );
55     int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
56     p.drawLine( rect().topLeft() + QPoint( 0, frame ),
57                 rect().bottomLeft() - QPoint( 0, frame ) );
58     QVLCFramelessButton::paintEvent( event );
59 }
60
61 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
62     : qitem(i), lblAction( NULL)
63 {
64     layout = new QHBoxLayout();
65     layout->setContentsMargins(0,0,0,0);
66     layout->addSpacing( 3 );
67
68     lbl = new QVLCElidingLabel( text );
69
70     layout->addWidget(lbl, 1);
71
72     setLayout( layout );
73
74     int height = qMax( 22, fontMetrics().height() + 8 );
75     setMinimumHeight( height );
76 }
77
78 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
79 {
80     if( lblAction ) return; //might change later
81
82     QIcon icon;
83
84     switch( act )
85     {
86     case ADD_ACTION:
87         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
88     case RM_ACTION:
89         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
90     }
91
92     lblAction = new SelectorActionButton();
93     lblAction->setIcon( icon );
94     lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
95
96     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
97
98     layout->addWidget( lblAction, 0 );
99     lblAction->hide();
100
101     CONNECT( lblAction, clicked(), this, triggerAction() );
102 }
103
104 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
105
106 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
107
108 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
109
110 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
111
112 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
113
114 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
115            : QTreeWidget( p ), p_intf(_p_intf)
116 {
117     setFrameStyle( QFrame::NoFrame );
118     viewport()->setAutoFillBackground( false );
119     setIconSize( QSize( 24,24 ) );
120     setIndentation( 12 );
121     header()->hide();
122     setRootIsDecorated( true );
123     setAlternatingRowColors( false );
124     podcastsParent = NULL;
125     podcastsParentId = -1;
126
127     viewport()->setAcceptDrops(true);
128     setDropIndicatorShown(true);
129     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
130
131     CONNECT( THEMIM, playlistItemAppended( int, int ),
132              this, plItemAdded( int, int ) );
133     CONNECT( THEMIM, playlistItemRemoved( int ),
134              this, plItemRemoved( int ) );
135     DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
136               this, inputItemUpdate( input_item_t * ) );
137
138     createItems();
139     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
140              this, setSource( QTreeWidgetItem *) );
141     CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
142              this, setSource( QTreeWidgetItem *) );
143
144     /* I believe this is unnecessary, seeing
145        QStyle::SH_ItemView_ActivateItemOnSingleClick
146         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
147              this, setSource( QTreeWidgetItem *) ); */
148
149     /* select the first item */
150 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
151 }
152
153 PLSelector::~PLSelector()
154 {
155     if( podcastsParent )
156     {
157         int c = podcastsParent->childCount();
158         for( int i = 0; i < c; i++ )
159         {
160             QTreeWidgetItem *item = podcastsParent->child(i);
161             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
162             vlc_gc_decref( p_input );
163         }
164     }
165 }
166
167 void PLSelector::setSource( QTreeWidgetItem *item )
168 {
169     if( !item )
170         return;
171
172     bool b_ok;
173     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
174     if( !b_ok || i_type == CATEGORY_TYPE )
175         return;
176
177     bool sd_loaded;
178     if( i_type == SD_TYPE )
179     {
180         QString qs = item->data( 0, NAME_ROLE ).toString();
181         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
182         if( !sd_loaded )
183             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
184     }
185
186     playlist_Lock( THEPL );
187
188     playlist_item_t *pl_item = NULL;
189
190     if( i_type == SD_TYPE )
191     {
192         pl_item = playlist_ChildSearchName( THEPL->p_root, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
193         if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
194         {
195             if( pl_item && !sd_loaded )
196             {
197                 podcastsParentId = pl_item->i_id;
198                 for( int i=0; i < pl_item->i_children; i++ )
199                     addPodcastItem( pl_item->pp_children[i] );
200             }
201             pl_item = NULL; //to prevent activating it
202         }
203     }
204     else
205         pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
206
207     playlist_Unlock( THEPL );
208
209     if( pl_item )
210        emit activated( pl_item );
211 }
212
213 PLSelItem * PLSelector::addItem (
214   SelectorItemType type, const char* str, bool drop,
215   QTreeWidgetItem* parentItem )
216 {
217   QTreeWidgetItem *item = parentItem ?
218       new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
219   PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
220   setItemWidget( item, 0, selItem );
221   item->setData( 0, TYPE_ROLE, (int)type );
222   if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
223
224   return selItem;
225 }
226
227 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
228 {
229     item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
230     item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
231     return item;
232 }
233
234 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
235 {
236     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
237 /*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
238     item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
239     return item;
240 }
241
242 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
243 {
244     vlc_gc_incref( p_item->p_input );
245     char *psz_name = input_item_GetName( p_item->p_input );
246     PLSelItem *item = addItem(
247             PL_ITEM_TYPE,  psz_name, false, podcastsParent );
248     item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
249     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
250     item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
251     item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
252     CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
253     free( psz_name );
254     return item;
255 }
256
257 void PLSelector::createItems()
258 {
259     PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, "Playlist", true ),
260                               THEPL->p_playing );
261     pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
262
263     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, "Media Library", true ),
264                               THEPL->p_media_library );
265     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
266
267     QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, "My Computer",
268                                         false )->treeItem();
269     QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, "Devices",
270                                         false )->treeItem();
271     QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, "Local Network",
272                                     false )->treeItem();
273     QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, "Internet",
274                                           false )->treeItem();;
275
276     char **ppsz_longnames;
277     int *p_categories;
278     char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
279     if( !ppsz_names )
280         return;
281
282     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
283     int *p_category = p_categories;
284     for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
285     {
286         //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
287
288         if( *p_category == SD_CAT_INTERNET )
289         {
290             PLSelItem *selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
291             putSDData( selItem, *ppsz_name, *ppsz_longname );
292             if( !strncmp( *ppsz_name, "podcast", 7 ) )
293             {
294                 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
295                 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
296                 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
297                 podcastsParent = selItem->treeItem();
298             }
299         }
300         else if( *p_category == SD_CAT_DEVICES )
301         {
302             putSDData( addItem( SD_TYPE, *ppsz_longname, false, devices ),
303                        *ppsz_name, *ppsz_longname );
304         }
305         else if( *p_category == SD_CAT_LAN )
306         {
307             putSDData( addItem( SD_TYPE, *ppsz_longname, false, lan ),
308                        *ppsz_name, *ppsz_longname );
309         }
310         else if( *p_category == SD_CAT_MYCOMPUTER )
311         {
312             putSDData( addItem( SD_TYPE, *ppsz_longname, false, mycomp ),
313                        *ppsz_name, *ppsz_longname );
314         }
315         else
316         {
317             putSDData( addItem( SD_TYPE, *ppsz_longname, false ),
318                        *ppsz_name, *ppsz_longname );
319         }
320
321         free( *ppsz_name );
322         free( *ppsz_longname );
323     }
324     free( ppsz_names );
325     free( ppsz_longnames );
326     free( p_categories );
327
328     if( mycomp->childCount() == 0 ) delete mycomp;
329     if( devices->childCount() == 0 ) delete devices;
330     if( lan->childCount() == 0 ) delete lan;
331     if( internet->childCount() == 0 ) delete internet;
332 }
333
334 QStringList PLSelector::mimeTypes() const
335 {
336     QStringList types;
337     types << "vlc/qt-input-items";
338     return types;
339 }
340
341 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
342   const QMimeData * data, Qt::DropAction action )
343 {
344     if( !parent ) return false;
345
346     QVariant type = parent->data( 0, TYPE_ROLE );
347     if( type == QVariant() ) return false;
348     int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
349
350     if( i_truth != IS_PL && i_truth != IS_ML ) return false;
351     bool to_pl = ( i_truth == IS_PL );
352
353     const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
354     if( !plMimeData ) return false;
355
356     QList<input_item_t*> inputItems = plMimeData->inputItems();
357
358     playlist_Lock( THEPL );
359
360     foreach( input_item_t *p_input, inputItems )
361     {
362         playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
363         if( !p_item ) continue;
364
365         playlist_NodeAddCopy( THEPL, p_item,
366                               to_pl ? THEPL->p_playing : THEPL->p_media_library,
367                               PLAYLIST_END );
368     }
369
370     playlist_Unlock( THEPL );
371
372     return true;
373 }
374
375 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
376 {
377     event->setDropAction( Qt::CopyAction );
378     QAbstractItemView::dragMoveEvent( event );
379 }
380
381 void PLSelector::plItemAdded( int item, int parent )
382 {
383     if( parent != podcastsParentId ) return;
384
385     playlist_Lock( THEPL );
386
387     playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
388     if( !p_item ) {
389         playlist_Unlock( THEPL );
390         return;
391     }
392
393     int c = podcastsParent->childCount();
394     for( int i = 0; i < c; i++ )
395     {
396         QTreeWidgetItem *podItem = podcastsParent->child(i);
397         if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
398         {
399           //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
400           playlist_Unlock( THEPL );
401           return;
402         }
403     }
404
405     //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
406     addPodcastItem( p_item );
407
408     playlist_Unlock( THEPL );
409
410     podcastsParent->setExpanded( true );
411 }
412
413 void PLSelector::plItemRemoved( int id )
414 {
415     int c = podcastsParent->childCount();
416     for( int i = 0; i < c; i++ )
417     {
418         QTreeWidgetItem *item = podcastsParent->child(i);
419         if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
420         {
421             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
422             //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
423             vlc_gc_decref( p_input );
424             delete item;
425             return;
426         }
427     }
428 }
429
430 void PLSelector::inputItemUpdate( input_item_t *arg )
431 {
432     int c = podcastsParent->childCount();
433     for( int i = 0; i < c; i++ )
434     {
435         QTreeWidgetItem *item = podcastsParent->child(i);
436         input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
437         if( p_input == arg )
438         {
439             PLSelItem *si = itemWidget( item );
440             char *psz_name = input_item_GetName( p_input );
441             si->setText( qfu( psz_name ) );
442             free( psz_name );
443             return;
444         }
445     }
446 }
447
448 void PLSelector::podcastAdd( PLSelItem* item )
449 {
450     bool ok;
451     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
452                                          qtr( "Enter URL of the podcast to subscribe to:" ),
453                                          QLineEdit::Normal, QString(), &ok );
454     if( !ok || url.isEmpty() ) return;
455
456     setSource( podcastsParent ); //to load the SD in case it's not loaded
457
458     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
459         p_intf->p_libvlc, "podcast", FIND_CHILD );
460     if( !p_obj ) return;
461
462     QString request("ADD:");
463     request += url.trimmed();
464     var_SetString( p_obj, "podcast-request", qtu( request ) );
465     vlc_object_release( p_obj );
466 }
467
468 void PLSelector::podcastRemove( PLSelItem* item )
469 {
470     QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
471     question = question.arg( item->text() );
472     QMessageBox::StandardButton res =
473         QMessageBox::question( this, qtr( "Unsubscribe" ), question,
474                                QMessageBox::Ok | QMessageBox::Cancel,
475                                QMessageBox::Cancel );
476     if( res == QMessageBox::Cancel ) return;
477
478     input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
479     if( !input ) return;
480
481     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
482         p_intf->p_libvlc, "podcast", FIND_CHILD );
483     if( !p_obj ) return;
484
485     QString request("RM:");
486     char *psz_uri = input_item_GetURI( input );
487     request += qfu( psz_uri );
488     var_SetString( p_obj, "podcast-request", qtu( request ) );
489     vlc_object_release( p_obj );
490     free( psz_uri );
491 }
492
493 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
494 {
495     return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
496 }
497
498 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
499 {
500     if( !model()->hasChildren( index ) ) return;
501     QStyleOption option;
502     option.initFrom( this );
503     option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
504     style()->drawPrimitive( isExpanded( index ) ?
505                             QStyle::PE_IndicatorArrowDown :
506                             QStyle::PE_IndicatorArrowRight, &option, painter );
507 }