]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
43143f44ecb7ba99600d3a4ff84b5eb9bc5a9b61
[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_item.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 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
48     : qitem(i), lblAction( NULL)
49 {
50     layout = new QHBoxLayout();
51     layout->setContentsMargins(0,0,0,0);
52     layout->addSpacing( 3 );
53
54     lbl = new QLabel( text );
55
56     layout->addWidget(lbl, 1);
57
58     setLayout( layout );
59
60     setMinimumHeight( 22 ); //Action icon height plus 6
61 }
62
63 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
64 {
65     if( lblAction ) return; //might change later
66
67     QIcon icon;
68
69     switch( act )
70     {
71     case ADD_ACTION:
72         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
73     case RM_ACTION:
74         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
75     }
76
77     lblAction = new QVLCFramelessButton();
78     lblAction->setIcon( icon );
79
80     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
81
82     layout->addWidget( lblAction, 0 );
83     lblAction->hide();
84     layout->addSpacing( 3 );
85
86     CONNECT( lblAction, clicked(), this, triggerAction() );
87 }
88
89 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
90
91 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
92
93 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
94
95 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
96
97 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
98
99 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
100            : QTreeWidget( p ), p_intf(_p_intf)
101 {
102     setFrameStyle( QFrame::NoFrame );
103     viewport()->setAutoFillBackground( false );
104     setIconSize( QSize( 24,24 ) );
105     setIndentation( 14 );
106     header()->hide();
107     setRootIsDecorated( true );
108     setAlternatingRowColors( false );
109     podcastsParent = NULL;
110     podcastsParentId = -1;
111
112     viewport()->setAcceptDrops(true);
113     setDropIndicatorShown(true);
114     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
115
116     CONNECT( THEMIM, playlistItemAppended( int, int ),
117              this, plItemAdded( int, int ) );
118     CONNECT( THEMIM, playlistItemRemoved( int ),
119              this, plItemRemoved( int ) );
120     CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
121             this, inputItemUpdate( input_item_t * ) );
122
123     createItems();
124     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
125              this, setSource( QTreeWidgetItem *) );
126     CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
127              this, setSource( QTreeWidgetItem *) );
128
129     /* I believe this is unnecessary, seeing
130        QStyle::SH_ItemView_ActivateItemOnSingleClick
131         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
132              this, setSource( QTreeWidgetItem *) ); */
133
134     /* select the first item */
135 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
136 }
137
138 PLSelector::~PLSelector()
139 {
140     if( podcastsParent )
141     {
142         int c = podcastsParent->childCount();
143         for( int i = 0; i < c; i++ )
144         {
145             QTreeWidgetItem *item = podcastsParent->child(i);
146             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
147             vlc_gc_decref( p_input );
148         }
149     }
150 }
151
152 void PLSelector::setSource( QTreeWidgetItem *item )
153 {
154     if( !item )
155         return;
156
157     bool b_ok;
158     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
159     if( !b_ok || i_type == CATEGORY_TYPE )
160         return;
161
162     bool sd_loaded;
163     if( i_type == SD_TYPE )
164     {
165         QString qs = item->data( 0, NAME_ROLE ).toString();
166         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
167         if( !sd_loaded )
168             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
169     }
170
171     playlist_Lock( THEPL );
172
173     playlist_item_t *pl_item = NULL;
174
175     if( i_type == SD_TYPE )
176     {
177         pl_item = playlist_ChildSearchName( THEPL->p_root, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
178         if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
179         {
180             if( pl_item && !sd_loaded )
181             {
182                 podcastsParentId = pl_item->i_id;
183                 for( int i=0; i < pl_item->i_children; i++ )
184                     addPodcastItem( pl_item->pp_children[i] );
185             }
186             pl_item = NULL; //to prevent activating it
187         }
188     }
189     else
190         pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
191
192     playlist_Unlock( THEPL );
193
194     if( pl_item )
195        emit activated( pl_item );
196 }
197
198 PLSelItem * PLSelector::addItem (
199   SelectorItemType type, const QString& str, bool drop,
200   QTreeWidgetItem* parentItem )
201 {
202   QTreeWidgetItem *item = parentItem ?
203       new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
204   PLSelItem *selItem = new PLSelItem( item, str );
205   setItemWidget( item, 0, selItem );
206   item->setData( 0, TYPE_ROLE, (int)type );
207   if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
208
209   return selItem;
210 }
211
212 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
213 {
214     item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
215     item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
216     return item;
217 }
218
219 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
220 {
221     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
222 /*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
223     item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
224     return item;
225 }
226
227 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
228 {
229   vlc_gc_incref( p_item->p_input );
230   char *psz_name = input_item_GetName( p_item->p_input );
231   PLSelItem *item = addItem(
232       PL_ITEM_TYPE, qfu( psz_name ), false, podcastsParent );
233   item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
234   item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
235   item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
236   item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
237   CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
238   free( psz_name );
239   return item;
240 }
241
242 void PLSelector::createItems()
243 {
244     PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, qtr( "Playlist" ), true ),
245                               THEPL->p_playing );
246     pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
247
248     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, qtr( "Media Library" ), true ),
249                               THEPL->p_media_library );
250     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
251
252     QTreeWidgetItem *mfldrs = NULL;
253
254     QTreeWidgetItem *shouts = NULL;
255
256     char **ppsz_longnames;
257     char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames );
258     if( !ppsz_names )
259         return;
260
261     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
262     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
263     {
264         //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
265 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
266
267         if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
268             SD_IS("frenchtv") || SD_IS("freebox") )
269         {
270             if( !shouts ) shouts = addItem( CATEGORY_TYPE, qtr( "Shoutcast" ),
271                                             false )->treeItem();
272             putSDData( addItem( SD_TYPE, *ppsz_longname, false, shouts ),
273                        *ppsz_name, *ppsz_longname );
274         }
275         else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
276         {
277             if( !mfldrs ) mfldrs = addItem( CATEGORY_TYPE, qtr( "Media Folders" ),
278                                             false )->treeItem();
279             putSDData( addItem( SD_TYPE, *ppsz_longname, false, mfldrs ),
280                        *ppsz_name, *ppsz_longname );
281         }
282         else if( !strncmp( *ppsz_name, "podcast", 7 ) )
283         {
284
285             PLSelItem *podItem = addItem( SD_TYPE, qtr( "Podcasts" ), false );
286             putSDData( podItem, *ppsz_name, *ppsz_longname );
287             podItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
288             podItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
289             CONNECT( podItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
290
291             podcastsParent = podItem->treeItem();
292         }
293         else
294         {
295             putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false ),
296                        *ppsz_name, *ppsz_longname );
297         }
298
299 #undef SD_IS
300
301         free( *ppsz_name );
302         free( *ppsz_longname );
303     }
304     free( ppsz_names );
305     free( ppsz_longnames );
306 }
307
308 QStringList PLSelector::mimeTypes() const
309 {
310     QStringList types;
311     types << "vlc/qt-playlist-item";
312     return types;
313 }
314
315 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
316   const QMimeData * data, Qt::DropAction action )
317 {
318     if( !parent ) return false;
319
320     QVariant type = parent->data( 0, TYPE_ROLE );
321     if( type == QVariant() ) return false;
322     int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
323
324     if( i_truth != IS_PL && i_truth != IS_ML ) return false;
325     bool to_pl = ( i_truth == IS_PL );
326
327     if( data->hasFormat( "vlc/qt-playlist-item" ) )
328     {
329         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
330         QDataStream stream( &encodedData, QIODevice::ReadOnly );
331         playlist_Lock( THEPL );
332         while( !stream.atEnd() )
333         {
334             PLItem *item;
335             stream.readRawData( (char*)&item, sizeof(PLItem*) );
336             input_item_t *pl_input =item->inputItem();
337             playlist_AddExt ( THEPL,
338                 pl_input->psz_uri, pl_input->psz_name,
339                 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
340                 pl_input->i_duration,
341                 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
342                 to_pl, true );
343         }
344         playlist_Unlock( THEPL );
345     }
346     return true;
347 }
348
349 void PLSelector::plItemAdded( int item, int parent )
350 {
351     if( parent != podcastsParentId ) return;
352
353     playlist_Lock( THEPL );
354
355     playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
356     if( !p_item ) {
357         playlist_Unlock( THEPL );
358         return;
359     }
360
361     int c = podcastsParent->childCount();
362     for( int i = 0; i < c; i++ )
363     {
364         QTreeWidgetItem *podItem = podcastsParent->child(i);
365         if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
366         {
367           //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
368           playlist_Unlock( THEPL );
369           return;
370         }
371     }
372
373     //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
374     addPodcastItem( p_item );
375
376     playlist_Unlock( THEPL );
377
378     podcastsParent->setExpanded( true );
379 }
380
381 void PLSelector::plItemRemoved( int id )
382 {
383     int c = podcastsParent->childCount();
384     for( int i = 0; i < c; i++ )
385     {
386         QTreeWidgetItem *item = podcastsParent->child(i);
387         if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
388         {
389             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
390             //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
391             vlc_gc_decref( p_input );
392             delete item;
393             return;
394         }
395     }
396 }
397
398 void PLSelector::inputItemUpdate( input_item_t *arg )
399 {
400     int c = podcastsParent->childCount();
401     for( int i = 0; i < c; i++ )
402     {
403         QTreeWidgetItem *item = podcastsParent->child(i);
404         input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
405         if( p_input == arg )
406         {
407             PLSelItem *si = itemWidget( item );
408             char *psz_name = input_item_GetName( p_input );
409             si->setText( qfu( psz_name ) );
410             free( psz_name );
411             return;
412         }
413     }
414 }
415
416 void PLSelector::podcastAdd( PLSelItem* item )
417 {
418     bool ok;
419     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
420                                          qtr( "Enter URL of the podcast to subscribe to:" ),
421                                          QLineEdit::Normal, QString(), &ok );
422     if( !ok || url.isEmpty() ) return;
423
424     setSource( podcastsParent ); //to load the SD in case it's not loaded
425
426     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
427         p_intf->p_libvlc, "podcast", FIND_CHILD );
428     if( !p_obj ) return;
429
430     QString request("ADD:");
431     request += url;
432     var_SetString( p_obj, "podcast-request", qtu( request ) );
433     vlc_object_release( p_obj );
434 }
435
436 void PLSelector::podcastRemove( PLSelItem* item )
437 {
438     //FIXME will translators know to leave that %1 somewhere inside?
439     QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
440     question = question.arg( item->text() );
441     QMessageBox::StandardButton res =
442         QMessageBox::question( this, qtr( "Unsubscribe" ), question,
443                                QMessageBox::Ok | QMessageBox::Cancel,
444                                QMessageBox::Cancel );
445     if( res == QMessageBox::Cancel ) return;
446
447     input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
448     if( !input ) return;
449
450     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
451         p_intf->p_libvlc, "podcast", FIND_CHILD );
452     if( !p_obj ) return;
453
454     QString request("RM:");
455     char *psz_uri = input_item_GetURI( input );
456     request += qfu( psz_uri );
457     var_SetString( p_obj, "podcast-request", qtu( request ) );
458     vlc_object_release( p_obj );
459     free( psz_uri );
460 }
461
462 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
463 {
464     return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
465 }
466
467 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
468 {
469     if( !model()->hasChildren( index ) ) return;
470     QStyleOption option;
471     option.initFrom( this );
472     option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
473
474     /*option.state = QStyle::State_Children;
475     if( isExpanded( index ) ) option.state |=  QStyle::State_Open;*/
476     style()->drawPrimitive( isExpanded( index ) ?
477                             QStyle::PE_IndicatorArrowDown :
478                             QStyle::PE_IndicatorArrowRight, &option, painter );
479 }