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