X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/0d719587f8a7f70393865d0038a6a7e2fce4eb76..b7831daf8a949090e7ae1c44e21b71d83d77a2c6:/disobedience/users.c diff --git a/disobedience/users.c b/disobedience/users.c index d345c4c..9031887 100644 --- a/disobedience/users.c +++ b/disobedience/users.c @@ -32,6 +32,13 @@ * * When you select 'add' a new empty set of details are displayed to be edited. * Again Apply will commit them. + * + * TODO: @ref RIGHT_ADMIN and @ref RIGHT_USERINFO should be applied here, so we + * can give decent error messages. + * + * 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 +58,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 @@ -71,12 +79,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 * * 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; @@ -92,6 +126,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 */ @@ -242,6 +281,20 @@ static void users_add_right_group(const char *title, gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(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 * @param name User name (users_edit()) or NULL (users_add()) * @param email Email address @@ -257,8 +310,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, @@ -327,33 +383,56 @@ static rights_type users_get_rights(void) { 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) + 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, cbd); + "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 @@ -361,68 +440,61 @@ static void users_apply(GtkButton attribute((unused)) *button, popup_submsg(users_window, GTK_MESSAGE_ERROR, "Must enter a username"); return; } - if(strcmp(gtk_entry_get_text(GTK_ENTRY(users_details_password)), - 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; } - cbd = xmalloc(sizeof *cbd); - cbd->onerror = users_adduser_failed; - cbd->u.edituser.user = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_name))); - cbd->u.edituser.email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email))); - if(*cbd->u.edituser.email && !strchr(cbd->u.edituser.email, '@')) { + 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, - cbd->u.edituser.user, - xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password))), + name, + password, rights_string(users_get_rights()), cbd); + /* We switch to no-op mode while creating the user */ mode(NONE); break; case MODE_EDIT: - if(strcmp(gtk_entry_get_text(GTK_ENTRY(users_details_password)), - gtk_entry_get_text(GTK_ENTRY(users_details_password2)))) { + /* 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; } - /* TODO */ - mode(NONE); - popup_submsg(users_window, GTK_MESSAGE_INFO, "Would edit user"); + 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_submsg(users_window, 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 */ @@ -445,7 +517,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); } @@ -509,13 +581,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 @@ -525,7 +597,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; @@ -578,19 +650,22 @@ 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); - gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0); - gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0); + gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 2); + gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 2); gtk_container_add(GTK_CONTAINER(users_window), hbox); }