Moves volume icon update in the timer
[vlc.git] / 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 "dialogs/playlist.hpp"
33 #include "menus.hpp"
34
35 #include <QMenuBar>
36 #include <QCloseEvent>
37 #include <QPushButton>
38 #include <QStatusBar>
39 #include <QKeyEvent>
40 #include <QUrl>
41 #include <QSystemTrayIcon>
42 #include <QSize>
43 #include <QMenu>
44 #include <QLabel>
45
46 #include <assert.h>
47 #include <vlc_keys.h>
48 #include <vlc_vout.h>
49
50 #ifdef WIN32
51     #define PREF_W 410
52     #define PREF_H 151
53 #else
54     #define PREF_W 450
55     #define PREF_H 160
56 #endif
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     embeddedPlaylistWasActive = videoIsActive = false;
92     input_name = "";
93
94     /**
95      *  Configuration and settings
96      **/
97     settings = new QSettings( "VideoLAN", "VLC" );
98     settings->beginGroup( "MainWindow" );
99
100     /* Main settings */
101     setFocusPolicy( Qt::StrongFocus );
102     setAcceptDrops(true);
103     setWindowIcon( QApplication::windowIcon() );
104     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
105
106     /* Set The Video In emebedded Mode or not */
107     videoEmbeddedFlag = false;
108     if( config_GetInt( p_intf, "embedded-video" ) )
109         videoEmbeddedFlag = true;
110
111     alwaysVideoFlag = false;
112     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
113         alwaysVideoFlag = true;
114
115     /* Set the other interface settings */
116     playlistEmbeddedFlag = settings->value( "playlist-embedded", true).toBool();
117     visualSelectorEnabled = settings->value( "visual-selector", false ).toBool();
118     notificationEnabled = config_GetInt( p_intf, "qt-notification" )
119                           ? true : false;
120     /**************************
121      *  UI and Widgets design
122      **************************/
123     setVLCWindowsTitle();
124     handleMainUi( settings );
125
126     /* Menu Bar */
127     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
128                              visualSelectorEnabled );
129
130     /* Status Bar */
131     /**
132      * TODO: clicking on the elapsed time should switch to the remaining time
133      **/
134     /**
135      * TODO: do we add a label for the current Volume ?
136      **/
137     b_remainingTime = false;
138     timeLabel = new QLabel;
139     nameLabel = new QLabel;
140     speedLabel = new QLabel( "1.0x" );
141     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
142     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
143     statusBar()->addWidget( nameLabel, 8 );
144     statusBar()->addPermanentWidget( speedLabel, 0 );
145     statusBar()->addPermanentWidget( timeLabel, 2 );
146     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
147     timeLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
148     timeLabel->installEventFilter( this );
149     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
150              this, showSpeedMenu( QPoint ) );
151     CONNECT( timeLabel, customContextMenuRequested( QPoint ),
152              this, showTimeMenu( QPoint ) );
153
154     /**********************
155      * Systray Management *
156      **********************/
157     sysTray = NULL;
158     bool b_createSystray = false;
159     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
160     if( config_GetInt( p_intf, "qt-start-minimized") )
161     {
162         if( b_systrayAvailable ){
163             b_createSystray = true;
164             hide(); //FIXME
165         }
166         else msg_Warn( p_intf, "You can't minize if you haven't a system "
167                 "tray bar" );
168     }
169     if( config_GetInt( p_intf, "qt-system-tray") )
170         b_createSystray = true;
171
172     if( b_systrayAvailable && b_createSystray )
173             createSystray();
174
175     /* Init input manager */
176     MainInputManager::getInstance( p_intf );
177     ON_TIMEOUT( updateOnTimer() );
178
179     /**
180      * Various CONNECTs
181      **/
182
183     /* Connect the input manager to the GUI elements it manages */
184     /* It is also connected to the control->slider, see the ControlsWidget */
185     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
186              this, setDisplayPosition( float, int, int ) );
187     
188     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
189     
190     /** Connects on nameChanged() */
191     /* Naming in the controller statusbar */
192     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
193              setName( QString ) );
194     /* and in the systray */
195     if( sysTray )
196     {
197         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
198                  updateSystrayTooltipName( QString ) );
199     }
200     /* and in the title of the controller */
201     if( config_GetInt( p_intf, "qt-name-in-title" ) )
202     {
203         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
204              setVLCWindowsTitle( QString ) );
205     }
206
207     /** CONNECTS on PLAY_STATUS **/
208     /* Status on the main controller */
209     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
210     /* and in the systray */
211     if( sysTray )
212     {
213         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
214                  updateSystrayTooltipStatus( int ) );
215     }
216
217     /**
218      * Callbacks
219      **/
220     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
221     var_AddCallback( p_intf, "interaction", InteractCallback, this );
222     p_intf->b_interaction = VLC_TRUE;
223
224     /* Register callback for the intf-popupmenu variable */
225     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
226                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
227     if( p_playlist != NULL )
228     {
229         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
230         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
231         vlc_object_release( p_playlist );
232     }
233 }
234
235 MainInterface::~MainInterface()
236 {
237     /* Unregister callback for the intf-popupmenu variable */
238     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
239                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
240     if( p_playlist != NULL )
241     {
242         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
243         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
244         vlc_object_release( p_playlist );
245     }
246
247     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
248     settings->setValue( "adv-controls", getControlsVisibilityStatus() & 0x1 );
249     settings->setValue( "pos", pos() );
250     settings->endGroup();
251     delete settings;
252     p_intf->b_interaction = VLC_FALSE;
253     var_DelCallback( p_intf, "interaction", InteractCallback, this );
254
255     p_intf->pf_request_window = NULL;
256     p_intf->pf_release_window = NULL;
257     p_intf->pf_control_window = NULL;
258 }
259
260 /*****************************
261  *   Main UI handling        *
262  *****************************/
263
264 /**
265  * Give the decorations of the Main Window a correct Name.
266  * If nothing is given, set it to VLC...
267  **/
268 void MainInterface::setVLCWindowsTitle( QString aTitle )
269 {
270     if( aTitle.isEmpty() )
271     {
272         setWindowTitle( qtr( "VLC media player" ) );
273     }
274     else
275     {
276         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
277     }
278 }
279
280
281 void MainInterface::handleMainUi( QSettings *settings )
282 {
283     /* Create the main Widget and the mainLayout */
284     QWidget *main = new QWidget( this );
285     mainLayout = new QVBoxLayout( main );
286     setCentralWidget( main );
287
288     /* Margins, spacing */
289     main->setContentsMargins( 0, 0, 0, 0 );
290     mainLayout->setMargin( 0 );
291
292     /* Create the CONTROLS Widget */
293     controls = new ControlsWidget( p_intf,
294                    settings->value( "adv-controls", false ).toBool() );
295
296     /* Configure the Controls */
297     BUTTON_SET_IMG( controls->playlistButton, "" , playlist_icon.png,
298                     playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
299                                             qtr( "Open playlist" ) );
300     BUTTONACT( controls->playlistButton, togglePlaylist() );
301
302     /* Add the controls Widget to the main Widget */
303     mainLayout->addWidget( controls );
304
305
306     /* Set initial size */
307     resize( PREF_W, PREF_H );
308     addSize = QSize( mainLayout->margin() * 2, PREF_H );
309
310     /* Visualisation */
311     visualSelector = new VisualSelector( p_intf );
312     mainLayout->insertWidget( 0, visualSelector );
313     visualSelector->hide();
314
315     /* And video Outputs */
316     if( alwaysVideoFlag )
317     {
318         bgWidget = new BackgroundWidget( p_intf );
319         bgWidget->widgetSize = settings->value( "backgroundSize",
320                                            QSize( 300, 300 ) ).toSize();
321         bgWidget->resize( bgWidget->widgetSize );
322         bgWidget->updateGeometry();
323         mainLayout->insertWidget( 0, bgWidget );
324     }
325
326     if( videoEmbeddedFlag )
327     {
328         videoWidget = new VideoWidget( p_intf );
329         videoWidget->widgetSize = QSize( 1, 1 );
330         //videoWidget->resize( videoWidget->widgetSize );
331         mainLayout->insertWidget( 0, videoWidget );
332
333         p_intf->pf_request_window  = ::DoRequest;
334         p_intf->pf_release_window  = ::DoRelease;
335         p_intf->pf_control_window  = ::DoControl;
336     }
337
338     /* Finish the sizing */
339     setMinimumSize( PREF_W, addSize.height() );
340 }
341
342 /**********************************************************************
343  * Handling of sizing of the components
344  **********************************************************************/
345 void MainInterface::calculateInterfaceSize()
346 {
347     int width = 0, height = 0;
348     if( VISIBLE( bgWidget ) )
349     {
350         width = bgWidget->widgetSize.width();
351         height = bgWidget->widgetSize.height();
352         assert( !(playlistWidget && playlistWidget->isVisible() ) );
353     }
354     else if( VISIBLE( playlistWidget ) )
355     {
356         width = playlistWidget->widgetSize.width();
357         height = playlistWidget->widgetSize.height();
358     }
359     else if( videoIsActive )
360     {
361         width =  videoWidget->widgetSize.width() ;
362         height = videoWidget->widgetSize.height();
363     }
364     else
365     {
366         width = PREF_W - addSize.width();
367         height = PREF_H - addSize.height();
368     }
369     if( VISIBLE( visualSelector ) )
370         height += visualSelector->height();
371 /*    if( VISIBLE( advControls) )
372     {
373         height += advControls->sizeHint().height();
374     }*/
375     mainSize = QSize( width + addSize.width(), height + addSize.height() );
376 }
377
378 void MainInterface::resizeEvent( QResizeEvent *e )
379 {
380     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
381     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
382     {
383         SET_WH( videoWidget, e->size().width() - addSize.width(),
384                              e->size().height()  - addSize.height() );
385         videoWidget->updateGeometry();
386     }
387     if( VISIBLE( playlistWidget ) )
388     {
389         SET_WH( playlistWidget , e->size().width() - addSize.width(),
390                                  e->size().height() - addSize.height() );
391         playlistWidget->updateGeometry();
392     }
393 }
394
395 /****************************************************************************
396  * Small right-click menus
397  ****************************************************************************/
398 void MainInterface::showSpeedMenu( QPoint pos )
399 {
400     QMenu menu( this );
401     menu.addAction( "Not Implemented Yet" );
402     menu.exec( QCursor::pos() );
403 }
404
405 void MainInterface::showTimeMenu( QPoint pos )
406 {
407     QMenu menu( this );
408     menu.addAction(  qtr("Elapsed Time") , this, SLOT( setElapsedTime() ) );
409     menu.addAction(  qtr("Remaining Time") , this, SLOT( setRemainTime() ) );
410     menu.exec( QCursor::pos() );
411 }
412
413 /****************************************************************************
414  * Video Handling
415  ****************************************************************************/
416 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
417                                    int *pi_y, unsigned int *pi_width,
418                                    unsigned int *pi_height )
419 {
420     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
421     if( ret )
422     {
423         videoIsActive = true;
424         if( VISIBLE( playlistWidget ) )
425         {
426             embeddedPlaylistWasActive = true;
427 //            playlistWidget->hide();
428         }
429         bool bgWasVisible = false;
430         if( VISIBLE( bgWidget) )
431         {
432             bgWasVisible = true;
433             bgWidget->hide();
434         }
435         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
436         {
437             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
438         }
439         else /* Background widget available, use its size */
440         {
441             /* Ok, our visualizations are bad, so don't do this for the moment
442              * use the requested size anyway */
443             // videoWidget->widgetSize = bgWidget->widgeTSize;
444             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
445         }
446 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
447         need_components_update = true;
448     }
449     return ret;
450 }
451
452 void MainInterface::releaseVideo( void *p_win )
453 {
454     videoWidget->release( p_win );
455     videoWidget->widgetSize = QSize( 0, 0 );
456     videoWidget->resize( videoWidget->widgetSize );
457
458     if( embeddedPlaylistWasActive )
459         playlistWidget->show();
460     else if( bgWidget )
461         bgWidget->show();
462
463     videoIsActive = false;
464     need_components_update = true;
465 }
466
467 class SetVideoOnTopQtEvent : public QEvent
468 {
469 public:
470     SetVideoOnTopQtEvent( bool _onTop ) :
471       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
472     {
473     }
474
475     bool OnTop() const
476     {
477         return onTop;
478     }
479
480 private:
481     bool onTop;
482 };
483
484 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
485 {
486     int i_ret = VLC_EGENERIC;
487     switch( i_query )
488     {
489         case VOUT_GET_SIZE:
490         {
491             unsigned int *pi_width  = va_arg( args, unsigned int * );
492             unsigned int *pi_height = va_arg( args, unsigned int * );
493             *pi_width = videoWidget->widgetSize.width();
494             *pi_height = videoWidget->widgetSize.height();
495             i_ret = VLC_SUCCESS;
496             break;
497         }
498         case VOUT_SET_SIZE:
499         {
500             unsigned int i_width  = va_arg( args, unsigned int );
501             unsigned int i_height = va_arg( args, unsigned int );
502             videoWidget->widgetSize = QSize( i_width, i_height );
503             // videoWidget->updateGeometry();
504             need_components_update = true;
505             i_ret = VLC_SUCCESS;
506             break;
507         }
508         case VOUT_SET_STAY_ON_TOP:
509         {
510             int i_arg = va_arg( args, int );
511             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
512             i_ret = VLC_SUCCESS;
513             break;
514         }
515         default:
516             msg_Warn( p_intf, "unsupported control query" );
517             break;
518     }
519     return i_ret;
520 }
521
522 /*****************************************************************************
523  * Playlist, Visualisation and Menus handling
524  *****************************************************************************/
525 /**
526  * Toggle the playlist widget or dialog
527  **/
528 void MainInterface::togglePlaylist()
529 {
530     // Toggle the playlist dialog if not embedded and return
531     if( !playlistEmbeddedFlag )
532     {
533         if( playlistWidget )
534         {
535             /// \todo Destroy it
536         }
537         THEDP->playlistDialog();
538         return;
539     }
540
541     // Create the playlist Widget and destroy the existing dialog
542     if( !playlistWidget )
543     {
544         PlaylistDialog::killInstance();
545         playlistWidget = new PlaylistWidget( p_intf );
546         mainLayout->insertWidget( 0, playlistWidget );
547         playlistWidget->widgetSize = settings->value( "playlistSize",
548                                                QSize( 650, 310 ) ).toSize();
549         playlistWidget->hide();
550         if(bgWidget)
551         CONNECT( playlistWidget, artSet( QString ), bgWidget, setArt(QString) );
552     }
553
554     // And toggle visibility
555     if( VISIBLE( playlistWidget ) )
556     {
557         playlistWidget->hide();
558         if( bgWidget ) bgWidget->show();
559         if( videoIsActive )
560         {
561             videoWidget->widgetSize = savedVideoSize;
562             videoWidget->resize( videoWidget->widgetSize );
563             videoWidget->updateGeometry();
564             if( bgWidget ) bgWidget->hide();
565         }
566     }
567     else
568     {
569         playlistWidget->show();
570         if( videoIsActive )
571         {
572             savedVideoSize = videoWidget->widgetSize;
573             videoWidget->widgetSize.setHeight( 0 );
574             videoWidget->resize( videoWidget->widgetSize );
575             videoWidget->updateGeometry();
576         }
577         if( VISIBLE( bgWidget ) ) bgWidget->hide();
578     }
579
580     doComponentsUpdate();
581 }
582
583 void MainInterface::undockPlaylist()
584 {
585     if( playlistWidget )
586     {
587         playlistWidget->hide();
588         playlistWidget->deleteLater();
589         mainLayout->removeWidget( playlistWidget );
590         playlistWidget = NULL;
591         playlistEmbeddedFlag = false;
592
593         menuBar()->clear();
594         QVLCMenu::createMenuBar( this, p_intf, false, visualSelectorEnabled);
595
596         if( videoIsActive )
597         {
598             videoWidget->widgetSize = savedVideoSize;
599             videoWidget->resize( videoWidget->widgetSize );
600             videoWidget->updateGeometry();
601         }
602
603         doComponentsUpdate();
604         THEDP->playlistDialog();
605     }
606 }
607
608 #if 0
609 void MainInterface::visual()
610 {
611     if( !VISIBLE( visualSelector) )
612     {
613         visualSelector->show();
614         if( !THEMIM->getIM()->hasVideo() )
615         {
616             /* Show the background widget */
617         }
618         visualSelectorEnabled = true;
619     }
620     else
621     {
622         /* Stop any currently running visualization */
623         visualSelector->hide();
624         visualSelectorEnabled = false;
625     }
626     doComponentsUpdate();
627 }
628 #endif
629
630 void MainInterface::toggleMenus()
631 {
632     msg_Dbg( p_intf, "I HAS HERE, HIDING YOUR MENUZ: \\_o<~~ coin coin" );
633     TOGGLEV( controls );
634     TOGGLEV( statusBar() );
635     updateGeometry();
636 }
637
638 /* Video widget cannot do this synchronously as it runs in another thread */
639 /* Well, could it, actually ? Probably dangerous ... */
640 void MainInterface::doComponentsUpdate()
641 {
642     calculateInterfaceSize();
643     resize( mainSize );
644 }
645
646 void MainInterface::toggleAdvanced()
647 {
648     controls->toggleAdvanced();
649 }
650
651 int MainInterface::getControlsVisibilityStatus()
652 {
653     return( (controls->isVisible() ? 0x2 : 0x0 )
654                 + controls->b_advancedVisible );
655 }
656
657 /************************************************************************
658  * Other stuff
659  ************************************************************************/
660 void MainInterface::setDisplayPosition( float pos, int time, int length )
661 {
662     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
663     secstotimestr( psz_length, length );
664     secstotimestr( psz_time, b_remainingTime ? length - time : time );
665     QString title; title.sprintf( "%s/%s", psz_time, psz_length );
666     if( b_remainingTime ) timeLabel->setText( " -"+title+" " );
667     else timeLabel->setText( " "+title+" " );
668 }
669
670 void MainInterface::toggleTimeDisplay()
671 {
672     b_remainingTime = ( b_remainingTime ? false : true );
673 }
674
675 void MainInterface::setElapsedTime(){ b_remainingTime = false; }
676 void MainInterface::setRemainTime(){ b_remainingTime = true; }
677
678 void MainInterface::setName( QString name )
679 {
680     input_name = name;
681     nameLabel->setText( " " + name+" " );
682 }
683
684 void MainInterface::setStatus( int status )
685 {
686     controls->setStatus( status );
687     if( sysTray )
688         updateSystrayMenu( status );
689 }
690
691 void MainInterface::setRate( int rate )
692 {
693     QString str;
694     str.setNum( ( 1000/(double)rate), 'f', 2 );
695     str.append( "x" );
696     speedLabel->setText( str );
697 }
698
699 void MainInterface::updateOnTimer()
700 {
701     /* \todo Make this event-driven */
702     if( intf_ShouldDie( p_intf ) )
703     {
704         QApplication::closeAllWindows();
705         QApplication::quit();
706     }
707     if( need_components_update )
708     {
709         doComponentsUpdate();
710         need_components_update = false;
711     }
712
713     controls->updateOnTimer();
714 }
715
716 /*****************************************************************************
717  * Systray Icon and Systray Menu
718  *****************************************************************************/
719
720 /**
721  * Create a SystemTray icon and a menu that would go with it.
722  * Connects to a click handler on the icon.
723  **/
724 void MainInterface::createSystray()
725 {
726     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
727     sysTray = new QSystemTrayIcon( iconVLC, this );
728     sysTray->setToolTip( qtr( "VLC media player" ));
729
730     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
731     systrayMenu->setIcon( iconVLC );
732
733     QVLCMenu::updateSystrayMenu( this, p_intf, true );
734     sysTray->show();
735
736     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
737             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
738 }
739
740 /**
741  * Update the menu of the Systray Icon.
742  * May be unneedded, since it just calls QVLCMenu::update
743  * FIXME !!!
744  **/
745 void MainInterface::updateSystrayMenu( int status )
746 {
747     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
748 }
749
750 /**
751  * Updates the Systray Icon's menu and toggle the main interface
752  */
753 void MainInterface::toggleUpdateSystrayMenu()
754 {
755     if( isHidden() )
756     {
757         show();
758         activateWindow();
759     }
760     else
761     {
762 #ifdef WIN32
763         /* check if any visible window is above vlc in the z-order,
764          * but ignore the ones always on top */
765         WINDOWINFO wi;
766         HWND hwnd;
767         wi.cbSize = sizeof( WINDOWINFO );
768         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
769                 hwnd && !IsWindowVisible( hwnd );
770                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
771         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
772                 (wi.dwExStyle&WS_EX_TOPMOST) )
773 #else
774         if( isActiveWindow() )
775 #endif
776         {
777             hide();
778         }
779         else
780         {
781             activateWindow();
782         }
783     }
784     QVLCMenu::updateSystrayMenu( this, p_intf );
785 }
786
787 void MainInterface::handleSystrayClick(
788                                     QSystemTrayIcon::ActivationReason reason )
789 {
790     switch( reason )
791     {
792         case QSystemTrayIcon::Trigger:
793             toggleUpdateSystrayMenu();
794             break;
795         case QSystemTrayIcon::MiddleClick:
796             sysTray->showMessage( qtr( "VLC media player" ),
797                     qtr( "Control menu for the player" ),
798                     QSystemTrayIcon::Information, 4000 );
799             break;
800     }
801 }
802
803 /**
804  * Updates the name of the systray Icon tooltip.
805  * Doesn't check if the systray exists, check before you call it.
806  * FIXME !!! Fusion with next function ?
807  **/
808 void MainInterface::updateSystrayTooltipName( QString name )
809 {
810     if( name.isEmpty() )
811     {
812         sysTray->setToolTip( qtr( "VLC media player" ) );
813     }
814     else
815     {
816         sysTray->setToolTip( name );
817         if( notificationEnabled )
818         {
819             sysTray->showMessage( qtr( "VLC media player" ), name,
820                     QSystemTrayIcon::NoIcon, 4000 );
821         }
822     }
823 }
824
825 /**
826  * Updates the status of the systray Icon tooltip.
827  * Doesn't check if the systray exists, check before you call it.
828  **/
829 void MainInterface::updateSystrayTooltipStatus( int i_status )
830 {
831     switch( i_status )
832     {
833         case  0:
834             {
835                 sysTray->setToolTip( qtr( "VLC media player" ) );
836                 break;
837             }
838         case PLAYING_S:
839             {
840                 sysTray->setToolTip( input_name );
841                 //+ " - " + qtr( "Playing" ) );
842                 break;
843             }
844         case PAUSE_S:
845             {
846                 sysTray->setToolTip( input_name + " - "
847                         + qtr( "Paused") );
848                 break;
849             }
850     }
851 }
852
853 /************************************************************************
854  * D&D Events
855  ************************************************************************/
856 void MainInterface::dropEvent(QDropEvent *event)
857 {
858      const QMimeData *mimeData = event->mimeData();
859
860      /* D&D of a subtitles file, add it on the fly */
861      if( mimeData->urls().size() == 1 )
862      {
863         if( THEMIM->getIM()->hasInput() )
864         {
865             if( input_AddSubtitles( THEMIM->getInput(),
866                                     qtu( mimeData->urls()[0].toString() ),
867                                     VLC_TRUE ) )
868             {
869                 event->acceptProposedAction();
870                 return;
871             }
872         }
873      }
874      bool first = true;
875      foreach( QUrl url, mimeData->urls() ) {
876         QString s = url.toString();
877         if( s.length() > 0 ) {
878             playlist_Add( THEPL, qtu(s), NULL,
879                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
880                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
881             first = false;
882         }
883      }
884      event->acceptProposedAction();
885 }
886 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
887 {
888      event->acceptProposedAction();
889 }
890 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
891 {
892      event->acceptProposedAction();
893 }
894 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
895 {
896      event->accept();
897 }
898
899 /************************************************************************
900  * Events stuff
901  ************************************************************************/
902 void MainInterface::customEvent( QEvent *event )
903 {
904     if( event->type() == PLDockEvent_Type )
905     {
906         PlaylistDialog::killInstance();
907         playlistEmbeddedFlag = true;
908         menuBar()->clear();
909         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
910         togglePlaylist();
911     }
912     else if ( event->type() == SetVideoOnTopEvent_Type )
913     {
914         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
915         if( p_event->OnTop() )
916             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
917         else
918             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
919         show(); /* necessary to apply window flags?? */
920     }
921 }
922
923 bool MainInterface::eventFilter(QObject *obj, QEvent *event)
924 {
925     if( (obj == timeLabel) && (event->type() == QEvent::MouseButtonPress) ) toggleTimeDisplay();
926 }
927
928
929 void MainInterface::keyPressEvent( QKeyEvent *e )
930 {
931     int i_vlck = qtEventToVLCKey( e );
932     if( i_vlck >= 0 )
933     {
934         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
935         e->accept();
936     }
937     else
938         e->ignore();
939 }
940
941 void MainInterface::wheelEvent( QWheelEvent *e )
942 {
943     int i_vlckey = qtWheelEventToVLCKey( e );
944     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
945     e->accept();
946 }
947
948 void MainInterface::closeEvent( QCloseEvent *e )
949 {
950     hide();
951     vlc_object_kill( p_intf );
952 }
953
954 /*****************************************************************************
955  * Callbacks
956  *****************************************************************************/
957 static int InteractCallback( vlc_object_t *p_this,
958                              const char *psz_var, vlc_value_t old_val,
959                              vlc_value_t new_val, void *param )
960 {
961     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
962     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
963     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
964     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
965     return VLC_SUCCESS;
966 }
967
968 /*****************************************************************************
969  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
970  *  We don't show the menu directly here because we don't want the
971  *  caller to block for a too long time.
972  *****************************************************************************/
973 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
974                         vlc_value_t old_val, vlc_value_t new_val, void *param )
975 {
976     intf_thread_t *p_intf = (intf_thread_t *)param;
977
978     if( p_intf->pf_show_dialog )
979     {
980         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
981                                 new_val.b_bool, 0 );
982     }
983
984     return VLC_SUCCESS;
985 }
986
987 /*****************************************************************************
988  * IntfShowCB: callback triggered by the intf-show playlist variable.
989  *****************************************************************************/
990 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
991                        vlc_value_t old_val, vlc_value_t new_val, void *param )
992 {
993     intf_thread_t *p_intf = (intf_thread_t *)param;
994     //p_intf->p_sys->b_intf_show = VLC_TRUE;
995
996     return VLC_SUCCESS;
997 }