implement repo New Credentials button

Tue, 29 Oct 2024 20:30:00 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 29 Oct 2024 20:30:00 +0100
changeset 74
4466ebbd9fd5
parent 73
ede7885491b1
child 75
73c8a3780c72

implement repo New Credentials button

application/settings.c file | annotate | diff | comparison | revisions
application/settings.h file | annotate | diff | comparison | revisions
--- a/application/settings.c	Tue Oct 29 19:07:11 2024 +0100
+++ b/application/settings.c	Tue Oct 29 20:30:00 2024 +0100
@@ -118,6 +118,108 @@
     settings_credentials_select(settings, NULL);
 }
 
+static void addcred_onclick(UiEvent *event, void *userdata) {
+    SettingsNewCredentialsDialog *wdata = event->window;
+    if(event->intval == 4) {
+        char *id = ui_get(wdata->id);
+        char *user = ui_get(wdata->user);
+        char *password = ui_get(wdata->password);
+        int addlocation = ui_get(wdata->addlocation);
+        
+        if(strlen(id) == 0) {
+            ui_set(wdata->message, "Missing identifier!");
+            return;
+        }
+        if(strlen(user) == 0) {
+            ui_set(wdata->message, "Missing user!");
+            return;
+        } 
+        if(strlen(password) == 0) {
+            ui_set(wdata->message, "Missing password!");
+            return;
+        }
+        
+        if(pwdstore_has_id(wdata->settings->pwdstore, id)) {
+            ui_set(wdata->message, "Identifier already in use!");
+            return;
+        }
+        
+        CxList *locations = NULL;
+        if(addlocation) {
+            locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+            cxListAdd(locations, strdup(wdata->url));
+        }
+        pwdstore_put(wdata->settings->pwdstore, id, user, password);
+        pwdstore_put_index(wdata->settings->pwdstore, strdup(id), locations);
+        
+        settings_reload_credentials(wdata->settings);
+        settings_reload_repo_credentials(wdata->settings);
+        
+        CxList *list = wdata->settings->repo_credentials->data;
+        ssize_t index = cxListFind(list, id);
+        if(index >= 0) {
+            ui_list_setselection(wdata->settings->repo_credentials, index);
+        }
+    }
+    
+    ui_close(event->obj);
+}
+
+// called by repo_edit
+// create new credentials in dialog
+static void credentials_new(UiEvent *event, void *userdata) {
+    SettingsWindow *settings = event->window;
+    
+    UiObject *dialog = ui_dialog_window(event->obj,
+            .modal = UI_ON,
+            .title = "New Credentials",
+            .show_closebutton = UI_OFF,
+            .lbutton1 = "Cancel",
+            .rbutton4 = "Add",
+            .onclick = addcred_onclick);
+    
+    SettingsNewCredentialsDialog *wdata = ui_malloc(dialog->ctx, sizeof(SettingsNewCredentialsDialog));
+    wdata->settings = settings;
+    wdata->id = ui_string_new(dialog->ctx, NULL);
+    wdata->user = ui_string_new(dialog->ctx, NULL);
+    wdata->password = ui_string_new(dialog->ctx, NULL);
+    wdata->addlocation = ui_int_new(dialog->ctx, NULL);
+    wdata->message = ui_string_new(dialog->ctx, NULL);
+    dialog->window = wdata;
+    
+    char *url = ui_get(settings->repo_url);
+    if(strlen(url) > 0) {
+        wdata->url = ui_strdup(dialog->ctx, url);
+    } else {
+        wdata->url = NULL;
+    }
+    
+    ui_grid(dialog, .margin = 16, .columnspacing = 40, .rowspacing = 10) {
+        ui_llabel(dialog, .label = "Identifier");
+        ui_textfield(dialog, .value = wdata->id,  .hexpand = TRUE);
+        ui_newline(dialog);
+        
+        ui_llabel(dialog, .label = "User");
+        ui_textfield(dialog, .value = wdata->user, .hexpand = TRUE);
+        ui_newline(dialog);
+        
+        ui_llabel(dialog, .label = "Password");
+        ui_passwordfield(dialog, .value = wdata->password, .hexpand = TRUE);
+        ui_newline(dialog);
+        
+        if(wdata->url) {
+            cxmutstr msg = cx_asprintf("Add URL %s to Credential Locations", url);
+            ui_checkbox(dialog, .label = msg.ptr, .value = wdata->addlocation, .colspan = 2);
+            ui_newline(dialog);
+            free(msg.ptr);
+        }
+        
+        ui_llabel(dialog, .value = wdata->message, .colspan = 2);
+    }
+    
+    ui_show(dialog);
+}
+
 static void credentials_remove(UiEvent *event, void *userdata) {
     SettingsWindow *settings = event->window;
     if(settings->credentials_selected_id) {
@@ -588,7 +690,7 @@
                                 ui_hbox(obj, .spacing = 4, .colspan = 2) {
                                     ui_combobox(obj, .list = wdata->repo_credentials);
                                     ui_hbox0(obj) {
-                                        ui_button(obj, .label = "New Credentials");
+                                        ui_button(obj, .label = "New Credentials", .onclick = credentials_new);
                                     }
                                 }
                                 ui_newline(obj);
--- a/application/settings.h	Tue Oct 29 19:07:11 2024 +0100
+++ b/application/settings.h	Tue Oct 29 20:30:00 2024 +0100
@@ -136,6 +136,17 @@
     
     DavBool add_to_repo;
 } SettingsNewKeyDialog;
+
+typedef struct SettingsNewCredentialsDialog {
+    SettingsWindow *settings;
+    
+    UiString *id;
+    UiString *user;
+    UiString *password;
+    UiInteger *addlocation;
+    UiString *message;
+    char *url;
+} SettingsNewCredentialsDialog;
     
 void settings_window_open();
 

mercurial