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