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