chiark / gitweb /
Merge from disorder.userman
[disorder] / disobedience / users.c
index 2be2db18d9de81cce560c12465053cbae6572e8b..fe904337caa5d8a2306ceb678096fe3661dc5500 100644 (file)
  *
  * When you select 'add' a new empty set of details are displayed to be edited.
  * Again Apply will commit them.
+ *
+ * TODO: it would be really nice if the Username entry could be removed and new
+ * user names entered in the list, rather off in the details panel.  This may
+ * be possible with a sufficiently clever GtkCellRenderer.
  */
 
 #include "disobedience.h"
@@ -51,6 +55,7 @@ static GtkWidget *users_details_password2;
 static GtkWidget *users_details_rights[32];
 static int users_details_row;
 static const char *users_selected;
+static const char *users_deferred_select;
 
 static int users_mode;
 #define MODE_NONE 0
@@ -59,8 +64,9 @@ static int users_mode;
 
 #define mode(X) do {                                    \
   users_mode = MODE_##X;                                \
-  fprintf(stderr, "%s:%d: %s(): mode -> %s\n",          \
+  if(0) fprintf(stderr, "%s:%d: %s(): mode -> %s\n",    \
           __FILE__, __LINE__, __FUNCTION__, #X);        \
+  users_details_sensitize_all();                        \
 } while(0)
 
 static const char *users_email, *users_rights, *users_password;
@@ -70,11 +76,38 @@ static int usercmp(const void *a, const void *b) {
   return strcmp(*(char **)a, *(char **)b);
 }
 
+/** @brief Find a user
+ * @param user User to find
+ * @param iter Iterator to point at user
+ * @return 0 on success, -1 if not found
+ */
+static int users_find_user(const char *user,
+                           GtkTreeIter *iter) {
+  char *who;
+
+  /* Find the user */
+  if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), iter))
+    return -1;
+  do {
+    gtk_tree_model_get(GTK_TREE_MODEL(users_list), iter,
+                      0, &who, -1);
+    if(!strcmp(who, user)) {
+      g_free(who);
+      return 0;
+    }
+    g_free(who);
+  } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), iter));
+  return -1;
+}
+
 /** @brief Called with the list of users
  *
- * Currently this is called when the window is created, and is responsible for
- * showing it.  There's currently no facility for refreshing the list, which
- * hopefuly would preserve the select user (if any).
+ * Called:
+ * - at startup to populate the initial list
+ * - when we add a user
+ * - maybe in the future when we delete a user
+ *
+ * If users_deferred_select is set then that user is selected.
  */
 static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
   int n;
@@ -90,6 +123,11 @@ static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
                                      -1);       /* no more columns */
   /* Only show the window when the list is populated */
   gtk_widget_show_all(users_window);
+  if(users_deferred_select) {
+    if(!users_find_user(users_deferred_select, &iter))
+      gtk_tree_selection_select_iter(users_selection, &iter);
+    users_deferred_select = 0;
+  }
 }
 
 /** @brief Text should be visible */
@@ -155,7 +193,7 @@ static void users_add_right(const char *title,
   GtkWidget **checkp = &users_details_rights[leftmost_bit(right)];
 
   if(!(check = *checkp)) {
-    *checkp = check = gtk_check_button_new();
+    *checkp = check = gtk_check_button_new_with_label("");
     users_detail_generic(title, check);
   }
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
@@ -165,7 +203,8 @@ static void users_add_right(const char *title,
 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));
+  const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
+                         && users_mode != MODE_NONE);
 
   gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
   gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
@@ -173,27 +212,19 @@ static void users_details_sensitize(rights_type r) {
 
 /** @brief Set sensitivity of everything in sight */
 static void users_details_sensitize_all(void) {
-  int apply_sensitive;
+  int n;
 
-  /* Three-right groups */
+  for(n = 0; n < 32; ++n)
+    if(users_details_rights[n])
+      gtk_widget_set_sensitive(users_details_rights[n], users_mode != MODE_NONE);
+  gtk_widget_set_sensitive(users_details_name, users_mode != MODE_NONE);
+  gtk_widget_set_sensitive(users_details_email, users_mode != MODE_NONE);
+  gtk_widget_set_sensitive(users_details_password, users_mode != MODE_NONE);
+  gtk_widget_set_sensitive(users_details_password2, users_mode != MODE_NONE);
   users_details_sensitize(RIGHT_MOVE_ANY);
   users_details_sensitize(RIGHT_REMOVE_ANY);
   users_details_sensitize(RIGHT_SCRATCH_ANY);
-  /* Apply button */
-  switch(users_mode) {
-  case MODE_NONE:
-    apply_sensitive = 0;
-    break;
-  case MODE_EDIT:
-    apply_sensitive = 1;
-    break;
-  case MODE_ADD:
-    apply_sensitive = !!*gtk_entry_get_text(GTK_ENTRY(users_details_name));
-    break;
-  default:
-    assert(!"reached");
-  }
-  gtk_widget_set_sensitive(users_apply_button, apply_sensitive);
+  gtk_widget_set_sensitive(users_apply_button, users_mode != MODE_NONE);
   gtk_widget_set_sensitive(users_delete_button, !!users_selected);
 }
 
@@ -217,26 +248,26 @@ static void users_add_right_group(const char *title,
                                   rights_type mask) {
   const uint32_t first = mask / 7;
   const int bit = leftmost_bit(first);
-  GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *random;
+  GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *rnd;
 
   if(!*widgets) {
     GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
 
     any = widgets[0] = gtk_check_button_new_with_label("Any");
     mine = widgets[1] = gtk_check_button_new_with_label("Own");
-    random = widgets[2] = gtk_check_button_new_with_label("Random");
+    rnd = widgets[2] = gtk_check_button_new_with_label("Random");
     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);
+    gtk_box_pack_start(GTK_BOX(hbox), rnd, FALSE, FALSE, 0);
     users_detail_generic(title, hbox);
     g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
     users_details_rights[bit] = any;
     users_details_rights[bit + 1] = mine;
-    users_details_rights[bit + 2] = random;
+    users_details_rights[bit + 2] = rnd;
   } else {
     any = widgets[0];
     mine = widgets[1];
-    random = widgets[2];
+    rnd = widgets[2];
   }
   /* Discard irrelevant bits */
   bits &= mask;
@@ -244,7 +275,21 @@ static void users_add_right_group(const char *title,
   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_toggle_button_set_active(GTK_TOGGLE_BUTTON(rnd), !!(bits & 4));
+}
+
+/** @brief Called when the details table is destroyed */
+static void users_details_destroyed(GtkWidget attribute((unused)) *widget,
+                                    GtkWidget attribute((unused)) **wp) {
+  users_details_table = 0;
+  g_object_unref(users_list);
+  users_list = 0;
+  users_details_name = 0;
+  users_details_email = 0;
+  users_details_password = 0;
+  users_details_password2 = 0;
+  memset(users_details_rights, 0, sizeof users_details_rights);
+  /* also users_selection?  Not AFAICT; _get_selection does upref */
 }
 
 /** @brief Create or modify the user details table
@@ -262,8 +307,11 @@ static void users_makedetails(const char *name,
   rights_type r = 0;
   
   /* Create the table if it doesn't already exist */
-  if(!users_details_table)
+  if(!users_details_table) {
     users_details_table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
+    g_signal_connect(users_details_table, "destroy",
+                     G_CALLBACK(users_details_destroyed), 0);
+  }
 
   /* Create or update the widgets */
   users_add_detail(&users_details_name, "Username", name,
@@ -300,9 +348,11 @@ static void users_add(GtkButton attribute((unused)) *button,
   /* Unselect whatever is selected */
   gtk_tree_selection_unselect_all(users_selection);
   /* Reset the form */
+  /* TODO it would be better to use the server default_rights if there's no
+   * client setting. */
   users_makedetails("",
                     "",
-                    "",                 /* TODO default_rights */
+                    config->default_rights,
                     "",
                     DETAIL_EDITABLE|DETAIL_VISIBLE,
                     DETAIL_EDITABLE|DETAIL_VISIBLE);
@@ -310,50 +360,138 @@ static void users_add(GtkButton attribute((unused)) *button,
   mode(ADD);
 }
 
+static rights_type users_get_rights(void) {
+  rights_type r = 0;
+  int n;
+
+  /* Extract the rights value */
+  for(n = 0; n < 32; ++n) {
+    if(users_details_rights[n])
+      if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(users_details_rights[n])))
+         r |= 1 << n;
+  }
+  /* Throw out redundant bits */
+  if(r & RIGHT_REMOVE_ANY)
+    r &= ~(rights_type)(RIGHT_REMOVE_MINE|RIGHT_REMOVE_RANDOM);
+  if(r & RIGHT_MOVE_ANY)
+    r &= ~(rights_type)(RIGHT_MOVE_MINE|RIGHT_MOVE_RANDOM);
+  if(r & RIGHT_SCRATCH_ANY)
+    r &= ~(rights_type)(RIGHT_SCRATCH_MINE|RIGHT_SCRATCH_RANDOM);
+  return r;
+}
+
+/** @brief Called when various things fail */
+static void users_op_failed(struct callbackdata attribute((unused)) *cbd,
+                            int attribute((unused)) code,
+                            const char *msg) {
+  popup_submsg(users_window, GTK_MESSAGE_ERROR, msg);
+}
+
+/** @brief Called when a new user has been created */
+static void users_adduser_completed(void *v) {
+  struct callbackdata *cbd = v;
+
+  /* Now the user is created we can go ahead and set the email address */
+  if(*cbd->u.edituser.email) {
+    struct callbackdata *ncbd = xmalloc(sizeof *cbd);
+    ncbd->onerror = users_op_failed;
+    disorder_eclient_edituser(client, NULL, cbd->u.edituser.user,
+                              "email", cbd->u.edituser.email, ncbd);
+  }
+  /* Refresh the list of users */
+  disorder_eclient_users(client, users_got_list, 0);
+  /* We'll select the newly created user */
+  users_deferred_select = cbd->u.edituser.user;
+}
+
+/** @brief Called if creating a new user fails */
+static void users_adduser_failed(struct callbackdata attribute((unused)) *cbd,
+                                 int attribute((unused)) code,
+                                 const char *msg) {
+  popup_submsg(users_window, GTK_MESSAGE_ERROR, msg);
+  mode(ADD);                            /* Let the user try again */
+}
+
 /** @brief Called when the 'Apply' button is pressed */
 static void users_apply(GtkButton attribute((unused)) *button,
                         gpointer attribute((unused)) userdata) {
+  struct callbackdata *cbd;
+  const char *password;
+  const char *password2;
+  const char *name;
+  const char *email;
+
   switch(users_mode) {
   case MODE_NONE:
     return;
   case MODE_ADD:
-    if(!*gtk_entry_get_text(GTK_ENTRY(users_details_name)))
+    name = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_name)));
+    email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
+    password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
+    password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
+    if(!*name) {
+      /* No username.  Really we wanted to desensitize the Apply button when
+       * there's no userame but there doesn't seem to be a signal to detect
+       * changes to the entry text.  Consequently we have error messages
+       * instead.  */
+      popup_submsg(users_window, GTK_MESSAGE_ERROR, "Must enter a username");
       return;
-    /* TODO create user */
+    }
+    if(strcmp(password, password2)) {
+      popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
+      return;
+    }
+    if(*email && !strchr(email, '@')) {
+      /* The server will complain about this but we can give a better error
+       * message this way */
+      popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
+      return;
+    }
+    cbd = xmalloc(sizeof *cbd);
+    cbd->onerror = users_adduser_failed;
+    cbd->u.edituser.user = name;
+    cbd->u.edituser.email = email;
+    disorder_eclient_adduser(client, users_adduser_completed,
+                             name,
+                             password,
+                             rights_string(users_get_rights()),
+                             cbd);
+    /* We switch to no-op mode while creating the user */
+    mode(NONE);
     break;
   case MODE_EDIT:
-    /* TODO */
+    /* Ugh, can we de-dupe with above? */
+    email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
+    password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
+    password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
+    if(strcmp(password, password2)) {
+      popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
+      return;
+    }
+    if(*email && !strchr(email, '@')) {
+      popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
+      return;
+    }
+    cbd = xmalloc(sizeof *cbd);
+    cbd->onerror = users_op_failed;
+    disorder_eclient_edituser(client, NULL, users_selected,
+                              "email", email, cbd);
+    disorder_eclient_edituser(client, NULL, users_selected,
+                              "password", password, cbd);
+    disorder_eclient_edituser(client, NULL, users_selected,
+                              "rights", rights_string(users_get_rights()), cbd);
+    /* We remain in edit mode */
     break;
   }
 }
 
-/** @brief Called when user deletion goes wrong */
-static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
-                               int attribute((unused)) code,
-                               const char *msg) {
-  popup_msg(GTK_MESSAGE_ERROR, msg);
-}
-
 /** @brief Called when a user has been deleted */
 static void users_deleted(void *v) {
   const struct callbackdata *const cbd = v;
-  GtkTreeIter iter;
-  char *who;
+  GtkTreeIter iter[1];
 
-  /* Find the user */
-  if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
-    return;
-  do {
-    gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
-                      0, &who, -1);
-    if(!strcmp(who, cbd->u.user))
-      break;
-    g_free(who);
-    who = 0;
-  } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
-  /* Remove them */
-  gtk_list_store_remove(users_list, &iter);
-  g_free(who);
+  if(!users_find_user(cbd->u.user, iter))    /* Find the user... */
+    gtk_list_store_remove(users_list, iter); /* ...and remove them */
 }
 
 /** @brief Called when the 'Delete' button is pressed */
@@ -376,7 +514,7 @@ static void users_delete(GtkButton attribute((unused)) *button,
   gtk_widget_destroy(yesno);
   if(res == GTK_RESPONSE_YES) {
     cbd = xmalloc(sizeof *cbd);
-    cbd->onerror = users_deleted_error;
+    cbd->onerror = users_op_failed;
     cbd->u.user = users_selected;
     disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
   }
@@ -440,13 +578,13 @@ static void users_selection_changed(GtkTreeSelection attribute((unused)) *treese
 /** @brief Table of buttons below the user list */
 static struct button users_buttons[] = {
   {
-    "Add user",
+    GTK_STOCK_ADD,
     users_add,
     "Create a new user",
     0
   },
   {
-    "Delete user",
+    GTK_STOCK_REMOVE,
     users_delete,
     "Delete a user",
     0
@@ -456,7 +594,7 @@ static struct button users_buttons[] = {
 
 /** @brief Pop up the user management window */
 void manage_users(void) {
-  GtkWidget *tree, *buttons, *hbox, *vbox, *vbox2;
+  GtkWidget *tree, *buttons, *hbox, *hbox2, *vbox, *vbox2;
   GtkCellRenderer *cr;
   GtkTreeViewColumn *col;
   
@@ -500,8 +638,8 @@ void manage_users(void) {
   users_delete_button = users_buttons[1].widget;
 
   /* Buttons live below the list */
-  vbox = gtk_vbox_new(FALSE, 2);
-  gtk_box_pack_start(GTK_BOX(vbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
+  vbox = gtk_vbox_new(FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
 
   /* Create an empty user details table, and put an apply button below it */
@@ -509,20 +647,26 @@ void manage_users(void) {
   users_makedetails("", "", "", "",
                     DETAIL_VISIBLE,
                     DETAIL_VISIBLE);
-  /* TODO apply button is much too wide right now... */
   g_signal_connect(users_apply_button, "clicked",
                    G_CALLBACK(users_apply), NULL);
-  vbox2 = gtk_vbox_new(FALSE, 2);
+  hbox2 = gtk_hbox_new(FALSE, 0);
+  gtk_box_pack_end(GTK_BOX(hbox2), users_apply_button,
+                   FALSE/*expand*/, FALSE, 0);
+  
+  vbox2 = gtk_vbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
                      TRUE/*expand*/, TRUE/*fill*/, 0);
-  gtk_box_pack_start(GTK_BOX(vbox2), users_apply_button,
+  gtk_box_pack_start(GTK_BOX(vbox2), hbox2,
                      FALSE/*expand*/, FALSE, 0);
   
-    /* User details are to the right of the list */
-  hbox = gtk_hbox_new(FALSE, 2);
+  /* User details are to the right of the list.  We put in a pointless event
+   * box as as spacer, so that the longest label in the user details isn't
+   * cuddled up to the user list. */
+  hbox = gtk_hbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
   gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
-  gtk_container_add(GTK_CONTAINER(users_window), hbox);
+  gtk_container_add(GTK_CONTAINER(users_window), frame_widget(hbox, NULL));
 }
 
 /*