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