1 /*****************************************************************************
2 * selector.cpp : Playlist source selector
3 ****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
31 #include "components/playlist/selector.hpp"
32 #include "playlist_item.hpp"
34 #include "../../dialogs_provider.hpp"
35 #include "playlist.hpp"
37 #include <QVBoxLayout>
38 #include <QHeaderView>
40 #include <QInputDialog>
41 #include <QMessageBox>
43 #include <vlc_playlist.h>
44 #include <vlc_services_discovery.h>
46 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
47 : qitem(i), btnAction( NULL )
49 layout = new QHBoxLayout();
50 layout->setContentsMargins(0,0,0,0);
52 lbl = new QLabel( text );
54 layout->addWidget(lbl);
59 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
61 if( btnAction ) return;
66 btnAction = new QPushButton("+"); break;
68 btnAction = new QPushButton("-"); break;
70 if( !tooltip.isEmpty() ) btnAction->setToolTip( tooltip );
71 btnAction->setMaximumWidth(23);
73 layout->addWidget( btnAction );
76 CONNECT( btnAction, clicked(), this, triggerAction() );
79 void PLSelItem::showAction() { if( btnAction ) btnAction->show(); }
81 void PLSelItem::hideAction() { if( btnAction ) btnAction->hide(); }
83 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
85 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
87 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
89 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
90 : QTreeWidget( p ), p_intf(_p_intf)
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 ) );
99 //setHeaderLabel( qtr( "Medias" ) );
100 //header()->setMovable( false );
101 setRootIsDecorated( false );
102 setAlternatingRowColors( false );
103 setItemDelegate( new PLSelectorDelegate() );
104 podcastsParent = NULL;
105 podcastsParentId = -1;
107 viewport()->setAcceptDrops(true);
108 setDropIndicatorShown(true);
109 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
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 * ) );
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 *) ); */
126 /* select the first item */
127 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
130 PLSelector::~PLSelector()
134 int c = podcastsParent->childCount();
135 for( int i = 0; i < c; i++ )
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 );
144 void PLSelector::setSource( QTreeWidgetItem *item )
150 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
151 if( !b_ok || i_type == CATEGORY_TYPE )
155 if( i_type == SD_TYPE )
157 QString qs = item->data( 0, NAME_ROLE ).toString();
158 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
160 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
163 playlist_Lock( THEPL );
165 playlist_item_t *pl_item = NULL;
167 if( i_type == SD_TYPE )
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 )
172 if( pl_item && !sd_loaded )
174 podcastsParentId = pl_item->i_id;
175 for( int i=0; i < pl_item->i_children; i++ )
176 addPodcastItem( pl_item->pp_children[i] );
178 pl_item = NULL; //to prevent activating it
182 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
184 playlist_Unlock( THEPL );
187 emit activated( pl_item );
190 PLSelItem * PLSelector::addItem (
191 SelectorItemType type, const QString& str, bool drop,
192 QTreeWidgetItem* parentItem )
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 );
204 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
206 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
207 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
211 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
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 ) ); );*/
219 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
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* ) );
234 void PLSelector::createItems()
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 ) );
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 ) );
244 QTreeWidgetItem *msrc = addItem( CATEGORY_TYPE, qtr( "Media Sources" ),
247 QTreeWidgetItem *mfldrs = NULL;
249 QTreeWidgetItem *shouts = NULL;
251 msrc->setExpanded( true );
253 char **ppsz_longnames;
254 char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
258 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
259 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
261 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
262 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
264 if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
265 SD_IS("frenchtv") || SD_IS("freebox") )
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 );
272 else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
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 );
279 else if( SD_IS("podcast") )
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* ) );
288 podcastsParent = podItem->treeItem();
292 putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false, msrc ),
293 *ppsz_name, *ppsz_longname );
299 free( *ppsz_longname );
302 free( ppsz_longnames );
305 QStringList PLSelector::mimeTypes() const
308 types << "vlc/qt-playlist-item";
312 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
313 const QMimeData * data, Qt::DropAction action )
315 if( !parent ) return false;
317 QVariant type = parent->data( 0, TYPE_ROLE );
318 if( type == QVariant() ) return false;
319 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
321 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
322 bool to_pl = ( i_truth == IS_PL );
324 if( data->hasFormat( "vlc/qt-playlist-item" ) )
326 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
327 QDataStream stream( &encodedData, QIODevice::ReadOnly );
328 playlist_Lock( THEPL );
329 while( !stream.atEnd() )
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,
341 playlist_Unlock( THEPL );
346 void PLSelector::plItemAdded( int item, int parent )
348 if( parent != podcastsParentId ) return;
350 playlist_Lock( THEPL );
352 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
354 playlist_Unlock( THEPL );
358 int c = podcastsParent->childCount();
359 for( int i = 0; i < c; i++ )
361 QTreeWidgetItem *podItem = podcastsParent->child(i);
362 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
364 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
365 playlist_Unlock( THEPL );
370 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
371 addPodcastItem( p_item );
373 playlist_Unlock( THEPL );
375 podcastsParent->setExpanded( true );
378 void PLSelector::plItemRemoved( int id )
380 int c = podcastsParent->childCount();
381 for( int i = 0; i < c; i++ )
383 QTreeWidgetItem *item = podcastsParent->child(i);
384 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
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 );
395 void PLSelector::inputItemUpdate( input_item_t *arg )
397 int c = podcastsParent->childCount();
398 for( int i = 0; i < c; i++ )
400 QTreeWidgetItem *item = podcastsParent->child(i);
401 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
404 PLSelItem *si = itemWidget( item );
405 char *psz_name = input_item_GetName( p_input );
406 si->setText( qfu( psz_name ) );
413 void PLSelector::podcastAdd( PLSelItem* item )
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;
421 setSource( podcastsParent ); //to load the SD in case it's not loaded
423 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
424 p_intf->p_libvlc, "podcast", FIND_CHILD );
427 QString request("ADD:");
429 var_SetString( p_obj, "podcast-request", qtu( request ) );
430 vlc_object_release( p_obj );
433 void PLSelector::podcastRemove( PLSelItem* item )
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;
444 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
447 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
448 p_intf->p_libvlc, "podcast", FIND_CHILD );
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 );
459 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
461 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );