]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4 - remove PlaylistWidget from interface_widget and move it to its own file in...
[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     //FIXME 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     // DEBUG FIXME
286     hide();
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", getControlsVisibilityStatus() & CONTROLS_ADVANCED );
301     settings->setValue( "pos", pos() );
302
303     settings->endGroup();
304     delete settings;
305
306     /* Unregister callback for the intf-popupmenu variable */
307     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
308                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
309     if( p_playlist != NULL )
310     {
311         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
312         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
313         vlc_object_release( p_playlist );
314     }
315
316     p_intf->b_interaction = VLC_FALSE;
317     var_DelCallback( p_intf, "interaction", InteractCallback, this );
318
319     p_intf->pf_request_window = NULL;
320     p_intf->pf_release_window = NULL;
321     p_intf->pf_control_window = NULL;
322 }
323
324 /*****************************
325  *   Main UI handling        *
326  *****************************/
327
328 /**
329  * Give the decorations of the Main Window a correct Name.
330  * If nothing is given, set it to VLC...
331  **/
332 void MainInterface::setVLCWindowsTitle( QString aTitle )
333 {
334     if( aTitle.isEmpty() )
335     {
336         setWindowTitle( qtr( "VLC media player" ) );
337     }
338     else
339     {
340         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
341     }
342 }
343
344 void MainInterface::handleMainUi( QSettings *settings )
345 {
346     /* Create the main Widget and the mainLayout */
347     QWidget *main = new QWidget;
348     setCentralWidget( main );
349     mainLayout = new QVBoxLayout( main );
350
351     /* Margins, spacing */
352     main->setContentsMargins( 0, 0, 0, 0 );
353     mainLayout->setMargin( 0 );
354
355     /* Create the CONTROLS Widget */
356     /* bool b_shiny = config_GetInt( p_intf, "qt-blingbling" ); */
357     controls = new ControlsWidget( p_intf, this,
358                    settings->value( "adv-controls", false ).toBool(),
359                    config_GetInt( p_intf, "qt-blingbling" ) );
360
361     /* Add the controls Widget to the main Widget */
362     mainLayout->insertWidget( 0, controls );
363
364     /* Create the Speed Control Widget */
365     speedControl = new SpeedControlWidget( p_intf );
366     speedControlMenu = new QMenu( this );
367
368     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
369     widgetAction->setDefaultWidget( speedControl );
370     speedControlMenu->addAction( widgetAction );
371
372     /* Visualisation */
373     /* Disabled for now, they SUCK */
374     #if 0
375     visualSelector = new VisualSelector( p_intf );
376     mainLayout->insertWidget( 0, visualSelector );
377     visualSelector->hide();
378     #endif
379
380     /* And video Outputs */
381     if( alwaysVideoFlag )
382     {
383         bgWidget = new BackgroundWidget( p_intf );
384         bgWidget->widgetSize = settings->value( "backgroundSize",
385                                            QSize( 300, 200 ) ).toSize();
386         bgWidget->resize( bgWidget->widgetSize );
387         bgWidget->updateGeometry();
388         mainLayout->insertWidget( 0, bgWidget );
389         CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
390     }
391
392     if( videoEmbeddedFlag )
393     {
394         videoWidget = new VideoWidget( p_intf );
395         videoWidget->widgetSize = QSize( 1, 1 );
396         //videoWidget->resize( videoWidget->widgetSize );
397         mainLayout->insertWidget( 0, videoWidget );
398
399         p_intf->pf_request_window  = ::DoRequest;
400         p_intf->pf_release_window  = ::DoRelease;
401         p_intf->pf_control_window  = ::DoControl;
402     }
403
404     /* Finish the sizing */
405     updateGeometry();
406 }
407
408 int MainInterface::privacyDialog( QList<ConfigControl *> controls )
409 {
410     QDialog *privacy = new QDialog( this );
411
412     privacy->setWindowTitle( qtr( "Privacy and Network policies" ) );
413
414     QGridLayout *gLayout = new QGridLayout( privacy );
415
416     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
417     QGridLayout *blablaLayout = new QGridLayout( blabla );
418     QLabel *text = new QLabel( qtr(
419         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
420         "online without authorisation.</p>\n "
421         "<p><i>VLC media player</i> can request limited information on "
422         "Internet, espically to get CD Covers and songs metadata or to know "
423         "if updates are available.</p>\n"
424         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
425         "information, even anonymously about your "
426         "usage.</p>\n"
427         "<p>Therefore please check the following options, the default being "
428         "almost no access on the web.</p>\n") );
429     text->setWordWrap( true );
430     text->setTextFormat( Qt::RichText );
431
432     blablaLayout->addWidget( text, 0, 0 ) ;
433
434     QGroupBox *options = new QGroupBox;
435     QGridLayout *optionsLayout = new QGridLayout( options );
436
437     gLayout->addWidget( blabla, 0, 0, 1, 3 );
438     gLayout->addWidget( options, 1, 0, 1, 3 );
439     module_config_t *p_config;
440     ConfigControl *control;
441     int line = 0;
442
443 #define CONFIG_GENERIC( option, type )                            \
444     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
445     if( p_config )                                                \
446     {                                                             \
447         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
448                 p_config, options, false, optionsLayout, line );  \
449         controls.append( control );                               \
450     }
451
452 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
453     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
454     if( p_config )                                                \
455     {                                                             \
456         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
457                 p_config, options, optionsLayout, line );  \
458         controls.append( control );                               \
459     }
460
461     CONFIG_GENERIC( "album-art", IntegerList ); line++;
462     CONFIG_GENERIC_NOBOOL( "fetch-meta", Bool ); line++;
463     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool );
464
465     QPushButton *ok = new QPushButton( qtr( "Ok" ) );
466
467     gLayout->addWidget( ok, 2, 2 );
468
469     CONNECT( ok, clicked(), privacy, accept() );
470     return privacy->exec();
471 }
472
473 //FIXME remove me at the end...
474 void MainInterface::debug()
475 {
476     msg_Dbg( p_intf, "size: %i - %i", controls->size().height(), controls->size().width() );
477     msg_Dbg( p_intf, "sizeHint: %i - %i", controls->sizeHint().height(), controls->sizeHint().width() );
478 }
479
480 /**********************************************************************
481  * Handling of sizing of the components
482  **********************************************************************/
483
484 /* This function is probably wrong, but we don't have many many choices...
485    Since we can't know from the playlist Widget if we are inside a dock or not,
486    because the playlist Widget can be called by THEDP, as a separate windows for
487    the skins.
488    Maybe the other solution is to redefine the sizeHint() of the playlist and
489    ask _parent->isFloating()...
490    If you think this would be better, please FIX it...
491 */
492 QSize MainInterface::sizeHint() const
493 {
494     QSize tempSize = controls->sizeHint() +
495         QSize( 100, menuBar()->size().height() + statusBar()->size().height() );
496
497     if( VISIBLE( bgWidget ) )
498         tempSize += bgWidget->sizeHint();
499     else if( videoIsActive )
500         tempSize += videoWidget->size();
501
502     if( !dockPL->isFloating() && dockPL->widget() )
503         tempSize += dockPL->widget()->size();
504
505     return tempSize;
506 }
507
508 #if 0
509 /* FIXME This is dead code and need to be removed AT THE END */
510 void MainInterface::resizeEvent( QResizeEvent *e )
511 {
512     if( videoWidget )
513         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
514     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
515     {
516         SET_WH( videoWidget, e->size().width() - addSize.width(),
517                              e->size().height()  - addSize.height() );
518         videoWidget->updateGeometry();
519     }
520     if( VISIBLE( playlistWidget ) )
521     {
522         //FIXME
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 //FIXME Remove this function at the end...
800 void MainInterface::updateOnTimer()
801 {
802  /*   if( intf_ShouldDie( p_intf ) )
803     {
804         QApplication::closeAllWindows();
805         QApplication::quit();
806     }*/
807 #if 0
808     if( need_components_update )
809     {
810         doComponentsUpdate();
811         need_components_update = false;
812     }
813 #endif
814
815     controls->updateOnTimer();
816 }
817
818 /*****************************************************************************
819  * Systray Icon and Systray Menu
820  *****************************************************************************/
821
822 /**
823  * Create a SystemTray icon and a menu that would go with it.
824  * Connects to a click handler on the icon.
825  **/
826 void MainInterface::createSystray()
827 {
828     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
829     sysTray = new QSystemTrayIcon( iconVLC, this );
830     sysTray->setToolTip( qtr( "VLC media player" ));
831
832     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
833     systrayMenu->setIcon( iconVLC );
834
835     QVLCMenu::updateSystrayMenu( this, p_intf, true );
836     sysTray->show();
837
838     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
839             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
840 }
841
842 /**
843  * Updates the Systray Icon's menu and toggle the main interface
844  */
845 void MainInterface::toggleUpdateSystrayMenu()
846 {
847     /* If hidden, show it */
848     if( isHidden() )
849     {
850         show();
851         activateWindow();
852     }
853     else if( isMinimized() )
854     {
855         /* Minimized */
856         showNormal();
857         activateWindow();
858     }
859     else
860     {
861         /* Visible */
862 #ifdef WIN32
863         /* check if any visible window is above vlc in the z-order,
864          * but ignore the ones always on top */
865         WINDOWINFO wi;
866         HWND hwnd;
867         wi.cbSize = sizeof( WINDOWINFO );
868         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
869                 hwnd && !IsWindowVisible( hwnd );
870                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
871         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
872                 (wi.dwExStyle&WS_EX_TOPMOST) )
873 #else
874         if( isActiveWindow() )
875 #endif
876         {
877             hide();
878         }
879         else
880         {
881             activateWindow();
882         }
883     }
884     QVLCMenu::updateSystrayMenu( this, p_intf );
885 }
886
887 void MainInterface::handleSystrayClick(
888                                     QSystemTrayIcon::ActivationReason reason )
889 {
890     switch( reason )
891     {
892         case QSystemTrayIcon::Trigger:
893             toggleUpdateSystrayMenu();
894             break;
895         case QSystemTrayIcon::MiddleClick:
896             sysTray->showMessage( qtr( "VLC media player" ),
897                     qtr( "Control menu for the player" ),
898                     QSystemTrayIcon::Information, 3000 );
899             break;
900     }
901 }
902
903 /**
904  * Updates the name of the systray Icon tooltip.
905  * Doesn't check if the systray exists, check before you call it.
906  **/
907 void MainInterface::updateSystrayTooltipName( QString name )
908 {
909     if( name.isEmpty() )
910     {
911         sysTray->setToolTip( qtr( "VLC media player" ) );
912     }
913     else
914     {
915         sysTray->setToolTip( name );
916         if( notificationEnabled && ( isHidden() || isMinimized() ) )
917         {
918             sysTray->showMessage( qtr( "VLC media player" ), name,
919                     QSystemTrayIcon::NoIcon, 3000 );
920         }
921     }
922 }
923
924 /**
925  * Updates the status of the systray Icon tooltip.
926  * Doesn't check if the systray exists, check before you call it.
927  **/
928 void MainInterface::updateSystrayTooltipStatus( int i_status )
929 {
930     switch( i_status )
931     {
932         case  0:
933         case  END_S:
934             {
935                 sysTray->setToolTip( qtr( "VLC media player" ) );
936                 break;
937             }
938         case PLAYING_S:
939             {
940                 sysTray->setToolTip( input_name );
941                 break;
942             }
943         case PAUSE_S:
944             {
945                 sysTray->setToolTip( input_name + " - "
946                         + qtr( "Paused") );
947                 break;
948             }
949     }
950 }
951
952 /************************************************************************
953  * D&D Events
954  ************************************************************************/
955 void MainInterface::dropEvent(QDropEvent *event)
956 {
957      const QMimeData *mimeData = event->mimeData();
958
959      /* D&D of a subtitles file, add it on the fly */
960      if( mimeData->urls().size() == 1 )
961      {
962         if( THEMIM->getIM()->hasInput() )
963         {
964             if( input_AddSubtitles( THEMIM->getInput(),
965                                     qtu( mimeData->urls()[0].toString() ),
966                                     VLC_TRUE ) )
967             {
968                 event->acceptProposedAction();
969                 return;
970             }
971         }
972      }
973      bool first = true;
974      foreach( QUrl url, mimeData->urls() ) {
975         QString s = url.toString();
976         if( s.length() > 0 ) {
977             playlist_Add( THEPL, qtu(s), NULL,
978                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
979                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
980             first = false;
981         }
982      }
983      event->acceptProposedAction();
984 }
985 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
986 {
987      event->acceptProposedAction();
988 }
989 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
990 {
991      event->acceptProposedAction();
992 }
993 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
994 {
995      event->accept();
996 }
997
998 /************************************************************************
999  * Events stuff
1000  ************************************************************************/
1001 void MainInterface::customEvent( QEvent *event )
1002 {
1003 #if 0
1004     if( event->type() == PLDockEvent_Type )
1005     {
1006         PlaylistDialog::killInstance();
1007         playlistEmbeddedFlag = true;
1008         menuBar()->clear();
1009         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1010         togglePlaylist();
1011     }
1012 #endif
1013     /*else */
1014     if ( event->type() == SetVideoOnTopEvent_Type )
1015     {
1016         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1017         if( p_event->OnTop() )
1018             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1019         else
1020             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1021         show(); /* necessary to apply window flags?? */
1022     }
1023 }
1024
1025 void MainInterface::keyPressEvent( QKeyEvent *e )
1026 {
1027     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() & Qt::Key_H )
1028           && menuBar()->isHidden() )
1029     {
1030         toggleMinimalView();
1031         e->accept();
1032     }
1033
1034     int i_vlck = qtEventToVLCKey( e );
1035     if( i_vlck > 0 )
1036     {
1037         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1038         e->accept();
1039     }
1040     else
1041         e->ignore();
1042 }
1043
1044 void MainInterface::wheelEvent( QWheelEvent *e )
1045 {
1046     int i_vlckey = qtWheelEventToVLCKey( e );
1047     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1048     e->accept();
1049 }
1050
1051 void MainInterface::closeEvent( QCloseEvent *e )
1052 {
1053     hide();
1054     vlc_object_kill( p_intf );
1055     QApplication::closeAllWindows();
1056     QApplication::quit();
1057 }
1058
1059 /*****************************************************************************
1060  * Callbacks
1061  *****************************************************************************/
1062 static int InteractCallback( vlc_object_t *p_this,
1063                              const char *psz_var, vlc_value_t old_val,
1064                              vlc_value_t new_val, void *param )
1065 {
1066     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1067     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1068     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1069     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1070     return VLC_SUCCESS;
1071 }
1072
1073 /*****************************************************************************
1074  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1075  *  We don't show the menu directly here because we don't want the
1076  *  caller to block for a too long time.
1077  *****************************************************************************/
1078 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1079                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1080 {
1081     intf_thread_t *p_intf = (intf_thread_t *)param;
1082
1083     if( p_intf->pf_show_dialog )
1084     {
1085         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1086                                 new_val.b_bool, 0 );
1087     }
1088
1089     return VLC_SUCCESS;
1090 }
1091
1092 /*****************************************************************************
1093  * IntfShowCB: callback triggered by the intf-show playlist variable.
1094  *****************************************************************************/
1095 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1096                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1097 {
1098     intf_thread_t *p_intf = (intf_thread_t *)param;
1099     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1100
1101     return VLC_SUCCESS;
1102 }