X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/73f1b9f30c98dc525a5b6a540f6f135855d640a0..33288048f694a84b9c40653f0b72b4ce44a94ab1:/disobedience/login.c diff --git a/disobedience/login.c b/disobedience/login.c index 729597b..3dff339 100644 --- a/disobedience/login.c +++ b/disobedience/login.c @@ -26,6 +26,7 @@ #include "filepart.h" #include #include +#include /** @brief One field in the login window */ struct login_window_item { @@ -87,9 +88,23 @@ static void update_config(void) { size_t n; for(n = 0; n < NLWIS; ++n) - lwis[n].set(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))); + lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])))); } +#if 0 +static int modified_config(void) { + size_t n; + + for(n = 0; n < NLWIS; ++n) { + const char *entered = gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])); + const char *current = lwis[n].get(); + if(strcmp(entered, current)) + return 1; + } + return 0; +} +#endif + static void login_ok(GtkButton attribute((unused)) *button, gpointer attribute((unused)) userdata) { update_config(); @@ -100,15 +115,36 @@ static void login_save(GtkButton attribute((unused)) *button, gpointer attribute((unused)) userdata) { char *path = config_userconf(0, 0), *tmp; FILE *fp; + GtkWidget *yorn = 0; update_config(); + /* See if the file already exists */ + if(access(path, F_OK) == 0) { + yorn = gtk_message_dialog_new + (GTK_WINDOW(login_window), + GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_NONE, + "File %s already exists.", path); + gtk_window_set_title(GTK_WINDOW(yorn), + "Configuration file already exists"); + gtk_dialog_add_buttons(GTK_DIALOG(yorn), + "Overwrite it", GTK_RESPONSE_ACCEPT, + "Don't save after all", GTK_RESPONSE_REJECT, + (char *)0); + if(gtk_dialog_run(GTK_DIALOG(yorn)) != GTK_RESPONSE_ACCEPT) + goto done; + gtk_widget_destroy(yorn); + yorn = 0; + } byte_xasprintf(&tmp, "%s.tmp", path); /* Make sure the directory exists; don't care if it already exists. */ mkdir(d_dirname(tmp), 02700); /* Write out the file */ if(!(fp = fopen(tmp, "w"))) { - fpopup_error("error opening %s: %s", tmp, strerror(errno)); - return; + fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", + tmp, strerror(errno)); + goto done; } if(fprintf(fp, "username %s\n" "password %s\n" @@ -117,19 +153,27 @@ static void login_save(GtkButton attribute((unused)) *button, quoteutf8(config->password), quoteutf8(config->connect.s[0]), quoteutf8(config->connect.s[1])) < 0) { - fpopup_error("error writing to %s: %s", tmp, strerror(errno)); + fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s", + tmp, strerror(errno)); fclose(fp); - return; + goto done; } if(fclose(fp) < 0) { - fpopup_error("error closing %s: %s", tmp, strerror(errno)); - return; + fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s", + tmp, strerror(errno)); + goto done; } /* Rename into place */ if(rename(tmp, path) < 0) { - fpopup_error("error renaming %s: %s", tmp, strerror(errno)); - return; + fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s", + tmp, strerror(errno)); + goto done; } + fpopup_msg(GTK_MESSAGE_INFO, "Saved login configuration to %s", path); + gtk_widget_destroy(login_window); +done: + if(yorn) + gtk_widget_destroy(yorn); } static void login_cancel(GtkButton attribute((unused)) *button, @@ -140,19 +184,19 @@ static void login_cancel(GtkButton attribute((unused)) *button, /* Buttons that appear at the bottom of the window */ static const struct button buttons[] = { { - GTK_STOCK_OK, + "Login", login_ok, - "Login with these settings", + "(Re-)connect using these settings", }, { GTK_STOCK_SAVE, login_save, - "Save these settings", + "Save these settings and close window", }, { - GTK_STOCK_CANCEL, + GTK_STOCK_CLOSE, login_cancel, - "Discard all changes and close window" + "Discard changes and close window" }, }; @@ -171,13 +215,16 @@ void login_box(void) { default_connect(); /* Create a new login window */ login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_widget_set_style(login_window, tool_style); g_signal_connect(login_window, "destroy", 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*/); + gtk_widget_set_style(table, tool_style); for(n = 0; n < NLWIS; ++n) { label = gtk_label_new(lwis[n].description); + gtk_widget_set_style(label, tool_style); gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/); gtk_table_attach(GTK_TABLE(table), label, 0, 1, /* left/right_attach */ @@ -185,6 +232,7 @@ void login_box(void) { GTK_FILL, 0, /* x/yoptions */ 1, 1); /* x/ypadding */ entry = gtk_entry_new(); + gtk_widget_set_style(entry, tool_style); gtk_entry_set_visibility(GTK_ENTRY(entry), lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE); gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get()); @@ -202,6 +250,8 @@ void login_box(void) { gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/); gtk_container_add(GTK_CONTAINER(login_window), vbox); + gtk_window_set_transient_for(GTK_WINDOW(login_window), + GTK_WINDOW(toplevel)); gtk_widget_show_all(login_window); }