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_model.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 QVLCElidingLabel( text );
70 layout->addWidget(lbl, 1);
74 int height = qMax( 22, fontMetrics().height() + 8 );
75 setMinimumHeight( height );
78 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
80 if( lblAction ) return; //might change later
87 icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
89 icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
92 lblAction = new SelectorActionButton();
93 lblAction->setIcon( icon );
94 lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
96 if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
98 layout->addWidget( lblAction, 0 );
101 CONNECT( lblAction, clicked(), this, triggerAction() );
104 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
106 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
108 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
110 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
112 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
114 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
115 : QTreeWidget( p ), p_intf(_p_intf)
117 setFrameStyle( QFrame::NoFrame );
118 viewport()->setAutoFillBackground( false );
119 setIconSize( QSize( 24,24 ) );
120 setIndentation( 12 );
122 setRootIsDecorated( true );
123 setAlternatingRowColors( false );
124 podcastsParent = NULL;
125 podcastsParentId = -1;
127 viewport()->setAcceptDrops(true);
128 setDropIndicatorShown(true);
129 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
131 CONNECT( THEMIM, playlistItemAppended( int, int ),
132 this, plItemAdded( int, int ) );
133 CONNECT( THEMIM, playlistItemRemoved( int ),
134 this, plItemRemoved( int ) );
135 DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
136 this, inputItemUpdate( input_item_t * ) );
139 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
140 this, setSource( QTreeWidgetItem *) );
141 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
142 this, setSource( QTreeWidgetItem *) );
144 /* I believe this is unnecessary, seeing
145 QStyle::SH_ItemView_ActivateItemOnSingleClick
146 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
147 this, setSource( QTreeWidgetItem *) ); */
149 /* select the first item */
150 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
153 PLSelector::~PLSelector()
157 int c = podcastsParent->childCount();
158 for( int i = 0; i < c; i++ )
160 QTreeWidgetItem *item = podcastsParent->child(i);
161 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
162 vlc_gc_decref( p_input );
167 void PLSelector::setSource( QTreeWidgetItem *item )
173 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
174 if( !b_ok || i_type == CATEGORY_TYPE )
178 if( i_type == SD_TYPE )
180 QString qs = item->data( 0, NAME_ROLE ).toString();
181 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
183 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
186 playlist_Lock( THEPL );
188 playlist_item_t *pl_item = NULL;
190 if( i_type == SD_TYPE )
192 pl_item = playlist_ChildSearchName( THEPL->p_root, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
193 if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
195 if( pl_item && !sd_loaded )
197 podcastsParentId = pl_item->i_id;
198 for( int i=0; i < pl_item->i_children; i++ )
199 addPodcastItem( pl_item->pp_children[i] );
201 pl_item = NULL; //to prevent activating it
205 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
207 playlist_Unlock( THEPL );
210 emit activated( pl_item );
213 PLSelItem * PLSelector::addItem (
214 SelectorItemType type, const QString& str, bool drop,
215 QTreeWidgetItem* parentItem )
217 QTreeWidgetItem *item = parentItem ?
218 new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
219 PLSelItem *selItem = new PLSelItem( item, str );
220 setItemWidget( item, 0, selItem );
221 item->setData( 0, TYPE_ROLE, (int)type );
222 if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
227 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
229 item->treeItem()->setData( 0, NAME_ROLE, qtr( name ) );
230 item->treeItem()->setData( 0, LONGNAME_ROLE, qtr( longname ) );
234 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
236 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
237 /* item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
238 item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
242 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
244 vlc_gc_incref( p_item->p_input );
245 char *psz_name = input_item_GetName( p_item->p_input );
246 PLSelItem *item = addItem(
247 PL_ITEM_TYPE, qfu( psz_name ), false, podcastsParent );
248 item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
249 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
250 item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
251 item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
252 CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
257 void PLSelector::createItems()
259 PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, qtr( "Playlist" ), true ),
261 pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
263 PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, qtr( "Media Library" ), true ),
264 THEPL->p_media_library );
265 ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
267 QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, qtr( "My Computer" ),
269 QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, qtr( "Devices" ),
271 QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, qtr( "Local Network" ),
273 QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, qtr( "Internet" ),
274 false )->treeItem();;
276 char **ppsz_longnames;
278 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
282 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
283 int *p_category = p_categories;
284 for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
286 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
288 if( *p_category == SD_CAT_INTERNET )
290 PLSelItem *selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
291 putSDData( selItem, *ppsz_name, *ppsz_longname );
292 if( !strncmp( *ppsz_name, "podcast", 7 ) )
294 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
295 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
296 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
297 podcastsParent = selItem->treeItem();
300 else if( *p_category == SD_CAT_DEVICES )
302 putSDData( addItem( SD_TYPE, *ppsz_longname, false, devices ),
303 *ppsz_name, *ppsz_longname );
305 else if( *p_category == SD_CAT_LAN )
307 putSDData( addItem( SD_TYPE, *ppsz_longname, false, lan ),
308 *ppsz_name, *ppsz_longname );
310 else if( *p_category == SD_CAT_MYCOMPUTER )
312 putSDData( addItem( SD_TYPE, *ppsz_longname, false, mycomp ),
313 *ppsz_name, *ppsz_longname );
317 putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false ),
318 *ppsz_name, *ppsz_longname );
322 free( *ppsz_longname );
325 free( ppsz_longnames );
326 free( p_categories );
328 if( mycomp->childCount() == 0 ) delete mycomp;
329 if( devices->childCount() == 0 ) delete devices;
330 if( lan->childCount() == 0 ) delete lan;
331 if( internet->childCount() == 0 ) delete internet;
334 QStringList PLSelector::mimeTypes() const
337 types << "vlc/qt-input-items";
341 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
342 const QMimeData * data, Qt::DropAction action )
344 if( !parent ) return false;
346 QVariant type = parent->data( 0, TYPE_ROLE );
347 if( type == QVariant() ) return false;
348 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
350 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
351 bool to_pl = ( i_truth == IS_PL );
353 const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
354 if( !plMimeData ) return false;
356 QList<input_item_t*> inputItems = plMimeData->inputItems();
358 playlist_Lock( THEPL );
360 foreach( input_item_t *p_input, inputItems )
362 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
363 if( !p_item ) continue;
365 playlist_NodeAddCopy( THEPL, p_item,
366 to_pl ? THEPL->p_playing : THEPL->p_media_library,
370 playlist_Unlock( THEPL );
375 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
377 event->setDropAction( Qt::CopyAction );
378 QAbstractItemView::dragMoveEvent( event );
381 void PLSelector::plItemAdded( int item, int parent )
383 if( parent != podcastsParentId ) return;
385 playlist_Lock( THEPL );
387 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
389 playlist_Unlock( THEPL );
393 int c = podcastsParent->childCount();
394 for( int i = 0; i < c; i++ )
396 QTreeWidgetItem *podItem = podcastsParent->child(i);
397 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
399 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
400 playlist_Unlock( THEPL );
405 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
406 addPodcastItem( p_item );
408 playlist_Unlock( THEPL );
410 podcastsParent->setExpanded( true );
413 void PLSelector::plItemRemoved( int id )
415 int c = podcastsParent->childCount();
416 for( int i = 0; i < c; i++ )
418 QTreeWidgetItem *item = podcastsParent->child(i);
419 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
421 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
422 //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
423 vlc_gc_decref( p_input );
430 void PLSelector::inputItemUpdate( input_item_t *arg )
432 int c = podcastsParent->childCount();
433 for( int i = 0; i < c; i++ )
435 QTreeWidgetItem *item = podcastsParent->child(i);
436 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
439 PLSelItem *si = itemWidget( item );
440 char *psz_name = input_item_GetName( p_input );
441 si->setText( qfu( psz_name ) );
448 void PLSelector::podcastAdd( PLSelItem* item )
451 QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
452 qtr( "Enter URL of the podcast to subscribe to:" ),
453 QLineEdit::Normal, QString(), &ok );
454 if( !ok || url.isEmpty() ) return;
456 setSource( podcastsParent ); //to load the SD in case it's not loaded
458 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
459 p_intf->p_libvlc, "podcast", FIND_CHILD );
462 QString request("ADD:");
463 request += url.trimmed();
464 var_SetString( p_obj, "podcast-request", qtu( request ) );
465 vlc_object_release( p_obj );
468 void PLSelector::podcastRemove( PLSelItem* item )
470 QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
471 question = question.arg( item->text() );
472 QMessageBox::StandardButton res =
473 QMessageBox::question( this, qtr( "Unsubscribe" ), question,
474 QMessageBox::Ok | QMessageBox::Cancel,
475 QMessageBox::Cancel );
476 if( res == QMessageBox::Cancel ) return;
478 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
481 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
482 p_intf->p_libvlc, "podcast", FIND_CHILD );
485 QString request("RM:");
486 char *psz_uri = input_item_GetURI( input );
487 request += qfu( psz_uri );
488 var_SetString( p_obj, "podcast-request", qtu( request ) );
489 vlc_object_release( p_obj );
493 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
495 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
498 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
500 if( !model()->hasChildren( index ) ) return;
502 option.initFrom( this );
503 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
504 style()->drawPrimitive( isExpanded( index ) ?
505 QStyle::PE_IndicatorArrowDown :
506 QStyle::PE_IndicatorArrowRight, &option, painter );