X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/e18c4734744e56b738ca2918dffe071f95f4c14b..319d7107dfbed2e6248f61ad463fa6ac55ebe178:/disobedience/login.c diff --git a/disobedience/login.c b/disobedience/login.c index 18afce4..73aaada 100644 --- a/disobedience/login.c +++ b/disobedience/login.c @@ -27,6 +27,9 @@ * window remains. * * It you hit Cancel then the window disappears without saving anything. + * + * TODO + * - cancel/close should be consistent with properties */ #include "disobedience.h" @@ -46,7 +49,7 @@ struct login_window_item { const char *(*get)(void); /** @brief Set a new value */ - void (*set)(const char *value); + void (*set)(struct config *c, const char *value); /** @brief Flags * @@ -77,10 +80,21 @@ static const char *get_service(void) { return config->connect.s[1]; } static const char *get_username(void) { return config->username; } static const char *get_password(void) { return config->password ? config->password : ""; } -static void set_hostname(const char *s) { config->connect.s[0] = (char *)s; } -static void set_service(const char *s) { config->connect.s[1] = (char *)s; } -static void set_username(const char *s) { config->username = s; } -static void set_password(const char *s) { config->password = s; } +static void set_hostname(struct config *c, const char *s) { + c->connect.s[0] = (char *)s; +} + +static void set_service(struct config *c, const char *s) { + c->connect.s[1] = (char *)s; +} + +static void set_username(struct config *c, const char *s) { + c->username = s; +} + +static void set_password(struct config *c, const char *s) { + c->password = s; +} /** @brief Table used to generate the form */ static const struct login_window_item lwis[] = { @@ -93,11 +107,15 @@ static const struct login_window_item lwis[] = { static GtkWidget *lwi_entry[NLWIS]; -static void login_update_config(void) { +static void login_update_config(struct config *c) { size_t n; + if(c->connect.n < 2) { + c->connect.n = 2; + c->connect.s = xcalloc(2, sizeof (char *)); + } for(n = 0; n < NLWIS; ++n) - lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])))); + lwis[n].set(c, xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])))); } /** @brief Save current login details */ @@ -145,15 +163,19 @@ done: static void login_ok(GtkButton attribute((unused)) *button, gpointer attribute((unused)) userdata) { disorder_client *c; + struct config *tmpconfig = xmalloc(sizeof *tmpconfig); /* Copy the new config into @ref config */ - login_update_config(); + login_update_config(tmpconfig); /* Attempt a login with the new details */ c = disorder_new(0); - if(!disorder_connect(c)) { + if(!disorder_connect_generic(tmpconfig, c, + tmpconfig->username, tmpconfig->password, + NULL/*cookie*/)) { /* Success; save the config and start using it */ + login_update_config(config); login_save_config(); - reset(); + logged_in(); /* Pop down login window */ gtk_widget_destroy(login_window); } else { @@ -169,6 +191,24 @@ static void login_cancel(GtkButton attribute((unused)) *button, gtk_widget_destroy(login_window); } +/** @brief Keypress handler */ +static gboolean login_keypress(GtkWidget attribute((unused)) *widget, + GdkEventKey *event, + gpointer attribute((unused)) user_data) { + if(event->state) + return FALSE; + switch(event->keyval) { + case GDK_Return: + login_ok(0, 0); + return TRUE; + case GDK_Escape: + login_cancel(0, 0); + return TRUE; + default: + return FALSE; + } +} + /* Buttons that appear at the bottom of the window */ static struct button buttons[] = { { @@ -205,7 +245,7 @@ void login_box(void) { G_CALLBACK(gtk_widget_destroyed), &login_window); gtk_window_set_title(GTK_WINDOW(login_window), "Login Details"); /* Construct the form */ - table = gtk_table_new(NLWIS + 1/*rows*/, 2/*columns*/, FALSE/*homogenous*/); + table = gtk_table_new(NLWIS/*rows*/, 2/*columns*/, FALSE/*homogenous*/); gtk_widget_set_style(table, tool_style); for(n = 0; n < NLWIS; ++n) { label = gtk_label_new(lwis[n].description); @@ -230,6 +270,11 @@ void login_box(void) { } buttonbox = create_buttons(buttons, NBUTTONS); vbox = gtk_vbox_new(FALSE, 1); + gtk_box_pack_start(GTK_BOX(vbox), + gtk_image_new_from_pixbuf(find_image("logo256.png")), + TRUE/*expand*/, + TRUE/*fill*/, + 4/*padding*/); gtk_box_pack_start(GTK_BOX(vbox), table, TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/); gtk_box_pack_start(GTK_BOX(vbox), buttonbox, @@ -237,6 +282,9 @@ void login_box(void) { gtk_container_add(GTK_CONTAINER(login_window), frame_widget(vbox, NULL)); gtk_window_set_transient_for(GTK_WINDOW(login_window), GTK_WINDOW(toplevel)); + /* Keyboard shortcuts */ + g_signal_connect(login_window, "key-press-event", + G_CALLBACK(login_keypress), 0); gtk_widget_show_all(login_window); }