]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4: change main interface mode from the UI thread
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2010 VideoLAN and AUTHORS
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"                    // Creation
34 #include "actions_manager.hpp"                  // killInstance
35 #include "extensions_manager.hpp"               // killInstance
36
37 #include "util/customwidgets.hpp"               // qtEventToVLCKey, QVLCStackedWidget
38 #include "util/qt_dirs.hpp"                     // toNativeSeparators
39
40 #include "components/interface_widgets.hpp"     // bgWidget, videoWidget
41 #include "components/controller.hpp"            // controllers
42 #include "components/playlist/playlist.hpp"     // plWidget
43 #include "dialogs/firstrun.hpp"                 // First Run
44
45 #include "menus.hpp"                            // Menu creation
46 #include "recents.hpp"                          // RecentItems when DnD
47
48 #include <QCloseEvent>
49 #include <QKeyEvent>
50
51 #include <QUrl>
52 #include <QSize>
53 #include <QDate>
54
55 #include <QMenu>
56 #include <QMenuBar>
57 #include <QStatusBar>
58 #include <QLabel>
59 #include <QStackedWidget>
60
61 #include <vlc_keys.h>                       /* Wheel event */
62 #include <vlc_vout_display.h>               /* vout_thread_t and VOUT_ events */
63
64 // #define DEBUG_INTF
65
66 /* Callback prototypes */
67 static int PopupMenuCB( 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 IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
70                        vlc_value_t old_val, vlc_value_t new_val, void *param );
71
72 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
73 {
74     /* Variables initialisation */
75     bgWidget             = NULL;
76     videoWidget          = NULL;
77     playlistWidget       = NULL;
78     stackCentralOldWidget= NULL;
79 #ifndef HAVE_MAEMO
80     sysTray              = NULL;
81 #endif
82     fullscreenControls   = NULL;
83     cryptedLabel         = NULL;
84     controls             = NULL;
85     inputC               = NULL;
86
87     b_hideAfterCreation  = false; // --qt-start-minimized
88     playlistVisible      = false;
89     input_name           = "";
90
91
92     /* Ask for Privacy */
93     FirstRun::CheckAndRun( this, p_intf );
94
95     /**
96      *  Configuration and settings
97      *  Pre-building of interface
98      **/
99     /* Main settings */
100     setFocusPolicy( Qt::StrongFocus );
101     setAcceptDrops( true );
102     setWindowRole( "vlc-main" );
103     setWindowIcon( QApplication::windowIcon() );
104     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
105
106     /* Is video in embedded in the UI or not */
107     b_videoEmbedded = var_InheritBool( p_intf, "embedded-video" );
108
109     /* Does the interface resize to video size or the opposite */
110     b_autoresize = var_InheritBool( p_intf, "qt-video-autoresize" );
111
112     /* Are we in the enhanced always-video mode or not ? */
113     i_visualmode = var_InheritInteger( p_intf, "qt-minimal-view" );
114
115     /* Do we want anoying popups or not */
116     b_notificationEnabled = var_InheritBool( p_intf, "qt-notification" );
117
118     /* Set the other interface settings */
119     settings = getSettings();
120     settings->beginGroup( "MainWindow" );
121
122     /* */
123     b_plDocked = getSettings()->value( "pl-dock-status", true ).toBool();
124
125     settings->endGroup( );
126
127     /**************
128      * Status Bar *
129      **************/
130     createStatusBar();
131
132     /**************************
133      *  UI and Widgets design
134      **************************/
135     setVLCWindowsTitle();
136
137     /************
138      * Menu Bar *
139      ************/
140     QVLCMenu::createMenuBar( this, p_intf );
141     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
142              this, destroyPopupMenu() );
143
144     createMainWidget( settings );
145     /*********************************
146      * Create the Systray Management *
147      *********************************/
148     initSystray();
149
150     /********************
151      * Input Manager    *
152      ********************/
153     MainInputManager::getInstance( p_intf );
154
155 #ifdef WIN32
156     himl = NULL;
157     p_taskbl = NULL;
158     taskbar_wmsg = RegisterWindowMessage("TaskbarButtonCreated");
159 #endif
160
161     /************************************************************
162      * Connect the input manager to the GUI elements it manages *
163      ************************************************************/
164     /**
165      * Connects on nameChanged()
166      * Those connects are different because options can impeach them to trigger.
167      **/
168     /* Main Interface statusbar */
169     CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
170              this, setName( const QString& ) );
171     /* and systray */
172 #ifndef HAVE_MAEMO
173     if( sysTray )
174     {
175         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
176                  this, updateSystrayTooltipName( const QString& ) );
177     }
178 #endif
179     /* and title of the Main Interface*/
180     if( var_InheritBool( p_intf, "qt-name-in-title" ) )
181     {
182         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
183                  this, setVLCWindowsTitle( const QString& ) );
184     }
185
186     /**
187      * CONNECTS on PLAY_STATUS
188      **/
189     /* Status on the systray */
190 #ifndef HAVE_MAEMO
191     if( sysTray )
192     {
193         CONNECT( THEMIM->getIM(), statusChanged( int ),
194                  this, updateSystrayTooltipStatus( int ) );
195     }
196 #endif
197
198     /* END CONNECTS ON IM */
199
200     /* VideoWidget connects for asynchronous calls */
201     connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
202              this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
203              Qt::BlockingQueuedConnection );
204     connect( this, SIGNAL(askReleaseVideo( void )),
205              this, SLOT(releaseVideoSlot( void )),
206              Qt::BlockingQueuedConnection );
207     CONNECT( this, askVideoOnTop(bool), this, setVideoOnTop(bool));
208
209     if( videoWidget )
210     {
211         if( b_autoresize )
212         {
213             CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
214                      this, setVideoSize( unsigned int, unsigned int ) );
215             CONNECT( videoWidget, sizeChanged( int, int ),
216                      this, resizeStack( int,  int ) );
217         }
218         CONNECT( this, askVideoSetFullScreen( bool ),
219                  videoWidget, SetFullScreen( bool ) );
220         CONNECT( videoWidget, keyPressed( QKeyEvent * ),
221                  this, handleKeyPress( QKeyEvent * ) );
222     }
223
224     CONNECT( THEDP, toolBarConfUpdated(), this, recreateToolbars() );
225
226     /** END of CONNECTS**/
227
228
229     /************
230      * Callbacks
231      ************/
232     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
233
234     /* Register callback for the intf-popupmenu variable */
235     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
236
237     /* Playlist */
238     int i_plVis = settings->value( "MainWindow/playlist-visible", false ).toBool();
239
240     if( i_plVis ) togglePlaylist();
241
242     /**** FINAL SIZING and placement of interface */
243     settings->beginGroup( "MainWindow" );
244     QVLCTools::restoreWidgetPosition( settings, this, QSize(400, 100) );
245     settings->endGroup();
246
247     /* Final sizing and showing */
248     setVisible( !b_hideAfterCreation );
249
250     setMinimumWidth( __MAX( controls->sizeHint().width(),
251                             menuBar()->sizeHint().width() ) + 30 );
252
253     /* Switch to minimal view if needed, must be called after the show() */
254     if( i_visualmode )
255         toggleMinimalView( true );
256 }
257
258 MainInterface::~MainInterface()
259 {
260     /* Unsure we hide the videoWidget before destroying it */
261     if( stackCentralOldWidget == videoWidget )
262         showTab( bgWidget );
263
264 #ifdef WIN32
265     if( himl )
266         ImageList_Destroy( himl );
267     if(p_taskbl)
268         p_taskbl->vt->Release(p_taskbl);
269     CoUninitialize();
270 #endif
271
272     /* Be sure to kill the actionsManager... Only used in the MI and control */
273     ActionsManager::killInstance();
274
275     /* Idem */
276     ExtensionsManager::killInstance();
277
278     /* Delete the FSC controller */
279     delete fullscreenControls;
280
281     /* Save states */
282     settings->beginGroup( "MainWindow" );
283
284     settings->setValue( "pl-dock-status", b_plDocked );
285     /* Save playlist state */
286     if( playlistWidget )
287     {
288         settings->setValue( "playlist-visible",
289                             isPlDocked() ?
290                             playlistVisible :
291                             playlistWidget->isVisible() /* FIXME */ );
292     }
293
294     settings->setValue( "adv-controls",
295                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
296
297     /* Save the stackCentralW sizes */
298     settings->setValue( "bgSize", stackWidgetsSizes[bgWidget] );
299     settings->setValue( "playlistSize", stackWidgetsSizes[playlistWidget] );
300
301     /* Save this size */
302     QVLCTools::saveWidgetPosition(settings, this);
303
304     settings->endGroup();
305
306     /* Save undocked playlist size */
307     if( playlistWidget && !isPlDocked() )
308         QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
309
310     delete playlistWidget;
311
312     delete statusBar();
313
314     /* Unregister callbacks */
315     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
316     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
317
318     p_intf->p_sys->p_mi = NULL;
319 }
320
321 /*****************************
322  *   Main UI handling        *
323  *****************************/
324 void MainInterface::recreateToolbars()
325 {
326     bool b_adv = getControlsVisibilityStatus() & CONTROLS_ADVANCED;
327
328     settings->beginGroup( "MainWindow" );
329     delete controls;
330     delete inputC;
331
332     controls = new ControlsWidget( p_intf, b_adv, this );
333     inputC = new InputControlsWidget( p_intf, this );
334
335     if( fullscreenControls )
336     {
337         delete fullscreenControls;
338         fullscreenControls = new FullscreenControllerWidget( p_intf, this );
339         CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
340                  this, handleKeyPress( QKeyEvent * ) );
341     }
342     mainLayout->insertWidget( 2, inputC );
343     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
344                               controls );
345     settings->endGroup();
346 }
347
348 void MainInterface::createMainWidget( QSettings *settings )
349 {
350     /* Create the main Widget and the mainLayout */
351     QWidget *main = new QWidget;
352     setCentralWidget( main );
353     mainLayout = new QVBoxLayout( main );
354     main->setContentsMargins( 0, 0, 0, 0 );
355     mainLayout->setSpacing( 0 ); mainLayout->setMargin( 0 );
356
357     /* */
358     stackCentralW = new QVLCStackedWidget( main );
359
360     /* Bg Cone */
361     bgWidget = new BackgroundWidget( p_intf );
362     stackCentralW->addWidget( bgWidget );
363
364     /* And video Outputs */
365     if( b_videoEmbedded )
366     {
367         videoWidget = new VideoWidget( p_intf );
368         stackCentralW->addWidget( videoWidget );
369     }
370     mainLayout->insertWidget( 1, stackCentralW );
371
372     settings->beginGroup( "MainWindow" );
373     stackWidgetsSizes[bgWidget] = settings->value( "bgSize", QSize( 400, 0 ) ).toSize();
374     /* Resize even if no-auto-resize, because we are at creation */
375     resizeStack( stackWidgetsSizes[bgWidget].width(), stackWidgetsSizes[bgWidget].height() );
376
377     /* Create the CONTROLS Widget */
378     controls = new ControlsWidget( p_intf,
379                    settings->value( "adv-controls", false ).toBool(), this );
380     inputC = new InputControlsWidget( p_intf, this );
381
382     mainLayout->insertWidget( 2, inputC );
383     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
384                               controls );
385
386     /* Visualisation, disabled for now, they SUCK */
387     #if 0
388     visualSelector = new VisualSelector( p_intf );
389     mainLayout->insertWidget( 0, visualSelector );
390     visualSelector->hide();
391     #endif
392
393     settings->endGroup();
394
395     /* Enable the popup menu in the MI */
396     main->setContextMenuPolicy( Qt::CustomContextMenu );
397     CONNECT( main, customContextMenuRequested( const QPoint& ),
398              this, popupMenu( const QPoint& ) );
399
400     if ( depth() > 8 ) /* 8bit depth has too many issues with opacity */
401         /* Create the FULLSCREEN CONTROLS Widget */
402         if( var_InheritBool( p_intf, "qt-fs-controller" ) )
403         {
404             fullscreenControls = new FullscreenControllerWidget( p_intf, this );
405             CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
406                      this, handleKeyPress( QKeyEvent * ) );
407         }
408 }
409
410 inline void MainInterface::initSystray()
411 {
412 #ifndef HAVE_MAEMO
413     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
414     bool b_systrayWanted = var_InheritBool( p_intf, "qt-system-tray" );
415
416     if( var_InheritBool( p_intf, "qt-start-minimized") )
417     {
418         if( b_systrayAvailable )
419         {
420             b_systrayWanted = true;
421             b_hideAfterCreation = true;
422         }
423         else
424             msg_Err( p_intf, "cannot start minimized without system tray bar" );
425     }
426
427     if( b_systrayAvailable && b_systrayWanted )
428         createSystray();
429 #endif
430 }
431
432 inline void MainInterface::createStatusBar()
433 {
434     /****************
435      *  Status Bar  *
436      ****************/
437     /* Widgets Creation*/
438     QStatusBar *statusBarr = statusBar();
439
440     TimeLabel *timeLabel = new TimeLabel( p_intf );
441     nameLabel = new QLabel( this );
442     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
443                                       | Qt::TextSelectableByKeyboard );
444     SpeedLabel *speedLabel = new SpeedLabel( p_intf, this );
445
446     /* Styling those labels */
447     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
448     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
449     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
450     timeLabel->setStyleSheet(
451             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
452     speedLabel->setStyleSheet(
453             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
454
455     /* and adding those */
456     statusBarr->addWidget( nameLabel, 8 );
457     statusBarr->addPermanentWidget( speedLabel, 0 );
458     statusBarr->addPermanentWidget( timeLabel, 0 );
459
460     /* timeLabel behaviour:
461        - double clicking opens the goto time dialog
462        - right-clicking and clicking just toggle between remaining and
463          elapsed time.*/
464     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
465
466     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
467              this, showCryptedLabel( bool ) );
468
469     CONNECT( THEMIM->getIM(), seekRequested( float ),
470              timeLabel, setDisplayPosition( float ) );
471 }
472
473 /**********************************************************************
474  * Handling of sizing of the components
475  **********************************************************************/
476
477 void MainInterface::debug()
478 {
479 #ifdef DEBUG_INTF
480     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
481     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
482     msg_Dbg( p_intf, "minimumsize: %i - %i", minimumSize().height(), minimumSize().width() );
483
484     msg_Dbg( p_intf, "Stack size: %i - %i", stackCentralW->size().height(), stackCentralW->size().width() );
485     msg_Dbg( p_intf, "Stack sizeHint: %i - %i", stackCentralW->sizeHint().height(), stackCentralW->sizeHint().width() );
486     msg_Dbg( p_intf, "Central size: %i - %i", centralWidget()->size().height(), centralWidget()->size().width() );
487 #endif
488 }
489
490 inline void MainInterface::showVideo() { showTab( videoWidget ); }
491 inline void MainInterface::restoreStackOldWidget()
492             { showTab( stackCentralOldWidget ); }
493
494 inline void MainInterface::showTab( QWidget *widget )
495 {
496 #ifdef DEBUG_INTF
497     msg_Warn( p_intf, "Old stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
498 #endif
499
500     stackCentralOldWidget = stackCentralW->currentWidget();
501     stackWidgetsSizes[stackCentralOldWidget] = stackCentralW->size();
502
503     stackCentralW->setCurrentWidget( widget );
504     if( b_autoresize )
505         resizeStack( stackWidgetsSizes[widget].width(), stackWidgetsSizes[widget].height() );
506
507 #ifdef DEBUG_INTF
508     msg_Warn( p_intf, "State change %i",  stackCentralW->currentIndex() );
509     msg_Warn( p_intf, "New stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
510 #endif
511 }
512
513 void MainInterface::destroyPopupMenu()
514 {
515     QVLCMenu::PopupMenu( p_intf, false );
516 }
517
518 void MainInterface::popupMenu( const QPoint &p )
519 {
520     QVLCMenu::PopupMenu( p_intf, true );
521 }
522
523 void MainInterface::toggleFSC()
524 {
525    if( !fullscreenControls ) return;
526
527    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
528    QApplication::postEvent( fullscreenControls, eShow );
529 }
530
531 /****************************************************************************
532  * Video Handling
533  ****************************************************************************/
534
535 /**
536  * NOTE:
537  * You must not change the state of this object or other Qt4 UI objects,
538  * from the video output thread - only from the Qt4 UI main loop thread.
539  * All window provider queries must be handled through signals or events.
540  * That's why we have all those emit statements...
541  */
542 WId MainInterface::getVideo( int *pi_x, int *pi_y,
543                              unsigned int *pi_width, unsigned int *pi_height )
544 {
545     if( !videoWidget )
546         return 0;
547
548     /* This is a blocking call signal. Results are returned through pointers.
549      * Beware of deadlocks! */
550     WId id;
551     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
552     return id;
553 }
554
555 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
556                                   unsigned *pi_width, unsigned *pi_height )
557 {
558     /* Request the videoWidget */
559     WId ret = videoWidget->request( pi_x, pi_y,
560                                     pi_width, pi_height, !b_autoresize );
561     *p_id = ret;
562     if( ret ) /* The videoWidget is available */
563     {
564         /* Consider the video active now */
565         showVideo();
566
567         /* Ask videoWidget to resize correctly, if we are in normal mode */
568         if( !isFullScreen() && !isMaximized() && b_autoresize )
569             videoWidget->SetSizing( *pi_width, *pi_height );
570     }
571 }
572
573 /* Asynchronous call from the WindowClose function */
574 void MainInterface::releaseVideo( void )
575 {
576     emit askReleaseVideo();
577 }
578
579 /* Function that is CONNECTED to the previous emit */
580 void MainInterface::releaseVideoSlot( void )
581 {
582     videoWidget->release();
583     setVideoOnTop( false );
584
585     if( stackCentralW->currentWidget() == videoWidget )
586         restoreStackOldWidget();
587
588     /* We don't want to have a blank video to popup */
589     stackCentralOldWidget = bgWidget;
590 }
591
592 void MainInterface::setVideoSize( unsigned int w, unsigned int h )
593 {
594     if( isFullScreen() || isMaximized() )
595         showNormal();
596     videoWidget->SetSizing( w, h );
597 }
598
599 /* Slot to change the video always-on-top flag.
600  * Emit askVideoOnTop() to invoke this from other thread. */
601 void MainInterface::setVideoOnTop( bool on_top )
602 {
603     Qt::WindowFlags oldflags = windowFlags(), newflags;
604
605     if( on_top )
606         newflags = oldflags | Qt::WindowStaysOnTopHint;
607     else
608         newflags = oldflags & ~Qt::WindowStaysOnTopHint;
609
610     if( newflags != oldflags )
611     {
612         setWindowFlags( newflags );
613         show(); /* necessary to apply window flags */
614     }
615 }
616
617 /* Asynchronous call from WindowControl function */
618 int MainInterface::controlVideo( int i_query, va_list args )
619 {
620     switch( i_query )
621     {
622     case VOUT_WINDOW_SET_SIZE:
623     {
624         unsigned int i_width  = va_arg( args, unsigned int );
625         unsigned int i_height = va_arg( args, unsigned int );
626
627         emit askVideoToResize( i_width, i_height );
628         return VLC_SUCCESS;
629     }
630     case VOUT_WINDOW_SET_STATE:
631     {
632         unsigned i_arg = va_arg( args, unsigned );
633         unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
634
635         emit askVideoOnTop( on_top != 0 );
636         return VLC_SUCCESS;
637     }
638     case VOUT_WINDOW_SET_FULLSCREEN:
639     {
640         bool b_fs = va_arg( args, int );
641         emit askVideoSetFullScreen( b_fs );
642         return VLC_SUCCESS;
643     }
644     default:
645         msg_Warn( p_intf, "unsupported control query" );
646         return VLC_EGENERIC;
647     }
648 }
649
650 /*****************************************************************************
651  * Playlist, Visualisation and Menus handling
652  *****************************************************************************/
653 /**
654  * Toggle the playlist widget or dialog
655  **/
656 void MainInterface::createPlaylist()
657 {
658     playlistWidget = new PlaylistWidget( p_intf, this );
659
660     if( b_plDocked )
661     {
662         stackCentralW->addWidget( playlistWidget );
663         stackWidgetsSizes[playlistWidget] = settings->value( "playlistSize", QSize( 500, 250 ) ).toSize();
664     }
665     else
666     {
667 #ifdef WIN32
668         playlistWidget->setParent( NULL );
669 #endif
670         playlistWidget->setWindowFlags( Qt::Window );
671
672         /* This will restore the geometry but will not work for position,
673            because of parenting */
674         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
675                 playlistWidget, QSize( 600, 300 ) );
676     }
677 }
678
679 void MainInterface::togglePlaylist()
680 {
681     if( !playlistWidget )
682     {
683         createPlaylist();
684     }
685
686     if( b_plDocked )
687     {
688         /* Playlist is not visible, show it */
689         if( stackCentralW->currentWidget() != playlistWidget )
690         {
691             showTab( playlistWidget );
692         }
693         else /* Hide it! */
694         {
695             restoreStackOldWidget();
696         }
697         playlistVisible = ( stackCentralW->currentWidget() == playlistWidget );
698     }
699     else
700     {
701 #ifdef WIN32
702         playlistWidget->setParent( NULL );
703 #endif
704         playlistWidget->setWindowFlags( Qt::Window );
705         playlistVisible = !playlistVisible;
706         playlistWidget->setVisible( playlistVisible );
707     }
708     debug();
709 }
710
711 void MainInterface::dockPlaylist( bool p_docked )
712 {
713     if( b_plDocked == p_docked ) return;
714     b_plDocked = p_docked;
715
716     if( !playlistWidget ) return; /* Playlist wasn't created yet */
717     if( !p_docked )
718     {
719         stackCentralW->removeWidget( playlistWidget );
720 #ifdef WIN32
721         playlistWidget->setParent( NULL );
722 #endif
723         playlistWidget->setWindowFlags( Qt::Window );
724         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
725                 playlistWidget, QSize( 600, 300 ) );
726         playlistWidget->show();
727         restoreStackOldWidget();
728     }
729     else
730     {
731         QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
732         playlistWidget->setWindowFlags( Qt::Widget ); // Probably a Qt bug here
733         // It would be logical that QStackWidget::addWidget reset the flags...
734         stackCentralW->addWidget( playlistWidget );
735         showTab( playlistWidget );
736     }
737     playlistVisible = true;
738 }
739
740 /*
741   If b_switch is false, then we are normalView
742  */
743 void MainInterface::toggleMinimalView( bool b_switch )
744 {
745     if( i_visualmode == 0 && b_autoresize ) /* Normal mode */
746     {
747         if( stackCentralW->currentWidget() == bgWidget )
748         {
749             if( stackCentralW->height() < 16 )
750             {
751                 resizeStack( stackCentralW->width(), 100 );
752             }
753         }
754     }
755
756     menuBar()->setVisible( !b_switch );
757     controls->setVisible( !b_switch );
758     statusBar()->setVisible( !b_switch );
759     inputC->setVisible( !b_switch );
760
761     emit minimalViewToggled( b_switch );
762 }
763
764 /* toggling advanced controls buttons */
765 void MainInterface::toggleAdvancedButtons()
766 {
767     controls->toggleAdvanced();
768 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
769 }
770
771 /* Get the visibility status of the controls (hidden or not, advanced or not) */
772 int MainInterface::getControlsVisibilityStatus()
773 {
774     if( !controls ) return 0;
775     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
776                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
777 }
778
779 #if 0
780 void MainInterface::visual()
781 {
782     if( !VISIBLE( visualSelector) )
783     {
784         visualSelector->show();
785         if( !THEMIM->getIM()->hasVideo() )
786         {
787             /* Show the background widget */
788         }
789         visualSelectorEnabled = true;
790     }
791     else
792     {
793         /* Stop any currently running visualization */
794         visualSelector->hide();
795         visualSelectorEnabled = false;
796     }
797 }
798 #endif
799
800 /************************************************************************
801  * Other stuff
802  ************************************************************************/
803 void MainInterface::setName( const QString& name )
804 {
805     input_name = name; /* store it for the QSystray use */
806     /* Display it in the status bar, but also as a Tooltip in case it doesn't
807        fit in the label */
808     nameLabel->setText( " " + name + " " );
809     nameLabel->setToolTip( " " + name +" " );
810 }
811
812 /**
813  * Give the decorations of the Main Window a correct Name.
814  * If nothing is given, set it to VLC...
815  **/
816 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
817 {
818     if( aTitle.isEmpty() )
819     {
820         setWindowTitle( qtr( "VLC media player" ) );
821     }
822     else
823     {
824         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
825     }
826 }
827
828 void MainInterface::showCryptedLabel( bool b_show )
829 {
830     if( cryptedLabel == NULL )
831     {
832         cryptedLabel = new QLabel;
833         // The lock icon is not the right one for DRM protection/scrambled.
834         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
835         cryptedLabel->setText( "DRM" );
836         statusBar()->addWidget( cryptedLabel );
837     }
838
839     cryptedLabel->setVisible( b_show );
840 }
841
842 void MainInterface::showBuffering( float f_cache )
843 {
844     QString amount = QString("Buffering: %1%").arg( (int)(100*f_cache) );
845     statusBar()->showMessage( amount, 1000 );
846 }
847
848 /*****************************************************************************
849  * Systray Icon and Systray Menu
850  *****************************************************************************/
851 #ifndef HAVE_MAEMO
852 /**
853  * Create a SystemTray icon and a menu that would go with it.
854  * Connects to a click handler on the icon.
855  **/
856 void MainInterface::createSystray()
857 {
858     QIcon iconVLC;
859     if( QDate::currentDate().dayOfYear() >= 354 )
860         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
861     else
862         iconVLC =  QIcon( ":/logo/vlc128.png" );
863     sysTray = new QSystemTrayIcon( iconVLC, this );
864     sysTray->setToolTip( qtr( "VLC media player" ));
865
866     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
867     systrayMenu->setIcon( iconVLC );
868
869     QVLCMenu::updateSystrayMenu( this, p_intf, true );
870     sysTray->show();
871
872     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
873             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
874 }
875
876 /**
877  * Updates the Systray Icon's menu and toggle the main interface
878  */
879 void MainInterface::toggleUpdateSystrayMenu()
880 {
881     /* If hidden, show it */
882     if( isHidden() )
883     {
884         show();
885         activateWindow();
886     }
887     else if( isMinimized() )
888     {
889         /* Minimized */
890         showNormal();
891         activateWindow();
892     }
893     else
894     {
895         /* Visible (possibly under other windows) */
896 #ifdef WIN32
897         /* check if any visible window is above vlc in the z-order,
898          * but ignore the ones always on top
899          * and the ones which can't be activated */
900         WINDOWINFO wi;
901         HWND hwnd;
902         wi.cbSize = sizeof( WINDOWINFO );
903         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
904                 hwnd && ( !IsWindowVisible( hwnd ) ||
905                     ( GetWindowInfo( hwnd, &wi ) &&
906                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
907                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
908             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
909                 (wi.dwExStyle&WS_EX_TOPMOST) )
910             {
911                 hide();
912             }
913             else
914             {
915                 activateWindow();
916             }
917 #else
918         hide();
919 #endif
920     }
921     QVLCMenu::updateSystrayMenu( this, p_intf );
922 }
923
924 void MainInterface::handleSystrayClick(
925                                     QSystemTrayIcon::ActivationReason reason )
926 {
927     switch( reason )
928     {
929         case QSystemTrayIcon::Trigger:
930         case QSystemTrayIcon::DoubleClick:
931             toggleUpdateSystrayMenu();
932             break;
933         case QSystemTrayIcon::MiddleClick:
934             sysTray->showMessage( qtr( "VLC media player" ),
935                     qtr( "Control menu for the player" ),
936                     QSystemTrayIcon::Information, 3000 );
937             break;
938         default:
939             break;
940     }
941 }
942
943 /**
944  * Updates the name of the systray Icon tooltip.
945  * Doesn't check if the systray exists, check before you call it.
946  **/
947 void MainInterface::updateSystrayTooltipName( const QString& name )
948 {
949     if( name.isEmpty() )
950     {
951         sysTray->setToolTip( qtr( "VLC media player" ) );
952     }
953     else
954     {
955         sysTray->setToolTip( name );
956         if( b_notificationEnabled && ( isHidden() || isMinimized() ) )
957         {
958             sysTray->showMessage( qtr( "VLC media player" ), name,
959                     QSystemTrayIcon::NoIcon, 3000 );
960         }
961     }
962
963     QVLCMenu::updateSystrayMenu( this, p_intf );
964 }
965
966 /**
967  * Updates the status of the systray Icon tooltip.
968  * Doesn't check if the systray exists, check before you call it.
969  **/
970 void MainInterface::updateSystrayTooltipStatus( int i_status )
971 {
972     switch( i_status )
973     {
974     case PLAYING_S:
975         sysTray->setToolTip( input_name );
976         break;
977     case PAUSE_S:
978         sysTray->setToolTip( input_name + " - " + qtr( "Paused") );
979         break;
980     default:
981         sysTray->setToolTip( qtr( "VLC media player" ) );
982         break;
983     }
984     QVLCMenu::updateSystrayMenu( this, p_intf );
985 }
986 #endif
987
988 /************************************************************************
989  * D&D Events
990  ************************************************************************/
991 void MainInterface::dropEvent(QDropEvent *event)
992 {
993     dropEventPlay( event, true );
994 }
995
996 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
997 {
998     event->setDropAction( Qt::CopyAction );
999     if( !event->possibleActions() & Qt::CopyAction )
1000         return;
1001
1002     const QMimeData *mimeData = event->mimeData();
1003
1004     /* D&D of a subtitles file, add it on the fly */
1005     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1006     {
1007         if( !input_AddSubtitle( THEMIM->getInput(),
1008                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1009                  true ) )
1010         {
1011             event->accept();
1012             return;
1013         }
1014     }
1015
1016     bool first = b_play;
1017     foreach( const QUrl &url, mimeData->urls() )
1018     {
1019         QString s = toNativeSeparators( url.toLocalFile() );
1020
1021         if( s.length() > 0 ) {
1022             char* psz_uri = make_URI( qtu(s) );
1023             playlist_Add( THEPL, psz_uri, NULL,
1024                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1025                           PLAYLIST_END, true, pl_Unlocked );
1026             free( psz_uri );
1027             first = false;
1028             RecentsMRL::getInstance( p_intf )->addRecent( s );
1029         }
1030     }
1031     event->accept();
1032 }
1033 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1034 {
1035      event->acceptProposedAction();
1036 }
1037 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1038 {
1039      event->acceptProposedAction();
1040 }
1041 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1042 {
1043      event->accept();
1044 }
1045
1046 /************************************************************************
1047  * Events stuff
1048  ************************************************************************/
1049 void MainInterface::keyPressEvent( QKeyEvent *e )
1050 {
1051     handleKeyPress( e );
1052 }
1053
1054 void MainInterface::handleKeyPress( QKeyEvent *e )
1055 {
1056     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1057           && !menuBar()->isVisible() )
1058     {
1059         toggleMinimalView( false );
1060         e->accept();
1061     }
1062
1063     int i_vlck = qtEventToVLCKey( e );
1064     if( i_vlck > 0 )
1065     {
1066         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1067         e->accept();
1068     }
1069     else
1070         e->ignore();
1071 }
1072
1073 void MainInterface::wheelEvent( QWheelEvent *e )
1074 {
1075     int i_vlckey = qtWheelEventToVLCKey( e );
1076     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1077     e->accept();
1078 }
1079
1080 void MainInterface::closeEvent( QCloseEvent *e )
1081 {
1082     e->accept();
1083     hide();
1084     THEDP->quit();
1085 }
1086
1087 void MainInterface::toggleFullScreen()
1088 {
1089     if( isFullScreen() )
1090     {
1091         showNormal();
1092         emit fullscreenInterfaceToggled( false );
1093     }
1094     else
1095     {
1096         showFullScreen();
1097         emit fullscreenInterfaceToggled( true );
1098     }
1099 }
1100
1101 /*****************************************************************************
1102  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1103  *  We don't show the menu directly here because we don't want the
1104  *  caller to block for a too long time.
1105  *****************************************************************************/
1106 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1107                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1108 {
1109     intf_thread_t *p_intf = (intf_thread_t *)param;
1110
1111     if( p_intf->pf_show_dialog )
1112     {
1113         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1114                                 new_val.b_bool, NULL );
1115     }
1116
1117     return VLC_SUCCESS;
1118 }
1119
1120 /*****************************************************************************
1121  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1122  *****************************************************************************/
1123 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1124                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1125 {
1126     intf_thread_t *p_intf = (intf_thread_t *)param;
1127     p_intf->p_sys->p_mi->toggleFSC();
1128
1129     /* Show event */
1130      return VLC_SUCCESS;
1131 }