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"
36 #include "util/customwidgets.hpp"
38 #include <QVBoxLayout>
39 #include <QHeaderView>
41 #include <QInputDialog>
42 #include <QMessageBox>
44 #include <vlc_playlist.h>
45 #include <vlc_services_discovery.h>
47 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
48 : qitem(i), lblAction( NULL)
50 layout = new QHBoxLayout();
51 layout->setContentsMargins(0,0,0,0);
52 layout->addSpacing( 3 );
54 lbl = new QLabel( text );
56 layout->addWidget(lbl, 1);
60 setMinimumHeight( 22 ); //Action icon height plus 6
63 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
65 if( lblAction ) return; //might change later
72 icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
74 icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
77 lblAction = new QVLCFramelessButton();
78 lblAction->setIcon( icon );
80 if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
82 layout->addWidget( lblAction, 0 );
84 layout->addSpacing( 3 );
86 CONNECT( lblAction, clicked(), this, triggerAction() );
89 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
91 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
93 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
95 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
97 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
99 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
100 : QTreeWidget( p ), p_intf(_p_intf)
102 setFrameStyle( QFrame::NoFrame );
103 viewport()->setAutoFillBackground( false );
104 setIconSize( QSize( 24,24 ) );
105 setIndentation( 14 );
107 setRootIsDecorated( true );
108 setAlternatingRowColors( false );
109 podcastsParent = NULL;
110 podcastsParentId = -1;
112 viewport()->setAcceptDrops(true);
113 setDropIndicatorShown(true);
114 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
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 * ) );
124 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
125 this, setSource( QTreeWidgetItem *) );
126 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
127 this, setSource( QTreeWidgetItem *) );
129 /* I believe this is unnecessary, seeing
130 QStyle::SH_ItemView_ActivateItemOnSingleClick
131 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
132 this, setSource( QTreeWidgetItem *) ); */
134 /* select the first item */
135 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
138 PLSelector::~PLSelector()
142 int c = podcastsParent->childCount();
143 for( int i = 0; i < c; i++ )
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 );
152 void PLSelector::setSource( QTreeWidgetItem *item )
158 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
159 if( !b_ok || i_type == CATEGORY_TYPE )
163 if( i_type == SD_TYPE )
165 QString qs = item->data( 0, NAME_ROLE ).toString();
166 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
168 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
171 playlist_Lock( THEPL );
173 playlist_item_t *pl_item = NULL;
175 if( i_type == SD_TYPE )
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 )
180 if( pl_item && !sd_loaded )
182 podcastsParentId = pl_item->i_id;
183 for( int i=0; i < pl_item->i_children; i++ )
184 addPodcastItem( pl_item->pp_children[i] );
186 pl_item = NULL; //to prevent activating it
190 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
192 playlist_Unlock( THEPL );
195 emit activated( pl_item );
198 PLSelItem * PLSelector::addItem (
199 SelectorItemType type, const QString& str, bool drop,
200 QTreeWidgetItem* parentItem )
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 );
212 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
214 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
215 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
219 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
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 ) ); );*/
227 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
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* ) );
242 void PLSelector::createItems()
244 PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, qtr( "Playlist" ), true ),
246 pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
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 ) );
252 QTreeWidgetItem *mfldrs = NULL;
254 QTreeWidgetItem *shouts = NULL;
256 char **ppsz_longnames;
257 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames );
261 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
262 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
264 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
265 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
267 if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
268 SD_IS("frenchtv") || SD_IS("freebox") )
270 if( !shouts ) shouts = addItem( CATEGORY_TYPE, qtr( "Shoutcast" ),
272 putSDData( addItem( SD_TYPE, *ppsz_longname, false, shouts ),
273 *ppsz_name, *ppsz_longname );
275 else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
277 if( !mfldrs ) mfldrs = addItem( CATEGORY_TYPE, qtr( "Media Folders" ),
279 putSDData( addItem( SD_TYPE, *ppsz_longname, false, mfldrs ),
280 *ppsz_name, *ppsz_longname );
282 else if( !strncmp( *ppsz_name, "podcast", 7 ) )
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* ) );
291 podcastsParent = podItem->treeItem();
295 putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false ),
296 *ppsz_name, *ppsz_longname );
302 free( *ppsz_longname );
305 free( ppsz_longnames );
308 QStringList PLSelector::mimeTypes() const
311 types << "vlc/qt-playlist-item";
315 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
316 const QMimeData * data, Qt::DropAction action )
318 if( !parent ) return false;
320 QVariant type = parent->data( 0, TYPE_ROLE );
321 if( type == QVariant() ) return false;
322 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
324 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
325 bool to_pl = ( i_truth == IS_PL );
327 if( data->hasFormat( "vlc/qt-playlist-item" ) )
329 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
330 QDataStream stream( &encodedData, QIODevice::ReadOnly );
331 playlist_Lock( THEPL );
332 while( !stream.atEnd() )
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,
344 playlist_Unlock( THEPL );
349 void PLSelector::plItemAdded( int item, int parent )
351 if( parent != podcastsParentId ) return;
353 playlist_Lock( THEPL );
355 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
357 playlist_Unlock( THEPL );
361 int c = podcastsParent->childCount();
362 for( int i = 0; i < c; i++ )
364 QTreeWidgetItem *podItem = podcastsParent->child(i);
365 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
367 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
368 playlist_Unlock( THEPL );
373 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
374 addPodcastItem( p_item );
376 playlist_Unlock( THEPL );
378 podcastsParent->setExpanded( true );
381 void PLSelector::plItemRemoved( int id )
383 int c = podcastsParent->childCount();
384 for( int i = 0; i < c; i++ )
386 QTreeWidgetItem *item = podcastsParent->child(i);
387 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
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 );
398 void PLSelector::inputItemUpdate( input_item_t *arg )
400 int c = podcastsParent->childCount();
401 for( int i = 0; i < c; i++ )
403 QTreeWidgetItem *item = podcastsParent->child(i);
404 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
407 PLSelItem *si = itemWidget( item );
408 char *psz_name = input_item_GetName( p_input );
409 si->setText( qfu( psz_name ) );
416 void PLSelector::podcastAdd( PLSelItem* item )
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;
424 setSource( podcastsParent ); //to load the SD in case it's not loaded
426 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
427 p_intf->p_libvlc, "podcast", FIND_CHILD );
430 QString request("ADD:");
432 var_SetString( p_obj, "podcast-request", qtu( request ) );
433 vlc_object_release( p_obj );
436 void PLSelector::podcastRemove( PLSelItem* item )
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;
447 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
450 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
451 p_intf->p_libvlc, "podcast", FIND_CHILD );
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 );
462 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
464 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
467 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
469 if( !model()->hasChildren( index ) ) return;
471 option.initFrom( this );
472 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
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 );