chiark / gitweb /
Sensitize icons according to current user rights. There's up to 10s
authorRichard Kettlewell <rjk@greenend.org.uk>
Mon, 9 Jun 2008 08:17:07 +0000 (09:17 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Mon, 9 Jun 2008 08:17:07 +0000 (09:17 +0100)
latency in rights taking effect, which is unavoidable given the server
currently doesn't tell us when they change.

disobedience/control.c
disobedience/disobedience.c
disobedience/disobedience.h
disobedience/menu.c

index 92532c448264e7ea09df1f9ec7a6462541f81533..fd3765147c65504c00de3656c6281331d4442b34 100644 (file)
@@ -138,17 +138,28 @@ static int pause_resume_on(void) {
 }
 
 static int pause_resume_sensitive(void) {
-  return !!(last_state & DISORDER_PLAYING);
+  return !!(last_state & DISORDER_PLAYING)
+    && (last_rights & RIGHT_PAUSE);
 }
 
 static int scratch_sensitive(void) {
-  return !!(last_state & DISORDER_PLAYING);
+  return !!(last_state & DISORDER_PLAYING)
+    && (last_rights & RIGHT_SCRATCH__MASK);
+  /* TODO: it's more complicated than that... */
+}
+
+static int random_sensitive(void) {
+  return !!(last_rights & RIGHT_GLOBAL_PREFS);
 }
 
 static int random_enabled(void) {
   return !!(last_state & DISORDER_RANDOM_ENABLED);
 }
 
+static int playing_sensitive(void) {
+  return !!(last_rights & RIGHT_GLOBAL_PREFS);
+}
+
 static int playing_enabled(void) {
   return !!(last_state & DISORDER_PLAYING_ENABLED);
 }
@@ -188,6 +199,7 @@ static struct icon icons[] = {
     tip_off: "Enable random play",
     menuitem: "<GdisorderMain>/Control/Random play",
     on: random_enabled,
+    sensitive: random_sensitive,
     action_go_on: disorder_eclient_random_enable,
     action_go_off: disorder_eclient_random_disable,
   },
@@ -197,6 +209,7 @@ static struct icon icons[] = {
     icon_off: "notes.png",
     tip_off: "Enable play",
     on: playing_enabled,
+    sensitive: playing_sensitive,
     action_go_on: disorder_eclient_enable,
     action_go_off: disorder_eclient_disable,
   },
@@ -328,6 +341,7 @@ GtkWidget *control_widget(void) {
   event_register("pause-changed", control_changed, 0);
   event_register("playing-changed", control_changed, 0);
   event_register("rtp-changed", control_changed, 0);
+  event_register("rights-changed", control_changed, 0);
   event_register("volume-changed", volume_changed, 0);
   return hbox;
 }
index 5e32d2e1172b52b4cd99971bde9540cb25d14364..e8bbc400bbe5cf7a9c991dc05f621aff6d56e6db 100644 (file)
@@ -78,6 +78,12 @@ static int nop_in_flight;
 /** @brief True if an rtp-address command is in flight */
 static int rtp_address_in_flight;
 
+/** @brief True if a rights lookup is in flight */
+static int rights_lookup_in_flight;
+
+/** @brief Current rights bitmap */
+rights_type last_rights;
+
 /** @brief Global tooltip group */
 GtkTooltips *tips;
 
@@ -270,9 +276,31 @@ static const GMemVTable glib_memvtable = {
 };
 #endif
 
+static void userinfo_rights_completed(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;
+  }
+  /* If rights have changed, signal everything that cares */
+  if(r != last_rights) {
+    last_rights = r;
+    ++suppress_actions;
+    event_raise("rights-changed", 0);
+    --suppress_actions;
+  }
+  rights_lookup_in_flight = 0;
+}
+
 /** @brief Called occasionally */
 static gboolean periodic_slow(gpointer attribute((unused)) data) {
-  D(("periodic"));
+  D(("periodic_slow"));
   /* Expire cached data */
   cache_expire();
   /* Update everything to be sure that the connection to the server hasn't
@@ -285,6 +313,14 @@ static gboolean periodic_slow(gpointer attribute((unused)) data) {
 #if MTRACK
   report_tags();
 #endif
+  /* Periodically check what our rights are */
+  if(!rights_lookup_in_flight) {
+    rights_lookup_in_flight = 1;
+    disorder_eclient_userinfo(client,
+                              userinfo_rights_completed,
+                              config->username, "rights",
+                              0);
+  }
   return TRUE;                          /* don't remove me */
 }
 
@@ -408,8 +444,10 @@ void logged_in(void) {
   disorder_eclient_close(logclient);
   rtp_supported = 0;
   event_raise("logged-in", 0);
-  /* Might be a new server so re-check */
+  /* Force the periodic checks */
   check_rtp_address();
+  periodic_slow(0);
+  periodic_fast(0);
 }
 
 int main(int argc, char **argv) {
@@ -450,7 +488,7 @@ int main(int argc, char **argv) {
      || !(logclient = gtkclient()))
     return 1;                           /* already reported an error */
   /* periodic operations (e.g. expiring the cache, checking local volume) */
-  g_timeout_add(600000/*milliseconds*/, periodic_slow, 0);
+  g_timeout_add(10000/*milliseconds*/, periodic_slow, 0);
   g_timeout_add(1000/*milliseconds*/, periodic_fast, 0);
   /* global tooltips */
   tips = gtk_tooltips_new();
@@ -466,8 +504,10 @@ int main(int argc, char **argv) {
                      0/*notify*/);
   /* Start monitoring the log */
   disorder_eclient_log(logclient, &log_callbacks, 0);
-  /* See if RTP play supported */
+  /* Initiate all the checks */
   check_rtp_address();
+  periodic_slow(0);
+  periodic_fast(0);
   suppress_actions = 0;
   /* If no password is set yet pop up a login box */
   if(!config->password)
index d945e32c8997629d8dee9cf36a0bec69ec5c5a04..4a9bfe51575ed20daee1933dbdea24d5a00ff212 100644 (file)
@@ -111,6 +111,7 @@ extern GtkWidget *tabs;                 /* main tabs */
 extern disorder_eclient *client;        /* main client */
 
 extern unsigned long last_state;        /* last reported state */
+extern rights_type last_rights;         /* last reported rights bitmap */
 extern int playing;                     /* true if playing some track */
 extern int volume_l, volume_r;          /* current volume */
 extern double goesupto;                 /* volume upper bound */
index b52190efcb3391306282c7f9b573ddf2b24b1e26..b2106ef367ed0ce90d2e4cd5edad942bbe2971b7 100644 (file)
@@ -228,29 +228,11 @@ 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));
-}
-
-/** @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 when our rights change */
+static void menu_rights_changed(const char attribute((unused)) *event,
+                                void attribute((unused)) *eventdata,
+                                void attribute((unused)) *callbackdata) {
+  users_set_sensitive(!!(last_rights & RIGHT_ADMIN));
 }
 
 /** @brief Create the menu bar widget */
@@ -421,8 +403,8 @@ GtkWidget *menubar(GtkWidget *w) {
   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);
+  event_register("rights-changed", menu_rights_changed, 0);
+  users_set_sensitive(0);
   m = gtk_item_factory_get_widget(mainmenufactory,
                                   "<GdisorderMain>");
   set_tool_colors(m);