chiark / gitweb /
debian/.gitignore: Move existing patterns from top-level `.gitignore'.
[disorder] / disobedience / menu.c
index b52190efcb3391306282c7f9b573ddf2b24b1e26..50611e6bc70522081a89c558125e23fe3efc6424 100644 (file)
@@ -1,21 +1,19 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2006-2008 Richard Kettlewell
+ * Copyright (C) 2006-2009, 2011, 2013 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /** @file disobedience/menu.c
  * @brief Main menu
 
 #include "disobedience.h"
 
+static void toggled_minimode(GtkCheckMenuItem *item, gpointer userdata);
+
 static GtkWidget *selectall_widget;
 static GtkWidget *selectnone_widget;
 static GtkWidget *properties_widget;
+GtkWidget *menu_playlists_widget;
+GtkWidget *playlists_menu;
+GtkWidget *menu_editplaylists_widget;
+static GtkWidget *menu_minimode_widget;
+static GtkWidget *apis_menu;
 
 /** @brief Main menu widgets */
 GtkItemFactory *mainmenufactory;
 
+/** @brief Set for full mode, clear for mini mode */
+int full_mode = 1;
+
 static void about_popup_got_version(void *v,
-                                    const char *error,
+                                    const char *err,
                                     const char *value);
 
 /** @brief Called when the quit option is activated
@@ -45,48 +53,24 @@ static void quit_program(gpointer attribute((unused)) callback_data,
   exit(0);
 }
 
-/* TODO can we have a single parameterized callback for all these */
-
-/** @brief Called when the select all option is activated
- *
- * Calls the per-tab select all function.
- */
-static void select_all(gpointer attribute((unused)) callback_data,
-                       guint attribute((unused)) callback_action,
-                       GtkWidget attribute((unused)) *menu_item) {
-  GtkWidget *tab = gtk_notebook_get_nth_page
-    (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
-  const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
-
-  t->selectall_activate(tab);
-}
-
-/** @brief Called when the select none option is activated
+/** @brief Called when an edit menu item is selected
  *
- * Calls the per-tab select none function.
+ * Shared by several menu items; callback_action is expected to be the offset
+ * of the activate member of struct tabtype.
  */
-static void select_none(gpointer attribute((unused)) callback_data,
-                        guint attribute((unused)) callback_action,
-                        GtkWidget attribute((unused)) *menu_item) {
-  GtkWidget *tab = gtk_notebook_get_nth_page
-    (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
-  const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
-
-  t->selectnone_activate(tab);
-}
-
-/** @brief Called when the track properties option is activated
- *
- * Calls the per-tab properties function.
- */
-static void properties_item(gpointer attribute((unused)) callback_data,
-                            guint attribute((unused)) callback_action,
+static void menu_tab_action(gpointer attribute((unused)) callback_data,
+                            guint callback_action,
                             GtkWidget attribute((unused)) *menu_item) {
   GtkWidget *tab = gtk_notebook_get_nth_page
     (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
 
-  t->properties_activate(tab);
+  void (**activatep)(GtkMenuItem *, gpointer)
+    = (void *)((const char *)t + callback_action);
+  void (*activate)(GtkMenuItem *, gpointer) = *activatep;
+  
+  if(activate)
+    activate(NULL, t->extra);
 }
 
 /** @brief Called when the login option is activated */
@@ -112,30 +96,34 @@ static void settings(gpointer attribute((unused)) callback_data,
 }
 #endif
 
-/** @brief Update menu state
+/** @brief Called when edit menu is shown
  *
  * Determines option sensitivity according to the current tab and adjusts the
  * widgets accordingly.  Knows about @ref DISORDER_CONNECTED so the callbacks
  * need not.
  */
-void menu_update(int page) {
+static void edit_menu_show(GtkWidget attribute((unused)) *widget,
+                           gpointer attribute((unused)) user_data) {
   if(tabs) {
     GtkWidget *tab = gtk_notebook_get_nth_page
       (GTK_NOTEBOOK(tabs),
-       page < 0 ? gtk_notebook_current_page(GTK_NOTEBOOK(tabs)) : page);
+       gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
     const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
 
     assert(t != 0);
     gtk_widget_set_sensitive(properties_widget,
-                             (t->properties_sensitive(tab)
+                             (t->properties_sensitive
+                              && t->properties_sensitive(t->extra)
                               && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
     gtk_widget_set_sensitive(selectall_widget,
-                             t->selectall_sensitive(tab));
+                             t->selectall_sensitive
+                             && t->selectall_sensitive(t->extra));
     gtk_widget_set_sensitive(selectnone_widget,
-                             t->selectnone_sensitive(tab));
+                             t->selectnone_sensitive
+                             && t->selectnone_sensitive(t->extra));
   }
 }
-   
+
 /** @brief Fetch version in order to display the about... popup */
 static void about_popup(gpointer attribute((unused)) callback_data,
                         guint attribute((unused)) callback_action,
@@ -153,12 +141,12 @@ static void manual_popup(gpointer attribute((unused)) callback_data,
                        GtkWidget attribute((unused)) *menu_item) {
   D(("manual_popup"));
 
-  popup_help();
+  popup_help(NULL);
 }
 
 /** @brief Called when version arrives, displays about... popup */
 static void about_popup_got_version(void attribute((unused)) *v,
-                                    const char attribute((unused)) *error,
+                                    const char attribute((unused)) *err,
                                     const char *value) {
   GtkWidget *w;
   char *server_version_string;
@@ -167,7 +155,7 @@ static void about_popup_got_version(void attribute((unused)) *v,
 
   if(!value)
     value = "[error]";
-  byte_xasprintf(&server_version_string, "Server version %s", value);
+  byte_xasprintf(&server_version_string, "DisOrder server version %s", value);
   byte_xasprintf(&short_version_string, "Disobedience %s",
                  disorder_short_version_string);
   w = gtk_dialog_new_with_buttons("About Disobedience",
@@ -195,7 +183,7 @@ static void about_popup_got_version(void attribute((unused)) *v,
                      FALSE/*fill*/,
                      1/*padding*/);
   gtk_box_pack_start(GTK_BOX(vbox),
-                     gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
+                     gtk_label_new("\xC2\xA9 2003-2013 Richard Kettlewell et al"),
                      FALSE/*expand*/,
                      FALSE/*fill*/,
                      1/*padding*/);
@@ -228,33 +216,34 @@ void users_set_sensitive(int sensitive) {
   gtk_widget_set_sensitive(w, sensitive);
 }
 
-/** @brief Called with current user's rights string */
-static void menu_got_rights(void attribute((unused)) *v,
-                            const char *error,
-                            const char *value) {
-  rights_type r;
-
-  if(error) {
-    popup_protocol_error(0, error);
-    r = 0;
-  } else {
-    if(parse_rights(value, &r, 0))
-      r = 0;
-  }
-  users_set_sensitive(!!(r & RIGHT_ADMIN));
+static void menu_check_userman(void attribute((unused)) *v,
+                               const char *err,
+                               const char attribute((unused)) *value) {
+  if(err && !strncmp(err, "510", 3))
+    users_set_sensitive(FALSE);
+  else
+    users_set_sensitive(TRUE);
+}
+
+/** @brief Called when our rights change */
+static void menu_rights_changed(const char attribute((unused)) *event,
+                                void attribute((unused)) *eventdata,
+                                void attribute((unused)) *callbackdata) {
+  if(last_rights & RIGHT_ADMIN)
+    disorder_eclient_userinfo(client, menu_check_userman, "", "email", 0);
+  else
+    users_set_sensitive(FALSE);
 }
 
-/** @brief Called after a fresh login */
-static void menu_logged_in(const char attribute((unused)) *event,
-                           void attribute((unused)) *eventdata,
-                           void attribute((unused)) *callbackdata) {
-  users_set_sensitive(0);               /* until we know better */
-  disorder_eclient_userinfo(client, menu_got_rights, config->username, "rights",
-                            0);
+/** @brief Called to select the network playback API */
+static void rtp_menu_activate(GtkMenuItem *menuitem,
+                              gpointer user_data) {
+  if(GTK_CHECK_MENU_ITEM(menuitem)->active)
+    change_rtp_api(user_data);
 }
 
 /** @brief Create the menu bar widget */
-GtkWidget *menubar(GtkWidget *w) {
+GtkWidget *menubar(GtkWidget *window) {
   GtkWidget *m;
 
   static const GtkItemFactoryEntry entries[] = {
@@ -312,28 +301,37 @@ GtkWidget *menubar(GtkWidget *w) {
     {
       (char *)"/Edit/Select all tracks", /* path */
       (char *)"<CTRL>A",                /* accelerator */
-      select_all,                       /* callback */
-      0,                                /* callback_action */
-      0,                                /* item_type */
-      0                                 /* extra_data */
+      menu_tab_action,                  /* callback */
+      offsetof(struct tabtype, selectall_activate), /* callback_action */
+      (char *)"<StockItem>",           /* item_type */
+      GTK_STOCK_SELECT_ALL,            /* extra_data */
     },
     {
       (char *)"/Edit/Deselect all tracks", /* path */
       (char *)"<CTRL><SHIFT>A",         /* accelerator */
-      select_none,                      /* callback */
-      0,                                /* callback_action */
+      menu_tab_action,                  /* callback */
+      offsetof(struct tabtype, selectnone_activate), /* callback_action */
       0,                                /* item_type */
       0                                 /* extra_data */
     },
     {
       (char *)"/Edit/Track properties", /* path */
       0,                                /* accelerator */
-      properties_item,                  /* callback */
+      menu_tab_action,                  /* callback */
+      offsetof(struct tabtype, properties_activate), /* callback_action */
+      (char *)"<StockItem>",            /* item_type */
+      GTK_STOCK_PROPERTIES,             /* extra_data */
+    },
+    {
+      (char *)"/Edit/Edit playlists",   /* path */
+      0,                                /* accelerator */
+      playlist_window_create,           /* callback */
       0,                                /* callback_action */
       0,                                /* item_type */
       0                                 /* extra_data */
     },
     
+    
     {
       (char *)"/Control",               /* path */
       0,                                /* accelerator */
@@ -347,8 +345,8 @@ GtkWidget *menubar(GtkWidget *w) {
       (char *)"<CTRL>S",                /* accelerator */
       0,                                /* callback */
       0,                                /* callback_action */
-      0,                                /* item_type */
-      0                                 /* extra_data */
+      (char *)"<StockItem>",            /* item_type */
+      GTK_STOCK_STOP,                   /* extra_data */
     },
     {
       (char *)"/Control/Playing",       /* path */
@@ -374,7 +372,39 @@ GtkWidget *menubar(GtkWidget *w) {
       (char *)"<CheckItem>",            /* item_type */
       0                                 /* extra_data */
     },
-    
+    {
+      (char *)"/Control/Compact mode",  /* path */
+      (char *)"<CTRL>M",                /* accelerator */
+      0,                                /* callback */
+      0,                                /* callback_action */
+      (char *)"<CheckItem>",            /* item_type */
+      0                                 /* extra_data */
+    },
+    {
+      (char *)"/Control/Global Preferences", /* path */
+      (char *)"<CTRL>G",                /* accelerator */
+      popup_globals,                    /* callback */
+      0,                                /* callback_action */
+      0,                                /* item_type */
+      0                                 /* extra_data */
+    },
+    {
+      (char *)"/Control/Activate playlist", /* path */
+      0,                                /* accelerator */
+      0,                                /* callback */
+      0,                                /* callback_action */
+      (char *)"<Branch>",               /* item_type */
+      0                                 /* extra_data */
+    },
+    {
+      (char *)"/Control/Network Playback API", /* path */
+      0,                                /* accelerator */
+      0,                                /* callback */
+      0,                                /* callback_action */
+      (char *)"<Branch>",               /* item_type */
+      0                                 /* extra_data */
+    },
+
     {
       (char *)"/Help",                  /* path */
       0,                                /* accelerator */
@@ -384,15 +414,15 @@ GtkWidget *menubar(GtkWidget *w) {
       0                                 /* extra_data */
     },
     {
-      (char *)"/Help/Manual page",      /* path */
+      (char *)"/Help/Manual",           /* path */
       0,                                /* accelerator */
       manual_popup,                     /* callback */
       0,                                /* callback_action */
-      0,                                /* item_type */
-      0                                 /* extra_data */
+      (char *)"<StockItem>",            /* item_type */
+      GTK_STOCK_HELP,                   /* extra_data */
     },
     {
-      (char *)"/Help/About DisOrder",   /* path */
+      (char *)"/Help/About Disobedience", /* path */
       0,                                /* accelerator */
       about_popup,                      /* callback */
       0,                                /* callback_action */
@@ -411,24 +441,75 @@ GtkWidget *menubar(GtkWidget *w) {
                                 sizeof entries / sizeof *entries,
                                 (GtkItemFactoryEntry *)entries,
                                 0);
-  gtk_window_add_accel_group(GTK_WINDOW(w), accel);
+  gtk_window_add_accel_group(GTK_WINDOW(window), accel);
   selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
                                                 "<GdisorderMain>/Edit/Select all tracks");
   selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
                                                 "<GdisorderMain>/Edit/Deselect all tracks");
   properties_widget = gtk_item_factory_get_widget(mainmenufactory,
                                                  "<GdisorderMain>/Edit/Track properties");
+  menu_playlists_widget = gtk_item_factory_get_item(mainmenufactory,
+                                               "<GdisorderMain>/Control/Activate playlist");
+  playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
+                                               "<GdisorderMain>/Control/Activate playlist");
+  apis_menu = gtk_item_factory_get_widget(mainmenufactory,
+                                               "<GdisorderMain>/Control/Network Playback API");
+  menu_editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory,
+                                                     "<GdisorderMain>/Edit/Edit playlists");
+  menu_minimode_widget = gtk_item_factory_get_widget(mainmenufactory,
+                                                     "<GdisorderMain>/Control/Compact mode");
   assert(selectall_widget != 0);
   assert(selectnone_widget != 0);
   assert(properties_widget != 0);
-  event_register("logged-in", menu_logged_in, 0);
-  menu_logged_in(0, 0, 0);
+  assert(menu_playlists_widget != 0);
+  assert(playlists_menu != 0);
+  assert(menu_editplaylists_widget != 0);
+  assert(apis_menu != 0);
+
+  GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
+                                                       "<GdisorderMain>/Edit");
+  g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
+
+  event_register("rights-changed", menu_rights_changed, 0);
+  users_set_sensitive(0);
   m = gtk_item_factory_get_widget(mainmenufactory,
                                   "<GdisorderMain>");
   set_tool_colors(m);
+  if(menu_minimode_widget)
+    g_signal_connect(G_OBJECT(menu_minimode_widget), "toggled",
+                     G_CALLBACK(toggled_minimode), NULL);
+
+  /* Populate the APIs menu */
+  GSList *playback_menu_group = NULL;
+  load_rtp_config();
+  assert(rtp_api != NULL);
+  for(int n = 0; uaudio_apis[n]; ++n) {
+    if(uaudio_apis[n]->flags & UAUDIO_API_CLIENT) {
+      GtkWidget *mw = gtk_radio_menu_item_new_with_label(playback_menu_group,
+                                                         uaudio_apis[n]->name);
+      playback_menu_group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(mw));
+      gtk_menu_shell_append(GTK_MENU_SHELL(apis_menu), mw);
+      /* Tick the currently selected API... */
+      if(!strcmp(uaudio_apis[n]->name, rtp_api))
+        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mw), TRUE);
+      /* ...and only then connect the signal */
+      g_signal_connect(mw, "toggled", G_CALLBACK(rtp_menu_activate),
+                       (char *)uaudio_apis[n]->name);
+    }
+  }
+
   return m;
 }
 
+static void toggled_minimode(GtkCheckMenuItem  *item,
+                             gpointer attribute((unused)) userdata) {
+  int new_full_mode = !gtk_check_menu_item_get_active(item);
+  if(full_mode != new_full_mode) {
+    full_mode = new_full_mode;
+    event_raise("mini-mode-changed", NULL);
+  }
+}
+
 /*
 Local Variables:
 c-basic-offset:2