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