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 void SelectorActionButton::paintEvent( QPaintEvent *event )
50 QColor color = palette().color( QPalette::HighlightedText );
53 p.fillRect( rect(), color );
55 int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
56 p.drawLine( rect().topLeft() + QPoint( 0, frame ),
57 rect().bottomLeft() - QPoint( 0, frame ) );
58 QVLCFramelessButton::paintEvent( event );
61 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
62 : qitem(i), lblAction( NULL)
64 layout = new QHBoxLayout();
65 layout->setContentsMargins(0,0,0,0);
66 layout->addSpacing( 3 );
68 lbl = new QLabel( text );
70 layout->addWidget(lbl, 1);
74 setMinimumHeight( 22 ); //Action icon height plus 6
77 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
79 if( lblAction ) return; //might change later
86 icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
88 icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
91 lblAction = new SelectorActionButton();
92 lblAction->setIcon( icon );
93 lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
95 if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
97 layout->addWidget( lblAction, 0 );
100 CONNECT( lblAction, clicked(), this, triggerAction() );
103 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
105 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
107 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
109 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
111 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
113 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
114 : QTreeWidget( p ), p_intf(_p_intf)
116 setFrameStyle( QFrame::NoFrame );
117 viewport()->setAutoFillBackground( false );
118 setIconSize( QSize( 24,24 ) );
119 setIndentation( 12 );
121 setRootIsDecorated( true );
122 setAlternatingRowColors( false );
123 podcastsParent = NULL;
124 podcastsParentId = -1;
126 viewport()->setAcceptDrops(true);
127 setDropIndicatorShown(true);
128 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
130 CONNECT( THEMIM, playlistItemAppended( int, int ),
131 this, plItemAdded( int, int ) );
132 CONNECT( THEMIM, playlistItemRemoved( int ),
133 this, plItemRemoved( int ) );
134 CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
135 this, inputItemUpdate( input_item_t * ) );
138 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
139 this, setSource( QTreeWidgetItem *) );
140 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
141 this, setSource( QTreeWidgetItem *) );
143 /* I believe this is unnecessary, seeing
144 QStyle::SH_ItemView_ActivateItemOnSingleClick
145 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
146 this, setSource( QTreeWidgetItem *) ); */
148 /* select the first item */
149 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
152 PLSelector::~PLSelector()
156 int c = podcastsParent->childCount();
157 for( int i = 0; i < c; i++ )
159 QTreeWidgetItem *item = podcastsParent->child(i);
160 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
161 vlc_gc_decref( p_input );
166 void PLSelector::setSource( QTreeWidgetItem *item )
172 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
173 if( !b_ok || i_type == CATEGORY_TYPE )
177 if( i_type == SD_TYPE )
179 QString qs = item->data( 0, NAME_ROLE ).toString();
180 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
182 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
185 playlist_Lock( THEPL );
187 playlist_item_t *pl_item = NULL;
189 if( i_type == SD_TYPE )
191 pl_item = playlist_ChildSearchName( THEPL->p_root, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
192 if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
194 if( pl_item && !sd_loaded )
196 podcastsParentId = pl_item->i_id;
197 for( int i=0; i < pl_item->i_children; i++ )
198 addPodcastItem( pl_item->pp_children[i] );
200 pl_item = NULL; //to prevent activating it
204 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
206 playlist_Unlock( THEPL );
209 emit activated( pl_item );
212 PLSelItem * PLSelector::addItem (
213 SelectorItemType type, const QString& str, bool drop,
214 QTreeWidgetItem* parentItem )
216 QTreeWidgetItem *item = parentItem ?
217 new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
218 PLSelItem *selItem = new PLSelItem( item, str );
219 setItemWidget( item, 0, selItem );
220 item->setData( 0, TYPE_ROLE, (int)type );
221 if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
226 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
228 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
229 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
233 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
235 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
236 /* item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
237 item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
241 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
243 vlc_gc_incref( p_item->p_input );
244 char *psz_name = input_item_GetName( p_item->p_input );
245 PLSelItem *item = addItem(
246 PL_ITEM_TYPE, qfu( psz_name ), false, podcastsParent );
247 item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
248 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
249 item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
250 item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
251 CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
256 void PLSelector::createItems()
258 PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, qtr( "Playlist" ), true ),
260 pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
262 PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, qtr( "Media Library" ), true ),
263 THEPL->p_media_library );
264 ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
266 QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, qtr( "My Computer" ),
268 QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, qtr( "Devices" ),
270 QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, qtr( "Local Network" ),
272 QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, qtr( "Internet" ),
273 false )->treeItem();;
275 char **ppsz_longnames;
277 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
281 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
282 int *p_category = p_categories;
283 for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
285 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
287 if( *p_category == SD_CAT_INTERNET )
289 PLSelItem *selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
290 putSDData( selItem, *ppsz_name, *ppsz_longname );
291 if( !strncmp( *ppsz_name, "podcast", 7 ) )
293 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
294 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
295 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
296 podcastsParent = selItem->treeItem();
299 else if( *p_category == SD_CAT_DEVICES )
301 putSDData( addItem( SD_TYPE, *ppsz_longname, false, devices ),
302 *ppsz_name, *ppsz_longname );
304 else if( *p_category == SD_CAT_LAN )
306 putSDData( addItem( SD_TYPE, *ppsz_longname, false, lan ),
307 *ppsz_name, *ppsz_longname );
309 else if( *p_category == SD_CAT_MYCOMPUTER )
311 putSDData( addItem( SD_TYPE, *ppsz_longname, false, mycomp ),
312 *ppsz_name, *ppsz_longname );
316 putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false ),
317 *ppsz_name, *ppsz_longname );
321 free( *ppsz_longname );
324 free( ppsz_longnames );
325 free( p_categories );
327 if( mycomp->childCount() == 0 ) delete mycomp;
328 if( devices->childCount() == 0 ) delete devices;
329 if( lan->childCount() == 0 ) delete lan;
330 if( internet->childCount() == 0 ) delete internet;
333 QStringList PLSelector::mimeTypes() const
336 types << "vlc/qt-playlist-item";
340 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
341 const QMimeData * data, Qt::DropAction action )
343 if( !parent ) return false;
345 QVariant type = parent->data( 0, TYPE_ROLE );
346 if( type == QVariant() ) return false;
347 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
349 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
350 bool to_pl = ( i_truth == IS_PL );
352 if( data->hasFormat( "vlc/qt-playlist-item" ) )
354 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
355 QDataStream stream( &encodedData, QIODevice::ReadOnly );
356 playlist_Lock( THEPL );
357 while( !stream.atEnd() )
360 stream.readRawData( (char*)&item, sizeof(PLItem*) );
361 input_item_t *pl_input =item->inputItem();
362 playlist_AddExt ( THEPL,
363 pl_input->psz_uri, pl_input->psz_name,
364 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
365 pl_input->i_duration,
366 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
369 playlist_Unlock( THEPL );
374 void PLSelector::plItemAdded( int item, int parent )
376 if( parent != podcastsParentId ) return;
378 playlist_Lock( THEPL );
380 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
382 playlist_Unlock( THEPL );
386 int c = podcastsParent->childCount();
387 for( int i = 0; i < c; i++ )
389 QTreeWidgetItem *podItem = podcastsParent->child(i);
390 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
392 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
393 playlist_Unlock( THEPL );
398 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
399 addPodcastItem( p_item );
401 playlist_Unlock( THEPL );
403 podcastsParent->setExpanded( true );
406 void PLSelector::plItemRemoved( int id )
408 int c = podcastsParent->childCount();
409 for( int i = 0; i < c; i++ )
411 QTreeWidgetItem *item = podcastsParent->child(i);
412 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
414 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
415 //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
416 vlc_gc_decref( p_input );
423 void PLSelector::inputItemUpdate( input_item_t *arg )
425 int c = podcastsParent->childCount();
426 for( int i = 0; i < c; i++ )
428 QTreeWidgetItem *item = podcastsParent->child(i);
429 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
432 PLSelItem *si = itemWidget( item );
433 char *psz_name = input_item_GetName( p_input );
434 si->setText( qfu( psz_name ) );
441 void PLSelector::podcastAdd( PLSelItem* item )
444 QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
445 qtr( "Enter URL of the podcast to subscribe to:" ),
446 QLineEdit::Normal, QString(), &ok );
447 if( !ok || url.isEmpty() ) return;
449 setSource( podcastsParent ); //to load the SD in case it's not loaded
451 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
452 p_intf->p_libvlc, "podcast", FIND_CHILD );
455 QString request("ADD:");
457 var_SetString( p_obj, "podcast-request", qtu( request ) );
458 vlc_object_release( p_obj );
461 void PLSelector::podcastRemove( PLSelItem* item )
463 //FIXME will translators know to leave that %1 somewhere inside?
464 QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
465 question = question.arg( item->text() );
466 QMessageBox::StandardButton res =
467 QMessageBox::question( this, qtr( "Unsubscribe" ), question,
468 QMessageBox::Ok | QMessageBox::Cancel,
469 QMessageBox::Cancel );
470 if( res == QMessageBox::Cancel ) return;
472 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
475 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
476 p_intf->p_libvlc, "podcast", FIND_CHILD );
479 QString request("RM:");
480 char *psz_uri = input_item_GetURI( input );
481 request += qfu( psz_uri );
482 var_SetString( p_obj, "podcast-request", qtu( request ) );
483 vlc_object_release( p_obj );
487 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
489 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
492 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
494 if( !model()->hasChildren( index ) ) return;
496 option.initFrom( this );
497 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
499 /*option.state = QStyle::State_Children;
500 if( isExpanded( index ) ) option.state |= QStyle::State_Open;*/
501 style()->drawPrimitive( isExpanded( index ) ?
502 QStyle::PE_IndicatorArrowDown :
503 QStyle::PE_IndicatorArrowRight, &option, painter );