ff22c46ab115e05fbd337eda6f98a3d9b362b61d
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. 
22  *****************************************************************************/
23
24 #include "qt4.hpp"
25 #include "main_interface.hpp"
26 #include "input_manager.hpp"
27 #include "util/input_slider.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
43 #include <assert.h>
44 #include <vlc_keys.h>
45 #include <vlc_vout.h>
46
47 #ifdef WIN32
48     #define PREF_W 410
49     #define PREF_H 121
50 #else
51     #define PREF_W 450
52     #define PREF_H 125
53 #endif
54
55 #define VISIBLE(i) (i && i->isVisible())
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     /* Configuration */
88     settings = new QSettings( "VideoLAN", "VLC" );
89     settings->beginGroup( "MainWindow" );
90
91     setWindowIcon( QApplication::windowIcon() );
92
93     need_components_update = false;
94     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
95     embeddedPlaylistWasActive = videoIsActive = false;
96  
97     input_name = "";
98
99     videoEmbeddedFlag = false;
100     if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
101
102     alwaysVideoFlag = false;
103     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
104         alwaysVideoFlag = true;
105
106     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
107     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
108     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
109
110     /* UI */
111     setVLCWindowsTitle();
112     handleMainUi( settings );
113     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
114                              advControlsEnabled, visualSelectorEnabled );
115     timeLabel = new QLabel( 0 );
116     nameLabel = new QLabel( 0 );
117     statusBar()->addWidget( nameLabel, 4 );
118     statusBar()->addPermanentWidget( timeLabel, 1 );
119
120     setFocusPolicy( Qt::StrongFocus );
121     setAcceptDrops(true);
122
123     /* Init input manager */
124     MainInputManager::getInstance( p_intf );
125     ON_TIMEOUT( updateOnTimer() );
126
127     /* Volume control */
128     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
129
130     /* Connect the input manager to the GUI elements it manages */
131     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
132              slider, setPosition( float,int, int ) );
133     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
134              this, setDisplay( float, int, int ) );
135
136     /* Naming in the controller */
137     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
138              setName( QString ) );
139     if( config_GetInt( p_intf, "qt-system-tray" ) )
140     {
141         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
142                  updateSystrayTooltipName(QString));
143     }
144     if( config_GetInt( p_intf, "qt-name-in-title" ) )
145     {
146         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
147              setVLCWindowsTitle( QString ) );
148     }
149
150     /* PLAY_STATUS */
151     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
152     CONNECT( THEMIM->getIM(), navigationChanged( int ),
153              this, setNavigation(int) );
154     if( config_GetInt( p_intf, "qt-system-tray" ) )
155     {
156         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
157                  updateSystrayTooltipStatus(int));
158     }
159     CONNECT( slider, sliderDragged( float ),
160              THEMIM->getIM(), sliderUpdate( float ) );
161
162     /* Buttons */
163     CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
164              sectionPrev() );
165     CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
166              sectionNext() );
167     CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
168              sectionMenu() );
169
170     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
171     var_AddCallback( p_intf, "interaction", InteractCallback, this );
172     p_intf->b_interaction = VLC_TRUE;
173
174     /* Register callback for the intf-popupmenu variable */
175     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
176                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
177     if( p_playlist != NULL )
178     {
179         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
180         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
181         vlc_object_release( p_playlist );
182     }
183     if( QSystemTrayIcon::isSystemTrayAvailable() &&
184                         ( config_GetInt( p_intf, "qt-start-mininimized") == 1))
185     {
186         hide();
187         createSystrayMenu();
188     }
189     if( QSystemTrayIcon::isSystemTrayAvailable() &&
190                               ( config_GetInt( p_intf, "qt-system-tray") == 1))
191             createSystrayMenu();
192
193 }
194
195 MainInterface::~MainInterface()
196 {
197     /* Unregister callback for the intf-popupmenu variable */
198     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
199                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
200     if( p_playlist != NULL )
201     {
202         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
203         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
204         vlc_object_release( p_playlist );
205     }
206
207     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
208     settings->setValue( "adv-controls", advControlsEnabled );
209     settings->setValue( "pos", pos() );
210     settings->endGroup();
211     delete settings;
212     p_intf->b_interaction = VLC_FALSE;
213     var_DelCallback( p_intf, "interaction", InteractCallback, this );
214
215     p_intf->pf_request_window = NULL;
216     p_intf->pf_release_window = NULL;
217     p_intf->pf_control_window = NULL;
218 }
219
220 void MainInterface::setVLCWindowsTitle( QString aTitle )
221 {
222     if( aTitle.isEmpty() )
223     {
224         this->setWindowTitle( qtr( "VLC media player" ) );
225     }
226     else
227     {
228         this->setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
229     }
230 }
231
232 void MainInterface::handleMainUi( QSettings *settings )
233 {
234     QWidget *main = new QWidget( this );
235     setCentralWidget( main );
236     ui.setupUi( centralWidget() );
237
238     slider = new InputSlider( Qt::Horizontal, NULL );
239     ui.vboxLayout->insertWidget( 0, slider );
240     ui.discFrame->hide();
241     BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
242     BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
243     BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
244
245     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
246                       qtr("Previous"), prev() );
247     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
248     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
249     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
250
251     /* Volume */
252     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
253     ui.volumeSlider->setMaximum( 100 );
254     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
255     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
256     ui.volMuteLabel->installEventFilter(h);
257     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
258
259     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
260                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
261                                                 qtr( "Open playlist" ) );
262     BUTTONACT( ui.playlistButton, playlist() );
263
264     /* Set initial size */
265     resize ( PREF_W, PREF_H );
266
267     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
268
269     advControls = new ControlsWidget( p_intf );
270     ui.vboxLayout->insertWidget( 0, advControls );
271     advControls->updateGeometry();
272     if( !advControlsEnabled ) advControls->hide();
273     need_components_update = true;
274
275     visualSelector = new VisualSelector( p_intf );
276     ui.vboxLayout->insertWidget( 0, visualSelector );
277     visualSelector->hide();
278
279     if( alwaysVideoFlag )
280     {
281         bgWidget = new BackgroundWidget( p_intf );
282         bgWidget->widgetSize = settings->value( "backgroundSize",
283                                                 QSize( 200, 200 ) ).toSize();
284         bgWidget->resize( bgWidget->widgetSize );
285         bgWidget->updateGeometry();
286         ui.vboxLayout->insertWidget( 0, bgWidget );
287     }
288
289     if( videoEmbeddedFlag )
290     {
291         videoWidget = new VideoWidget( p_intf );
292         videoWidget->widgetSize = QSize( 1, 1 );
293         videoWidget->resize( videoWidget->widgetSize );
294         ui.vboxLayout->insertWidget( 0, videoWidget );
295
296         p_intf->pf_request_window  = ::DoRequest;
297         p_intf->pf_release_window  = ::DoRelease;
298         p_intf->pf_control_window  = ::DoControl;
299     }
300     setMinimumSize( PREF_W, addSize.height() );
301 }
302
303 void MainInterface::createSystrayMenu()
304 {
305     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
306     sysTray = new QSystemTrayIcon( iconVLC, this );
307     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
308     systrayMenu->setIcon( iconVLC );
309     sysTray->setToolTip( qtr( "VLC media player" ));
310     QVLCMenu::updateSystrayMenu( this, p_intf, true );
311     sysTray->show();
312     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
313             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
314 }
315
316 void MainInterface::updateSystrayMenu( int status )
317 {
318     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
319 }
320
321 void MainInterface::toggleUpdateSystrayMenu()
322 {
323     QVLCMenu::updateSystrayMenu( this, p_intf );
324     toggleVisible();
325 }
326
327 void MainInterface::handleSystrayClick( QSystemTrayIcon::ActivationReason reason )
328 {
329     switch( reason )
330     {
331         case QSystemTrayIcon::Trigger:
332             this->toggleVisible(); break;
333         case QSystemTrayIcon::MiddleClick:
334             sysTray->showMessage( qtr( "VLC media player" ),
335                     qtr( "Control menu for the player" )
336                     + nameLabel->text() ,
337                     QSystemTrayIcon::Information, 4000 );
338             break;
339     }
340 }
341
342
343 void MainInterface::updateSystrayTooltipName( QString name )
344 {
345     if( name.isEmpty() )
346     {
347         sysTray->setToolTip( qtr( "VLC media player" ) );
348     }
349     else
350     {
351         sysTray->setToolTip( name );
352     }
353 }
354
355 void MainInterface::updateSystrayTooltipStatus( int i_status )
356 {
357     switch( i_status )
358      {
359          case  0:
360              {
361                  sysTray->setToolTip( qtr( "VLC media player" ) );
362                  break;
363              }
364          case PLAYING_S:
365              {
366                   sysTray->setToolTip( input_name );
367                   //+ " - " + qtr( "Playing" ) );
368                   break;
369              }
370          case PAUSE_S:
371              {
372                  sysTray->setToolTip( input_name + " - " 
373                                       + qtr( "Paused") );
374                  break;
375              }
376      }
377 }
378 /**********************************************************************
379  * Handling of the components
380  **********************************************************************/
381 void MainInterface::calculateInterfaceSize()
382 {
383     int width = 0, height = 0;
384     if( VISIBLE( bgWidget ) )
385     {
386         width = bgWidget->widgetSize.width();
387         height = bgWidget->widgetSize.height();
388         assert( !(playlistWidget && playlistWidget->isVisible() ) );
389     }
390     else if( VISIBLE( playlistWidget ) )
391     {
392         width = playlistWidget->widgetSize.width();
393         height = playlistWidget->widgetSize.height();
394     }
395     else if( videoIsActive )
396     {
397         width =  videoWidget->widgetSize.width() ;
398         height = videoWidget->widgetSize.height();
399     }
400     else
401     {
402         width = PREF_W - addSize.width();
403         height = PREF_H - addSize.height();
404     }
405     if( VISIBLE( visualSelector ) )
406         height += visualSelector->height();
407     if( VISIBLE( advControls) )
408     {
409         height += advControls->sizeHint().height();
410     }
411     mainSize = QSize( width + addSize.width(), height + addSize.height() );
412 }
413
414 void MainInterface::resizeEvent( QResizeEvent *e )
415 {
416     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
417     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
418     {
419         SET_WH( videoWidget, e->size().width() - addSize.width(),
420                              e->size().height()  - addSize.height() );
421         videoWidget->updateGeometry();
422     }
423     if( VISIBLE( playlistWidget ) )
424     {
425         SET_WH( playlistWidget , e->size().width() - addSize.width(),
426                                  e->size().height() - addSize.height() );
427         playlistWidget->updateGeometry();
428     }
429 }
430
431 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
432                                    int *pi_y, unsigned int *pi_width,
433                                    unsigned int *pi_height )
434 {
435     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
436     if( ret )
437     {
438         videoIsActive = true;
439         if( VISIBLE( playlistWidget ) )
440         {
441             embeddedPlaylistWasActive = true;
442 //            playlistWidget->hide();
443         }
444         bool bgWasVisible = false;
445         if( VISIBLE( bgWidget) )
446         {
447             bgWasVisible = true;
448 //            bgWidget->hide();
449         }
450         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
451         {
452             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
453         }
454         else /* Background widget available, use its size */
455         {
456             /* Ok, our visualizations are bad, so don't do this for the moment
457              * use the requested size anyway */
458             // videoWidget->widgetSize = bgWidget->widgeTSize;
459             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
460         }
461 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
462         need_components_update = true;
463     }
464     return ret;
465 }
466
467 void MainInterface::releaseVideo( void *p_win )
468 {
469     videoWidget->release( p_win );
470     videoWidget->widgetSize = QSize( 0, 0 );
471     videoWidget->resize( videoWidget->widgetSize );
472
473     if( embeddedPlaylistWasActive )
474         ;//playlistWidget->show();
475     else if( bgWidget )
476         ;//bgWidget->show();
477
478     videoIsActive = false;
479     need_components_update = true;
480 }
481
482 class SetVideoOnTopQtEvent : public QEvent
483 {
484 public:
485     SetVideoOnTopQtEvent( bool _onTop ) :
486       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
487     {
488     }
489
490     bool OnTop() const
491     {
492         return onTop;
493     }
494
495 private:
496     bool onTop;
497 };
498
499 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
500 {
501     int i_ret = VLC_EGENERIC;
502     switch( i_query )
503     {
504         case VOUT_GET_SIZE:
505         {
506             unsigned int *pi_width  = va_arg( args, unsigned int * );
507             unsigned int *pi_height = va_arg( args, unsigned int * );
508             *pi_width = videoWidget->widgetSize.width();
509             *pi_height = videoWidget->widgetSize.height();
510             i_ret = VLC_SUCCESS;
511             break;
512         }
513         case VOUT_SET_SIZE:
514         {
515             unsigned int i_width  = va_arg( args, unsigned int );
516             unsigned int i_height = va_arg( args, unsigned int );
517             videoWidget->widgetSize = QSize( i_width, i_height );
518             // videoWidget->updateGeometry();
519             need_components_update = true;
520             i_ret = VLC_SUCCESS;
521             break;
522         }
523         case VOUT_SET_STAY_ON_TOP:
524         {
525             int i_arg = va_arg( args, int );
526             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
527             i_ret = VLC_SUCCESS;
528             break;
529         }
530         default:
531             msg_Warn( p_intf, "unsupported control query" );
532             break;
533     }
534     return i_ret;
535 }
536
537 void MainInterface::advanced()
538 {
539     if( !VISIBLE( advControls ) )
540     {
541         advControls->show();
542         advControlsEnabled = true;
543     }
544     else
545     {
546         advControls->hide();
547         advControlsEnabled = false;
548     }
549     doComponentsUpdate();
550 }
551
552 void MainInterface::visual()
553 {
554     if( !VISIBLE( visualSelector) )
555     {
556         visualSelector->show();
557         if( !THEMIM->getIM()->hasVideo() )
558         {
559             /* Show the background widget */
560         }
561         visualSelectorEnabled = true;
562     }
563     else
564     {
565         /* Stop any currently running visualization */
566         visualSelector->hide();
567         visualSelectorEnabled = false;
568     }
569     doComponentsUpdate();
570 }
571
572 void MainInterface::playlist()
573 {
574     // Toggle the playlist dialog
575     if( !playlistEmbeddedFlag )
576     {
577         if( playlistWidget )
578         {
579             /// \todo Destroy it
580         }
581         THEDP->playlistDialog();
582         return;
583     }
584
585     if( !playlistWidget )
586     {
587         PlaylistDialog::killInstance();
588         playlistWidget = new PlaylistWidget( p_intf );
589         ui.vboxLayout->insertWidget( 0, playlistWidget );
590         playlistWidget->widgetSize = settings->value( "playlistSize",
591                                                QSize( 650, 310 ) ).toSize();
592         playlistWidget->hide();
593     }
594     if( VISIBLE( playlistWidget ) )
595     {
596         playlistWidget->hide();
597         if( videoIsActive )
598         {
599             videoWidget->widgetSize = savedVideoSize;
600             videoWidget->resize( videoWidget->widgetSize );
601             videoWidget->updateGeometry();
602         }
603     }
604     else
605     {
606         playlistWidget->show();
607         if( videoIsActive )
608         {
609             savedVideoSize = videoWidget->widgetSize;
610             videoWidget->widgetSize.setHeight( 0 );
611             videoWidget->resize( videoWidget->widgetSize );
612             videoWidget->updateGeometry();
613         }
614         if( VISIBLE( bgWidget ) ) bgWidget->hide();
615     }
616     doComponentsUpdate();
617 }
618
619 /* Video widget cannot do this synchronously as it runs in another thread */
620 /* Well, could it, actually ? Probably dangerous ... */
621 void MainInterface::doComponentsUpdate()
622 {
623     calculateInterfaceSize();
624     resize( mainSize );
625 }
626
627 void MainInterface::undockPlaylist()
628 {
629     if( playlistWidget )
630     {
631         playlistWidget->hide();
632         playlistWidget->deleteLater();
633         ui.vboxLayout->removeWidget( playlistWidget );
634         playlistWidget = NULL;
635         playlistEmbeddedFlag = false;
636
637         menuBar()->clear();
638         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
639                                  visualSelectorEnabled);
640
641         if( videoIsActive )
642         {
643             videoWidget->widgetSize = savedVideoSize;
644             videoWidget->resize( videoWidget->widgetSize );
645             videoWidget->updateGeometry();
646         }
647
648         doComponentsUpdate();
649         THEDP->playlistDialog();
650     }
651 }
652
653 void MainInterface::customEvent( QEvent *event )
654 {
655     if( event->type() == PLDockEvent_Type )
656     {
657         PlaylistDialog::killInstance();
658         playlistEmbeddedFlag = true;
659         menuBar()->clear();
660         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
661                                 visualSelectorEnabled);
662         playlist();
663     }
664     else if ( event->type() == SetVideoOnTopEvent_Type )
665     {
666         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
667         if( p_event->OnTop() )
668             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
669         else
670             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
671         show(); /* necessary to apply window flags?? */
672     }
673 }
674
675
676 /************************************************************************
677  * D&D
678  ************************************************************************/
679 void MainInterface::dropEvent(QDropEvent *event)
680 {
681      const QMimeData *mimeData = event->mimeData();
682
683      /* D&D of a subtitles file, add it on the fly */
684      if( mimeData->urls().size() == 1 )
685      {
686         if( THEMIM->getIM()->hasInput() )
687         {
688             if( input_AddSubtitles( THEMIM->getInput(),
689                                     qtu( mimeData->urls()[0].toString() ),
690                                     VLC_TRUE ) )
691             {
692                 event->acceptProposedAction();
693                 return;
694             }
695         }
696      }
697      bool first = true;
698      foreach( QUrl url, mimeData->urls() ) {
699         QString s = url.toString();
700         if( s.length() > 0 ) {
701             playlist_Add( THEPL, qtu(s), NULL,
702                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
703                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
704             first = false;
705         }
706      }
707      event->acceptProposedAction();
708 }
709 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
710 {
711      event->acceptProposedAction();
712 }
713 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
714 {
715      event->acceptProposedAction();
716 }
717 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
718 {
719      event->accept();
720 }
721
722 /************************************************************************
723  * Other stuff
724  ************************************************************************/
725 void MainInterface::keyPressEvent( QKeyEvent *e )
726 {
727     int i_vlck = qtEventToVLCKey( e );
728     if( i_vlck >= 0 )
729     {
730         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
731         e->accept();
732     }
733     else
734         e->ignore();
735 }
736
737 void MainInterface::wheelEvent( QWheelEvent *e )
738 {
739     int i_vlckey = qtWheelEventToVLCKey( e );
740     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
741     e->accept();
742 }
743
744 void MainInterface::stop()
745 {
746     THEMIM->stop();
747 }
748
749 void MainInterface::play()
750 {
751     if( playlist_IsEmpty(THEPL) )
752     {
753         /* The playlist is empty, open a file requester */
754         THEDP->openFileDialog();
755         setStatus( 0 );
756         return;
757     }
758     THEMIM->togglePlayPause();
759 }
760
761 void MainInterface::prev()
762 {
763     THEMIM->prev();
764 }
765
766 void MainInterface::next()
767 {
768     THEMIM->next();
769 }
770
771 void MainInterface::setDisplay( float pos, int time, int length )
772 {
773     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
774     secstotimestr( psz_length, length );
775     secstotimestr( psz_time, time );
776     QString title;
777     title.sprintf( "%s/%s", psz_time, psz_length );
778     timeLabel->setText( " "+title+" " );
779 }
780
781 void MainInterface::setName( QString name )
782 {
783     input_name = name;
784     nameLabel->setText( " " + name+" " );
785
786 }
787
788 void MainInterface::setStatus( int status )
789 {
790     if( status == 1 ) // Playing
791         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
792     else
793         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
794     updateSystrayMenu( status );
795 }
796
797 #define HELP_MENU N_("Menu")
798 #define HELP_PCH N_("Previous chapter")
799 #define HELP_NCH N_("Next chapter")
800 #define HELP_PTR N_("Previous track")
801 #define HELP_NTR N_("Next track")
802
803 void MainInterface::setNavigation( int navigation )
804 {
805     // 1 = chapter, 2 = title, 0 = no
806     if( navigation == 0 )
807     {
808         ui.discFrame->hide();
809     } else if( navigation == 1 ) {
810         ui.prevSectionButton->show();
811         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
812         ui.nextSectionButton->show();
813         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
814         ui.menuButton->show();
815         ui.discFrame->show();
816     } else {
817         ui.prevSectionButton->show();
818         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
819         ui.nextSectionButton->show();
820         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
821         ui.menuButton->hide();
822         ui.discFrame->show();
823     }
824 }
825
826 static bool b_my_volume;
827
828 void MainInterface::updateOnTimer()
829 {
830     /* \todo Make this event-driven */
831     advControls->enableInput( THEMIM->getIM()->hasInput() );
832     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
833
834     if( intf_ShouldDie( p_intf ) )
835     {
836         QApplication::closeAllWindows();
837         QApplication::quit();
838     }
839     if( need_components_update )
840     {
841         doComponentsUpdate();
842         need_components_update = false;
843     }
844
845     audio_volume_t i_volume;
846     aout_VolumeGet( p_intf, &i_volume );
847     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
848     int i_gauge = ui.volumeSlider->value();
849     b_my_volume = false;
850     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
851     {
852         b_my_volume = true;
853         ui.volumeSlider->setValue( i_volume );
854         b_my_volume = false;
855     }
856 }
857
858 void MainInterface::closeEvent( QCloseEvent *e )
859 {
860     hide();
861     p_intf->b_die = VLC_TRUE;
862 }
863
864 void MainInterface::updateVolume( int sliderVolume )
865 {
866     if( !b_my_volume )
867     {
868         int i_res = sliderVolume * AOUT_VOLUME_MAX /
869                             (2*ui.volumeSlider->maximum() );
870         aout_VolumeSet( p_intf, i_res );
871     }
872 }
873
874 static int InteractCallback( vlc_object_t *p_this,
875                              const char *psz_var, vlc_value_t old_val,
876                              vlc_value_t new_val, void *param )
877 {
878     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
879     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
880     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
881     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
882     return VLC_SUCCESS;
883 }
884
885 /*****************************************************************************
886  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
887  *  We don't show the menu directly here because we don't want the
888  *  caller to block for a too long time.
889  *****************************************************************************/
890 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
891                         vlc_value_t old_val, vlc_value_t new_val, void *param )
892 {
893     intf_thread_t *p_intf = (intf_thread_t *)param;
894
895     if( p_intf->pf_show_dialog )
896     {
897         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
898                                 new_val.b_bool, 0 );
899     }
900
901     return VLC_SUCCESS;
902 }
903
904 /*****************************************************************************
905  * IntfShowCB: callback triggered by the intf-show playlist variable.
906  *****************************************************************************/
907 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
908                        vlc_value_t old_val, vlc_value_t new_val, void *param )
909 {
910     intf_thread_t *p_intf = (intf_thread_t *)param;
911     //p_intf->p_sys->b_intf_show = VLC_TRUE;
912
913     return VLC_SUCCESS;
914 }