]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4: fix race in requestVideo and simplify
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2009 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"
34 #include "actions_manager.hpp"
35
36 #include "util/customwidgets.hpp"
37 #include "util/qt_dirs.hpp"
38
39 #include "components/interface_widgets.hpp"
40 #include "components/controller.hpp"
41 #include "components/playlist/playlist.hpp"
42 #include "dialogs/external.hpp"
43
44 #include "menus.hpp"
45 #include "recents.hpp"
46
47 #include <QCloseEvent>
48 #include <QKeyEvent>
49
50 #include <QUrl>
51 #include <QSize>
52 #include <QDate>
53
54 #include <QMenu>
55 #include <QMenuBar>
56 #include <QStatusBar>
57 #include <QLabel>
58 #include <QGroupBox>
59 #include <QPushButton>
60
61 #include <assert.h>
62
63 #include <vlc_keys.h> /* Wheel event */
64 #include <vlc_vout_window.h>
65 #include <vlc_vout.h>
66
67 /* Callback prototypes */
68 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
69                         vlc_value_t old_val, vlc_value_t new_val, void *param );
70 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
71                        vlc_value_t old_val, vlc_value_t new_val, void *param );
72
73 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
74 {
75     /* Variables initialisation */
76     // need_components_update = false;
77     bgWidget             = NULL;
78     videoWidget          = NULL;
79     playlistWidget       = NULL;
80     sysTray              = NULL;
81     videoIsActive        = false;
82     playlistVisible      = false;
83     input_name           = "";
84     fullscreenControls   = NULL;
85     cryptedLabel         = NULL;
86     controls             = NULL;
87     inputC               = NULL;
88     b_shouldHide         = false;
89
90     bgWasVisible         = false;
91     i_bg_height          = 0;
92
93     /* Ask for privacy */
94     askForPrivacy();
95
96     /**
97      *  Configuration and settings
98      *  Pre-building of interface
99      **/
100     /* Main settings */
101     setFocusPolicy( Qt::StrongFocus );
102     setAcceptDrops( true );
103     setWindowIcon( QApplication::windowIcon() );
104     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
105
106     /* Set The Video In emebedded Mode or not */
107     videoEmbeddedFlag = config_GetInt( p_intf, "embedded-video" );
108
109     /* Does the interface resize to video size or the opposite */
110     b_keep_size = !config_GetInt( p_intf, "qt-video-autoresize" );
111
112     /* Are we in the enhanced always-video mode or not ? */
113     i_visualmode = config_GetInt( p_intf, "qt-display-mode" );
114
115     /* Set the other interface settings */
116     settings = getSettings();
117     settings->beginGroup( "MainWindow" );
118
119     /**
120      * Retrieve saved sizes for main window
121      *   mainBasedSize = based window size for normal mode
122      *                  (no video, no background)
123      *   mainVideoSize = window size with video (all modes)
124      **/
125     mainBasedSize = settings->value( "mainBasedSize", QSize( 350, 120 ) ).toSize();
126     mainVideoSize = settings->value( "mainVideoSize", QSize( 400, 300 ) ).toSize();
127
128     /* Do we want anoying popups or not */
129     notificationEnabled = (bool)config_GetInt( p_intf, "qt-notification" );
130
131     /**************
132      * Status Bar *
133      **************/
134     createStatusBar();
135
136     /**************************
137      *  UI and Widgets design
138      **************************/
139     setVLCWindowsTitle();
140     createMainWidget( settings );
141
142     /************
143      * Menu Bar *
144      ************/
145     QVLCMenu::createMenuBar( this, p_intf );
146     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
147              this, destroyPopupMenu() );
148
149 #if 0
150     /* Create a Dock to get the playlist */
151     dockPL = new QDockWidget( qtr( "Playlist" ), this );
152     dockPL->setSizePolicy( QSizePolicy::Preferred,
153                            QSizePolicy::Expanding );
154     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
155     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
156                            | Qt::RightDockWidgetArea
157                            | Qt::BottomDockWidgetArea );
158     dockPL->hide();
159 #endif
160
161     /********************
162      * Input Manager    *
163      ********************/
164     MainInputManager::getInstance( p_intf );
165
166     /*********************************
167      * Create the Systray Management *
168      *********************************/
169     initSystray();
170
171     /**************************
172      * Various CONNECTs on IM *
173      **************************/
174     /* Connect the input manager to the GUI elements it manages */
175
176     /**
177      * Connects on nameChanged()
178      * Those connects are not merged because different options can trigger
179      * them down.
180      */
181     /* Naming in the controller statusbar */
182     CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
183              this, setName( const QString& ) );
184     /* and in the systray */
185     if( sysTray )
186     {
187         CONNECT( THEMIM->getIM(), nameChanged( const QString& ), this,
188                  updateSystrayTooltipName( const QString& ) );
189     }
190     /* and in the title of the controller */
191     if( config_GetInt( p_intf, "qt-name-in-title" ) )
192     {
193         CONNECT( THEMIM->getIM(), nameChanged( const QString& ), this,
194              setVLCWindowsTitle( const QString& ) );
195     }
196
197     /**
198      * CONNECTS on PLAY_STATUS
199      **/
200     /* Status on the systray */
201     if( sysTray )
202     {
203         CONNECT( THEMIM->getIM(), statusChanged( int ),
204                  this, updateSystrayTooltipStatus( int ) );
205     }
206
207
208     /* END CONNECTS ON IM */
209
210     /************
211      * Callbacks
212      ************/
213     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
214
215     /* Register callback for the intf-popupmenu variable */
216     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
217
218
219     /* VideoWidget connects for asynchronous calls */
220     connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
221              this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
222              Qt::BlockingQueuedConnection );
223     connect( this, SIGNAL(askReleaseVideo( void )),
224              this, SLOT(releaseVideoSlot( void )),
225              Qt::BlockingQueuedConnection );
226
227     if( videoWidget )
228     {
229         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
230                  videoWidget, SetSizing( unsigned int, unsigned int ) );
231     }
232
233     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
234     CONNECT( THEDP, toolBarConfUpdated(), this, recreateToolbars() );
235
236     /* Size and placement of interface */
237     settings->beginGroup( "MainWindow" );
238     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
239
240     /* resize to previously saved main window size if appicable */
241     if( b_keep_size )
242     {
243        if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
244            i_visualmode == QT_MINIMAL_MODE )
245        {
246            resize( mainVideoSize );
247        }
248        else
249        {
250            resize( mainBasedSize );
251        }
252     }
253
254     bool b_visible = settings->value( "playlist-visible", 0 ).toInt();
255     settings->endGroup();
256
257     /* Playlist */
258     if( b_visible ) togglePlaylist();
259
260     /* Enable the popup menu in the MI */
261     setContextMenuPolicy( Qt::CustomContextMenu );
262     CONNECT( this, customContextMenuRequested( const QPoint& ),
263              this, popupMenu( const QPoint& ) );
264
265     /* Final sizing and showing */
266     setMinimumWidth( __MAX( controls->sizeHint().width(),
267                             menuBar()->sizeHint().width() ) );
268     setVisible( !b_shouldHide );
269
270     /* And switch to minimal view if needed
271        Must be called after the show() */
272     if( i_visualmode == QT_MINIMAL_MODE )
273         toggleMinimalView( true );
274
275     /* Update the geometry : It is useful if you switch between
276        qt-display-modes ?*/
277     updateGeometry();
278     resize( sizeHint() );
279
280 }
281
282 MainInterface::~MainInterface()
283 {
284     msg_Dbg( p_intf, "Destroying the main interface" );
285
286     /* Unsure we hide the videoWidget before destroying it */
287     if( videoIsActive ) videoWidget->hide();
288
289     /* Save playlist state */
290     if( playlistWidget )
291     {
292         if( !isDocked() )
293             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
294
295         delete playlistWidget;
296     }
297
298     /* Be sure to kill the actionsManager... FIXME */
299     ActionsManager::killInstance();
300
301     /* Delete the FSC controller */
302     delete fullscreenControls;
303
304     /* Save states */
305     settings->beginGroup( "MainWindow" );
306     settings->setValue( "pl-dock-status", (int)i_pl_dock );
307     settings->setValue( "playlist-visible", (int)playlistVisible );
308     settings->setValue( "adv-controls",
309                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
310
311     settings->setValue( "mainBasedSize", mainBasedSize );
312     settings->setValue( "mainVideoSize", mainVideoSize );
313
314     if( bgWidget )
315         settings->setValue( "backgroundSize", bgWidget->size() );
316
317     /* Save this size */
318     QVLCTools::saveWidgetPosition(settings, this);
319     settings->endGroup();
320
321
322     /* Unregister callbacks */
323     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
324     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
325
326     p_intf->p_sys->p_mi = NULL;
327 }
328
329 /*****************************
330  *   Main UI handling        *
331  *****************************/
332 void MainInterface::recreateToolbars()
333 {
334     settings->beginGroup( "MainWindow" );
335     delete controls;
336     delete inputC;
337     controls = new ControlsWidget( p_intf, false, this ); /* FIXME */
338     CONNECT( controls, advancedControlsToggled( bool ),
339              this, doComponentsUpdate() );
340     CONNECT( controls, sizeChanged(),
341              this, doComponentsUpdate() );
342     inputC = new InputControlsWidget( p_intf, this );
343
344     mainLayout->insertWidget( 2, inputC, 0, Qt::AlignBottom );
345     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
346                               controls, 0, Qt::AlignBottom );
347     settings->endGroup();
348 }
349
350 void MainInterface::createMainWidget( QSettings *settings )
351 {
352     /* Create the main Widget and the mainLayout */
353     QWidget *main = new QWidget;
354     setCentralWidget( main );
355     mainLayout = new QVBoxLayout( main );
356
357     /* Margins, spacing */
358     main->setContentsMargins( 0, 0, 0, 0 );
359     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
360     mainLayout->setSpacing( 0 );
361     mainLayout->setMargin( 0 );
362
363     /* Visualisation */
364     /* Disabled for now, they SUCK */
365     #if 0
366     visualSelector = new VisualSelector( p_intf );
367     mainLayout->insertWidget( 0, visualSelector );
368     visualSelector->hide();
369     #endif
370
371     /* Bg Cone */
372     bgWidget = new BackgroundWidget( p_intf );
373     bgWidget->resize(
374             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
375     bgWidget->updateGeometry();
376
377     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
378         i_visualmode != QT_MINIMAL_MODE )
379     {
380         bgWidget->hide();
381     }
382
383     /* And video Outputs */
384     if( videoEmbeddedFlag )
385         videoWidget = new VideoWidget( p_intf );
386
387     /* Create the CONTROLS Widget */
388     controls = new ControlsWidget( p_intf,
389                    settings->value( "adv-controls", false ).toBool(), this );
390     CONNECT( controls, advancedControlsToggled( bool ),
391              this, doComponentsUpdate() );
392     CONNECT( controls, sizeChanged(),
393              this, doComponentsUpdate() );
394     inputC = new InputControlsWidget( p_intf, this );
395
396
397     /* Add the controls Widget to the main Widget */
398     mainLayout->insertWidget( 0, bgWidget );
399     if( videoWidget ) mainLayout->insertWidget( 0, videoWidget, 10 );
400     mainLayout->insertWidget( 2, inputC, 0, Qt::AlignBottom );
401     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
402                               controls, 0, Qt::AlignBottom );
403
404     /* Finish the sizing */
405     main->updateGeometry();
406
407     getSettings()->endGroup();
408 #ifdef WIN32
409     if ( depth() > 8 )
410 #endif
411     /* Create the FULLSCREEN CONTROLS Widget */
412     if( config_GetInt( p_intf, "qt-fs-controller" ) )
413     {
414         fullscreenControls = new FullscreenControllerWidget( p_intf, this );
415     }
416 }
417
418 inline void MainInterface::createStatusBar()
419 {
420     /****************
421      *  Status Bar  *
422      ****************/
423     /* Widgets Creation*/
424     QStatusBar *statusBarr = statusBar();
425
426     TimeLabel *timeLabel = new TimeLabel( p_intf );
427     nameLabel = new QLabel( this );
428     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
429                                       | Qt::TextSelectableByKeyboard );
430     SpeedLabel *speedLabel = new SpeedLabel( p_intf, "1.00x", this );
431
432     /* Styling those labels */
433     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
434     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
435     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
436
437     /* and adding those */
438     statusBarr->addWidget( nameLabel, 8 );
439     statusBarr->addPermanentWidget( speedLabel, 0 );
440     statusBarr->addPermanentWidget( timeLabel, 0 );
441
442     /* timeLabel behaviour:
443        - double clicking opens the goto time dialog
444        - right-clicking and clicking just toggle between remaining and
445          elapsed time.*/
446     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
447
448     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
449              this, showCryptedLabel( bool ) );
450 }
451
452 inline void MainInterface::initSystray()
453 {
454     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
455     bool b_systrayWanted = config_GetInt( p_intf, "qt-system-tray" );
456
457     if( config_GetInt( p_intf, "qt-start-minimized") > 0 )
458     {
459         if( b_systrayAvailable )
460         {
461             b_systrayWanted = true;
462             b_shouldHide = true;
463         }
464         else
465             msg_Err( p_intf, "cannot start minimized without system tray bar" );
466     }
467
468     if( b_systrayAvailable && b_systrayWanted )
469             createSystray();
470 }
471
472 inline void MainInterface::askForPrivacy()
473 {
474     /**
475      * Ask for the network policy on FIRST STARTUP
476      **/
477     if( config_GetInt( p_intf, "qt-privacy-ask") )
478     {
479         QList<ConfigControl *> controls;
480         if( privacyDialog( &controls ) == QDialog::Accepted )
481         {
482             QList<ConfigControl *>::Iterator i;
483             for(  i = controls.begin() ; i != controls.end() ; i++ )
484             {
485                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
486                 c->doApply( p_intf );
487             }
488
489             config_PutInt( p_intf,  "qt-privacy-ask" , 0 );
490             /* We have to save here because the user may not launch Prefs */
491             config_SaveConfigFile( p_intf, NULL );
492         }
493     }
494 }
495
496 int MainInterface::privacyDialog( QList<ConfigControl *> *controls )
497 {
498     QDialog *privacy = new QDialog( this );
499
500     privacy->setWindowTitle( qtr( "Privacy and Network Policies" ) );
501
502     QGridLayout *gLayout = new QGridLayout( privacy );
503
504     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
505     QGridLayout *blablaLayout = new QGridLayout( blabla );
506     QLabel *text = new QLabel( qtr(
507         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
508         "online without authorization.</p>\n "
509         "<p><i>VLC media player</i> can retreive limited information from "
510         "the Internet in order to get CD covers or to check "
511         "for available updates.</p>\n"
512         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
513         "information, even anonymously, about your usage.</p>\n"
514         "<p>Therefore please select from the following options, the default being "
515         "almost no access to the web.</p>\n") );
516     text->setWordWrap( true );
517     text->setTextFormat( Qt::RichText );
518
519     blablaLayout->addWidget( text, 0, 0 ) ;
520
521     QGroupBox *options = new QGroupBox;
522     QGridLayout *optionsLayout = new QGridLayout( options );
523
524     gLayout->addWidget( blabla, 0, 0, 1, 3 );
525     gLayout->addWidget( options, 1, 0, 1, 3 );
526     module_config_t *p_config;
527     ConfigControl *control;
528     int line = 0;
529
530 #define CONFIG_GENERIC( option, type )                            \
531     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
532     if( p_config )                                                \
533     {                                                             \
534         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
535                 p_config, options, false, optionsLayout, line );  \
536         controls->append( control );                               \
537     }
538
539 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
540     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
541     if( p_config )                                                \
542     {                                                             \
543         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
544                 p_config, options, optionsLayout, line );  \
545         controls->append( control );                               \
546     }
547
548     CONFIG_GENERIC( "album-art", IntegerList ); line++;
549 #ifdef UPDATE_CHECK
550     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool ); line++;
551 #endif
552
553     QPushButton *ok = new QPushButton( qtr( "OK" ) );
554
555     gLayout->addWidget( ok, 2, 2 );
556
557     CONNECT( ok, clicked(), privacy, accept() );
558     return privacy->exec();
559 }
560
561
562 /**********************************************************************
563  * Handling of sizing of the components
564  **********************************************************************/
565
566 /* This function is probably wrong, but we don't have many many choices...
567    Since we can't know from the playlist Widget if we are inside a dock or not,
568    because the playlist Widget can be called by THEDP, as a separate windows for
569    the skins.
570    Maybe the other solution is to redefine the sizeHint() of the playlist and
571    ask _parent->isFloating()...
572    If you think this would be better, please FIXME it...
573 */
574
575 QSize MainInterface::sizeHint() const
576 {
577     if( b_keep_size )
578     {
579         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
580             i_visualmode == QT_MINIMAL_MODE )
581         {
582                 return mainVideoSize;
583         }
584         else
585         {
586             if( VISIBLE( bgWidget) ||
587                 ( videoIsActive && videoWidget->isVisible() )
588               )
589                 return mainVideoSize;
590             else
591                 return mainBasedSize;
592         }
593     }
594
595     int nwidth  = controls->sizeHint().width();
596     int nheight = controls->isVisible() ?
597                   controls->size().height()
598                   + inputC->size().height()
599                   + menuBar()->size().height()
600                   + statusBar()->size().height()
601                   : 0 ;
602
603     if( VISIBLE( bgWidget ) )
604     {
605         if( i_bg_height )
606             nheight += i_bg_height;
607         else
608             nheight += bgWidget->size().height();
609         nwidth  = __MAX( nwidth, bgWidget->size().width() );
610     }
611     else if( videoIsActive && videoWidget->isVisible() )
612     {
613         nheight += videoWidget->sizeHint().height();
614         nwidth  = __MAX( nwidth, videoWidget->sizeHint().width() );
615     }
616 #if 0
617     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
618     {
619         nheight += dockPL->size().height();
620         nwidth = __MAX( nwidth, dockPL->size().width() );
621         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
622     }
623 #endif
624     return QSize( nwidth, nheight );
625 }
626
627 /* Video widget cannot do this synchronously as it runs in another thread */
628 /* Well, could it, actually ? Probably dangerous ... */
629
630 /* This function is called:
631    - toggling of minimal View
632    - through askUpdate() by Vout thread request video and resize video (zoom)
633    - Advanced buttons toggled
634  */
635 void MainInterface::doComponentsUpdate()
636 {
637     if( isFullScreen() || isMaximized() ) return;
638
639     msg_Dbg( p_intf, "Updating the geometry" );
640     /* Here we resize to sizeHint() and not adjustsize because we want
641        the videoWidget to be exactly the correctSize */
642     resize( sizeHint() );
643     //    adjustSize()  ;
644 #ifndef NDEBUG
645     debug();
646 #endif
647 }
648
649 void MainInterface::debug()
650 {
651 #ifndef NDEBUG
652     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
653     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
654     if( videoWidget && videoWidget->isVisible() )
655     {
656         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
657         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
658     }
659 #endif
660 }
661
662 void MainInterface::destroyPopupMenu()
663 {
664     QVLCMenu::PopupMenu(p_intf, false );
665 }
666
667
668 void MainInterface::toggleFSC()
669 {
670    if( !fullscreenControls ) return;
671
672    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
673    QApplication::postEvent( fullscreenControls, eShow );
674 }
675
676 void MainInterface::popupMenu( const QPoint &p )
677 {
678     /* Ow, that's ugly: don't show the popup menu if cursor over
679      * the main menu bar or the status bar */
680     if( !childAt( p ) || ( ( childAt( p ) != menuBar() )
681                         && ( childAt( p )->parentWidget() != statusBar() ) ) )
682         QVLCMenu::PopupMenu( p_intf, true );
683 }
684
685 /****************************************************************************
686  * Video Handling
687  ****************************************************************************/
688
689 /* This event is used to deal with the fullscreen and always on top
690    issue conflict (bug in wx) */
691 class SetVideoOnTopQtEvent : public QEvent
692 {
693 public:
694     SetVideoOnTopQtEvent( bool _onTop ) :
695       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
696     {}
697
698     bool OnTop() const { return onTop; }
699
700 private:
701     bool onTop;
702 };
703
704 /**
705  * NOTE:
706  * You must note change the state of this object or other Qt4 UI objects,
707  * from the video output thread - only from the Qt4 UI main loop thread.
708  * All window provider queries must be handled through signals or events.
709  * That's why we have all those emit statements...
710  */
711 WId MainInterface::getVideo( int *pi_x, int *pi_y,
712                              unsigned int *pi_width, unsigned int *pi_height )
713 {
714     if( !videoWidget )
715         return 0;
716
717     /* This is a blocking call signal. Results are returned through pointers.
718      * Beware of deadlocks! */
719     WId id;
720     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
721     return id;
722 }
723
724 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
725                                   unsigned *pi_width, unsigned *pi_height )
726 {
727     /* Request the videoWidget */
728     WId ret = videoWidget->request( pi_x, pi_y,
729                                     pi_width, pi_height, b_keep_size );
730     *p_id = ret;
731     if( ret ) /* The videoWidget is available */
732     {
733         /* Did we have a bg ? Hide it! */
734         if( VISIBLE( bgWidget) )
735         {
736             bgWasVisible = true;
737             bgWidget->toggle();
738         }
739         else
740             bgWasVisible = false;
741
742         /* ask videoWidget to show */
743         videoWidget->SetSizing( *pi_width, *pi_height );
744
745         /* Consider the video active now */
746         videoIsActive = true;
747
748         emit askUpdate();
749     }
750 }
751
752 /* Asynchronous call from the WindowClose function */
753 void MainInterface::releaseVideo( void )
754 {
755     emit askReleaseVideo( );
756 }
757
758 /* Function that is CONNECTED to the previous emit */
759 void MainInterface::releaseVideoSlot( void )
760 {
761     videoWidget->release( );
762
763     if( bgWasVisible )
764     {
765         /* Reset the bg state */
766         bgWasVisible = false;
767         bgWidget->show();
768     }
769
770     videoIsActive = false;
771
772     /* Try to resize, except when you are in Fullscreen mode */
773     doComponentsUpdate();
774 }
775
776 /* Asynchronous call from WindowControl function */
777 int MainInterface::controlVideo( int i_query, va_list args )
778 {
779     switch( i_query )
780     {
781     case VOUT_WINDOW_SET_SIZE:
782     {
783         unsigned int i_width  = va_arg( args, unsigned int );
784         unsigned int i_height = va_arg( args, unsigned int );
785         emit askVideoToResize( i_width, i_height );
786         emit askUpdate();
787         return VLC_SUCCESS;
788     }
789     case VOUT_WINDOW_SET_ON_TOP:
790     {
791         int i_arg = va_arg( args, int );
792         QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
793         return VLC_SUCCESS;
794     }
795     default:
796         msg_Warn( p_intf, "unsupported control query" );
797         return VLC_EGENERIC;
798     }
799 }
800
801 /*****************************************************************************
802  * Playlist, Visualisation and Menus handling
803  *****************************************************************************/
804 /**
805  * Toggle the playlist widget or dialog
806  **/
807 void MainInterface::togglePlaylist()
808 {
809     /* CREATION
810     If no playlist exist, then create one and attach it to the DockPL*/
811     if( !playlistWidget )
812     {
813         playlistWidget = new PlaylistWidget( p_intf );
814
815         i_pl_dock = PL_UNDOCKED;
816 /*        i_pl_dock = (pl_dock_e)getSettings()
817                          ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
818
819         if( i_pl_dock == PL_UNDOCKED )
820         {
821             playlistWidget->setWindowFlags( Qt::Window );
822
823             /* This will restore the geometry but will not work for position,
824                because of parenting */
825             QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
826                     playlistWidget, QSize( 600, 300 ) );
827         }
828         else
829         {
830             mainLayout->insertWidget( 4, playlistWidget );
831         }
832         playlistVisible = true;
833
834         playlistWidget->show();
835     }
836     else
837     {
838     /* toggle the visibility of the playlist */
839        TOGGLEV( playlistWidget );
840        playlistVisible = !playlistVisible;
841        //doComponentsUpdate(); //resize( sizeHint() );
842     }
843 }
844
845 /* Function called from the menu to undock the playlist */
846 void MainInterface::undockPlaylist()
847 {
848 //    dockPL->setFloating( true );
849 //    adjustSize();
850 }
851
852 void MainInterface::dockPlaylist( pl_dock_e i_pos )
853 {
854 }
855
856 void MainInterface::toggleMinimalView( bool b_switch )
857 {
858     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
859         i_visualmode != QT_MINIMAL_MODE )
860     { /* NORMAL MODE then */
861         if( !videoWidget || videoWidget->isHidden() )
862         {
863             bgWidget->toggle();
864         }
865         else
866         {
867             /* If video is visible, then toggle the status of bgWidget */
868             bgWasVisible = !bgWasVisible;
869         }
870     }
871
872     i_bg_height = bgWidget->height();
873
874     menuBar()->setVisible( !b_switch );
875     controls->setVisible( !b_switch );
876     statusBar()->setVisible( !b_switch );
877     inputC->setVisible( !b_switch );
878
879     doComponentsUpdate();
880
881     emit minimalViewToggled( b_switch );
882 }
883
884 /* toggling advanced controls buttons */
885 void MainInterface::toggleAdvanced()
886 {
887     controls->toggleAdvanced();
888 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
889 }
890
891 /* Get the visibility status of the controls (hidden or not, advanced or not) */
892 int MainInterface::getControlsVisibilityStatus()
893 {
894     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
895                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
896 }
897
898 #if 0
899 void MainInterface::visual()
900 {
901     if( !VISIBLE( visualSelector) )
902     {
903         visualSelector->show();
904         if( !THEMIM->getIM()->hasVideo() )
905         {
906             /* Show the background widget */
907         }
908         visualSelectorEnabled = true;
909     }
910     else
911     {
912         /* Stop any currently running visualization */
913         visualSelector->hide();
914         visualSelectorEnabled = false;
915     }
916     doComponentsUpdate();
917 }
918 #endif
919
920 /************************************************************************
921  * Other stuff
922  ************************************************************************/
923 void MainInterface::setName( const QString& name )
924 {
925     input_name = name; /* store it for the QSystray use */
926     /* Display it in the status bar, but also as a Tooltip in case it doesn't
927        fit in the label */
928     nameLabel->setText( " " + name + " " );
929     nameLabel->setToolTip( " " + name +" " );
930 }
931
932 /**
933  * Give the decorations of the Main Window a correct Name.
934  * If nothing is given, set it to VLC...
935  **/
936 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
937 {
938     if( aTitle.isEmpty() )
939     {
940         setWindowTitle( qtr( "VLC media player" ) );
941     }
942     else
943     {
944         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
945     }
946 }
947
948 void MainInterface::showCryptedLabel( bool b_show )
949 {
950     if( cryptedLabel == NULL )
951     {
952         cryptedLabel = new QLabel;
953         // The lock icon is not the right one for DRM protection/scrambled.
954         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
955         cryptedLabel->setText( "DRM" );
956         statusBar()->addWidget( cryptedLabel );
957     }
958
959     cryptedLabel->setVisible( b_show );
960 }
961
962 /*****************************************************************************
963  * Systray Icon and Systray Menu
964  *****************************************************************************/
965
966 /**
967  * Create a SystemTray icon and a menu that would go with it.
968  * Connects to a click handler on the icon.
969  **/
970 void MainInterface::createSystray()
971 {
972     QIcon iconVLC;
973     if( QDate::currentDate().dayOfYear() >= 354 )
974         iconVLC =  QIcon( QPixmap( ":/logo/vlc128-christmas.png" ) );
975     else
976         iconVLC =  QIcon( QPixmap( ":/logo/vlc128.png" ) );
977     sysTray = new QSystemTrayIcon( iconVLC, this );
978     sysTray->setToolTip( qtr( "VLC media player" ));
979
980     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
981     systrayMenu->setIcon( iconVLC );
982
983     QVLCMenu::updateSystrayMenu( this, p_intf, true );
984     sysTray->show();
985
986     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
987             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
988 }
989
990 /**
991  * Updates the Systray Icon's menu and toggle the main interface
992  */
993 void MainInterface::toggleUpdateSystrayMenu()
994 {
995     /* If hidden, show it */
996     if( isHidden() )
997     {
998         show();
999         activateWindow();
1000     }
1001     else if( isMinimized() )
1002     {
1003         /* Minimized */
1004         showNormal();
1005         activateWindow();
1006     }
1007     else
1008     {
1009         /* Visible (possibly under other windows) */
1010 #ifdef WIN32
1011         /* check if any visible window is above vlc in the z-order,
1012          * but ignore the ones always on top
1013          * and the ones which can't be activated */
1014         WINDOWINFO wi;
1015         HWND hwnd;
1016         wi.cbSize = sizeof( WINDOWINFO );
1017         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
1018                 hwnd && ( !IsWindowVisible( hwnd ) ||
1019                     ( GetWindowInfo( hwnd, &wi ) &&
1020                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
1021                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
1022             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
1023                 (wi.dwExStyle&WS_EX_TOPMOST) )
1024             {
1025                 hide();
1026             }
1027             else
1028             {
1029                 activateWindow();
1030             }
1031 #else
1032         hide();
1033 #endif
1034     }
1035     QVLCMenu::updateSystrayMenu( this, p_intf );
1036 }
1037
1038 void MainInterface::handleSystrayClick(
1039                                     QSystemTrayIcon::ActivationReason reason )
1040 {
1041     switch( reason )
1042     {
1043         case QSystemTrayIcon::Trigger:
1044         case QSystemTrayIcon::DoubleClick:
1045             toggleUpdateSystrayMenu();
1046             break;
1047         case QSystemTrayIcon::MiddleClick:
1048             sysTray->showMessage( qtr( "VLC media player" ),
1049                     qtr( "Control menu for the player" ),
1050                     QSystemTrayIcon::Information, 3000 );
1051             break;
1052         default:
1053             break;
1054     }
1055 }
1056
1057 /**
1058  * Updates the name of the systray Icon tooltip.
1059  * Doesn't check if the systray exists, check before you call it.
1060  **/
1061 void MainInterface::updateSystrayTooltipName( const QString& name )
1062 {
1063     if( name.isEmpty() )
1064     {
1065         sysTray->setToolTip( qtr( "VLC media player" ) );
1066     }
1067     else
1068     {
1069         sysTray->setToolTip( name );
1070         if( notificationEnabled && ( isHidden() || isMinimized() ) )
1071         {
1072             sysTray->showMessage( qtr( "VLC media player" ), name,
1073                     QSystemTrayIcon::NoIcon, 3000 );
1074         }
1075     }
1076
1077     QVLCMenu::updateSystrayMenu( this, p_intf );
1078 }
1079
1080 /**
1081  * Updates the status of the systray Icon tooltip.
1082  * Doesn't check if the systray exists, check before you call it.
1083  **/
1084 void MainInterface::updateSystrayTooltipStatus( int i_status )
1085 {
1086     switch( i_status )
1087     {
1088         case  0:
1089         case  END_S:
1090             {
1091                 sysTray->setToolTip( qtr( "VLC media player" ) );
1092                 break;
1093             }
1094         case PLAYING_S:
1095             {
1096                 sysTray->setToolTip( input_name );
1097                 break;
1098             }
1099         case PAUSE_S:
1100             {
1101                 sysTray->setToolTip( input_name + " - "
1102                         + qtr( "Paused") );
1103                 break;
1104             }
1105     }
1106     QVLCMenu::updateSystrayMenu( this, p_intf );
1107 }
1108
1109 /************************************************************************
1110  * D&D Events
1111  ************************************************************************/
1112 void MainInterface::dropEvent(QDropEvent *event)
1113 {
1114     dropEventPlay( event, true );
1115 }
1116
1117 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1118 {
1119      event->setDropAction( Qt::CopyAction );
1120      if( !event->possibleActions() & Qt::CopyAction )
1121          return;
1122
1123      const QMimeData *mimeData = event->mimeData();
1124
1125      /* D&D of a subtitles file, add it on the fly */
1126      if( mimeData->urls().size() == 1 )
1127      {
1128         if( THEMIM->getIM()->hasInput() )
1129         {
1130             if( !input_AddSubtitle( THEMIM->getInput(),
1131                                     qtu( toNativeSeparators(
1132                                          mimeData->urls()[0].toLocalFile() ) ),
1133                                     true ) )
1134             {
1135                 event->accept();
1136                 return;
1137             }
1138         }
1139      }
1140      bool first = b_play;
1141      foreach( const QUrl &url, mimeData->urls() )
1142      {
1143         QString s = toNativeSeparators( url.toLocalFile() );
1144
1145         if( s.length() > 0 ) {
1146             playlist_Add( THEPL, qtu(s), NULL,
1147                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1148                           PLAYLIST_END, true, pl_Unlocked );
1149             first = false;
1150             RecentsMRL::getInstance( p_intf )->addRecent( s );
1151         }
1152      }
1153      event->accept();
1154 }
1155 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1156 {
1157      event->acceptProposedAction();
1158 }
1159 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1160 {
1161      event->acceptProposedAction();
1162 }
1163 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1164 {
1165      event->accept();
1166 }
1167
1168 /************************************************************************
1169  * Events stuff
1170  ************************************************************************/
1171 void MainInterface::customEvent( QEvent *event )
1172 {
1173 #if 0
1174     if( event->type() == PLDockEvent_Type )
1175     {
1176         PlaylistDialog::killInstance();
1177         playlistEmbeddedFlag = true;
1178         menuBar()->clear();
1179         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1180         togglePlaylist();
1181     }
1182 #endif
1183     /*else */
1184     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1185     {
1186         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1187         if( p_event->OnTop() )
1188             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1189         else
1190             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1191         show(); /* necessary to apply window flags */
1192     }
1193 }
1194
1195 void MainInterface::keyPressEvent( QKeyEvent *e )
1196 {
1197     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1198           && !menuBar()->isVisible() )
1199     {
1200         toggleMinimalView( false );
1201         e->accept();
1202     }
1203
1204     int i_vlck = qtEventToVLCKey( e );
1205     if( i_vlck > 0 )
1206     {
1207         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1208         e->accept();
1209     }
1210     else
1211         e->ignore();
1212 }
1213
1214 void MainInterface::resizeEvent( QResizeEvent * event )
1215 {
1216     if( b_keep_size )
1217     {
1218         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
1219             i_visualmode == QT_MINIMAL_MODE )
1220         {
1221                 mainVideoSize = size();
1222         }
1223         else
1224         {
1225             if( VISIBLE( bgWidget) ||
1226                 ( videoIsActive && videoWidget->isVisible() )
1227               )
1228                 mainVideoSize = size();
1229             else
1230                 mainBasedSize = size();
1231         }
1232     }
1233 }
1234
1235 void MainInterface::wheelEvent( QWheelEvent *e )
1236 {
1237     int i_vlckey = qtWheelEventToVLCKey( e );
1238     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1239     e->accept();
1240 }
1241
1242 void MainInterface::closeEvent( QCloseEvent *e )
1243 {
1244     e->accept();
1245     hide();
1246     THEDP->quit();
1247 }
1248
1249 void MainInterface::toggleFullScreen( void )
1250 {
1251     if( isFullScreen() )
1252     {
1253         showNormal();
1254         emit askUpdate(); // Needed if video was launched after the F11
1255         emit fullscreenInterfaceToggled( false );
1256     }
1257     else
1258     {
1259         showFullScreen();
1260         emit fullscreenInterfaceToggled( true );
1261     }
1262
1263 }
1264
1265 /*****************************************************************************
1266  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1267  *  We don't show the menu directly here because we don't want the
1268  *  caller to block for a too long time.
1269  *****************************************************************************/
1270 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1271                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1272 {
1273     intf_thread_t *p_intf = (intf_thread_t *)param;
1274
1275     if( p_intf->pf_show_dialog )
1276     {
1277         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1278                                 new_val.b_bool, NULL );
1279     }
1280
1281     return VLC_SUCCESS;
1282 }
1283
1284 /*****************************************************************************
1285  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1286  *****************************************************************************/
1287 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1288                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1289 {
1290     intf_thread_t *p_intf = (intf_thread_t *)param;
1291     p_intf->p_sys->p_mi->toggleFSC();
1292
1293     /* Show event */
1294      return VLC_SUCCESS;
1295 }
1296