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 *****************************************************************************/
30 #include "components/playlist/selector.hpp"
31 #include "playlist_model.hpp" /* plMimeData */
32 #include "input_manager.hpp" /* MainInputManager, for podcast */
34 #include <QApplication>
35 #include <QInputDialog>
36 #include <QMessageBox>
38 #include <QDragMoveEvent>
39 #include <QTreeWidgetItem>
40 #include <QHBoxLayout>
45 #include <vlc_playlist.h>
46 #include <vlc_services_discovery.h>
48 void SelectorActionButton::paintEvent( QPaintEvent *event )
51 QColor color = palette().color( QPalette::HighlightedText );
54 p.fillRect( rect(), color );
56 int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
57 p.drawLine( rect().topLeft() + QPoint( 0, frame ),
58 rect().bottomLeft() - QPoint( 0, frame ) );
59 QFramelessButton::paintEvent( event );
62 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
63 : qitem(i), lblAction( NULL)
65 layout = new QHBoxLayout( this );
66 layout->setContentsMargins(0,0,0,0);
67 layout->addSpacing( 3 );
69 lbl = new QElidingLabel( text );
70 layout->addWidget(lbl, 1);
72 int height = qMax( 22, fontMetrics().height() + 8 );
73 setMinimumHeight( height );
76 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
78 if( lblAction ) return; //might change later
85 icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
87 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() );
105 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
106 : QTreeWidget( p ), p_intf(_p_intf)
109 setFrameStyle( QFrame::NoFrame );
110 setAttribute( Qt::WA_MacShowFocusRect, false );
111 viewport()->setAutoFillBackground( false );
112 setIconSize( QSize( 24,24 ) );
113 setIndentation( 12 );
114 setHeaderHidden( true );
115 setRootIsDecorated( true );
116 setAlternatingRowColors( false );
119 viewport()->setAcceptDrops(true);
120 setDropIndicatorShown(true);
121 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
124 setAutoFillBackground( true );
126 palette.setColor( QPalette::Window, QColor(209,215,226) );
127 setPalette( palette );
129 setMinimumHeight( 120 );
132 podcastsParent = NULL;
133 podcastsParentId = -1;
135 /* Podcast connects */
136 CONNECT( THEMIM, playlistItemAppended( int, int ),
137 this, plItemAdded( int, int ) );
138 CONNECT( THEMIM, playlistItemRemoved( int ),
139 this, plItemRemoved( int ) );
140 DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
141 this, inputItemUpdate( input_item_t * ) );
145 /* Expand at least to show level 2 */
146 for ( int i = 0; i < topLevelItemCount(); i++ )
147 expandItem( topLevelItem( i ) );
150 * We need to react to both clicks and activation (enter-key) here.
151 * We use curItem to avoid rebuilding twice.
152 * See QStyle::SH_ItemView_ActivateItemOnSingleClick
155 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
156 this, setSource( QTreeWidgetItem *) );
157 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
158 this, setSource( QTreeWidgetItem *) );
161 PLSelector::~PLSelector()
165 int c = podcastsParent->childCount();
166 for( int i = 0; i < c; i++ )
168 QTreeWidgetItem *item = podcastsParent->child(i);
169 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
170 vlc_gc_decref( p_input );
175 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
177 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
178 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
182 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
184 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
185 /* item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
186 item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
190 void PLSelector::createItems()
193 PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
195 pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
196 setCurrentItem( pl->treeItem() );
199 PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
200 THEPL->p_media_library );
201 ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
205 addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
209 QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer") )->treeItem();
210 QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices") )->treeItem();
211 QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network") )->treeItem();
212 QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet") )->treeItem();
215 char **ppsz_longnames;
217 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
221 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
222 int *p_category = p_categories;
223 for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
225 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
228 switch( *p_category )
230 case SD_CAT_INTERNET:
232 selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
233 if( !strncmp( *ppsz_name, "podcast", 7 ) )
235 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
236 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
237 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
238 podcastsParent = selItem->treeItem();
243 selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
246 selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
248 case SD_CAT_MYCOMPUTER:
249 selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
252 selItem = addItem( SD_TYPE, *ppsz_longname );
255 putSDData( selItem, *ppsz_name, *ppsz_longname );
257 free( *ppsz_longname );
260 free( ppsz_longnames );
261 free( p_categories );
263 if( mycomp->childCount() == 0 ) delete mycomp;
264 if( devices->childCount() == 0 ) delete devices;
265 if( lan->childCount() == 0 ) delete lan;
266 if( internet->childCount() == 0 ) delete internet;
269 void PLSelector::setSource( QTreeWidgetItem *item )
271 if( !item || item == curItem )
275 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
276 if( !b_ok || i_type == CATEGORY_TYPE )
280 if( i_type == SD_TYPE )
282 QString qs = item->data( 0, NAME_ROLE ).toString();
283 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
286 if ( playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) ) != VLC_SUCCESS )
289 services_discovery_descriptor_t *p_test = new services_discovery_descriptor_t;
290 int i_ret = playlist_ServicesDiscoveryControl( THEPL, qtu( qs ), SD_CMD_DESCRIPTOR, p_test );
291 if( i_ret == VLC_SUCCESS && p_test->i_capabilities & SD_CAP_SEARCH )
292 item->setData( 0, CAP_SEARCH_ROLE, true );
296 else if( i_type == SQL_ML_TYPE )
298 emit categoryActivated( NULL, true );
306 playlist_Lock( THEPL );
307 playlist_item_t *pl_item = NULL;
309 /* Special case for podcast */
311 if( i_type == SD_TYPE )
313 /* Find the right item for the SD */
314 pl_item = playlist_ChildSearchName( THEPL->p_root,
315 qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
318 if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
320 if( pl_item && !sd_loaded )
322 podcastsParentId = pl_item->i_id;
323 for( int i=0; i < pl_item->i_children; i++ )
324 addPodcastItem( pl_item->pp_children[i] );
326 pl_item = NULL; //to prevent activating it
330 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
332 playlist_Unlock( THEPL );
336 emit categoryActivated( pl_item, false );
339 PLSelItem * PLSelector::addItem (
340 SelectorItemType type, const char* str, bool drop,
341 QTreeWidgetItem* parentItem )
343 QTreeWidgetItem *item = parentItem ?
344 new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
346 PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
347 setItemWidget( item, 0, selItem );
348 item->setData( 0, TYPE_ROLE, (int)type );
349 if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
354 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
356 vlc_gc_incref( p_item->p_input );
358 char *psz_name = input_item_GetName( p_item->p_input );
359 PLSelItem *item = addItem( PL_ITEM_TYPE, psz_name, false, podcastsParent );
362 item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
363 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
364 item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
365 item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
366 CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
370 QStringList PLSelector::mimeTypes() const
373 types << "vlc/qt-input-items";
377 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int,
378 const QMimeData * data, Qt::DropAction )
380 if( !parent ) return false;
382 QVariant type = parent->data( 0, TYPE_ROLE );
383 if( type == QVariant() ) return false;
385 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
386 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
388 bool to_pl = ( i_truth == IS_PL );
390 const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
391 if( !plMimeData ) return false;
393 QList<input_item_t*> inputItems = plMimeData->inputItems();
395 playlist_Lock( THEPL );
397 foreach( input_item_t *p_input, inputItems )
399 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
400 if( !p_item ) continue;
402 playlist_NodeAddCopy( THEPL, p_item,
403 to_pl ? THEPL->p_playing : THEPL->p_media_library,
407 playlist_Unlock( THEPL );
412 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
414 event->setDropAction( Qt::CopyAction );
415 QAbstractItemView::dragMoveEvent( event );
418 void PLSelector::plItemAdded( int item, int parent )
420 if( parent != podcastsParentId ) return;
422 playlist_Lock( THEPL );
424 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
426 playlist_Unlock( THEPL );
430 int c = podcastsParent->childCount();
431 for( int i = 0; i < c; i++ )
433 QTreeWidgetItem *podItem = podcastsParent->child(i);
434 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
436 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
437 playlist_Unlock( THEPL );
442 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
443 addPodcastItem( p_item );
445 playlist_Unlock( THEPL );
447 podcastsParent->setExpanded( true );
450 void PLSelector::plItemRemoved( int id )
452 int c = podcastsParent->childCount();
453 for( int i = 0; i < c; i++ )
455 QTreeWidgetItem *item = podcastsParent->child(i);
456 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
458 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
459 //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
460 vlc_gc_decref( p_input );
467 void PLSelector::inputItemUpdate( input_item_t *arg )
469 int c = podcastsParent->childCount();
470 for( int i = 0; i < c; i++ )
472 QTreeWidgetItem *item = podcastsParent->child(i);
473 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
476 PLSelItem *si = itemWidget( item );
477 char *psz_name = input_item_GetName( p_input );
478 si->setText( qfu( psz_name ) );
485 void PLSelector::podcastAdd( PLSelItem * )
488 QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
489 qtr( "Enter URL of the podcast to subscribe to:" ),
490 QLineEdit::Normal, QString(), &ok );
491 if( !ok || url.isEmpty() ) return;
493 setSource( podcastsParent ); //to load the SD in case it's not loaded
495 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name( p_intf->p_libvlc, "podcast" );
498 QString request("ADD:");
499 request += url.trimmed();
500 var_SetString( p_obj, "podcast-request", qtu( request ) );
501 vlc_object_release( p_obj );
504 void PLSelector::podcastRemove( PLSelItem* item )
506 QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
507 question = question.arg( item->text() );
508 QMessageBox::StandardButton res =
509 QMessageBox::question( this, qtr( "Unsubscribe" ), question,
510 QMessageBox::Ok | QMessageBox::Cancel,
511 QMessageBox::Cancel );
512 if( res == QMessageBox::Cancel ) return;
514 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
517 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
518 p_intf->p_libvlc, "podcast" );
521 QString request("RM:");
522 char *psz_uri = input_item_GetURI( input );
523 request += qfu( psz_uri );
524 var_SetString( p_obj, "podcast-request", qtu( request ) );
525 vlc_object_release( p_obj );
529 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
531 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
534 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
536 if( !model()->hasChildren( index ) ) return;
538 option.initFrom( this );
539 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
540 style()->drawPrimitive( isExpanded( index ) ?
541 QStyle::PE_IndicatorArrowDown :
542 QStyle::PE_IndicatorArrowRight, &option, painter );
545 void PLSelector::getCurrentItemInfos( int* type, bool* can_delay_search, QString *string)
547 *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
548 *string = currentItem()->data( 0, NAME_ROLE ).toString();
549 *can_delay_search = currentItem()->data( 0, CAP_SEARCH_ROLE ).toBool();
552 int PLSelector::getCurrentItemCategory()
554 return currentItem()->data( 0, SPECIAL_ROLE ).toInt();
557 void PLSelector::wheelEvent( QWheelEvent *e )
559 if( verticalScrollBar()->isVisible() && (
560 (verticalScrollBar()->value() != verticalScrollBar()->minimum() && e->delta() >= 0 ) ||
561 (verticalScrollBar()->value() != verticalScrollBar()->maximum() && e->delta() < 0 )
563 QApplication::sendEvent(verticalScrollBar(), e);
565 // Accept this event in order to prevent unwanted volume up/down changes