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