implement repo add/remove

Sun, 27 Oct 2024 20:34:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Oct 2024 20:34:38 +0100
changeset 62
dbde5e038ea9
parent 61
eb63af2f2bdd
child 63
d0eeb2b98dae

implement repo add/remove

application/settings.c file | annotate | diff | comparison | revisions
application/settings.h file | annotate | diff | comparison | revisions
libidav/config.c file | annotate | diff | comparison | revisions
--- a/application/settings.c	Sun Oct 27 19:19:51 2024 +0100
+++ b/application/settings.c	Sun Oct 27 20:34:38 2024 +0100
@@ -61,8 +61,25 @@
     }
 }
 
+static void repolist_add(UiEvent *event, void *userdata) {
+    SettingsWindow *settings = event->window;
+    settings->repo_new = TRUE;
+    settings->selected_repo = -1;
+    // switch to editing tab
+    ui_set(settings->repo_tabview, 1);
+}
+
 static void repolist_remove(UiEvent *event, void *userdata) {
-    // TODO
+    SettingsWindow *settings = event->window;
+    DavCfgRepository *repo = ui_list_get(settings->repos, settings->selected_repo);
+    if(!repo) {
+        fprintf(stderr, "Error: cannot get repository at index %d\n", settings->selected_repo);
+        return;
+    }
+    dav_repository_remove_and_free(settings->config, repo);
+    settings_update_repolist(settings);
+    settings->selected_repo = -1;
+    ui_unset_group(event->obj->ctx, SETTINGS_STATE_REPOLIST_SELECTED);
 }
 
 static void editrepo_go_back(UiEvent *event, void *userdata) {
@@ -128,9 +145,9 @@
                 ui_tab(obj, "list") {
                     ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
                         ui_hbox(obj, .spacing = 10) {
-                            ui_button(obj, .label = "Add");
+                            ui_button(obj, .label = "Add", .onclick = repolist_add);
                             ui_button(obj, .label = "Edit", .onclick = repolist_edit, .groups = UI_GROUPS(SETTINGS_STATE_REPOLIST_SELECTED));
-                            ui_button(obj, .label = "Remove", .groups = UI_GROUPS(SETTINGS_STATE_REPOLIST_SELECTED));
+                            ui_button(obj, .label = "Remove", .onclick = repolist_remove, .groups = UI_GROUPS(SETTINGS_STATE_REPOLIST_SELECTED));
                         }
                         ui_newline(obj);
                         
@@ -384,10 +401,21 @@
 
 void settings_store_repository(SettingsWindow *settings) {
     DavConfig *config = settings->config;
-    DavCfgRepository *repo = ui_list_get(settings->repos, settings->selected_repo);
-    if(!repo) {
-        fprintf(stderr, "Error: cannot get repository at index %d\n", settings->selected_repo);
-        return;
+    DavCfgRepository *repo;
+    if(settings->repo_new) {
+        settings->repo_new = FALSE;
+        char *name = ui_get(settings->repo_name);
+        if(strlen(name) == 0) {
+            return;
+        }
+        repo = dav_repository_new(config);
+        dav_config_add_repository(config, repo);
+    } else {
+        repo = ui_list_get(settings->repos, settings->selected_repo);
+        if(!repo) {
+            fprintf(stderr, "Error: cannot get repository at index %d\n", settings->selected_repo);
+            return;
+        }
     }
     
     // always store name/url nodes
@@ -445,6 +473,7 @@
         dav_cfg_int_remove(&repo->ssl_version);
     }
     
+    settings_update_repolist(settings);
     settings->selected_repo = -1;
 }
 
@@ -456,6 +485,10 @@
     for (DavCfgRepository *repo = config->repositories; repo; repo = repo->next) {
         ui_list_append(settings->repos, repo);
     }
+    
+    if(settings->repos->update) {
+        ui_list_update(settings->repos);
+    }
 }
 
 void* settings_repolist_getvalue(DavCfgRepository *repo, int col) {
--- a/application/settings.h	Sun Oct 27 19:19:51 2024 +0100
+++ b/application/settings.h	Sun Oct 27 20:34:38 2024 +0100
@@ -44,6 +44,7 @@
     UiList *repos;
     UiInteger *repo_tabview;
     
+    DavBool repo_new;
     UiString *repo_name;
     UiString *repo_url;
     UiList *repo_credentials;
--- a/libidav/config.c	Sun Oct 27 19:19:51 2024 +0100
+++ b/libidav/config.c	Sun Oct 27 20:34:38 2024 +0100
@@ -458,7 +458,7 @@
     xmlNode *repoNode = xmlNewNode(NULL, BAD_CAST "repository");
     xmlNode *rtext1 = xmlNewDocText(config->doc, BAD_CAST "\n");
     xmlAddChild(repoNode, rtext1);
-    
+    repo->node = repoNode;
     if(repo->name.value.ptr) {
         repo->name.node = addXmlNode(repoNode, "name", repo->name.value);
     }

mercurial