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