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 <QInputDialog>
35 #include <QMessageBox>
37 #include <QDragMoveEvent>
38 #include <QTreeWidgetItem>
39 #include <QHBoxLayout>
42 #include <vlc_playlist.h>
43 #include <vlc_services_discovery.h>
45 void SelectorActionButton::paintEvent( QPaintEvent *event )
48 QColor color = palette().color( QPalette::HighlightedText );
51 p.fillRect( rect(), color );
53 int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
54 p.drawLine( rect().topLeft() + QPoint( 0, frame ),
55 rect().bottomLeft() - QPoint( 0, frame ) );
56 QVLCFramelessButton::paintEvent( event );
59 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
60 : qitem(i), lblAction( NULL)
62 layout = new QHBoxLayout( this );
63 layout->setContentsMargins(0,0,0,0);
64 layout->addSpacing( 3 );
66 lbl = new QVLCElidingLabel( text );
67 layout->addWidget(lbl, 1);
69 int height = qMax( 22, fontMetrics().height() + 8 );
70 setMinimumHeight( height );
73 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
75 if( lblAction ) return; //might change later
82 icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
84 icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
87 lblAction = new SelectorActionButton();
88 lblAction->setIcon( icon );
89 lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
91 if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
93 layout->addWidget( lblAction, 0 );
96 CONNECT( lblAction, clicked(), this, triggerAction() );
100 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
101 : QTreeWidget( p ), p_intf(_p_intf)
104 setFrameStyle( QFrame::NoFrame );
105 viewport()->setAutoFillBackground( false );
106 setIconSize( QSize( 24,24 ) );
107 setIndentation( 12 );
108 setHeaderHidden( true );
109 setRootIsDecorated( true );
110 setAlternatingRowColors( false );
113 viewport()->setAcceptDrops(true);
114 setDropIndicatorShown(true);
115 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
118 podcastsParent = NULL;
119 podcastsParentId = -1;
121 /* Podcast connects */
122 CONNECT( THEMIM, playlistItemAppended( int, int ),
123 this, plItemAdded( int, int ) );
124 CONNECT( THEMIM, playlistItemRemoved( int ),
125 this, plItemRemoved( int ) );
126 DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
127 this, inputItemUpdate( input_item_t * ) );
130 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
131 this, setSource( QTreeWidgetItem *) );
132 CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
133 this, setSource( QTreeWidgetItem *) );
135 /* I believe this is unnecessary, seeing
136 QStyle::SH_ItemView_ActivateItemOnSingleClick
137 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
138 this, setSource( QTreeWidgetItem *) ); */
140 /* select the first item */
141 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
144 PLSelector::~PLSelector()
148 int c = podcastsParent->childCount();
149 for( int i = 0; i < c; i++ )
151 QTreeWidgetItem *item = podcastsParent->child(i);
152 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
153 vlc_gc_decref( p_input );
158 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
160 item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
161 item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
165 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
167 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
168 /* item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
169 item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
173 void PLSelector::createItems()
176 PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, "Playlist", true ),
178 pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
181 PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, "Media Library", true ),
182 THEPL->p_media_library );
183 ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
186 QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, "My Computer" )->treeItem();
187 QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, "Devices" )->treeItem();
188 QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, "Local Network" )->treeItem();
189 QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, "Internet" )->treeItem();
192 char **ppsz_longnames;
194 char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
198 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
199 int *p_category = p_categories;
200 for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
202 //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
205 switch( *p_category )
207 case SD_CAT_INTERNET:
209 selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
210 if( !strncmp( *ppsz_name, "podcast", 7 ) )
212 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
213 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
214 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
215 podcastsParent = selItem->treeItem();
220 selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
223 selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
225 case SD_CAT_MYCOMPUTER:
226 selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
229 selItem = addItem( SD_TYPE, *ppsz_longname );
232 putSDData( selItem, *ppsz_name, *ppsz_longname );
234 free( *ppsz_longname );
237 free( ppsz_longnames );
238 free( p_categories );
240 if( mycomp->childCount() == 0 ) delete mycomp;
241 if( devices->childCount() == 0 ) delete devices;
242 if( lan->childCount() == 0 ) delete lan;
243 if( internet->childCount() == 0 ) delete internet;
246 void PLSelector::setSource( QTreeWidgetItem *item )
252 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
253 if( !b_ok || i_type == CATEGORY_TYPE )
257 if( i_type == SD_TYPE )
259 QString qs = item->data( 0, NAME_ROLE ).toString();
260 sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
262 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
266 playlist_Lock( THEPL );
267 playlist_item_t *pl_item = NULL;
269 if( i_type == SD_TYPE )
271 /* Find the right item for the SD */
272 pl_item = playlist_ChildSearchName( THEPL->p_root,
273 qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
276 if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
278 if( pl_item && !sd_loaded )
280 podcastsParentId = pl_item->i_id;
281 for( int i=0; i < pl_item->i_children; i++ )
282 addPodcastItem( pl_item->pp_children[i] );
284 pl_item = NULL; //to prevent activating it
288 pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
290 playlist_Unlock( THEPL );
294 emit activated( pl_item );
297 PLSelItem * PLSelector::addItem (
298 SelectorItemType type, const char* str, bool drop,
299 QTreeWidgetItem* parentItem )
301 QTreeWidgetItem *item = parentItem ?
302 new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
304 PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
305 setItemWidget( item, 0, selItem );
306 item->setData( 0, TYPE_ROLE, (int)type );
307 if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
312 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
314 vlc_gc_incref( p_item->p_input );
316 char *psz_name = input_item_GetName( p_item->p_input );
317 PLSelItem *item = addItem( PL_ITEM_TYPE, psz_name, false, podcastsParent );
320 item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
321 item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
322 item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
323 item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
324 CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
328 QStringList PLSelector::mimeTypes() const
331 types << "vlc/qt-input-items";
335 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
336 const QMimeData * data, Qt::DropAction action )
338 if( !parent ) return false;
340 QVariant type = parent->data( 0, TYPE_ROLE );
341 if( type == QVariant() ) return false;
343 int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
344 if( i_truth != IS_PL && i_truth != IS_ML ) return false;
346 bool to_pl = ( i_truth == IS_PL );
348 const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
349 if( !plMimeData ) return false;
351 QList<input_item_t*> inputItems = plMimeData->inputItems();
353 playlist_Lock( THEPL );
355 foreach( input_item_t *p_input, inputItems )
357 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
358 if( !p_item ) continue;
360 playlist_NodeAddCopy( THEPL, p_item,
361 to_pl ? THEPL->p_playing : THEPL->p_media_library,
365 playlist_Unlock( THEPL );
370 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
372 event->setDropAction( Qt::CopyAction );
373 QAbstractItemView::dragMoveEvent( event );
376 void PLSelector::plItemAdded( int item, int parent )
378 if( parent != podcastsParentId ) return;
380 playlist_Lock( THEPL );
382 playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
384 playlist_Unlock( THEPL );
388 int c = podcastsParent->childCount();
389 for( int i = 0; i < c; i++ )
391 QTreeWidgetItem *podItem = podcastsParent->child(i);
392 if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
394 //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
395 playlist_Unlock( THEPL );
400 //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
401 addPodcastItem( p_item );
403 playlist_Unlock( THEPL );
405 podcastsParent->setExpanded( true );
408 void PLSelector::plItemRemoved( int id )
410 int c = podcastsParent->childCount();
411 for( int i = 0; i < c; i++ )
413 QTreeWidgetItem *item = podcastsParent->child(i);
414 if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
416 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
417 //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
418 vlc_gc_decref( p_input );
425 void PLSelector::inputItemUpdate( input_item_t *arg )
427 int c = podcastsParent->childCount();
428 for( int i = 0; i < c; i++ )
430 QTreeWidgetItem *item = podcastsParent->child(i);
431 input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
434 PLSelItem *si = itemWidget( item );
435 char *psz_name = input_item_GetName( p_input );
436 si->setText( qfu( psz_name ) );
443 void PLSelector::podcastAdd( PLSelItem* item )
446 QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
447 qtr( "Enter URL of the podcast to subscribe to:" ),
448 QLineEdit::Normal, QString(), &ok );
449 if( !ok || url.isEmpty() ) return;
451 setSource( podcastsParent ); //to load the SD in case it's not loaded
453 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
454 p_intf->p_libvlc, "podcast", FIND_CHILD );
457 QString request("ADD:");
458 request += url.trimmed();
459 var_SetString( p_obj, "podcast-request", qtu( request ) );
460 vlc_object_release( p_obj );
463 void PLSelector::podcastRemove( PLSelItem* item )
465 QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
466 question = question.arg( item->text() );
467 QMessageBox::StandardButton res =
468 QMessageBox::question( this, qtr( "Unsubscribe" ), question,
469 QMessageBox::Ok | QMessageBox::Cancel,
470 QMessageBox::Cancel );
471 if( res == QMessageBox::Cancel ) return;
473 input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
476 vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
477 p_intf->p_libvlc, "podcast", FIND_CHILD );
480 QString request("RM:");
481 char *psz_uri = input_item_GetURI( input );
482 request += qfu( psz_uri );
483 var_SetString( p_obj, "podcast-request", qtu( request ) );
484 vlc_object_release( p_obj );
488 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
490 return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
493 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
495 if( !model()->hasChildren( index ) ) return;
497 option.initFrom( this );
498 option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
499 style()->drawPrimitive( isExpanded( index ) ?
500 QStyle::PE_IndicatorArrowDown :
501 QStyle::PE_IndicatorArrowRight, &option, painter );