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