chiark / gitweb /
Desensitive _MINE and _RANDOM if _ALL is checked
[disorder] / disobedience / users.c
index d0fb3dcc89e414a0901dc5588e0e2170568f249c..bca35aaab2a9d3917163544e662d7e2fdb609fda 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include "disobedience.h"
+#include "bits.h"
 
 static GtkWidget *users_window;
 static GtkListStore *users_list;
@@ -32,7 +33,9 @@ static GtkWidget *users_details_name;
 static GtkWidget *users_details_email;
 static GtkWidget *users_details_password;
 static GtkWidget *users_details_password2;
-//static GtkWidget *users_details_rights;
+static GtkWidget *users_details_rights[32];
+
+static const char *users_who, *users_email, *users_rights, *users_password;
 
 static int usercmp(const void *a, const void *b) {
   return strcmp(*(char **)a, *(char **)b);
@@ -76,13 +79,36 @@ static char *users_getuser(void) {
 /** @brief Text should be editable */
 #define DETAIL_EDITABLE 2
 
+static GtkWidget *users_detail_generic(GtkWidget *table,
+                                       int *rowp,
+                                       const char *title,
+                                       GtkWidget *selector) {
+  const int row = (*rowp)++;
+  GtkWidget *const label = gtk_label_new(title);
+  gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
+  gtk_table_attach(GTK_TABLE(table),
+                   label,
+                   0, 1,                /* left/right_attach */
+                   row, row+1,          /* top/bottom_attach */
+                   GTK_FILL,            /* xoptions */
+                   0,                   /* yoptions */
+                   1, 1);               /* x/ypadding */
+  gtk_table_attach(GTK_TABLE(table),
+                   selector,
+                   1, 2,                /* left/right_attach */
+                   row, row + 1,        /* top/bottom_attach */
+                   GTK_EXPAND|GTK_FILL, /* xoptions */
+                   GTK_FILL,            /* yoptions */
+                   1, 1);               /* x/ypadding */
+  return selector;
+}
+
 /** @brief Add a row to the user details table
  * @param table Containin table widget
  * @param rowp Pointer to row number, incremented
  * @param title Label for this row
  * @param value Initial value or NULL
  * @param flags Flags word
- *
  * @return The text entry widget
  */
 static GtkWidget *users_add_detail(GtkWidget *table,
@@ -90,35 +116,96 @@ static GtkWidget *users_add_detail(GtkWidget *table,
                                    const char *title,
                                    const char *value,
                                    unsigned flags) {
-  const int row = (*rowp)++;
-  GtkWidget *label, *entry;
-  
-  label = gtk_label_new(title);
-  gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
-  gtk_table_attach(GTK_TABLE(table),
-                   label,
-                   0, 1,                /* left/right_attach */
-                   row, row+1,          /* top/bottom_attach */
-                   GTK_FILL,            /* xoptions */
-                   0,                   /* yoptions */
-                   1, 1);               /* x/ypadding */
-  entry = gtk_entry_new();
+  GtkWidget *entry = gtk_entry_new();
+
   gtk_entry_set_visibility(GTK_ENTRY(entry),
                            !!(flags & DETAIL_VISIBLE));
   gtk_editable_set_editable(GTK_EDITABLE(entry),
                             !!(flags & DETAIL_EDITABLE));
   if(value)
     gtk_entry_set_text(GTK_ENTRY(entry), value);
-  gtk_table_attach(GTK_TABLE(table),
-                   entry,
-                   1, 2,                /* left/right_attach */
-                   row, row + 1,        /* top/bottom_attach */
-                   GTK_EXPAND|GTK_FILL, /* xoptions */
-                   GTK_FILL,            /* yoptions */
-                   1, 1);               /* x/ypadding */
-  return entry;
+  return users_detail_generic(table, rowp, title, entry);
+}
+
+/** @brief Add a checkbox for a right
+ * @param table Containing table
+ * @param rowp Pointer to row number, incremented
+ * @param title Label for this row
+ * @param value Right bit (masked but not necessarily normalized)
+ */
+static void users_add_right(GtkWidget *table,
+                            int *rowp,
+                            const char *title,
+                            rights_type value) {
+  GtkWidget *check = gtk_check_button_new();
+  
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
+  users_details_rights[leftmost_bit(value)] = check;
+  users_detail_generic(table, rowp, title, check);
+}
+
+/** @brief Set sensitivity of particular mine/random rights bits */
+static void users_details_sensitize(rights_type r) {
+  const int bit = leftmost_bit(r);
+  const GtkWidget *all = users_details_rights[bit];
+  const int sensitive = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all));
+  
+  gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
+  gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
+}
+
+/** @brief Set sensitivity of all mine/random rights bits */
+static void users_details_sensitize_all(void) {
+  users_details_sensitize(RIGHT_MOVE_ANY);
+  users_details_sensitize(RIGHT_REMOVE_ANY);
+  users_details_sensitize(RIGHT_SCRATCH_ANY);
+}
+
+/** @brief Called when an _ALL widget is toggled
+ *
+ * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets.  We
+ * just do the lot rather than trying to figure out which one changed,
+ */
+static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
+                              gpointer attribute((unused)) user_data) {
+  users_details_sensitize_all();
+}
+
+/** @brief Add a checkbox for a three-right group
+ * @param table Containing table
+ * @param rowp Pointer to row number, incremented
+ * @param title Label for this row
+ * @param bits Rights bits (not masked or normalized)
+ * @param mask Mask for this group (must be 7.2^n)
+ */
+static void users_add_right_group(GtkWidget *table,
+                                  int *rowp,
+                                  const char *title,
+                                  rights_type bits,
+                                  rights_type mask) {
+  GtkWidget *any = gtk_check_button_new_with_label("Any");
+  GtkWidget *mine = gtk_check_button_new_with_label("Own");
+  GtkWidget *random = gtk_check_button_new_with_label("Random");
+  GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
+  const uint32_t first = mask / 7;
+  const int bit = leftmost_bit(first);
+
+  /* Discard irrelevant bits */
+  bits &= mask;
+  /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
+  bits >>= bit;
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
+  gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
+  users_details_rights[bit] = any;
+  users_details_rights[bit + 1] = mine;
+  users_details_rights[bit + 2] = random;
+  users_detail_generic(table, rowp, title, hbox);
+  g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
 }
-                                                   
 
 /** @brief Create the user details window
  * @param title Window title
@@ -138,10 +225,11 @@ static GtkWidget *users_add_detail(GtkWidget *table,
 static void users_makedetails(const char *title,
                               const char *name,
                               const char *email,
-                              const char attribute((unused)) *rights,
+                              const char *rights,
                               const char *password) {
   GtkWidget *table;
   int row = 0;
+  rights_type r = 0;
   
   /* Create the window */
   users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@@ -167,7 +255,21 @@ static void users_makedetails(const char *title,
                                              password,
                                              DETAIL_EDITABLE);
 
-  /* TODO rights */
+  parse_rights(rights, &r, 1);
+  users_add_right(table, &row, "Read operations", r & RIGHT_READ);
+  users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
+  users_add_right_group(table, &row, "Move", r, RIGHT_MOVE__MASK);
+  users_add_right_group(table, &row, "Remove", r, RIGHT_REMOVE__MASK);
+  users_add_right_group(table, &row, "Scratch", r, RIGHT_SCRATCH__MASK);
+  users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
+  users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
+  users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
+  users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
+  users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
+  users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
+  users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
+  users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
+  users_details_sensitize_all();
 
   gtk_container_add(GTK_CONTAINER(users_details_window), table);
   gtk_widget_show_all(users_details_window);
@@ -175,6 +277,7 @@ static void users_makedetails(const char *title,
 
 static void users_add(GtkButton attribute((unused)) *button,
                      gpointer attribute((unused)) userdata) {
+  /* TODO */
 }
 
 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
@@ -229,13 +332,32 @@ static void users_delete(GtkButton attribute((unused)) *button,
   }
 }
 
+static void users_got_email(void attribute((unused)) *v, const char *value) {
+  users_email = value;
+}
+
+static void users_got_rights(void attribute((unused)) *v, const char *value) {
+  users_rights = value;
+}
+
+static void users_got_password(void attribute((unused)) *v, const char *value) {
+  users_password = value;
+  users_makedetails("editing user details",
+                    users_who,
+                    users_email,
+                    users_rights,
+                    users_password);
+}
+
 static void users_edit(GtkButton attribute((unused)) *button,
                       gpointer attribute((unused)) userdata) {
-  char *who;
-  
-  if(!(who = users_getuser()))
+  if(!(users_who = users_getuser()))
     return;
-  users_makedetails("editing user details", who, "foo@bar", "wibble", "wobble");
+  /* Schedule user lookups for all the properties we know about.  This is all a
+   * bit ad-hoc but will do for now. */
+  disorder_eclient_userinfo(client, users_got_email, users_who, "email", 0);
+  disorder_eclient_userinfo(client, users_got_rights, users_who, "rights", 0);
+  disorder_eclient_userinfo(client, users_got_password, users_who, "password", 0);
 }
 
 static const struct button users_buttons[] = {