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