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