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