]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
Avoid a segfault when services_discovery_GetServicesNames fail.
[vlc] / modules / gui / qt4 / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : Qt menus
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc/vlc.h>
30
31 #include <vlc_intf_strings.h>
32
33 #include "main_interface.hpp"
34 #include "menus.hpp"
35 #include "dialogs_provider.hpp"
36 #include "input_manager.hpp"
37
38 #include <QMenu>
39 #include <QMenuBar>
40 #include <QAction>
41 #include <QActionGroup>
42 #include <QSignalMapper>
43 #include <QSystemTrayIcon>
44
45 enum
46 {
47     ITEM_NORMAL,
48     ITEM_CHECK,
49     ITEM_RADIO
50 };
51
52 static QActionGroup *currentGroup;
53
54 // Add static entries to menus
55 void addDPStaticEntry( QMenu *menu,
56                        const QString text,
57                        const char *help,
58                        const char *icon,
59                        const char *member,
60                        const char *shortcut )
61 {
62     if( !EMPTY_STR( icon ) > 0 )
63     {
64         if( !EMPTY_STR( shortcut ) > 0 )
65             menu->addAction( QIcon( icon ), text, THEDP, member, qtr( shortcut ) );
66         else
67             menu->addAction( QIcon( icon ), text, THEDP, member );
68     }
69     else
70     {
71         if( !EMPTY_STR( shortcut ) > 0 )
72             menu->addAction( text, THEDP, member, qtr( shortcut ) );
73         else
74             menu->addAction( text, THEDP, member );
75     }
76 }
77
78 void addMIMStaticEntry( intf_thread_t *p_intf,
79                         QMenu *menu,
80                         const QString text,
81                         const char *help,
82                         const char *icon,
83                         const char *member )
84 {
85     if( strlen( icon ) > 0 )
86     {
87         QAction *action = menu->addAction( text, THEMIM,  member );
88         action->setIcon( QIcon( icon ) );
89     }
90     else
91     {
92         menu->addAction( text, THEMIM, member );
93     }
94 }
95
96 /*****************************************************************************
97  * Definitions of variables for the dynamic menus
98  *****************************************************************************/
99 #define PUSH_VAR( var ) varnames.push_back( var ); \
100     objects.push_back( p_object->i_object_id )
101
102 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
103     objects.push_back( 0 ); varnames.push_back( "" ); \
104     i_last_separator = objects.size(); }
105
106 static int InputAutoMenuBuilder( vlc_object_t *p_object,
107         vector<int> &objects,
108         vector<const char *> &varnames )
109 {
110     PUSH_VAR( "bookmark" );
111     PUSH_VAR( "title" );
112     PUSH_VAR( "chapter" );
113     PUSH_VAR( "program" );
114     PUSH_VAR( "navigation" );
115     PUSH_VAR( "dvd_menus" );
116     return VLC_SUCCESS;
117 }
118
119 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
120         vector<int> &objects,
121         vector<const char *> &varnames )
122 {
123     PUSH_VAR( "fullscreen" );
124     PUSH_VAR( "zoom" );
125     PUSH_VAR( "deinterlace" );
126     PUSH_VAR( "aspect-ratio" );
127     PUSH_VAR( "crop" );
128     PUSH_VAR( "video-on-top" );
129     PUSH_VAR( "directx-wallpaper" );
130     PUSH_VAR( "video-snapshot" );
131
132     vlc_object_t *p_dec_obj = ( vlc_object_t * )vlc_object_find( p_object,
133             VLC_OBJECT_DECODER,
134             FIND_PARENT );
135     if( p_dec_obj != NULL )
136     {
137         vlc_object_t *p_object = p_dec_obj;
138         PUSH_VAR( "ffmpeg-pp-q" );
139         vlc_object_release( p_dec_obj );
140     }
141     return VLC_SUCCESS;
142 }
143
144 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
145         vector<int> &objects,
146         vector<const char *> &varnames )
147 {
148     PUSH_VAR( "audio-device" );
149     PUSH_VAR( "audio-channels" );
150     PUSH_VAR( "visual" );
151     PUSH_VAR( "equalizer" );
152     return VLC_SUCCESS;
153 }
154
155 /*****************************************************************************
156  * All normal menus
157  * Simple Code
158  *****************************************************************************/
159
160 #define BAR_ADD( func, title ) { \
161     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); }
162
163 #define BAR_DADD( func, title, id ) { \
164     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); \
165     MenuFunc *f = new MenuFunc( menu, id ); \
166     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
167     THEDP->menusUpdateMapper->setMapping( menu, f ); }
168
169 /**
170  * Main Menu Bar Creation
171  **/
172 void QVLCMenu::createMenuBar( MainInterface *mi,
173                               intf_thread_t *p_intf,
174                               bool visual_selector_enabled )
175 {
176     QMenuBar *bar = mi->menuBar();
177     BAR_ADD( FileMenu(), qtr( "&Media" ) );
178     BAR_ADD( PlaylistMenu( p_intf, mi ), qtr( "&Playlist" ) );
179     BAR_ADD( ToolsMenu( p_intf, mi, visual_selector_enabled, true ),
180              qtr( "&Tools" ) );
181     BAR_DADD( AudioMenu( p_intf, NULL ), qtr( "&Audio" ), 2 );
182     BAR_DADD( VideoMenu( p_intf, NULL ), qtr( "&Video" ), 1 );
183     BAR_DADD( NavigMenu( p_intf, NULL ), qtr( "&Playback" ), 3 );
184
185     BAR_ADD( HelpMenu(), qtr( "&Help" ) );
186 }
187 #undef BAR_ADD
188 #undef BAR_DADD
189
190 /**
191  * Media ( File ) Menu
192  * Opening, streaming and quit
193  **/
194 QMenu *QVLCMenu::FileMenu()
195 {
196     QMenu *menu = new QMenu();
197
198     addDPStaticEntry( menu, qtr( "&Open File..." ), "",
199         ":/pixmaps/file-asym_16px.png", SLOT( openFileDialog() ), "Ctrl+O" );
200     addDPStaticEntry( menu, qtr( I_OPEN_FOLDER ), "",
201         ":/pixmaps/folder-grey_16px.png", SLOT( PLAppendDir() ), "Ctrl+F" );
202     addDPStaticEntry( menu, qtr( "Open &Disc..." ), "",
203         ":/pixmaps/disc_16px.png", SLOT( openDiscDialog() ), "Ctrl+D" );
204     addDPStaticEntry( menu, qtr( "Open &Network..." ), "",
205         ":/pixmaps/network_16px.png", SLOT( openNetDialog() ), "Ctrl+N" );
206     addDPStaticEntry( menu, qtr( "Open &Capture Device..." ), "",
207         ":/pixmaps/capture-card_16px.png", SLOT( openCaptureDialog() ),
208         "Ctrl+C" );
209     menu->addSeparator();
210
211     addDPStaticEntry( menu, qtr( "&Streaming..." ), "",
212         ":/pixmaps/menus_stream_16px.png", SLOT( openThenStreamingDialogs() ),
213         "Ctrl+S" );
214     addDPStaticEntry( menu, qtr( "Conve&rt / Save..." ), "", "",
215         SLOT( openThenTranscodingDialogs() ), "Ctrl+R" );
216     menu->addSeparator();
217
218     addDPStaticEntry( menu, qtr( "&Quit" ) , "",
219         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "Ctrl+Q" );
220     return menu;
221 }
222
223 /* Playlist/MediaLibrary Control */
224 QMenu *QVLCMenu::PlaylistMenu( intf_thread_t *p_intf, MainInterface *mi )
225 {
226     QMenu *menu = new QMenu();
227     menu->addMenu( SDMenu( p_intf ) );
228     menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
229                      qtr( "Show Playlist" ), mi, SLOT( togglePlaylist() ) );
230     menu->addSeparator();
231
232     addDPStaticEntry( menu, qtr( I_PL_LOAD ), "", "", SLOT( openAPlaylist() ),
233         "Ctrl+X" );
234     addDPStaticEntry( menu, qtr( I_PL_SAVE ), "", "", SLOT( saveAPlaylist() ),
235         "Ctrl+Y" );
236     menu->addSeparator();
237     menu->addAction( qtr( "Undock from interface" ), mi,
238                      SLOT( undockPlaylist() ), qtr( "Ctrl+U" ) );
239     return menu;
240 }
241
242 /**
243  * Tools/View Menu
244  * This is kept in the same menu for now, but could change if it gets much
245  * longer.
246  * This menu can be an interface menu but also a right click menu.
247  **/
248 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf,
249                             MainInterface *mi,
250                             bool visual_selector_enabled,
251                             bool with_intf )
252 {
253     QMenu *menu = new QMenu;
254     if( mi )
255     {
256         menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
257                          qtr( "Playlist..." ), mi, SLOT( togglePlaylist() ),
258                          qtr( "Ctrl+L" ) );
259     }
260     addDPStaticEntry( menu, qtr( I_MENU_EXT ), "",
261         ":/pixmaps/menus_settings_16px.png", SLOT( extendedDialog() ),
262         "Ctrl+E" );
263
264     menu->addSeparator();
265
266     if( with_intf )
267     {
268         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
269         intfmenu->setTitle( qtr( "Interfaces" ) );
270         menu->addMenu( intfmenu );
271         menu->addSeparator();
272     }
273     if( mi )
274     {
275         /* Minimal View */
276         QAction *action=menu->addAction( qtr( "Minimal View..." ), mi,
277                 SLOT( toggleMinimalView() ), qtr( "Ctrl+H" ) );
278         action->setCheckable( true );
279         if( mi->getControlsVisibilityStatus() & CONTROLS_VISIBLE )
280             action->setChecked( true );
281
282         /* FullScreen View */
283         action = menu->addAction( qtr( "Toggle Fullscreen Interface" ), mi,
284                 SLOT( toggleFullScreen() ), qtr( "F11" ) );
285
286         /* Advanced Controls */
287         action = menu->addAction( qtr( "Advanced controls" ), mi,
288                 SLOT( toggleAdvanced() ) );
289         action->setCheckable( true );
290         if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
291             action->setChecked( true );
292 #if 0 /* For Visualisations. Not yet working */
293         adv = menu->addAction( qtr( "Visualizations selector" ),
294                 mi, SLOT( visual() ) );
295         adv->setCheckable( true );
296         if( visual_selector_enabled ) adv->setChecked( true );
297 #endif
298     }
299
300     menu->addSeparator();
301
302     addDPStaticEntry( menu, qtr( I_MENU_MSG ), "",
303         ":/pixmaps/menus_messages_16px.png", SLOT( messagesDialog() ),
304         "Ctrl+M" );
305     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , "", "",
306         SLOT( mediaInfoDialog() ), "Ctrl+I" );
307     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) , "",
308         ":/pixmaps/menus_info_16px.png", SLOT( mediaCodecDialog() ), "Ctrl+J" );
309     addDPStaticEntry( menu, qtr( I_MENU_BOOKMARK ), "","",
310                       SLOT( bookmarksDialog() ), "Ctrl+B" );
311 #ifdef ENABLE_VLM
312     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", "", SLOT( vlmDialog() ),
313         "Ctrl+W" );
314 #endif
315
316     menu->addSeparator();
317     addDPStaticEntry( menu, qtr( "Preferences..." ), "",
318         ":/pixmaps/menus_preferences_16px.png", SLOT( prefsDialog() ), "Ctrl+P" );
319     return menu;
320 }
321
322 /**
323  * Interface Sub-Menu, to list extras interface and skins
324  **/
325 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
326 {
327     vector<int> objects;
328     vector<const char *> varnames;
329     /** \todo add "switch to XXX" */
330     varnames.push_back( "intf-add" );
331     objects.push_back( p_intf->i_object_id );
332
333     QMenu *menu = Populate( p_intf, current, varnames, objects );
334
335     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
336     THEDP->menusUpdateMapper->setMapping( menu, 4 );
337     return menu;
338 }
339
340 /**
341  * Main Audio Menu
342  */
343 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
344 {
345     vector<int> objects;
346     vector<const char *> varnames;
347
348     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_find( p_intf,
349             VLC_OBJECT_INPUT, FIND_ANYWHERE );
350     if( p_object != NULL )
351     {
352         PUSH_VAR( "audio-es" );
353         vlc_object_release( p_object );
354     }
355
356     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_AOUT,
357             FIND_ANYWHERE );
358     if( p_object )
359     {
360         AudioAutoMenuBuilder( p_object, objects, varnames );
361         vlc_object_release( p_object );
362     }
363     return Populate( p_intf, current, varnames, objects );
364 }
365
366 /**
367  * Main Video Menu
368  * Subtitles are part of Video.
369  **/
370 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
371 {
372     vlc_object_t *p_object;
373     vector<int> objects;
374     vector<const char *> varnames;
375
376     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
377             FIND_ANYWHERE );
378     if( p_object != NULL )
379     {
380         PUSH_VAR( "video-es" );
381         PUSH_VAR( "spu-es" );
382         vlc_object_release( p_object );
383     }
384
385     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
386             FIND_ANYWHERE );
387     if( p_object != NULL )
388     {
389         VideoAutoMenuBuilder( p_object, objects, varnames );
390         vlc_object_release( p_object );
391     }
392     return Populate( p_intf, current, varnames, objects );
393 }
394
395 /**
396  * Navigation Menu
397  * For DVD, MP4, MOV and other chapter based format
398  **/
399 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *navMenu )
400 {
401     vlc_object_t *p_object;
402     vector<int> objects;
403     vector<const char *> varnames;
404
405     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
406             FIND_ANYWHERE );
407     if( p_object != NULL )
408     {
409         InputAutoMenuBuilder(  p_object, objects, varnames );
410         PUSH_VAR( "prev-title" ); PUSH_VAR ( "next-title" );
411         PUSH_VAR( "prev-chapter" ); PUSH_VAR( "next-chapter" );
412         vlc_object_release( p_object );
413     }
414     navMenu = new QMenu();
415     addDPStaticEntry( navMenu, qtr( I_MENU_GOTOTIME ), "","",
416         SLOT( gotoTimeDialog() ), "Ctrl+T" );
417     navMenu->addSeparator();
418     return Populate( p_intf, navMenu, varnames, objects, true );
419 }
420
421 /**
422  * Service Discovery SubMenu
423  **/
424 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
425 {
426     QMenu *menu = new QMenu();
427     menu->setTitle( qtr( I_PL_SD ) );
428     char **ppsz_longnames;
429     char **ppsz_names = services_discovery_GetServicesNames( p_intf,
430                                                              &ppsz_longnames );
431     if( !ppsz_names )
432         return menu;
433
434     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
435     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
436     {
437         QAction *a = new QAction( qfu( *ppsz_longname ), menu );
438         a->setCheckable( true );
439         if( playlist_IsServicesDiscoveryLoaded( THEPL, *ppsz_name ) )
440             a->setChecked( true );
441         CONNECT( a , triggered(), THEDP->SDMapper, map() );
442         THEDP->SDMapper->setMapping( a, QString( *ppsz_name ) );
443         menu->addAction( a );
444
445         if( !strcmp( *ppsz_name, "podcast" ) )
446         {
447             QAction *b = new QAction( qfu( "Configure podcasts..." ), menu );
448             //b->setEnabled( a->isChecked() );
449             menu->addAction( b );
450             CONNECT( b, triggered(), THEDP, podcastConfigureDialog() );
451         }
452         free( *ppsz_name );
453         free( *ppsz_longname );
454     }
455     free( ppsz_names );
456     free( ppsz_longnames );
457     return menu;
458 }
459 /**
460  * Help/About Menu
461 **/
462 QMenu *QVLCMenu::HelpMenu()
463 {
464     QMenu *menu = new QMenu();
465     addDPStaticEntry( menu, qtr( "Help..." ) , "",
466         ":/pixmaps/menus_help_16px.png", SLOT( helpDialog() ), "F1" );
467 #ifdef UPDATE_CHECK
468     addDPStaticEntry( menu, qtr( "Check for updates..." ) , "", "", SLOT( updateDialog() ), "");
469 #endif
470     menu->addSeparator();
471     addDPStaticEntry( menu, qtr( I_MENU_ABOUT ), "", "", SLOT( aboutDialog() ),
472         "Ctrl+F1" );
473     return menu;
474 }
475
476
477 /*****************************************************************************
478  * Popup menus - Right Click menus                                           *
479  *****************************************************************************/
480 #define POPUP_BOILERPLATE \
481     unsigned int i_last_separator = 0; \
482     vector<int> objects; \
483     vector<const char *> varnames; \
484     input_thread_t *p_input = THEMIM->getInput();
485
486 #define CREATE_POPUP \
487     Populate( p_intf, menu, varnames, objects ); \
488     p_intf->p_sys->p_popup_menu = menu; \
489     menu->popup( QCursor::pos() ); \
490     p_intf->p_sys->p_popup_menu = NULL; \
491     i_last_separator = 0;
492
493 void QVLCMenu::PopupMenuControlEntries( QMenu *menu,
494                                         intf_thread_t *p_intf,
495                                         input_thread_t *p_input )
496 {
497     if( p_input )
498     {
499         vlc_value_t val;
500         var_Get( p_input, "state", &val );
501         if( val.i_int == PLAYING_S )
502             addMIMStaticEntry( p_intf, menu, qtr( "Pause" ), "",
503                     ":/pixmaps/pause_16px.png", SLOT( togglePlayPause() ) );
504         else
505             addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
506                     ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
507     }
508     else if( THEPL->items.i_size && THEPL->i_enabled )
509         addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
510                 ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
511
512     addMIMStaticEntry( p_intf, menu, qtr( "Stop" ), "",
513             ":/pixmaps/stop_16px.png", SLOT( stop() ) );
514     addMIMStaticEntry( p_intf, menu, qtr( "Previous" ), "",
515             ":/pixmaps/previous_16px.png", SLOT( prev() ) );
516     addMIMStaticEntry( p_intf, menu, qtr( "Next" ), "",
517             ":/pixmaps/next_16px.png", SLOT( next() ) );
518     }
519
520 void QVLCMenu::PopupMenuStaticEntries( intf_thread_t *p_intf, QMenu *menu )
521 {
522     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, true );
523     toolsmenu->setTitle( qtr( "Tools" ) );
524     menu->addMenu( toolsmenu );
525
526     QMenu *openmenu = new QMenu( qtr( "Open" ) );
527     openmenu->addAction( qtr( "Open &File..." ), THEDP,
528                          SLOT( openFileDialog() ) );
529     openmenu->addAction( qtr( "Open &Disc..." ), THEDP,
530                          SLOT( openDiscDialog() ) );
531     openmenu->addAction( qtr( "Open &Network..." ), THEDP,
532                          SLOT( openNetDialog() ) );
533     openmenu->addAction( qtr( "Open &Capture Device..." ), THEDP,
534                          SLOT( openCaptureDialog() ) );
535     menu->addMenu( openmenu );
536
537     menu->addSeparator();
538     QMenu *helpmenu = HelpMenu();
539     helpmenu->setTitle( qtr( "Help" ) );
540     menu->addMenu( helpmenu );
541
542     addDPStaticEntry( menu, qtr( "Quit" ), "", "", SLOT( quit() ) , "Ctrl+Q" );
543 }
544
545 /* Video Tracks and Subtitles tracks */
546 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
547 {
548     POPUP_BOILERPLATE;
549     if( p_input )
550     {
551         vlc_object_yield( p_input );
552         varnames.push_back( "video-es" );
553         objects.push_back( p_input->i_object_id );
554         varnames.push_back( "spu-es" );
555         objects.push_back( p_input->i_object_id );
556         vlc_object_t *p_vout = ( vlc_object_t * )vlc_object_find( p_input,
557                 VLC_OBJECT_VOUT, FIND_CHILD );
558         if( p_vout )
559         {
560             VideoAutoMenuBuilder( p_vout, objects, varnames );
561             vlc_object_release( p_vout );
562         }
563         vlc_object_release( p_input );
564     }
565     QMenu *menu = new QMenu();
566     CREATE_POPUP;
567 }
568
569 /* Audio Tracks */
570 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
571 {
572     POPUP_BOILERPLATE;
573     if( p_input )
574     {
575         vlc_object_yield( p_input );
576         varnames.push_back( "audio-es" );
577         objects.push_back( p_input->i_object_id );
578         vlc_object_t *p_aout = ( vlc_object_t * )vlc_object_find( p_input,
579                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
580         if( p_aout )
581         {
582             AudioAutoMenuBuilder( p_aout, objects, varnames );
583             vlc_object_release( p_aout );
584         }
585         vlc_object_release( p_input );
586     }
587     QMenu *menu = new QMenu();
588     CREATE_POPUP;
589 }
590
591 /* Navigation stuff, and general menus ( open ) */
592 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
593 {
594     vlc_value_t val;
595     POPUP_BOILERPLATE;
596
597     if( p_input )
598     {
599         vlc_object_yield( p_input );
600         varnames.push_back( "audio-es" );
601         InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
602         PUSH_SEPARATOR;
603     }
604
605     QMenu *menu = new QMenu();
606     Populate( p_intf, menu, varnames, objects );
607
608     menu->addSeparator();
609     PopupMenuControlEntries( menu, p_intf, p_input );
610
611     menu->addSeparator();
612     PopupMenuStaticEntries( p_intf, menu );
613
614     p_intf->p_sys->p_popup_menu = menu;
615     menu->popup( QCursor::pos() );
616     p_intf->p_sys->p_popup_menu = NULL;
617 }
618
619 /* Main Menu that sticks everything together  */
620 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
621 {
622     if( show )
623     {
624         // create a  popup if there is none
625         if( ! p_intf->p_sys->p_popup_menu )
626         {
627             POPUP_BOILERPLATE;
628             if( p_input )
629             {
630                 vlc_object_yield( p_input );
631                 InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
632
633                 /* Audio menu */
634                 PUSH_SEPARATOR;
635                 varnames.push_back( "audio-es" );
636                 objects.push_back( p_input->i_object_id );
637                 vlc_object_t *p_aout = ( vlc_object_t * )
638                     vlc_object_find( p_input, VLC_OBJECT_AOUT, FIND_ANYWHERE );
639                 if( p_aout )
640                 {
641                     AudioAutoMenuBuilder( p_aout, objects, varnames );
642                     vlc_object_release( p_aout );
643                 }
644
645                 /* Video menu */
646                 PUSH_SEPARATOR;
647                 varnames.push_back( "video-es" );
648                 objects.push_back( p_input->i_object_id );
649                 varnames.push_back( "spu-es" );
650                 objects.push_back( p_input->i_object_id );
651                 vlc_object_t *p_vout = ( vlc_object_t * )
652                     vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
653                 if( p_vout )
654                 {
655                     VideoAutoMenuBuilder( p_vout, objects, varnames );
656                     vlc_object_release( p_vout );
657                 }
658             }
659
660             QMenu *menu = new QMenu();
661             Populate( p_intf, menu, varnames, objects );
662             menu->addSeparator();
663             PopupMenuControlEntries( menu, p_intf, p_input );
664             menu->addSeparator();
665             PopupMenuStaticEntries( p_intf, menu );
666
667             p_intf->p_sys->p_popup_menu = menu;
668         }
669         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
670     }
671     else
672     {
673         // destroy popup if there is one
674         delete p_intf->p_sys->p_popup_menu;
675         p_intf->p_sys->p_popup_menu = NULL;
676     }
677 }
678
679 /************************************************************************
680  * Systray Menu                                                         *
681  ************************************************************************/
682
683 void QVLCMenu::updateSystrayMenu( MainInterface *mi,
684                                   intf_thread_t *p_intf,
685                                   bool b_force_visible )
686 {
687     POPUP_BOILERPLATE;
688
689     /* Get the systray menu and clean it */
690     QMenu *sysMenu = mi->getSysTrayMenu();
691     sysMenu->clear();
692
693     /* Hide / Show VLC and cone */
694     if( mi->isVisible() || b_force_visible )
695     {
696         sysMenu->addAction( QIcon( ":/vlc16.png" ),
697                 qtr( "Hide VLC media player in taskbar" ), mi,
698                 SLOT( toggleUpdateSystrayMenu() ) );
699     }
700     else
701     {
702         sysMenu->addAction( QIcon( ":/vlc16.png" ),
703                 qtr( "Show VLC media player" ), mi,
704                 SLOT( toggleUpdateSystrayMenu() ) );
705     }
706
707     sysMenu->addSeparator();
708     PopupMenuControlEntries( sysMenu, p_intf, p_input );
709
710     sysMenu->addSeparator();
711     addDPStaticEntry( sysMenu, qtr( "&Open Media" ), "",
712             ":/pixmaps/file-wide_16px.png", SLOT( openFileDialog() ), "" );
713     addDPStaticEntry( sysMenu, qtr( "&Quit" ) , "",
714         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "" );
715
716     /* Set the menu */
717     mi->getSysTray()->setContextMenu( sysMenu );
718 }
719
720 #undef PUSH_VAR
721 #undef PUSH_SEPARATOR
722
723
724 /*************************************************************************
725  * Builders for automenus
726  *************************************************************************/
727 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf,
728                             QMenu *current,
729                             vector< const char *> & varnames,
730                             vector<int> & objects,
731                             bool append )
732 {
733     QMenu *menu = current;
734     if( !menu )
735         menu = new QMenu();
736     else if( !append )
737         menu->clear();
738
739     currentGroup = NULL;
740
741     vlc_object_t *p_object;
742     vlc_bool_t b_section_empty = VLC_FALSE;
743     int i;
744
745 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr( "Empty" ) ); \
746     action->setEnabled( false ); }
747
748     for( i = 0; i < ( int )objects.size() ; i++ )
749     {
750         if( !varnames[i] || !*varnames[i] )
751         {
752             if( b_section_empty )
753                 APPEND_EMPTY;
754             menu->addSeparator();
755             b_section_empty = VLC_TRUE;
756             continue;
757         }
758
759         if( objects[i] == 0 )
760         {
761             /// \bug What is this ?
762             // Append( menu, varnames[i], NULL );
763             b_section_empty = VLC_FALSE;
764             continue;
765         }
766
767         p_object = ( vlc_object_t * )vlc_object_get( objects[i] );
768         if( p_object == NULL ) continue;
769
770         b_section_empty = VLC_FALSE;
771         /* Ugly specific stuff */
772         if( strstr( varnames[i], "intf-add" ) )
773             CreateItem( menu, varnames[i], p_object, false );
774         else
775             CreateItem( menu, varnames[i], p_object, true );
776         vlc_object_release( p_object );
777     }
778
779     /* Special case for empty menus */
780     if( menu->actions().size() == 0 || b_section_empty )
781         APPEND_EMPTY
782
783     return menu;
784 }
785 #undef APPEND_EMPTY
786
787 /*****************************************************************************
788  * Private methods.
789  *****************************************************************************/
790
791 static bool IsMenuEmpty( const char *psz_var,
792                          vlc_object_t *p_object,
793                          bool b_root = true )
794 {
795     vlc_value_t val, val_list;
796     int i_type, i_result, i;
797
798     /* Check the type of the object variable */
799     i_type = var_Type( p_object, psz_var );
800
801     /* Check if we want to display the variable */
802     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
803
804     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
805     if( val.i_int == 0 ) return true;
806
807     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
808     {
809         if( val.i_int == 1 && b_root ) return true;
810         else return false;
811     }
812
813     /* Check children variables in case of VLC_VAR_VARIABLE */
814     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
815     {
816         return true;
817     }
818
819     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
820     {
821         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
822                     p_object, false ) )
823         {
824             i_result = false;
825             break;
826         }
827     }
828
829     /* clean up everything */
830     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
831
832     return i_result;
833 }
834
835 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
836         vlc_object_t *p_object, bool b_submenu )
837 {
838     vlc_value_t val, text;
839     int i_type;
840
841     /* Check the type of the object variable */
842     i_type = var_Type( p_object, psz_var );
843
844     switch( i_type & VLC_VAR_TYPE )
845     {
846         case VLC_VAR_VOID:
847         case VLC_VAR_BOOL:
848         case VLC_VAR_VARIABLE:
849         case VLC_VAR_STRING:
850         case VLC_VAR_INTEGER:
851         case VLC_VAR_FLOAT:
852             break;
853         default:
854             /* Variable doesn't exist or isn't handled */
855             return;
856     }
857
858     /* Make sure we want to display the variable */
859     if( IsMenuEmpty( psz_var, p_object ) )  return;
860
861     /* Get the descriptive name of the variable */
862     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
863
864     if( i_type & VLC_VAR_HASCHOICE )
865     {
866         /* Append choices menu */
867         if( b_submenu )
868         {
869             QMenu *submenu = new QMenu();
870             submenu->setTitle( qfu( text.psz_string ?
871                         text.psz_string : psz_var ) );
872             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 )
873                 menu->addMenu( submenu );
874         }
875         else
876             CreateChoicesMenu( menu, psz_var, p_object, true );
877         FREENULL( text.psz_string );
878         return;
879     }
880
881 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
882
883     switch( i_type & VLC_VAR_TYPE )
884     {
885         case VLC_VAR_VOID:
886             var_Get( p_object, psz_var, &val );
887             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
888                     p_object->i_object_id, val, i_type );
889             break;
890
891         case VLC_VAR_BOOL:
892             var_Get( p_object, psz_var, &val );
893             val.b_bool = !val.b_bool;
894             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
895                     p_object->i_object_id, val, i_type, !val.b_bool );
896             break;
897     }
898     FREENULL( text.psz_string );
899 }
900
901
902 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
903         vlc_object_t *p_object, bool b_root )
904 {
905     vlc_value_t val, val_list, text_list;
906     int i_type, i;
907
908     /* Check the type of the object variable */
909     i_type = var_Type( p_object, psz_var );
910
911     /* Make sure we want to display the variable */
912     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
913
914     switch( i_type & VLC_VAR_TYPE )
915     {
916         case VLC_VAR_VOID:
917         case VLC_VAR_BOOL:
918         case VLC_VAR_VARIABLE:
919         case VLC_VAR_STRING:
920         case VLC_VAR_INTEGER:
921         case VLC_VAR_FLOAT:
922             break;
923         default:
924             /* Variable doesn't exist or isn't handled */
925             return VLC_EGENERIC;
926     }
927
928     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
929                 &val_list, &text_list ) < 0 )
930     {
931         return VLC_EGENERIC;
932     }
933
934 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
935 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
936 #define CURVAL val_list.p_list->p_values[i]
937 #define CURTEXT text_list.p_list->p_values[i].psz_string
938
939     for( i = 0; i < val_list.p_list->i_count; i++ )
940     {
941         vlc_value_t another_val;
942         QString menutext;
943         QMenu *subsubmenu = new QMenu();
944
945         switch( i_type & VLC_VAR_TYPE )
946         {
947             case VLC_VAR_VARIABLE:
948                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
949                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
950                 submenu->addMenu( subsubmenu );
951                 break;
952
953             case VLC_VAR_STRING:
954                 var_Get( p_object, psz_var, &val );
955                 another_val.psz_string = strdup( CURVAL.psz_string );
956                 menutext = qfu( "Add " ) /* If this function is more used, FIX*/
957                          + qfu( CURTEXT ? CURTEXT : another_val.psz_string );
958                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
959                         p_object->i_object_id, another_val, i_type,
960                         NOTCOMMAND && val.psz_string &&
961                         !strcmp( val.psz_string, CURVAL.psz_string ) );
962
963                 if( val.psz_string ) free( val.psz_string );
964                 break;
965
966             case VLC_VAR_INTEGER:
967                 var_Get( p_object, psz_var, &val );
968                 if( CURTEXT ) menutext = qfu( CURTEXT );
969                 else menutext.sprintf( "%d", CURVAL.i_int );
970                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
971                         p_object->i_object_id, CURVAL, i_type,
972                         NOTCOMMAND && CURVAL.i_int == val.i_int );
973                 break;
974
975             case VLC_VAR_FLOAT:
976                 var_Get( p_object, psz_var, &val );
977                 if( CURTEXT ) menutext = qfu( CURTEXT );
978                 else menutext.sprintf( "%.2f", CURVAL.f_float );
979                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
980                         p_object->i_object_id, CURVAL, i_type,
981                         NOTCOMMAND && CURVAL.f_float == val.f_float );
982                 break;
983
984             default:
985                 break;
986         }
987     }
988     currentGroup = NULL;
989
990     /* clean up everything */
991     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
992
993 #undef NORMAL_OR_RADIO
994 #undef NOTCOMMAND
995 #undef CURVAL
996 #undef CURTEXT
997     return VLC_SUCCESS;
998 }
999
1000 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
1001         QString text, QString help,
1002         int i_item_type, int i_object_id,
1003         vlc_value_t val, int i_val_type,
1004         bool checked )
1005 {
1006     QAction *action = new QAction( text, menu );
1007     action->setText( text );
1008     action->setToolTip( help );
1009
1010     if( i_item_type == ITEM_CHECK )
1011     {
1012         action->setCheckable( true );
1013     }
1014     else if( i_item_type == ITEM_RADIO )
1015     {
1016         action->setCheckable( true );
1017         if( !currentGroup )
1018             currentGroup = new QActionGroup( menu );
1019         currentGroup->addAction( action );
1020     }
1021
1022     if( checked )
1023     {
1024         action->setChecked( true );
1025     }
1026     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1027             val, psz_var );
1028     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1029     THEDP->menusMapper->setMapping( action, itemData );
1030     menu->addAction( action );
1031 }
1032
1033 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1034 {
1035     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1036     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( itemData->i_object_id );
1037     if( p_object == NULL ) return;
1038
1039     var_Set( p_object, itemData->psz_var, itemData->val );
1040     vlc_object_release( p_object );
1041 }
1042