]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
Qt: add playlist total duration
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
1 /*****************************************************************************
2  * selector.cpp : Playlist source selector
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "components/playlist/selector.hpp"
31 #include "playlist_model.hpp"                /* plMimeData */
32 #include "input_manager.hpp"                 /* MainInputManager, for podcast */
33
34 #include <QApplication>
35 #include <QInputDialog>
36 #include <QMessageBox>
37 #include <QMimeData>
38 #include <QDragMoveEvent>
39 #include <QTreeWidgetItem>
40 #include <QHBoxLayout>
41 #include <QPainter>
42 #include <QPalette>
43 #include <QScrollBar>
44 #include <assert.h>
45
46 #include <vlc_playlist.h>
47 #include <vlc_services_discovery.h>
48
49 void SelectorActionButton::paintEvent( QPaintEvent *event )
50 {
51     QPainter p( this );
52     QColor color = palette().color( QPalette::HighlightedText );
53     color.setAlpha( 80 );
54     if( underMouse() )
55         p.fillRect( rect(), color );
56     p.setPen( color );
57     int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
58     p.drawLine( rect().topLeft() + QPoint( 0, frame ),
59                 rect().bottomLeft() - QPoint( 0, frame ) );
60     QFramelessButton::paintEvent( event );
61 }
62
63 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
64     : qitem(i), lblAction( NULL)
65 {
66     layout = new QHBoxLayout( this );
67     layout->setContentsMargins(0,0,0,0);
68     layout->addSpacing( 3 );
69
70     lbl = new QElidingLabel( text );
71     layout->addWidget(lbl, 1);
72
73     int height = qMax( 22, fontMetrics().height() + 8 );
74     setMinimumHeight( height );
75 }
76
77 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
78 {
79     if( lblAction ) return; //might change later
80
81     QIcon icon;
82
83     switch( act )
84     {
85     case ADD_ACTION:
86         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
87     case RM_ACTION:
88         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
89     default:
90         return;
91     }
92
93     lblAction = new SelectorActionButton();
94     lblAction->setIcon( icon );
95     lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
96
97     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
98
99     layout->addWidget( lblAction, 0 );
100     lblAction->hide();
101
102     CONNECT( lblAction, clicked(), this, triggerAction() );
103 }
104
105
106 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
107            : QTreeWidget( p ), p_intf(_p_intf)
108 {
109     /* Properties */
110     setFrameStyle( QFrame::NoFrame );
111     setAttribute( Qt::WA_MacShowFocusRect, false );
112     viewport()->setAutoFillBackground( false );
113     setIconSize( QSize( 24,24 ) );
114     setIndentation( 12 );
115     setHeaderHidden( true );
116     setRootIsDecorated( true );
117     setAlternatingRowColors( false );
118
119     /* drops */
120     viewport()->setAcceptDrops(true);
121     setDropIndicatorShown(true);
122     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
123
124 #ifdef Q_WS_MAC
125     setAutoFillBackground( true );
126     QPalette palette;
127     palette.setColor( QPalette::Window, QColor(209,215,226) );
128     setPalette( palette );
129 #endif
130     setMinimumHeight( 120 );
131
132     /* Podcasts */
133     podcastsParent = NULL;
134     podcastsParentId = -1;
135
136     /* Podcast connects */
137     CONNECT( THEMIM, playlistItemAppended( int, int ),
138              this, plItemAdded( int, int ) );
139     CONNECT( THEMIM, playlistItemRemoved( int ),
140              this, plItemRemoved( int ) );
141     DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
142               this, inputItemUpdate( input_item_t * ) );
143
144     createItems();
145
146     /* Expand at least to show level 2 */
147     for ( int i = 0; i < topLevelItemCount(); i++ )
148         expandItem( topLevelItem( i ) );
149
150     /***
151      * We need to react to both clicks and activation (enter-key) here.
152      * We use curItem to avoid rebuilding twice.
153      * See QStyle::SH_ItemView_ActivateItemOnSingleClick
154      ***/
155     curItem = NULL;
156     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
157              this, setSource( QTreeWidgetItem *) );
158     CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
159              this, setSource( QTreeWidgetItem *) );
160 }
161
162 PLSelector::~PLSelector()
163 {
164     if( podcastsParent )
165     {
166         int c = podcastsParent->childCount();
167         for( int i = 0; i < c; i++ )
168         {
169             QTreeWidgetItem *item = podcastsParent->child(i);
170             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
171             vlc_gc_decref( p_input );
172         }
173     }
174 }
175
176 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
177 {
178     item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
179     item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
180     return item;
181 }
182
183 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
184 {
185     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
186 /*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
187     item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
188     return item;
189 }
190
191 /*
192  * Reads and updates the playlist's duration as [xx:xx] after the label in the tree
193  * item - the treeview item to get the duration for
194  * prefix - the string to use before the time (should be the category name)
195  */
196 void PLSelector::updateTotalDuration( PLSelItem* item, const char* prefix )
197 {
198     /* Getting  the playlist */
199     QVariant playlistVariant = item->treeItem()->data( 0, PL_ITEM_ROLE );
200     playlist_item_t* node = playlistVariant.value<playlist_item_t*>();
201
202     /* Formatting time */
203     QString qs_timeLabel( prefix );
204     mtime_t mt_duration = playlist_GetNodeDuration( node );
205     int i_seconds = mt_duration / 1000000;
206     int i_minutes = i_seconds / 60;
207     i_seconds = i_seconds % 60;
208     if( i_minutes >= 60 )
209     {
210         int i_hours = i_minutes / 60;
211         i_minutes = i_minutes % 60;
212         qs_timeLabel += QString(" [%1:%2:%3]").arg( i_hours ).arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
213     }
214     else
215         qs_timeLabel += QString( " [%1:%2]").arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
216
217     item->setText( qs_timeLabel );
218 }
219
220 void PLSelector::createItems()
221 {
222     /* PL */
223     playlistItem = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
224                               THEPL->p_playing );
225     playlistItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
226     setCurrentItem( playlistItem->treeItem() );
227
228     /* ML */
229     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
230                               THEPL->p_media_library );
231     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
232
233 #ifdef MEDIA_LIBRARY
234     /* SQL ML */
235     addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
236 #endif
237
238     /* SD nodes */
239     QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer") )->treeItem();
240     QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices") )->treeItem();
241     QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network") )->treeItem();
242     QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet") )->treeItem();
243
244 #define NOT_SELECTABLE(w) w->setFlags( w->flags() ^ Qt::ItemIsSelectable );
245     NOT_SELECTABLE( mycomp );
246     NOT_SELECTABLE( devices );
247     NOT_SELECTABLE( lan );
248     NOT_SELECTABLE( internet );
249 #undef NOT_SELECTABLE
250
251     /* SD subnodes */
252     char **ppsz_longnames;
253     int *p_categories;
254     char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
255     if( !ppsz_names )
256         return;
257
258     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
259     int *p_category = p_categories;
260     for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
261     {
262         //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
263
264         PLSelItem *selItem;
265         switch( *p_category )
266         {
267         case SD_CAT_INTERNET:
268             {
269             selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
270             if( !strncmp( *ppsz_name, "podcast", 7 ) )
271             {
272                 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
273                 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
274                 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
275                 podcastsParent = selItem->treeItem();
276             }
277             }
278             break;
279         case SD_CAT_DEVICES:
280             selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
281             break;
282         case SD_CAT_LAN:
283             selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
284             break;
285         case SD_CAT_MYCOMPUTER:
286             selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
287             break;
288         default:
289             selItem = addItem( SD_TYPE, *ppsz_longname );
290         }
291
292         putSDData( selItem, *ppsz_name, *ppsz_longname );
293         free( *ppsz_name );
294         free( *ppsz_longname );
295     }
296     free( ppsz_names );
297     free( ppsz_longnames );
298     free( p_categories );
299
300     if( mycomp->childCount() == 0 ) delete mycomp;
301     if( devices->childCount() == 0 ) delete devices;
302     if( lan->childCount() == 0 ) delete lan;
303     if( internet->childCount() == 0 ) delete internet;
304 }
305
306 void PLSelector::setSource( QTreeWidgetItem *item )
307 {
308     if( !item || item == curItem )
309         return;
310
311     bool b_ok;
312     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
313     if( !b_ok || i_type == CATEGORY_TYPE )
314         return;
315
316     bool sd_loaded;
317     if( i_type == SD_TYPE )
318     {
319         QString qs = item->data( 0, NAME_ROLE ).toString();
320         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
321         if( !sd_loaded )
322         {
323             if ( playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) ) != VLC_SUCCESS )
324                 return ;
325
326             services_discovery_descriptor_t *p_test = new services_discovery_descriptor_t;
327             int i_ret = playlist_ServicesDiscoveryControl( THEPL, qtu( qs ), SD_CMD_DESCRIPTOR, p_test );
328             if( i_ret == VLC_SUCCESS && p_test->i_capabilities & SD_CAP_SEARCH )
329                 item->setData( 0, CAP_SEARCH_ROLE, true );
330         }
331     }
332 #ifdef MEDIA_LIBRARY
333     else if( i_type == SQL_ML_TYPE )
334     {
335         emit categoryActivated( NULL, true );
336         curItem = item;
337         return;
338     }
339 #endif
340
341     curItem = item;
342
343     /* */
344     playlist_Lock( THEPL );
345     playlist_item_t *pl_item = NULL;
346
347     /* Special case for podcast */
348     // FIXME: simplify
349     if( i_type == SD_TYPE )
350     {
351         /* Find the right item for the SD */
352         pl_item = playlist_ChildSearchName( THEPL->p_root,
353                       qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
354
355         /* Podcasts */
356         if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
357         {
358             if( pl_item && !sd_loaded )
359             {
360                 podcastsParentId = pl_item->i_id;
361                 for( int i=0; i < pl_item->i_children; i++ )
362                     addPodcastItem( pl_item->pp_children[i] );
363             }
364             pl_item = NULL; //to prevent activating it
365         }
366     }
367     else
368         pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
369
370     playlist_Unlock( THEPL );
371
372     /* */
373     if( pl_item )
374         emit categoryActivated( pl_item, false );
375 }
376
377 PLSelItem * PLSelector::addItem (
378     SelectorItemType type, const char* str, bool drop,
379     QTreeWidgetItem* parentItem )
380 {
381   QTreeWidgetItem *item = parentItem ?
382       new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
383
384   PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
385   setItemWidget( item, 0, selItem );
386   item->setData( 0, TYPE_ROLE, (int)type );
387   if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
388
389   return selItem;
390 }
391
392 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
393 {
394     vlc_gc_incref( p_item->p_input );
395
396     char *psz_name = input_item_GetName( p_item->p_input );
397     PLSelItem *item = addItem( PL_ITEM_TYPE,  psz_name, false, podcastsParent );
398     free( psz_name );
399
400     item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
401     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
402     item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
403     item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
404     CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
405     return item;
406 }
407
408 QStringList PLSelector::mimeTypes() const
409 {
410     QStringList types;
411     types << "vlc/qt-input-items";
412     return types;
413 }
414
415 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int,
416     const QMimeData * data, Qt::DropAction )
417 {
418     if( !parent ) return false;
419
420     QVariant type = parent->data( 0, TYPE_ROLE );
421     if( type == QVariant() ) return false;
422
423     int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
424     if( i_truth != IS_PL && i_truth != IS_ML ) return false;
425
426     bool to_pl = ( i_truth == IS_PL );
427
428     const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
429     if( !plMimeData ) return false;
430
431     QList<input_item_t*> inputItems = plMimeData->inputItems();
432
433     playlist_Lock( THEPL );
434
435     foreach( input_item_t *p_input, inputItems )
436     {
437         playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
438         if( !p_item ) continue;
439
440         playlist_NodeAddCopy( THEPL, p_item,
441                               to_pl ? THEPL->p_playing : THEPL->p_media_library,
442                               PLAYLIST_END );
443     }
444
445     playlist_Unlock( THEPL );
446
447     return true;
448 }
449
450 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
451 {
452     event->setDropAction( Qt::CopyAction );
453     QAbstractItemView::dragMoveEvent( event );
454 }
455
456 void PLSelector::plItemAdded( int item, int parent )
457 {
458     updateTotalDuration(playlistItem, "Playlist");
459     if( parent != podcastsParentId || podcastsParent == NULL ) return;
460
461     playlist_Lock( THEPL );
462
463     playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
464     if( !p_item ) {
465         playlist_Unlock( THEPL );
466         return;
467     }
468
469     int c = podcastsParent->childCount();
470     for( int i = 0; i < c; i++ )
471     {
472         QTreeWidgetItem *podItem = podcastsParent->child(i);
473         if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
474         {
475           //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
476           playlist_Unlock( THEPL );
477           return;
478         }
479     }
480
481     //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
482     addPodcastItem( p_item );
483
484     playlist_Unlock( THEPL );
485
486     podcastsParent->setExpanded( true );
487 }
488
489 void PLSelector::plItemRemoved( int id )
490 {
491     updateTotalDuration(playlistItem, "Playlist");
492     if( !podcastsParent ) return;
493
494     int c = podcastsParent->childCount();
495     for( int i = 0; i < c; i++ )
496     {
497         QTreeWidgetItem *item = podcastsParent->child(i);
498         if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
499         {
500             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
501             //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
502             vlc_gc_decref( p_input );
503             delete item;
504             return;
505         }
506     }
507 }
508
509 void PLSelector::inputItemUpdate( input_item_t *arg )
510 {
511     updateTotalDuration(playlistItem, "Playlist");
512
513     if( podcastsParent == NULL )
514         return;
515
516     int c = podcastsParent->childCount();
517     for( int i = 0; i < c; i++ )
518     {
519         QTreeWidgetItem *item = podcastsParent->child(i);
520         input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
521         if( p_input == arg )
522         {
523             PLSelItem *si = itemWidget( item );
524             char *psz_name = input_item_GetName( p_input );
525             si->setText( qfu( psz_name ) );
526             free( psz_name );
527             return;
528         }
529     }
530 }
531
532 void PLSelector::podcastAdd( PLSelItem * )
533 {
534     assert( podcastsParent );
535
536     bool ok;
537     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
538                                          qtr( "Enter URL of the podcast to subscribe to:" ),
539                                          QLineEdit::Normal, QString(), &ok );
540     if( !ok || url.isEmpty() ) return;
541
542     setSource( podcastsParent ); //to load the SD in case it's not loaded
543
544     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name( p_intf->p_libvlc, "podcast" );
545     if( !p_obj ) return;
546
547     QString request("ADD:");
548     request += url.trimmed();
549     var_SetString( p_obj, "podcast-request", qtu( request ) );
550     vlc_object_release( p_obj );
551 }
552
553 void PLSelector::podcastRemove( PLSelItem* item )
554 {
555     QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
556     question = question.arg( item->text() );
557     QMessageBox::StandardButton res =
558         QMessageBox::question( this, qtr( "Unsubscribe" ), question,
559                                QMessageBox::Yes | QMessageBox::No,
560                                QMessageBox::No );
561     if( res == QMessageBox::No ) return;
562
563     input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
564     if( !input ) return;
565
566     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
567         p_intf->p_libvlc, "podcast" );
568     if( !p_obj ) return;
569
570     QString request("RM:");
571     char *psz_uri = input_item_GetURI( input );
572     request += qfu( psz_uri );
573     var_SetString( p_obj, "podcast-request", qtu( request ) );
574     vlc_object_release( p_obj );
575     free( psz_uri );
576 }
577
578 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
579 {
580     return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
581 }
582
583 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
584 {
585     if( !model()->hasChildren( index ) ) return;
586     QStyleOption option;
587     option.initFrom( this );
588     option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
589     style()->drawPrimitive( isExpanded( index ) ?
590                             QStyle::PE_IndicatorArrowDown :
591                             QStyle::PE_IndicatorArrowRight, &option, painter );
592 }
593
594 void PLSelector::getCurrentItemInfos( int* type, bool* can_delay_search, QString *string)
595 {
596     *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
597     *string = currentItem()->data( 0, NAME_ROLE ).toString();
598     *can_delay_search = currentItem()->data( 0, CAP_SEARCH_ROLE ).toBool();
599 }
600
601 int PLSelector::getCurrentItemCategory()
602 {
603     return currentItem()->data( 0, SPECIAL_ROLE ).toInt();
604 }
605
606 void PLSelector::wheelEvent( QWheelEvent *e )
607 {
608     if( verticalScrollBar()->isVisible() && (
609         (verticalScrollBar()->value() != verticalScrollBar()->minimum() && e->delta() >= 0 ) ||
610         (verticalScrollBar()->value() != verticalScrollBar()->maximum() && e->delta() < 0 )
611         ) )
612         QApplication::sendEvent(verticalScrollBar(), e);
613
614     // Accept this event in order to prevent unwanted volume up/down changes
615     e->accept();
616 }