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