chiark / gitweb /
cgi/cgimain.c: Make the CGI program be (a little) locale-aware.
[disorder] / disobedience / menu.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006-2009, 2011, 2013 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file disobedience/menu.c
19  * @brief Main menu
20  */
21
22 #include "disobedience.h"
23
24 static void toggled_minimode(GtkCheckMenuItem *item, gpointer userdata);
25
26 static GtkWidget *selectall_widget;
27 static GtkWidget *selectnone_widget;
28 static GtkWidget *properties_widget;
29 GtkWidget *menu_playlists_widget;
30 GtkWidget *playlists_menu;
31 GtkWidget *menu_editplaylists_widget;
32 static GtkWidget *menu_minimode_widget;
33 static GtkWidget *apis_menu;
34
35 /** @brief Main menu widgets */
36 GtkItemFactory *mainmenufactory;
37
38 /** @brief Set for full mode, clear for mini mode */
39 int full_mode = 1;
40
41 static void about_popup_got_version(void *v,
42                                     const char *err,
43                                     const char *value);
44
45 /** @brief Called when the quit option is activated
46  *
47  * Just exits.
48  */
49 static void quit_program(gpointer attribute((unused)) callback_data,
50                          guint attribute((unused)) callback_action,
51                          GtkWidget attribute((unused)) *menu_item) {
52   D(("quit_program"));
53   exit(0);
54 }
55
56 /** @brief Called when an edit menu item is selected
57  *
58  * Shared by several menu items; callback_action is expected to be the offset
59  * of the activate member of struct tabtype.
60  */
61 static void menu_tab_action(gpointer attribute((unused)) callback_data,
62                             guint callback_action,
63                             GtkWidget attribute((unused)) *menu_item) {
64   GtkWidget *tab = gtk_notebook_get_nth_page
65     (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
66   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
67
68   void (**activatep)(GtkMenuItem *, gpointer)
69     = (void *)((const char *)t + callback_action);
70   void (*activate)(GtkMenuItem *, gpointer) = *activatep;
71   
72   if(activate)
73     activate(NULL, t->extra);
74 }
75
76 /** @brief Called when the login option is activated */
77 static void login(gpointer attribute((unused)) callback_data,
78                   guint attribute((unused)) callback_action,
79                   GtkWidget attribute((unused)) *menu_item) {
80   login_box();
81 }
82
83 /** @brief Called when the login option is activated */
84 static void users(gpointer attribute((unused)) callback_data,
85                   guint attribute((unused)) callback_action,
86                   GtkWidget attribute((unused)) *menu_item) {
87   manage_users();
88 }
89
90 #if 0
91 /** @brief Called when the settings option is activated */
92 static void settings(gpointer attribute((unused)) callback_data,
93                      guint attribute((unused)) callback_action,
94                      GtkWidget attribute((unused)) *menu_item) {
95   popup_settings();
96 }
97 #endif
98
99 /** @brief Called when edit menu is shown
100  *
101  * Determines option sensitivity according to the current tab and adjusts the
102  * widgets accordingly.  Knows about @ref DISORDER_CONNECTED so the callbacks
103  * need not.
104  */
105 static void edit_menu_show(GtkWidget attribute((unused)) *widget,
106                            gpointer attribute((unused)) user_data) {
107   if(tabs) {
108     GtkWidget *tab = gtk_notebook_get_nth_page
109       (GTK_NOTEBOOK(tabs),
110        gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
111     const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
112
113     assert(t != 0);
114     gtk_widget_set_sensitive(properties_widget,
115                              (t->properties_sensitive
116                               && t->properties_sensitive(t->extra)
117                               && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
118     gtk_widget_set_sensitive(selectall_widget,
119                              t->selectall_sensitive
120                              && t->selectall_sensitive(t->extra));
121     gtk_widget_set_sensitive(selectnone_widget,
122                              t->selectnone_sensitive
123                              && t->selectnone_sensitive(t->extra));
124   }
125 }
126
127 /** @brief Fetch version in order to display the about... popup */
128 static void about_popup(gpointer attribute((unused)) callback_data,
129                         guint attribute((unused)) callback_action,
130                         GtkWidget attribute((unused)) *menu_item) {
131   D(("about_popup"));
132
133   gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
134   disorder_eclient_version(client,
135                            about_popup_got_version,
136                            0);
137 }
138
139 static void manual_popup(gpointer attribute((unused)) callback_data,
140                        guint attribute((unused)) callback_action,
141                        GtkWidget attribute((unused)) *menu_item) {
142   D(("manual_popup"));
143
144   popup_help(NULL);
145 }
146
147 /** @brief Called when version arrives, displays about... popup */
148 static void about_popup_got_version(void attribute((unused)) *v,
149                                     const char attribute((unused)) *err,
150                                     const char *value) {
151   GtkWidget *w;
152   char *server_version_string;
153   char *short_version_string;
154   GtkWidget *hbox, *vbox, *title;
155
156   if(!value)
157     value = "[error]";
158   byte_xasprintf(&server_version_string, "DisOrder server version %s", value);
159   byte_xasprintf(&short_version_string, "Disobedience %s",
160                  disorder_short_version_string);
161   w = gtk_dialog_new_with_buttons("About Disobedience",
162                                   GTK_WINDOW(toplevel),
163                                   (GTK_DIALOG_MODAL
164                                    |GTK_DIALOG_DESTROY_WITH_PARENT),
165                                   GTK_STOCK_OK,
166                                   GTK_RESPONSE_ACCEPT,
167                                   (char *)NULL);
168   hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
169   vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
170   gtk_box_pack_start(GTK_BOX(hbox),
171                      gtk_image_new_from_pixbuf(find_image("duck.png")),
172                      FALSE/*expand*/,
173                      FALSE/*fill*/,
174                      4/*padding*/);
175   gtk_box_pack_start(GTK_BOX(vbox),
176                      gtk_label_new(short_version_string),
177                      FALSE/*expand*/,
178                      FALSE/*fill*/,
179                      1/*padding*/);
180   gtk_box_pack_start(GTK_BOX(vbox),
181                      gtk_label_new(server_version_string),
182                      FALSE/*expand*/,
183                      FALSE/*fill*/,
184                      1/*padding*/);
185   gtk_box_pack_start(GTK_BOX(vbox),
186                      gtk_label_new("\xC2\xA9 2003-2013 Richard Kettlewell et al"),
187                      FALSE/*expand*/,
188                      FALSE/*fill*/,
189                      1/*padding*/);
190   gtk_box_pack_end(GTK_BOX(hbox),
191                    vbox,
192                    FALSE/*expand*/,
193                    FALSE/*fill*/,
194                    0/*padding*/);
195   title = gtk_label_new(0);
196   gtk_label_set_markup(GTK_LABEL(title),
197                        "<span font_desc=\"Sans 36\">Disobedience</span>");
198   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
199                      FALSE/*expand*/,
200                      FALSE/*fill*/,
201                      0/*padding*/);
202   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
203                      FALSE/*expand*/,
204                      FALSE/*fill*/,
205                      0/*padding*/);
206   set_tool_colors(w);
207   gtk_widget_show_all(w);
208   gtk_dialog_run(GTK_DIALOG(w));
209   gtk_widget_destroy(w);
210 }
211
212 /** @brief Set 'Manage Users' menu item sensitivity */
213 void users_set_sensitive(int sensitive) {
214   GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
215                                              "<GdisorderMain>/Server/Manage users");
216   gtk_widget_set_sensitive(w, sensitive);
217 }
218
219 static void menu_check_userman(void attribute((unused)) *v,
220                                const char *err,
221                                const char attribute((unused)) *value) {
222   if(err && !strncmp(err, "510", 3))
223     users_set_sensitive(FALSE);
224   else
225     users_set_sensitive(TRUE);
226 }
227
228 /** @brief Called when our rights change */
229 static void menu_rights_changed(const char attribute((unused)) *event,
230                                 void attribute((unused)) *eventdata,
231                                 void attribute((unused)) *callbackdata) {
232   if(last_rights & RIGHT_ADMIN)
233     disorder_eclient_userinfo(client, menu_check_userman, "", "email", 0);
234   else
235     users_set_sensitive(FALSE);
236 }
237
238 /** @brief Called to select the network playback API */
239 static void rtp_menu_activate(GtkMenuItem *menuitem,
240                               gpointer user_data) {
241   if(GTK_CHECK_MENU_ITEM(menuitem)->active)
242     change_rtp_api(user_data);
243 }
244
245 /** @brief Create the menu bar widget */
246 GtkWidget *menubar(GtkWidget *window) {
247   GtkWidget *m;
248
249   static const GtkItemFactoryEntry entries[] = {
250     {
251       (char *)"/Server",                /* path */
252       0,                                /* accelerator */
253       0,                                /* callback */
254       0,                                /* callback_action */
255       (char *)"<Branch>",               /* item_type */
256       0                                 /* extra_data */
257     },
258     { 
259       (char *)"/Server/Login",          /* path */
260       (char *)"<CTRL>L",                /* accelerator */
261       login,                            /* callback */
262       0,                                /* callback_action */
263       0,                                /* item_type */
264       0                                 /* extra_data */
265     },
266     { 
267       (char *)"/Server/Manage users",   /* path */
268       0,                                /* accelerator */
269       users,                            /* callback */
270       0,                                /* callback_action */
271       0,                                /* item_type */
272       0                                 /* extra_data */
273     },
274 #if 0
275     {
276       (char *)"/Server/Settings",       /* path */
277       0,                                /* accelerator */
278       settings,                         /* callback */
279       0,                                /* callback_action */
280       0,                                /* item_type */
281       0                                 /* extra_data */
282     },
283 #endif
284     {
285       (char *)"/Server/Quit Disobedience", /* path */
286       (char *)"<CTRL>Q",                /* accelerator */
287       quit_program,                     /* callback */
288       0,                                /* callback_action */
289       (char *)"<StockItem>",            /* item_type */
290       GTK_STOCK_QUIT                    /* extra_data */
291     },
292     
293     {
294       (char *)"/Edit",                  /* path */
295       0,                                /* accelerator */
296       0,                                /* callback */
297       0,                                /* callback_action */
298       (char *)"<Branch>",               /* item_type */
299       0                                 /* extra_data */
300     },
301     {
302       (char *)"/Edit/Select all tracks", /* path */
303       (char *)"<CTRL>A",                /* accelerator */
304       menu_tab_action,                  /* callback */
305       offsetof(struct tabtype, selectall_activate), /* callback_action */
306       (char *)"<StockItem>",           /* item_type */
307       GTK_STOCK_SELECT_ALL,            /* extra_data */
308     },
309     {
310       (char *)"/Edit/Deselect all tracks", /* path */
311       (char *)"<CTRL><SHIFT>A",         /* accelerator */
312       menu_tab_action,                  /* callback */
313       offsetof(struct tabtype, selectnone_activate), /* callback_action */
314       0,                                /* item_type */
315       0                                 /* extra_data */
316     },
317     {
318       (char *)"/Edit/Track properties", /* path */
319       0,                                /* accelerator */
320       menu_tab_action,                  /* callback */
321       offsetof(struct tabtype, properties_activate), /* callback_action */
322       (char *)"<StockItem>",            /* item_type */
323       GTK_STOCK_PROPERTIES,             /* extra_data */
324     },
325     {
326       (char *)"/Edit/Edit playlists",   /* path */
327       0,                                /* accelerator */
328       playlist_window_create,           /* callback */
329       0,                                /* callback_action */
330       0,                                /* item_type */
331       0                                 /* extra_data */
332     },
333     
334     
335     {
336       (char *)"/Control",               /* path */
337       0,                                /* accelerator */
338       0,                                /* callback */
339       0,                                /* callback_action */
340       (char *)"<Branch>",               /* item_type */
341       0                                 /* extra_data */
342     },
343     {
344       (char *)"/Control/Scratch",       /* path */
345       (char *)"<CTRL>S",                /* accelerator */
346       0,                                /* callback */
347       0,                                /* callback_action */
348       (char *)"<StockItem>",            /* item_type */
349       GTK_STOCK_STOP,                   /* extra_data */
350     },
351     {
352       (char *)"/Control/Playing",       /* path */
353       (char *)"<CTRL>P",                /* accelerator */
354       0,                                /* callback */
355       0,                                /* callback_action */
356       (char *)"<CheckItem>",            /* item_type */
357       0                                 /* extra_data */
358     },
359     {
360       (char *)"/Control/Random play",   /* path */
361       (char *)"<CTRL>R",                /* accelerator */
362       0,                                /* callback */
363       0,                                /* callback_action */
364       (char *)"<CheckItem>",            /* item_type */
365       0                                 /* extra_data */
366     },
367     {
368       (char *)"/Control/Network player", /* path */
369       (char *)"<CTRL>N",                /* accelerator */
370       0,                                /* callback */
371       0,                                /* callback_action */
372       (char *)"<CheckItem>",            /* item_type */
373       0                                 /* extra_data */
374     },
375     {
376       (char *)"/Control/Compact mode",  /* path */
377       (char *)"<CTRL>M",                /* accelerator */
378       0,                                /* callback */
379       0,                                /* callback_action */
380       (char *)"<CheckItem>",            /* item_type */
381       0                                 /* extra_data */
382     },
383     {
384       (char *)"/Control/Global Preferences", /* path */
385       (char *)"<CTRL>G",                /* accelerator */
386       popup_globals,                    /* callback */
387       0,                                /* callback_action */
388       0,                                /* item_type */
389       0                                 /* extra_data */
390     },
391     {
392       (char *)"/Control/Activate playlist", /* path */
393       0,                                /* accelerator */
394       0,                                /* callback */
395       0,                                /* callback_action */
396       (char *)"<Branch>",               /* item_type */
397       0                                 /* extra_data */
398     },
399     {
400       (char *)"/Control/Network Playback API", /* path */
401       0,                                /* accelerator */
402       0,                                /* callback */
403       0,                                /* callback_action */
404       (char *)"<Branch>",               /* item_type */
405       0                                 /* extra_data */
406     },
407
408     {
409       (char *)"/Help",                  /* path */
410       0,                                /* accelerator */
411       0,                                /* callback */
412       0,                                /* callback_action */
413       (char *)"<Branch>",               /* item_type */
414       0                                 /* extra_data */
415     },
416     {
417       (char *)"/Help/Manual",           /* path */
418       0,                                /* accelerator */
419       manual_popup,                     /* callback */
420       0,                                /* callback_action */
421       (char *)"<StockItem>",            /* item_type */
422       GTK_STOCK_HELP,                   /* extra_data */
423     },
424     {
425       (char *)"/Help/About Disobedience", /* path */
426       0,                                /* accelerator */
427       about_popup,                      /* callback */
428       0,                                /* callback_action */
429       (char *)"<StockItem>",            /* item_type */
430       GTK_STOCK_ABOUT                   /* extra_data */
431     },
432   };
433
434   GtkAccelGroup *accel = gtk_accel_group_new();
435
436   D(("add_menubar"));
437   /* TODO: item factories are deprecated in favour of some XML thing */
438   mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
439                                          accel);
440   gtk_item_factory_create_items(mainmenufactory,
441                                 sizeof entries / sizeof *entries,
442                                 (GtkItemFactoryEntry *)entries,
443                                 0);
444   gtk_window_add_accel_group(GTK_WINDOW(window), accel);
445   selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
446                                                  "<GdisorderMain>/Edit/Select all tracks");
447   selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
448                                                  "<GdisorderMain>/Edit/Deselect all tracks");
449   properties_widget = gtk_item_factory_get_widget(mainmenufactory,
450                                                   "<GdisorderMain>/Edit/Track properties");
451   menu_playlists_widget = gtk_item_factory_get_item(mainmenufactory,
452                                                "<GdisorderMain>/Control/Activate playlist");
453   playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
454                                                "<GdisorderMain>/Control/Activate playlist");
455   apis_menu = gtk_item_factory_get_widget(mainmenufactory,
456                                                "<GdisorderMain>/Control/Network Playback API");
457   menu_editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory,
458                                                      "<GdisorderMain>/Edit/Edit playlists");
459   menu_minimode_widget = gtk_item_factory_get_widget(mainmenufactory,
460                                                      "<GdisorderMain>/Control/Compact mode");
461   assert(selectall_widget != 0);
462   assert(selectnone_widget != 0);
463   assert(properties_widget != 0);
464   assert(menu_playlists_widget != 0);
465   assert(playlists_menu != 0);
466   assert(menu_editplaylists_widget != 0);
467   assert(apis_menu != 0);
468
469   GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
470                                                        "<GdisorderMain>/Edit");
471   g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
472
473   event_register("rights-changed", menu_rights_changed, 0);
474   users_set_sensitive(0);
475   m = gtk_item_factory_get_widget(mainmenufactory,
476                                   "<GdisorderMain>");
477   set_tool_colors(m);
478   if(menu_minimode_widget)
479     g_signal_connect(G_OBJECT(menu_minimode_widget), "toggled",
480                      G_CALLBACK(toggled_minimode), NULL);
481
482   /* Populate the APIs menu */
483   GSList *playback_menu_group = NULL;
484   load_rtp_config();
485   assert(rtp_api != NULL);
486   for(int n = 0; uaudio_apis[n]; ++n) {
487     if(uaudio_apis[n]->flags & UAUDIO_API_CLIENT) {
488       GtkWidget *mw = gtk_radio_menu_item_new_with_label(playback_menu_group,
489                                                          uaudio_apis[n]->name);
490       playback_menu_group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(mw));
491       gtk_menu_shell_append(GTK_MENU_SHELL(apis_menu), mw);
492       /* Tick the currently selected API... */
493       if(!strcmp(uaudio_apis[n]->name, rtp_api))
494         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mw), TRUE);
495       /* ...and only then connect the signal */
496       g_signal_connect(mw, "toggled", G_CALLBACK(rtp_menu_activate),
497                        (char *)uaudio_apis[n]->name);
498     }
499   }
500
501   return m;
502 }
503
504 static void toggled_minimode(GtkCheckMenuItem  *item,
505                              gpointer attribute((unused)) userdata) {
506   int new_full_mode = !gtk_check_menu_item_get_active(item);
507   if(full_mode != new_full_mode) {
508     full_mode = new_full_mode;
509     event_raise("mini-mode-changed", NULL);
510   }
511 }
512
513 /*
514 Local Variables:
515 c-basic-offset:2
516 comment-column:40
517 fill-column:79
518 indent-tabs-mode:nil
519 End:
520 */