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