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>
47 #include <vlc_playlist.h>
48 #include <vlc_services_discovery.h>
50 void SelectorActionButton::paintEvent( QPaintEvent *event )
53 QColor color = palette().color( QPalette::HighlightedText );
56 p.fillRect( rect(), color );
58 int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
59 p.drawLine( rect().topLeft() + QPoint( 0, frame ),
60 rect().bottomLeft() - QPoint( 0, frame ) );
61 QFramelessButton::paintEvent( event );
64 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
65 : qitem(i), lblAction( NULL)
67 layout = new QHBoxLayout( this );
68 layout->setContentsMargins(0,0,0,0);
69 layout->addSpacing( 3 );
71 lbl = new QElidingLabel( text );
72 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;
94 lblAction = new SelectorActionButton();
95 lblAction->setIcon( icon );
96 lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
98 if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
100 layout->addWidget( lblAction, 0 );
103 CONNECT( lblAction, clicked(), this, triggerAction() );
107 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
108 : QTreeWidget( p ), p_intf(_p_intf)
111 setFrameStyle( QFrame::NoFrame );
112 setAttribute( Qt::WA_MacShowFocusRect, false );
113 viewport()->setAutoFillBackground( false );
114 setIconSize( QSize( 24,24 ) );
115 setIndentation( 12 );
116 setHeaderHidden( true );
117 setRootIsDecorated( true );
118 setAlternatingRowColors( false );
121 viewport()->setAcceptDrops(true);
122 setDropIndicatorShown(true);
123 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
126 setAutoFillBackground( true );
128 palette.setColor( QPalette::Window, QColor(209,215,226) );
129 setPalette( palette );
131 setMinimumHeight( 120 );
134 podcastsParent = NULL;
135 podcastsParentId = -1;
137 /* Podcast connects */
138 CONNECT( THEMIM, playlistItemAppended( int, int ),
139 this, plItemAdded( int, int ) );
140 CONNECT( THEMIM, playlistItemRemoved( int ),
141 this, plItemRemoved( int ) );
142 DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
143 this, inputItemUpdate( input_item_t * ) );
147 setRootIsDecorated( false );
149 /* Expand at least to show level 2 */
150 for ( int i = 0; i < topLevelItemCount(); i++ )
151 expandItem( topLevelItem( i ) );
154 * We need to react to both clicks and activation (enter-key) here.
155 * We use curItem to avoid rebuilding twice.
156 * See QStyle::SH_ItemView_ActivateItemOnSingleClick
159 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
160 this, setSource( QTreeWidgetItem *) );
161 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
162 this, setSource( QTreeWidgetItem *) );
165 PLSelector::~PLSelector()
169 int c = podcastsParent->childCount();
170 for( int i = 0; i < c; i++ )
172 QTreeWidgetItem *item = podcastsParent->child(i);
173 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
174 vlc_gc_decref( p_input );
179 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
181 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
182 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
186 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
188 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
189 /* item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
190 item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
195 * Reads and updates the playlist's duration as [xx:xx] after the label in the tree
196 * item - the treeview item to get the duration for
197 * prefix - the string to use before the time (should be the category name)
199 void PLSelector::updateTotalDuration( PLSelItem* item, const char* prefix )
201 /* Getting the playlist */
202 QVariant playlistVariant = item->treeItem()->data( 0, PL_ITEM_ROLE );
203 playlist_item_t* node = playlistVariant.value<playlist_item_t*>();
205 /* Get the duration of the playlist item */
206 playlist_Lock( THEPL );
207 mtime_t mt_duration = playlist_GetNodeDuration( node );
208 playlist_Unlock( THEPL );
210 /* Formatting time */
211 QString qs_timeLabel( prefix );
213 int i_seconds = mt_duration / 1000000;
214 int i_minutes = i_seconds / 60;
215 i_seconds = i_seconds % 60;
216 if( i_minutes >= 60 )
218 int i_hours = i_minutes / 60;
219 i_minutes = i_minutes % 60;
220 qs_timeLabel += QString(" [%1:%2:%3]").arg( i_hours ).arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
223 qs_timeLabel += QString( " [%1:%2]").arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
225 item->setText( qs_timeLabel );
228 void PLSelector::createItems()
231 playlistItem = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
233 playlistItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
234 playlistItem->treeItem()->setData( 0, Qt::DecorationRole, QIcon( ":/sidebar/playlist" ) );
235 setCurrentItem( playlistItem->treeItem() );
238 PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
239 THEPL->p_media_library );
240 ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
241 ml->treeItem()->setData( 0, Qt::DecorationRole, QIcon( ":/sidebar/library" ) );
245 ml = addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
246 ml->treeItem()->setData( 0, Qt::DecorationRole, QIcon( ":/sidebar/library" ) );
250 QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer"), false, true )->treeItem();
251 QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices"), false, true )->treeItem();
252 QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network"), false, true )->treeItem();
253 QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet"), false, true )->treeItem();
255 #define NOT_SELECTABLE(w) w->setFlags( w->flags() ^ Qt::ItemIsSelectable );
256 NOT_SELECTABLE( mycomp );
257 NOT_SELECTABLE( devices );
258 NOT_SELECTABLE( lan );
259 NOT_SELECTABLE( internet );
260 #undef NOT_SELECTABLE
263 char **ppsz_longnames;
265 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
269 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
270 int *p_category = p_categories;
271 for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
273 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
277 QString name( *ppsz_name );
278 switch( *p_category )
280 case SD_CAT_INTERNET:
282 selItem = addItem( SD_TYPE, *ppsz_longname, false, false, internet );
283 if( name.startsWith( "podcast" ) )
285 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
286 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
287 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
288 podcastsParent = selItem->treeItem();
289 icon = QIcon( ":/sidebar/podcast" );
291 else if ( name.startsWith( "lua{" ) )
293 int i_head = name.indexOf( "sd='" ) + 4;
294 int i_tail = name.indexOf( '\'', i_head );
295 name.mid( i_head, i_tail - i_head );
296 QString iconname = QString( ":/sidebar/sd/%1" ).arg( name.mid( i_head, i_tail - i_head ) );
297 QResource resource( iconname );
298 if ( !resource.isValid() )
299 icon = QIcon( ":/sidebar/network" );
301 icon = QIcon( iconname );
306 name = name.mid( 0, name.indexOf( '{' ) );
307 selItem = addItem( SD_TYPE, *ppsz_longname, false, false, devices );
308 if ( name == "xcb_apps" )
309 icon = QIcon( ":/sidebar/screen" );
310 else if ( name == "mtp" )
311 icon = QIcon( ":/sidebar/mtp" );
312 else if ( name == "disc" )
313 icon = QIcon( ":/sidebar/disc" );
315 icon = QIcon( ":/sidebar/capture" );
318 selItem = addItem( SD_TYPE, *ppsz_longname, false, false, lan );
319 icon = QIcon( ":/sidebar/lan" );
321 case SD_CAT_MYCOMPUTER:
322 name = name.mid( 0, name.indexOf( '{' ) );
323 selItem = addItem( SD_TYPE, *ppsz_longname, false, false, mycomp );
324 if ( name == "video_dir" )
325 icon = QIcon( ":/sidebar/movie" );
326 else if ( name == "audio_dir" )
327 icon = QIcon( ":/sidebar/music" );
328 else if ( name == "picture_dir" )
329 icon = QIcon( ":/sidebar/pictures" );
331 icon = QIcon( ":/sidebar/movie" );
334 selItem = addItem( SD_TYPE, *ppsz_longname );
337 putSDData( selItem, *ppsz_name, *ppsz_longname );
338 if ( ! icon.isNull() )
339 selItem->treeItem()->setData( 0, Qt::DecorationRole, icon );
342 free( *ppsz_longname );
345 free( ppsz_longnames );
346 free( p_categories );
348 if( mycomp->childCount() == 0 ) delete mycomp;
349 if( devices->childCount() == 0 ) delete devices;
350 if( lan->childCount() == 0 ) delete lan;
351 if( internet->childCount() == 0 ) delete internet;
354 void PLSelector::setSource( QTreeWidgetItem *item )
356 if( !item || item == curItem )
360 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
361 if( !b_ok || i_type == CATEGORY_TYPE )
365 if( i_type == SD_TYPE )
367 QString qs = item->data( 0, NAME_ROLE ).toString();
368 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
371 if ( playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) ) != VLC_SUCCESS )
374 services_discovery_descriptor_t *p_test = new services_discovery_descriptor_t;
375 int i_ret = playlist_ServicesDiscoveryControl( THEPL, qtu( qs ), SD_CMD_DESCRIPTOR, p_test );
376 if( i_ret == VLC_SUCCESS && p_test->i_capabilities & SD_CAP_SEARCH )
377 item->setData( 0, CAP_SEARCH_ROLE, true );
381 else if( i_type == SQL_ML_TYPE )
383 emit categoryActivated( NULL, true );
392 playlist_Lock( THEPL );
393 playlist_item_t *pl_item = NULL;
395 /* Special case for podcast */
397 if( i_type == SD_TYPE )
399 /* Find the right item for the SD */
400 pl_item = playlist_ChildSearchName( THEPL->p_root,
401 qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
404 if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
406 if( pl_item && !sd_loaded )
408 podcastsParentId = pl_item->i_id;
409 for( int i=0; i < pl_item->i_children; i++ )
410 addPodcastItem( pl_item->pp_children[i] );
412 pl_item = NULL; //to prevent activating it
416 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
418 playlist_Unlock( THEPL );
422 emit categoryActivated( pl_item, false );
425 PLSelItem * PLSelector::addItem (
426 SelectorItemType type, const char* str, bool drop, bool bold,
427 QTreeWidgetItem* parentItem )
429 QTreeWidgetItem *item = parentItem ?
430 new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
432 PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
433 if ( bold ) selItem->setStyleSheet( "font-weight: bold;" );
434 setItemWidget( item, 0, selItem );
435 item->setData( 0, TYPE_ROLE, (int)type );
436 if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
441 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
443 vlc_gc_incref( p_item->p_input );
445 char *psz_name = input_item_GetName( p_item->p_input );
446 PLSelItem *item = addItem( PL_ITEM_TYPE, psz_name, false, false, podcastsParent );
449 item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
450 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
451 item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
452 item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
453 CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
457 QStringList PLSelector::mimeTypes() const
460 types << "vlc/qt-input-items";
464 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int,
465 const QMimeData * data, Qt::DropAction )
467 if( !parent ) return false;
469 QVariant type = parent->data( 0, TYPE_ROLE );
470 if( type == QVariant() ) return false;
472 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
473 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
475 bool to_pl = ( i_truth == IS_PL );
477 const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
478 if( !plMimeData ) return false;
480 QList<input_item_t*> inputItems = plMimeData->inputItems();
482 playlist_Lock( THEPL );
484 foreach( input_item_t *p_input, inputItems )
486 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
487 if( !p_item ) continue;
489 playlist_NodeAddCopy( THEPL, p_item,
490 to_pl ? THEPL->p_playing : THEPL->p_media_library,
494 playlist_Unlock( THEPL );
499 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
501 event->setDropAction( Qt::CopyAction );
502 QAbstractItemView::dragMoveEvent( event );
505 void PLSelector::plItemAdded( int item, int parent )
507 updateTotalDuration(playlistItem, "Playlist");
508 if( parent != podcastsParentId || podcastsParent == NULL ) return;
510 playlist_Lock( THEPL );
512 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
514 playlist_Unlock( THEPL );
518 int c = podcastsParent->childCount();
519 for( int i = 0; i < c; i++ )
521 QTreeWidgetItem *podItem = podcastsParent->child(i);
522 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
524 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
525 playlist_Unlock( THEPL );
530 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
531 addPodcastItem( p_item );
533 playlist_Unlock( THEPL );
535 podcastsParent->setExpanded( true );
538 void PLSelector::plItemRemoved( int id )
540 updateTotalDuration(playlistItem, "Playlist");
541 if( !podcastsParent ) return;
543 int c = podcastsParent->childCount();
544 for( int i = 0; i < c; i++ )
546 QTreeWidgetItem *item = podcastsParent->child(i);
547 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
549 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
550 //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
551 vlc_gc_decref( p_input );
558 void PLSelector::inputItemUpdate( input_item_t *arg )
560 updateTotalDuration(playlistItem, "Playlist");
562 if( podcastsParent == NULL )
565 int c = podcastsParent->childCount();
566 for( int i = 0; i < c; i++ )
568 QTreeWidgetItem *item = podcastsParent->child(i);
569 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
572 PLSelItem *si = itemWidget( item );
573 char *psz_name = input_item_GetName( p_input );
574 si->setText( qfu( psz_name ) );
581 void PLSelector::podcastAdd( PLSelItem * )
583 assert( podcastsParent );
586 QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
587 qtr( "Enter URL of the podcast to subscribe to:" ),
588 QLineEdit::Normal, QString(), &ok );
589 if( !ok || url.isEmpty() ) return;
591 setSource( podcastsParent ); //to load the SD in case it's not loaded
593 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name( p_intf->p_libvlc, "podcast" );
596 QString request("ADD:");
597 request += url.trimmed();
598 var_SetString( p_obj, "podcast-request", qtu( request ) );
599 vlc_object_release( p_obj );
602 void PLSelector::podcastRemove( PLSelItem* item )
604 QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
605 question = question.arg( item->text() );
606 QMessageBox::StandardButton res =
607 QMessageBox::question( this, qtr( "Unsubscribe" ), question,
608 QMessageBox::Yes | QMessageBox::No,
610 if( res == QMessageBox::No ) return;
612 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
615 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
616 p_intf->p_libvlc, "podcast" );
619 QString request("RM:");
620 char *psz_uri = input_item_GetURI( input );
621 request += qfu( psz_uri );
622 var_SetString( p_obj, "podcast-request", qtu( request ) );
623 vlc_object_release( p_obj );
627 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
629 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
632 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
634 if( !model()->hasChildren( index ) ) return;
636 option.initFrom( this );
637 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
638 style()->drawPrimitive( isExpanded( index ) ?
639 QStyle::PE_IndicatorArrowDown :
640 QStyle::PE_IndicatorArrowRight, &option, painter );
643 void PLSelector::getCurrentItemInfos( int* type, bool* can_delay_search, QString *string)
645 *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
646 *string = currentItem()->data( 0, NAME_ROLE ).toString();
647 *can_delay_search = currentItem()->data( 0, CAP_SEARCH_ROLE ).toBool();
650 int PLSelector::getCurrentItemCategory()
652 return currentItem()->data( 0, SPECIAL_ROLE ).toInt();
655 void PLSelector::wheelEvent( QWheelEvent *e )
657 if( verticalScrollBar()->isVisible() && (
658 (verticalScrollBar()->value() != verticalScrollBar()->minimum() && e->delta() >= 0 ) ||
659 (verticalScrollBar()->value() != verticalScrollBar()->maximum() && e->delta() < 0 )
661 QApplication::sendEvent(verticalScrollBar(), e);
663 // Accept this event in order to prevent unwanted volume up/down changes