]> uap-core.de Git - note.git/commitdiff
implement button for adding new repositories
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 26 Jan 2026 17:11:12 +0000 (18:11 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 26 Jan 2026 17:11:12 +0000 (18:11 +0100)
application/nbconfig.c
application/store.c

index a7219839ba0827e6fb2fc2eb57607b35826693ef..5f04251cf3511b731a588601fb7d33f9ebb15e11 100644 (file)
@@ -87,6 +87,13 @@ void nbconfig_update_notebooklist(NotebookConfigDialog *wdata, Resource *res) {
     ui_list_update_row(wdata->tab1_notebooks, index);
 }
 
+void nbconfig_update_repositorylist(NotebookConfigDialog *wdata, Repository *repo) {
+    CxList *rplist = wdata->tab3_repositories->data;
+    size_t index = cxListFind(rplist, repo);
+    ui_list_update_row(wdata->tab3_repositories, index);
+    ui_list_update(wdata->tab1_repositories);
+}
+
 void nbconfig_update_lists(NotebookConfigDialog *wdata) {
     ui_list_clear(wdata->tab1_groups);
     ui_list_clear(wdata->tab1_repositories);
@@ -135,6 +142,7 @@ void nbconfig_build_current_names_map(NotebookConfigDialog *wdata, Resource *res
 static void nbconfig_close(UiEvent *event, void *userdata) {
     NotebookConfigDialog *wdata = event->window;
     nbconfig_notebook_save(wdata);
+    nbconfig_repository_save(wdata);
 }
 
 static int nbconfig_list_activate_event(UiEvent *event, UiList *list, void **listelm) {
@@ -496,8 +504,40 @@ static void nbconfig_notebooklist_name_changed(UiEvent *event, void *userdata) {
 
 // ---------------------------- Repo Config -----------------------------------
 
+static void repository_saved(UiEvent *event, int error, void *userdata) {
+    if(error != 0) {
+        fprintf(stderr, "Error: note_store_save_repository_async failed\n");
+    }
+}
+
 void nbconfig_repository_save(NotebookConfigDialog *nbconfig) {
+    if(!nbconfig->selected_repository) {
+        return;
+    }
+    
+    Repository *repo = nbconfig->selected_repository;
+    
+    NoteStore *store = note_store_get();
+    const CxAllocator *a = store->mp->allocator;
+    
+    char *prev_name = repo->name;
+    char *prev_local_path = repo->local_path;
+    char *prev_url = repo->url;
+    char *prev_default_key = repo->default_key;
+    
+    // TODO: check changes and prepare migration
+    
+    repo->name = cx_strdup_a(a, ui_get(nbconfig->tab3_repo_name)).ptr;
+    repo->local_path = cx_strdup_a(a, ui_get(nbconfig->tab3_repo_local_path)).ptr;
+    repo->url = cx_strdup_a(a, ui_get(nbconfig->tab3_repo_url)).ptr;
+    repo->default_key = NULL;
     
+    cxFree(a, prev_name);
+    cxFree(a, prev_local_path);
+    cxFree(a, prev_url);
+    cxFree(a, prev_default_key);
+    
+    note_store_save_repository_async(application_global_obj(), repo, repository_saved, nbconfig);
 }
 
 static void nbconfig_load_repository(NotebookConfigDialog *wdata, Repository *repo) {
@@ -519,7 +559,26 @@ static void nbconfig_load_repository(NotebookConfigDialog *wdata, Repository *re
 }
 
 static void nbconfig_repolist_add(UiEvent *event, void *userdata) {
+    NotebookConfigDialog *wdata = event->window;
+    NoteStore *store = note_store_get();
+    UiContext *ctx = wdata->obj->ctx;
+    
+    nbconfig_repository_save(wdata);
+    
+    Repository *repo = cxZalloc(store->mp->allocator, sizeof(Repository));
+    wdata->selected_repository = repo;
     
+    cxListAdd(wdata->repositories, repo);
+    nbconfig_update_lists(wdata);
+    
+    size_t nrepos = cxListSize(wdata->repositories);
+    ui_list_setselection2(wdata->tab3_repositories, nrepos-1, FALSE);
+    
+    nbconfig_load_repository(wdata, repo);
+    
+    ui_free(ctx, wdata->selected_repository_name);
+    wdata->selected_repository_name = ui_strdup(ctx, "New");
+    ui_set(wdata->tab3_repo_name, wdata->selected_repository_name); // override name textfield
 }
 
 static void nbconfig_repolist_delete(UiEvent *event, void *userdata) {
@@ -531,7 +590,7 @@ static void nbconfig_repolist_name_changed(UiEvent *event, void *userdata) {
 }
 
 static void nbconfig_repolist_onselect(UiEvent *event, void *userdata) {
-    NotebookConfigDialog *wdata = userdata;
+    NotebookConfigDialog *wdata = event->window;
     if(wdata->valuechange) {
         return;
     }
index cadcc67647ea538064c9f10533a4d16dd3fb8909..f1cea5e87212f471d03aee3133df363fcfeed00c 100644 (file)
 
 #define SQL_REPOSITORY_NEW \
     "insert into repositories(name, url, encryption, default_key, authmethod, local_path) " \
-    "values (?, ?, ?, ?, ?, ?) repository_id;"
+    "values (?, ?, ?, ?, ?, ?) returning repository_id;"
 
 #define SQL_REPOSITORY_SAVE \
     "update repositories set " \
@@ -1420,7 +1420,7 @@ static int qthr_store_save_repository(SaveRepositoryJob *job) {
     } else {
         dbuQuerySetParamNull(q, 2);
     }
-    dbuQuerySetParamInt(q, 4, repo->encryption);
+    dbuQuerySetParamInt(q, 3, repo->encryption);
     if(repo->url) {
         dbuQuerySetParamString(q, 4, cx_str(repo->default_key));
     } else {