]> uap-core.de Git - note.git/commitdiff
update NoteStore tree after note_store_notebook_swap_position_async main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 24 Jan 2026 17:35:31 +0000 (18:35 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 24 Jan 2026 17:35:31 +0000 (18:35 +0100)
application/nbconfig.c
application/store.c
application/store.h
application/tests/test-store.c

index 8d11b80fc372965960bfd82649f812c43685f9f0..792b28689b506c0f77717d29a02c5f461a33f4db 100644 (file)
@@ -119,7 +119,7 @@ void nbconfig_update_lists(NotebookConfigDialog *wdata) {
 
 void nbconfig_build_current_names_map(NotebookConfigDialog *wdata, Resource *res) {
     NoteStore *store = note_store_get();
-    Resource *parent = note_store_get_notebook_by_id(store, res->parent_id);
+    Resource *parent = note_store_get_notebook_by_resource_id(store, res->parent_id);
     if(parent) {
         cxMapClear(wdata->current_names);
         CxIterator i = cxListIterator(parent->children);
@@ -430,9 +430,7 @@ Resource* nbconfig_notebooklist_find_next(NotebookConfigDialog *wdata, Resource
 }
 
 static void swap_result(UiEvent *event, int error, void *userdata) {
-    if(error == 0) {
-        note_store_groups_updated();
-    } else {
+    if(!error == 0) {
         fprintf(stderr, "Error: cannot wrap notebook positions\n");
     }
 }
index ee274e5367b5b36dee1d0e150013bf7e01a22457..cadcc67647ea538064c9f10533a4d16dd3fb8909 100644 (file)
@@ -508,7 +508,7 @@ NoteStore* note_store_get() {
     return current_store;
 }
 
-Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id) {
+Resource* note_store_get_notebook_by_resource_id(NoteStore *store, int64_t resource_id) {
     if(!store || !store->root) {
         return NULL;
     }
@@ -528,6 +528,32 @@ Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id) {
     return NULL;
 }
 
+Resource* note_store_get_notebook_by_notebook_id(NoteStore *store, int64_t notebook_id) {
+    if(!store || !store->root) {
+        return NULL;
+    }
+    
+    CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS);
+    cxListAdd(stack, current_store->root);
+    
+    while(cxListSize(stack) > 0) {
+        Resource *parent = NULL;
+        cxListRemoveAndGet(stack, 0, &parent);
+        CxIterator i = cxListIterator(parent->children);
+        cx_foreach(Resource *, res, i) {
+            if(res->notebook && res->notebook->notebook_id == notebook_id) {
+                return res;
+            }
+            if(res->children) {
+                cxListAdd(stack, res);
+            }
+        }
+    }
+    cxListFree(stack);
+    
+    return NULL;
+}
+
 int64_t note_store_count_children(int64_t resource_id) {
     DBUQuery *q = connection->createQuery(connection, NULL);
     dbuQuerySetSQL(q, SQL_RESOURCE_COUNT_CHILDREN);
@@ -1116,8 +1142,8 @@ static void uithr_save_resource_finished(UiEvent *event, int error, void *userda
         if(job->prev_parent_id != 0 && job->prev_parent_id != job->res->parent_id) {
             // move resource to new parent
             NoteStore *store = note_store_get();
-            Resource *old_parent = note_store_get_notebook_by_id(store, job->prev_parent_id);
-            Resource *new_parent = note_store_get_notebook_by_id(store, job->res->parent_id);
+            Resource *old_parent = note_store_get_notebook_by_resource_id(store, job->prev_parent_id);
+            Resource *new_parent = note_store_get_notebook_by_resource_id(store, job->res->parent_id);
             if(old_parent && new_parent && old_parent->children) {
                 size_t i = cxListFind(old_parent->children, job->res);
                 cxListRemove(old_parent->children, i);
@@ -1139,7 +1165,7 @@ static void uithr_save_new_resource_finished(UiEvent *event, int64_t newid, int
     if(!error) {
         NoteStore *store = job->store;
         job->res->resource_id = newid;
-        Resource *parent = note_store_get_notebook_by_id(store, job->res->parent_id);
+        Resource *parent = note_store_get_notebook_by_resource_id(store, job->res->parent_id);
         if(parent) {
             if(!parent->children) {
                 parent->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);
@@ -1322,6 +1348,26 @@ static int qthr_store_notebook_swap_position(ExecJob *job) {
     return 0;
 }
 
+static void uithr_swap_positions_finished(UiEvent *event, ExecJob *job) {
+    Resource *res1 = note_store_get_notebook_by_notebook_id(current_store, job->id1);
+    Resource *res2 = note_store_get_notebook_by_notebook_id(current_store, job->id2);
+    // res1 && res2 should always exist and the parent should be the same
+    if(res1 && res2 && res1->parent_id == res2->parent_id) {
+        Resource *parent = note_store_get_notebook_by_resource_id(current_store, res1->parent_id);
+        if(parent) {
+            size_t index1 = cxListFind(parent->children, res1);
+            size_t index2 = cxListFind(parent->children, res2);
+            cxListSwap(parent->children, index1, index2);
+            note_store_groups_updated();
+        }
+    }
+    
+    if(job->resultcb) {
+        job->resultcb(event, job->error, job->userdata);
+    }
+    free(job);
+}
+
 void note_store_notebook_swap_position_async(
         UiObject *obj,
         Notebook *nb1,
@@ -1337,7 +1383,7 @@ void note_store_notebook_swap_position_async(
     job->flag = 0;
     job->id1 = nb1->notebook_id;
     job->id2 = nb2->notebook_id;
-    ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_execjob_finished, job);
+    ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_swap_positions_finished, job);
 }
 
 
index d7757483db6845eb5198ef2091a47726cfb11008..40dca82ffc88d08548d7f6ab88263ab348ede388 100644 (file)
@@ -117,7 +117,7 @@ int note_store_reload();
 
 NoteStore* note_store_get();
 
-Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id);
+Resource* note_store_get_notebook_by_resource_id(NoteStore *store, int64_t resource_id);
 
 int64_t note_store_count_children(int64_t resource_id);
 
index 44f0744cc85ca875772f779a7726aabf1215878a..6d687b8c9ac40fa022d33c2716759275d86ff7b0 100644 (file)
@@ -357,7 +357,7 @@ CX_TEST(test_note_store_save_notebook_async) {
         CX_TEST_ASSERT(store && store->root && store->root->children);
         CX_TEST_ASSERT(cxListSize(store->root->children) == numchildren + 1);
         
-        res0 = note_store_get_notebook_by_id(store, resource_id);
+        res0 = note_store_get_notebook_by_resource_id(store, resource_id);
         CX_TEST_ASSERT(res0);
         CX_TEST_ASSERT(res0->resource_id == resource_id);
         CX_TEST_ASSERT(!cx_strcmp(res0->nodename, "test_save_notebook_res0"));
@@ -378,7 +378,7 @@ CX_TEST(test_note_store_save_notebook_async) {
         CX_TEST_ASSERT(store && store->root && store->root->children);
         CX_TEST_ASSERT(cxListSize(store->root->children) == numchildren);
         
-        res0 = note_store_get_notebook_by_id(store, resource_id);
+        res0 = note_store_get_notebook_by_resource_id(store, resource_id);
         CX_TEST_ASSERT(res0);
         CX_TEST_ASSERT(!cx_strcmp(res0->nodename, "test_save_notebook_update0"));
         
@@ -460,8 +460,8 @@ CX_TEST(test_note_store_save_notebook_async_with_move) {
         store = note_store_get();
         CX_TEST_ASSERT(store && store->root && store->root->children);
         
-        Resource *test_res0 = note_store_get_notebook_by_id(store, group1_id);
-        Resource *test_res1 = note_store_get_notebook_by_id(store, group2_id);
+        Resource *test_res0 = note_store_get_notebook_by_resource_id(store, group1_id);
+        Resource *test_res1 = note_store_get_notebook_by_resource_id(store, group2_id);
         
         CX_TEST_ASSERT(test_res0 && test_res1);
         CX_TEST_ASSERT(!cx_strcmp(test_res0->nodename, "movetest_group1"));
@@ -497,7 +497,7 @@ CX_TEST(test_note_store_get_notebook_by_id) {
         
         CxIterator i = cxListIterator(store->root->children);
         cx_foreach(Resource *, res, i) {
-            Resource *xres = note_store_get_notebook_by_id(store, res->resource_id);
+            Resource *xres = note_store_get_notebook_by_resource_id(store, res->resource_id);
             CX_TEST_ASSERT(res == xres);
         }
     }
@@ -561,7 +561,7 @@ CX_TEST(test_note_store_delete_empty_collection_async) {
         store = note_store_get();
         CX_TEST_ASSERT(store);
         
-        Resource *res0_deleted = note_store_get_notebook_by_id(store, res0_id);
+        Resource *res0_deleted = note_store_get_notebook_by_resource_id(store, res0_id);
         CX_TEST_ASSERT(res0_deleted == NULL);
         
         // test delete-fail
@@ -576,7 +576,7 @@ CX_TEST(test_note_store_delete_empty_collection_async) {
         store = note_store_get();
         CX_TEST_ASSERT(store);
         
-        Resource *res1_not_deleted = note_store_get_notebook_by_id(store, res1_id);
+        Resource *res1_not_deleted = note_store_get_notebook_by_resource_id(store, res1_id);
         CX_TEST_ASSERT(res1_not_deleted != NULL);
         
         // cleanup
@@ -651,7 +651,7 @@ CX_TEST(test_note_store_delete_collection_async) {
         store = note_store_get();
         CX_TEST_ASSERT(store);
         
-        Resource *res1_deleted = note_store_get_notebook_by_id(store, res1_id);
+        Resource *res1_deleted = note_store_get_notebook_by_resource_id(store, res1_id);
         CX_TEST_ASSERT(res1_deleted == NULL);
         
         int64_t nchildren = note_store_count_children(res1_id);