]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
remove that kludge to use playlistwidget for getting art for
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 #include "qt4.hpp"
26 #include "main_interface.hpp"
27 #include "input_manager.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "components/playlist/playlist.hpp"
33 #include "dialogs/extended.hpp"
34 #include "dialogs/playlist.hpp"
35 #include "menus.hpp"
36
37 #include <QMenuBar>
38 #include <QCloseEvent>
39 #include <QPushButton>
40 #include <QStatusBar>
41 #include <QKeyEvent>
42 #include <QUrl>
43 #include <QSystemTrayIcon>
44 #include <QSize>
45 #include <QMenu>
46 #include <QLabel>
47 #include <QSlider>
48 #include <QWidgetAction>
49 #include <QDockWidget>
50 #include <QToolBar>
51 #include <QGroupBox>
52
53 #include <assert.h>
54 #include <vlc_keys.h>
55 #include <vlc_vout.h>
56
57 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
58 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
59 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
60
61 #define DS(i) i.width(),i.height()
62
63 /* Callback prototypes */
64 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
65                         vlc_value_t old_val, vlc_value_t new_val, void *param );
66 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
67                        vlc_value_t old_val, vlc_value_t new_val, void *param );
68 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
69                              vlc_value_t, void *);
70 /* Video handling */
71 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
72                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
73 {
74     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
75 }
76 static void DoRelease( intf_thread_t *p_intf, void *p_win )
77 {
78     return p_intf->p_sys->p_mi->releaseVideo( p_win );
79 }
80 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
81 {
82     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
83 }
84
85 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
86 {
87     /* Variables initialisation */
88     // need_components_update = false;
89     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
90     videoIsActive = false;
91     input_name = "";
92
93     /**
94      * Ask for the network policy on FIRST STARTUP
95      **/
96     if( config_GetInt( p_intf, "privacy-ask") )
97     {
98         QList<ConfigControl *> controls;
99         if( privacyDialog( controls ) == QDialog::Accepted )
100         {
101             QList<ConfigControl *>::Iterator i;
102             for(  i = controls.begin() ; i != controls.end() ; i++ )
103             {
104                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
105                 c->doApply( p_intf );
106             }
107
108             config_PutInt( p_intf,  "privacy-ask" , 0 );
109             config_SaveConfigFile( p_intf, NULL );
110         }
111     }
112
113     /**
114      *  Configuration and settings
115      **/
116     settings = new QSettings( "vlc", "vlc-qt-interface" );
117     settings->beginGroup( "MainWindow" );
118
119     /* Main settings */
120     setFocusPolicy( Qt::StrongFocus );
121     setAcceptDrops( true );
122     setWindowIcon( QApplication::windowIcon() );
123     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
124
125     /* Set The Video In emebedded Mode or not */
126     videoEmbeddedFlag = false;
127     if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
128
129     /* Are we in the enhanced always-video mode or not ? */
130     alwaysVideoFlag = false;
131     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
132         alwaysVideoFlag = true;
133
134     /* Set the other interface settings */
135     //TODO: I don't like that code
136     visualSelectorEnabled = settings->value( "visual-selector", false ).toBool();
137     notificationEnabled = config_GetInt( p_intf, "qt-notification" )
138                           ? true : false;
139
140     /**************************
141      *  UI and Widgets design
142      **************************/
143     setVLCWindowsTitle();
144     handleMainUi( settings );
145
146     /* Create a Dock to get the playlist */
147     dockPL = new QDockWidget( qtr( "Playlist" ), this );
148     dockPL->setSizePolicy( QSizePolicy::Preferred,
149                            QSizePolicy::MinimumExpanding );
150     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
151     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
152                            | Qt::RightDockWidgetArea
153                            | Qt::BottomDockWidgetArea );
154
155     /************
156      * Menu Bar
157      ************/
158     QVLCMenu::createMenuBar( this, p_intf, visualSelectorEnabled );
159
160     /****************
161      *  Status Bar  *
162      ****************/
163
164     /* Widgets Creation*/
165     b_remainingTime = false;
166     timeLabel = new TimeLabel;
167     nameLabel = new QLabel;
168     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
169                                       | Qt::TextSelectableByKeyboard );
170     speedLabel = new QLabel( "1.00x" );
171     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
172
173     /* Styling those labels */
174     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
175     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
176     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
177
178     /* and adding those */
179     statusBar()->addWidget( nameLabel, 8 );
180     statusBar()->addPermanentWidget( speedLabel, 0 );
181     statusBar()->addPermanentWidget( timeLabel, 2 );
182
183     /* timeLabel behaviour:
184        - double clicking opens the goto time dialog
185        - right-clicking and clicking just toggle between remaining and
186          elapsed time.*/
187     CONNECT( timeLabel, timeLabelClicked(), this, toggleTimeDisplay() );
188     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
189     CONNECT( timeLabel, timeLabelDoubleClicked(), this, toggleTimeDisplay() );
190
191     /* Speed Label behaviour:
192        - right click gives the vertical speed slider */
193     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
194              this, showSpeedMenu( QPoint ) );
195
196     /**********************
197      * Systray Management *
198      **********************/
199     sysTray = NULL;
200     bool b_createSystray = false;
201     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
202     if( config_GetInt( p_intf, "qt-start-minimized") )
203     {
204         if( b_systrayAvailable ){
205             b_createSystray = true;
206             hide(); //FIXME BUG HERE
207         }
208         else msg_Warn( p_intf, "You can't minize if you haven't a system "
209                 "tray bar" );
210     }
211     if( config_GetInt( p_intf, "qt-system-tray") )
212         b_createSystray = true;
213
214     if( b_systrayAvailable && b_createSystray )
215             createSystray();
216
217     if( config_GetInt( p_intf, "qt-minimal-view" ) )
218         toggleMinimalView();
219
220     /* Init input manager */
221     MainInputManager::getInstance( p_intf );
222     ON_TIMEOUT( updateOnTimer() );
223     //ON_TIMEOUT( debug() ):;
224
225     /********************
226      * Various CONNECTs *
227      ********************/
228
229     /* Connect the input manager to the GUI elements it manages */
230     /* It is also connected to the control->slider, see the ControlsWidget */
231     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
232              this, setDisplayPosition( float, int, int ) );
233
234     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
235
236     /**
237      * Connects on nameChanged()
238      */
239     /* Naming in the controller statusbar */
240     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
241              setName( QString ) );
242     /* and in the systray */
243     if( sysTray )
244     {
245         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
246                  updateSystrayTooltipName( QString ) );
247     }
248     /* and in the title of the controller */
249     if( config_GetInt( p_intf, "qt-name-in-title" ) )
250     {
251         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
252              setVLCWindowsTitle( QString ) );
253     }
254
255     /** CONNECTS on PLAY_STATUS **/
256     /* Status on the main controller */
257     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
258     /* and in the systray */
259     if( sysTray )
260     {
261         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
262                  updateSystrayTooltipStatus( int ) );
263     }
264
265     /**
266      * Callbacks
267      **/
268     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
269     var_AddCallback( p_intf, "interaction", InteractCallback, this );
270     p_intf->b_interaction = VLC_TRUE;
271
272     /* Register callback for the intf-popupmenu variable */
273     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
274                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
275     if( p_playlist != NULL )
276     {
277         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
278         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
279         vlc_object_release( p_playlist );
280     }
281
282     CONNECT( this, askReleaseVideo( void * ), this, releaseVideoSlot( void * ) );
283
284     CONNECT( dockPL, topLevelChanged( bool ), this, doComponentsUpdate() );
285     CONNECT( controls, advancedControlsToggled( bool ), 
286              this, doComponentsUpdate() );
287
288     updateGeometry();
289     settings->endGroup();
290 }
291
292 MainInterface::~MainInterface()
293 {
294     if( playlistWidget ) playlistWidget->savingSettings( settings );
295     if( ExtendedDialog::exists() )
296         ExtendedDialog::getInstance( p_intf )->savingSettings();
297
298     settings->beginGroup( "MainWindow" );
299     settings->setValue( "playlist-floats", dockPL->isFloating() );
300     settings->setValue( "adv-controls", 
301                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
302     settings->setValue( "pos", pos() );
303
304     settings->endGroup();
305     delete settings;
306
307     /* Unregister callback for the intf-popupmenu variable */
308     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
309                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
310     if( p_playlist != NULL )
311     {
312         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
313         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
314         vlc_object_release( p_playlist );
315     }
316
317     p_intf->b_interaction = VLC_FALSE;
318     var_DelCallback( p_intf, "interaction", InteractCallback, this );
319
320     p_intf->pf_request_window = NULL;
321     p_intf->pf_release_window = NULL;
322     p_intf->pf_control_window = NULL;
323 }
324
325 /*****************************
326  *   Main UI handling        *
327  *****************************/
328
329 /**
330  * Give the decorations of the Main Window a correct Name.
331  * If nothing is given, set it to VLC...
332  **/
333 void MainInterface::setVLCWindowsTitle( QString aTitle )
334 {
335     if( aTitle.isEmpty() )
336     {
337         setWindowTitle( qtr( "VLC media player" ) );
338     }
339     else
340     {
341         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
342     }
343 }
344
345 void MainInterface::handleMainUi( QSettings *settings )
346 {
347     /* Create the main Widget and the mainLayout */
348     QWidget *main = new QWidget;
349     setCentralWidget( main );
350     mainLayout = new QVBoxLayout( main );
351
352     /* Margins, spacing */
353     main->setContentsMargins( 0, 0, 0, 0 );
354     mainLayout->setMargin( 0 );
355
356     /* Create the CONTROLS Widget */
357     /* bool b_shiny = config_GetInt( p_intf, "qt-blingbling" ); */
358     controls = new ControlsWidget( p_intf, this,
359                    settings->value( "adv-controls", false ).toBool(),
360                    config_GetInt( p_intf, "qt-blingbling" ) );
361
362     /* Add the controls Widget to the main Widget */
363     mainLayout->insertWidget( 0, controls );
364
365     /* Create the Speed Control Widget */
366     speedControl = new SpeedControlWidget( p_intf );
367     speedControlMenu = new QMenu( this );
368
369     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
370     widgetAction->setDefaultWidget( speedControl );
371     speedControlMenu->addAction( widgetAction );
372
373     /* Visualisation */
374     /* Disabled for now, they SUCK */
375     #if 0
376     visualSelector = new VisualSelector( p_intf );
377     mainLayout->insertWidget( 0, visualSelector );
378     visualSelector->hide();
379     #endif
380
381     /* And video Outputs */
382     if( alwaysVideoFlag )
383     {
384         bgWidget = new BackgroundWidget( p_intf );
385         bgWidget->widgetSize = settings->value( "backgroundSize",
386                                            QSize( 300, 200 ) ).toSize();
387         bgWidget->resize( bgWidget->widgetSize );
388         bgWidget->updateGeometry();
389         mainLayout->insertWidget( 0, bgWidget );
390         CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
391     }
392
393     if( videoEmbeddedFlag )
394     {
395         videoWidget = new VideoWidget( p_intf );
396         videoWidget->widgetSize = QSize( 1, 1 );
397         //videoWidget->resize( videoWidget->widgetSize );
398         mainLayout->insertWidget( 0, videoWidget );
399
400         p_intf->pf_request_window  = ::DoRequest;
401         p_intf->pf_release_window  = ::DoRelease;
402         p_intf->pf_control_window  = ::DoControl;
403     }
404
405     /* Finish the sizing */
406     updateGeometry();
407 }
408
409 int MainInterface::privacyDialog( QList<ConfigControl *> controls )
410 {
411     QDialog *privacy = new QDialog( this );
412
413     privacy->setWindowTitle( qtr( "Privacy and Network policies" ) );
414
415     QGridLayout *gLayout = new QGridLayout( privacy );
416
417     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
418     QGridLayout *blablaLayout = new QGridLayout( blabla );
419     QLabel *text = new QLabel( qtr(
420         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
421         "online without authorisation.</p>\n "
422         "<p><i>VLC media player</i> can request limited information on "
423         "Internet, espically to get CD Covers and songs metadata or to know "
424         "if updates are available.</p>\n"
425         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
426         "information, even anonymously about your "
427         "usage.</p>\n"
428         "<p>Therefore please check the following options, the default being "
429         "almost no access on the web.</p>\n") );
430     text->setWordWrap( true );
431     text->setTextFormat( Qt::RichText );
432
433     blablaLayout->addWidget( text, 0, 0 ) ;
434
435     QGroupBox *options = new QGroupBox;
436     QGridLayout *optionsLayout = new QGridLayout( options );
437
438     gLayout->addWidget( blabla, 0, 0, 1, 3 );
439     gLayout->addWidget( options, 1, 0, 1, 3 );
440     module_config_t *p_config;
441     ConfigControl *control;
442     int line = 0;
443
444 #define CONFIG_GENERIC( option, type )                            \
445     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
446     if( p_config )                                                \
447     {                                                             \
448         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
449                 p_config, options, false, optionsLayout, line );  \
450         controls.append( control );                               \
451     }
452
453 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
454     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
455     if( p_config )                                                \
456     {                                                             \
457         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
458                 p_config, options, optionsLayout, line );  \
459         controls.append( control );                               \
460     }
461
462     CONFIG_GENERIC( "album-art", IntegerList ); line++;
463     CONFIG_GENERIC_NOBOOL( "fetch-meta", Bool ); line++;
464     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool );
465
466     QPushButton *ok = new QPushButton( qtr( "Ok" ) );
467
468     gLayout->addWidget( ok, 2, 2 );
469
470     CONNECT( ok, clicked(), privacy, accept() );
471     return privacy->exec();
472 }
473
474 //FIXME remove me at the end...
475 void MainInterface::debug()
476 {
477     msg_Dbg( p_intf, "size: %i - %i", controls->size().height(), controls->size().width() );
478     msg_Dbg( p_intf, "sizeHint: %i - %i", controls->sizeHint().height(), controls->sizeHint().width() );
479 }
480
481 /**********************************************************************
482  * Handling of sizing of the components
483  **********************************************************************/
484
485 /* This function is probably wrong, but we don't have many many choices...
486    Since we can't know from the playlist Widget if we are inside a dock or not,
487    because the playlist Widget can be called by THEDP, as a separate windows for
488    the skins.
489    Maybe the other solution is to redefine the sizeHint() of the playlist and
490    ask _parent->isFloating()...
491    If you think this would be better, please FIXME it...
492 */
493 QSize MainInterface::sizeHint() const
494 {
495     QSize tempSize = controls->sizeHint() +
496         QSize( 100, menuBar()->size().height() + statusBar()->size().height() );
497
498     if( VISIBLE( bgWidget ) )
499         tempSize += bgWidget->sizeHint();
500     else if( videoIsActive )
501         tempSize += videoWidget->size();
502
503     if( !dockPL->isFloating() && dockPL->widget() )
504         tempSize += dockPL->widget()->size();
505
506     return tempSize;
507 }
508
509 #if 0
510 /* FIXME This is dead code and need to be removed AT THE END */
511 void MainInterface::resizeEvent( QResizeEvent *e )
512 {
513     if( videoWidget )
514         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
515     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
516     {
517         SET_WH( videoWidget, e->size().width() - addSize.width(),
518                              e->size().height()  - addSize.height() );
519         videoWidget->updateGeometry();
520     }
521     if( VISIBLE( playlistWidget ) )
522     {
523 //        SET_WH( playlistWidget , e->size().width() - addSize.width(),
524               //                   e->size().height() - addSize.height() );
525         playlistWidget->updateGeometry();
526     }
527 }
528 #endif
529
530 /****************************************************************************
531  * Small right-click menu for rate control
532  ****************************************************************************/
533 void MainInterface::showSpeedMenu( QPoint pos )
534 {
535     speedControlMenu->exec( QCursor::pos() - pos
536                           + QPoint( 0, speedLabel->height() ) );
537 }
538
539 /****************************************************************************
540  * Video Handling
541  ****************************************************************************/
542 class SetVideoOnTopQtEvent : public QEvent
543 {
544 public:
545     SetVideoOnTopQtEvent( bool _onTop ) :
546       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
547     {}
548
549     bool OnTop() const
550     {
551         return onTop;
552     }
553
554 private:
555     bool onTop;
556 };
557
558 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
559                                    int *pi_y, unsigned int *pi_width,
560                                    unsigned int *pi_height )
561 {
562     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
563     if( ret )
564     {
565         videoIsActive = true;
566         bool bgWasVisible = false;
567
568         /* Did we have a bg ? */
569         if( VISIBLE( bgWidget) )
570         {
571             bgWasVisible = true;
572             emit askBgWidgetToToggle();
573         }
574
575         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
576         {
577             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
578         }
579         else /* Background widget available, use its size */
580         {
581             /* Ok, our visualizations are bad, so don't do this for the moment
582              * use the requested size anyway */
583             // videoWidget->widgetSize = bgWidget->widgeTSize;
584             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
585         }
586         videoWidget->updateGeometry(); // Needed for deinterlace
587         updateGeometry();
588     }
589     return ret;
590 }
591
592 void MainInterface::releaseVideo( void *p_win )
593 {
594     emit askReleaseVideo( p_win );
595 }
596
597 void MainInterface::releaseVideoSlot( void *p_win )
598 {
599     videoWidget->release( p_win );
600     videoWidget->hide();
601
602     if( bgWidget )
603         bgWidget->show();
604
605     videoIsActive = false;
606     updateGeometry();
607 }
608
609 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
610 {
611     int i_ret = VLC_EGENERIC;
612     switch( i_query )
613     {
614         case VOUT_GET_SIZE:
615         {
616             unsigned int *pi_width  = va_arg( args, unsigned int * );
617             unsigned int *pi_height = va_arg( args, unsigned int * );
618             *pi_width = videoWidget->widgetSize.width();
619             *pi_height = videoWidget->widgetSize.height();
620             i_ret = VLC_SUCCESS;
621             break;
622         }
623         case VOUT_SET_SIZE:
624         {
625             unsigned int i_width  = va_arg( args, unsigned int );
626             unsigned int i_height = va_arg( args, unsigned int );
627             videoWidget->widgetSize = QSize( i_width, i_height );
628             videoWidget->updateGeometry();
629             updateGeometry();
630             i_ret = VLC_SUCCESS;
631             break;
632         }
633         case VOUT_SET_STAY_ON_TOP:
634         {
635             int i_arg = va_arg( args, int );
636             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
637             i_ret = VLC_SUCCESS;
638             break;
639         }
640         default:
641             msg_Warn( p_intf, "unsupported control query" );
642             break;
643     }
644     return i_ret;
645 }
646
647 /*****************************************************************************
648  * Playlist, Visualisation and Menus handling
649  *****************************************************************************/
650 /**
651  * Toggle the playlist widget or dialog
652  **/
653 void MainInterface::togglePlaylist()
654 {
655     /* CREATION
656     If no playlist exist, then create one and attach it to the DockPL*/
657     if( !playlistWidget )
658     {
659         msg_Dbg( p_intf, "Creating a new playlist" );
660         playlistWidget = new PlaylistWidget( p_intf, settings );
661
662         /* Add it to the parent DockWidget */
663         dockPL->setWidget( playlistWidget );
664
665         /* Add the dock to the main Interface */
666         addDockWidget( Qt::BottomDockWidgetArea, dockPL );
667
668         /* Make the playlist floating is requested. Default is not. */
669         if( settings->value( "playlist-floats", false ).toBool() );
670         {
671             msg_Dbg( p_intf, "we don't want it inside");
672             dockPL->setFloating( true );
673         }
674     }
675     else
676     {
677     /* toggle the visibility of the playlist */
678        TOGGLEV( dockPL );
679        //resize(sizeHint());
680     }
681 #if 0
682     doComponentsUpdate();
683 #endif
684     updateGeometry();
685 }
686
687 /* Function called from the menu to undock the playlist */
688 void MainInterface::undockPlaylist()
689 {
690     dockPL->setFloating( true );
691     updateGeometry();
692 }
693
694 void MainInterface::toggleMinimalView()
695 {
696     TOGGLEV( menuBar() );
697     TOGGLEV( controls );
698     TOGGLEV( statusBar() );
699     updateGeometry();
700 }
701
702 /* Video widget cannot do this synchronously as it runs in another thread */
703 /* Well, could it, actually ? Probably dangerous ... */
704 void MainInterface::doComponentsUpdate()
705 {
706     updateGeometry();
707 }
708
709 /* toggling advanced controls buttons */
710 void MainInterface::toggleAdvanced()
711 {
712     controls->toggleAdvanced();
713 }
714
715 /* Get the visibility status of the controls (hidden or not, advanced or not) */
716 int MainInterface::getControlsVisibilityStatus()
717 {
718     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
719                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
720 }
721
722 #if 0
723 void MainInterface::visual()
724 {
725     if( !VISIBLE( visualSelector) )
726     {
727         visualSelector->show();
728         if( !THEMIM->getIM()->hasVideo() )
729         {
730             /* Show the background widget */
731         }
732         visualSelectorEnabled = true;
733     }
734     else
735     {
736         /* Stop any currently running visualization */
737         visualSelector->hide();
738         visualSelectorEnabled = false;
739     }
740     doComponentsUpdate();
741 }
742 #endif
743
744 /************************************************************************
745  * Other stuff
746  ************************************************************************/
747 void MainInterface::setDisplayPosition( float pos, int time, int length )
748 {
749     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
750     secstotimestr( psz_length, length );
751     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
752                                                            : time );
753
754     QString title;
755     title.sprintf( "%s/%s", psz_time,
756                             ( !length && time ) ? "--:--" : psz_length );
757
758     /* Add a minus to remaining time*/
759     if( b_remainingTime && length ) timeLabel->setText( " -"+title+" " );
760     else timeLabel->setText( " "+title+" " );
761 }
762
763 void MainInterface::toggleTimeDisplay()
764 {
765     b_remainingTime = !b_remainingTime;
766     //b_remainingTime = ( b_remainingTime ? false : true );
767 }
768
769 void MainInterface::setName( QString name )
770 {
771     input_name = name; /* store it for the QSystray use */
772     /* Display it in the status bar, but also as a Tooltip in case it doesn't
773        fit in the label */
774     nameLabel->setText( " " + name + " " );
775     nameLabel->setToolTip( " " + name +" " );
776 }
777
778 void MainInterface::setStatus( int status )
779 {
780     /* Forward the status to the controls to toggle Play/Pause */
781     controls->setStatus( status );
782     /* And in the systray for the menu */
783     if( sysTray )
784         QVLCMenu::updateSystrayMenu( this, p_intf );
785 }
786
787 void MainInterface::setRate( int rate )
788 {
789     QString str;
790     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
791     str.append( "x" );
792     speedLabel->setText( str );
793     speedControl->updateControls( rate );
794 }
795
796 void MainInterface::updateOnTimer()
797 {
798 #if 0
799     if( intf_ShouldDie( p_intf ) )
800     {
801         QApplication::closeAllWindows();
802         QApplication::quit();
803     }
804     if( need_components_update )
805     {
806         doComponentsUpdate();
807         need_components_update = false;
808     }
809 #endif
810
811     controls->updateOnTimer();
812 }
813
814 /*****************************************************************************
815  * Systray Icon and Systray Menu
816  *****************************************************************************/
817
818 /**
819  * Create a SystemTray icon and a menu that would go with it.
820  * Connects to a click handler on the icon.
821  **/
822 void MainInterface::createSystray()
823 {
824     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
825     sysTray = new QSystemTrayIcon( iconVLC, this );
826     sysTray->setToolTip( qtr( "VLC media player" ));
827
828     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
829     systrayMenu->setIcon( iconVLC );
830
831     QVLCMenu::updateSystrayMenu( this, p_intf, true );
832     sysTray->show();
833
834     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
835             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
836 }
837
838 /**
839  * Updates the Systray Icon's menu and toggle the main interface
840  */
841 void MainInterface::toggleUpdateSystrayMenu()
842 {
843     /* If hidden, show it */
844     if( isHidden() )
845     {
846         show();
847         activateWindow();
848     }
849     else if( isMinimized() )
850     {
851         /* Minimized */
852         showNormal();
853         activateWindow();
854     }
855     else
856     {
857         /* Visible */
858 #ifdef WIN32
859         /* check if any visible window is above vlc in the z-order,
860          * but ignore the ones always on top */
861         WINDOWINFO wi;
862         HWND hwnd;
863         wi.cbSize = sizeof( WINDOWINFO );
864         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
865                 hwnd && !IsWindowVisible( hwnd );
866                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
867         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
868                 (wi.dwExStyle&WS_EX_TOPMOST) )
869 #else
870         if( isActiveWindow() )
871 #endif
872         {
873             hide();
874         }
875         else
876         {
877             activateWindow();
878         }
879     }
880     QVLCMenu::updateSystrayMenu( this, p_intf );
881 }
882
883 void MainInterface::handleSystrayClick(
884                                     QSystemTrayIcon::ActivationReason reason )
885 {
886     switch( reason )
887     {
888         case QSystemTrayIcon::Trigger:
889             toggleUpdateSystrayMenu();
890             break;
891         case QSystemTrayIcon::MiddleClick:
892             sysTray->showMessage( qtr( "VLC media player" ),
893                     qtr( "Control menu for the player" ),
894                     QSystemTrayIcon::Information, 3000 );
895             break;
896     }
897 }
898
899 /**
900  * Updates the name of the systray Icon tooltip.
901  * Doesn't check if the systray exists, check before you call it.
902  **/
903 void MainInterface::updateSystrayTooltipName( QString name )
904 {
905     if( name.isEmpty() )
906     {
907         sysTray->setToolTip( qtr( "VLC media player" ) );
908     }
909     else
910     {
911         sysTray->setToolTip( name );
912         if( notificationEnabled && ( isHidden() || isMinimized() ) )
913         {
914             sysTray->showMessage( qtr( "VLC media player" ), name,
915                     QSystemTrayIcon::NoIcon, 3000 );
916         }
917     }
918 }
919
920 /**
921  * Updates the status of the systray Icon tooltip.
922  * Doesn't check if the systray exists, check before you call it.
923  **/
924 void MainInterface::updateSystrayTooltipStatus( int i_status )
925 {
926     switch( i_status )
927     {
928         case  0:
929         case  END_S:
930             {
931                 sysTray->setToolTip( qtr( "VLC media player" ) );
932                 break;
933             }
934         case PLAYING_S:
935             {
936                 sysTray->setToolTip( input_name );
937                 break;
938             }
939         case PAUSE_S:
940             {
941                 sysTray->setToolTip( input_name + " - "
942                         + qtr( "Paused") );
943                 break;
944             }
945     }
946 }
947
948 /************************************************************************
949  * D&D Events
950  ************************************************************************/
951 void MainInterface::dropEvent(QDropEvent *event)
952 {
953      const QMimeData *mimeData = event->mimeData();
954
955      /* D&D of a subtitles file, add it on the fly */
956      if( mimeData->urls().size() == 1 )
957      {
958         if( THEMIM->getIM()->hasInput() )
959         {
960             if( input_AddSubtitles( THEMIM->getInput(),
961                                     qtu( mimeData->urls()[0].toString() ),
962                                     VLC_TRUE ) )
963             {
964                 event->acceptProposedAction();
965                 return;
966             }
967         }
968      }
969      bool first = true;
970      foreach( QUrl url, mimeData->urls() ) {
971         QString s = url.toString();
972         if( s.length() > 0 ) {
973             playlist_Add( THEPL, qtu(s), NULL,
974                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
975                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
976             first = false;
977         }
978      }
979      event->acceptProposedAction();
980 }
981 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
982 {
983      event->acceptProposedAction();
984 }
985 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
986 {
987      event->acceptProposedAction();
988 }
989 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
990 {
991      event->accept();
992 }
993
994 /************************************************************************
995  * Events stuff
996  ************************************************************************/
997 void MainInterface::customEvent( QEvent *event )
998 {
999 #if 0
1000     if( event->type() == PLDockEvent_Type )
1001     {
1002         PlaylistDialog::killInstance();
1003         playlistEmbeddedFlag = true;
1004         menuBar()->clear();
1005         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1006         togglePlaylist();
1007     }
1008 #endif
1009     /*else */
1010     if ( event->type() == SetVideoOnTopEvent_Type )
1011     {
1012         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1013         if( p_event->OnTop() )
1014             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1015         else
1016             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1017         show(); /* necessary to apply window flags?? */
1018     }
1019 }
1020
1021 void MainInterface::keyPressEvent( QKeyEvent *e )
1022 {
1023     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() & Qt::Key_H )
1024           && menuBar()->isHidden() )
1025     {
1026         toggleMinimalView();
1027         e->accept();
1028     }
1029
1030     int i_vlck = qtEventToVLCKey( e );
1031     if( i_vlck > 0 )
1032     {
1033         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1034         e->accept();
1035     }
1036     else
1037         e->ignore();
1038 }
1039
1040 void MainInterface::wheelEvent( QWheelEvent *e )
1041 {
1042     int i_vlckey = qtWheelEventToVLCKey( e );
1043     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1044     e->accept();
1045 }
1046
1047 void MainInterface::closeEvent( QCloseEvent *e )
1048 {
1049     hide();
1050     vlc_object_kill( p_intf );
1051     QApplication::closeAllWindows();
1052     QApplication::quit();
1053 }
1054
1055 /*****************************************************************************
1056  * Callbacks
1057  *****************************************************************************/
1058 static int InteractCallback( vlc_object_t *p_this,
1059                              const char *psz_var, vlc_value_t old_val,
1060                              vlc_value_t new_val, void *param )
1061 {
1062     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1063     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1064     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1065     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1066     return VLC_SUCCESS;
1067 }
1068
1069 /*****************************************************************************
1070  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1071  *  We don't show the menu directly here because we don't want the
1072  *  caller to block for a too long time.
1073  *****************************************************************************/
1074 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1075                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1076 {
1077     intf_thread_t *p_intf = (intf_thread_t *)param;
1078
1079     if( p_intf->pf_show_dialog )
1080     {
1081         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1082                                 new_val.b_bool, 0 );
1083     }
1084
1085     return VLC_SUCCESS;
1086 }
1087
1088 /*****************************************************************************
1089  * IntfShowCB: callback triggered by the intf-show playlist variable.
1090  *****************************************************************************/
1091 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1092                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1093 {
1094     intf_thread_t *p_intf = (intf_thread_t *)param;
1095     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1096
1097     return VLC_SUCCESS;
1098 }