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