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