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