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