]> uap-core.de Git - note.git/commitdiff
add NoteStoreListener, implement notebook tree update after groups are added
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 11 Jan 2026 12:39:05 +0000 (13:39 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 11 Jan 2026 12:39:05 +0000 (13:39 +0100)
application/nbconfig.c
application/store.c
application/store.h
application/window.c
application/window.h

index 596ca5bf1e97f34247236524f8f638e5436b78f3..3f7ff55d6d32a5419c695025974e6a63469e30f5 100644 (file)
@@ -133,11 +133,14 @@ static void group_created(UiEvent *event, int64_t newid, int error, void *userda
             store->root->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);;
         }
         cxListAdd(store->root->children, res);
+        note_store_groups_updated();
     }
 }
 
 static void group_saved(UiEvent *event, int error, void *userdata) {
-    
+    if(error) {
+        fprintf(stderr, "Error: note_store_save_resource failed\n");
+    }
 }
 
 static void nbconfig_group_save(NotebookConfigDialog *nbconfig) {
@@ -150,6 +153,7 @@ static void nbconfig_group_save(NotebookConfigDialog *nbconfig) {
         note_store_new_resource_async(nbconfig->obj, res, group_created, res);
     } else {
         note_store_save_resource_async(nbconfig->obj, res, group_saved, res);
+        note_store_groups_updated();
     }
 }
 
index 45a7f2575ed3aea458c9bf8ffcff6391959a8f3d..a521e16c1ea408a0e6db568c3257ba808ce2fcb7 100644 (file)
@@ -367,6 +367,34 @@ int note_store_create_default(const char *host, const char *user) {
     return err;
 }
 
+void note_store_add_listener(NoteStoreListener *listener) {
+    if(current_store) {
+        cxListAdd(current_store->listener, listener);
+    }
+}
+
+void note_store_remove_listener(void *userdata) {
+    if(current_store) {
+        CxIterator i = cxListIterator(current_store->listener);
+        cx_foreach(NoteStoreListener *, ls, i) {
+            if(ls->userdata = userdata) {
+                cxIteratorFlagRemoval(i);
+            }
+        }
+    }
+    
+}
+
+void note_store_groups_updated() {
+    if(current_store) {
+        CxIterator i = cxListIterator(current_store->listener);
+        cx_foreach(NoteStoreListener *, ls, i) {
+            if(ls->groups_updated) {
+                ls->groups_updated(current_store, ls->userdata);
+            }
+        }
+    }
+}
 
 /*
  * Reloads the NoteStore structure. The previous NoteStore* pointer will be
@@ -439,6 +467,9 @@ int note_store_reload() {
     
     if(current_store) {
         cxMempoolFree(current_store->mp);
+        store->listener = current_store->listener;
+    } else {
+        store->listener = cxLinkedListCreate(NULL, sizeof(NoteStoreListener));
     }
     
     current_store = store;
index d60dacfd6d2f9df0aedfa4ca23f07398d89bdd5c..8d62f2a4f8c149946e67aa9454184efd4e4528da 100644 (file)
@@ -46,7 +46,31 @@ typedef struct NoteStore {
     Resource *root;
     Resource *trash;
     CxList   *repositories;
+    
+    /*
+     * Type: NoteStoreListener
+     * 
+     * Listeners are called, when a change is made to the notebook tree
+     * or other NoteStore changes are made.
+     * 
+     * The List of listeners will be moved to the next NoteStore object,
+     * when the store is reloaded.
+     */
+    CxList   *listener;
 } NoteStore;
+
+typedef void (*groups_updated_func)(NoteStore *store, void *userdata);
+typedef struct NoteStoreListener {
+    /*
+     * called when root->children is updated
+     */
+    groups_updated_func groups_updated;
+    
+    /*
+     * userdata passed to callbacks
+     */
+    void *userdata;
+} NoteStoreListener;
     
 typedef struct AsyncListResult {
     CxMempool *mp;
@@ -84,6 +108,11 @@ void note_store_set_settings(
 
 int note_store_create_default(const char *host, const char *user);
 
+void note_store_add_listener(NoteStoreListener *listener);
+void note_store_remove_listener(void *userdata);
+
+void note_store_groups_updated();
+
 int note_store_reload();
 
 NoteStore* note_store_get();
index 4103013a5d99880ee356aebe62ece4d7eb87ca0a..53cc0f3ca7ee512eb85881b905de372b07da1641 100644 (file)
 #include <cx/array_list.h>
 #include <cx/hash_map.h>
 
+static void window_closed(UiEvent *event, MainWindow *wdata) {
+    note_store_remove_listener(wdata);
+}
 
 void window_create() {
     UiObject *obj = ui_splitview_window("note", TRUE);
     MainWindow *wdata = window_init_data(obj);
+    ui_context_closefunc(obj->ctx, (ui_callback)window_closed, wdata);
     
     /*
     UiSubList sublists[] = { 
@@ -136,6 +140,11 @@ MainWindow* window_init_data(UiObject *obj) {
     
     wdata->notebook_cache = cxHashMapCreate(NULL, CX_STORE_POINTERS, 32);
     
+    NoteStoreListener listener = { 0 };
+    listener.groups_updated = (groups_updated_func)window_store_groups_updated;
+    listener.userdata = wdata;
+    note_store_add_listener(&listener);
+    
     return wdata;
 }
 
@@ -268,6 +277,10 @@ void window_navigate(MainWindow *window, NavStack *nav) {
 }
 
 
+void window_store_groups_updated(NoteStore *store, MainWindow *window) {
+    update_sublists(window->obj->ctx, window->notebooks);
+}
+
 
 typedef struct NotebookCreatedResult {
     MainWindow *window;
@@ -278,7 +291,7 @@ typedef struct NotebookCreatedResult {
 static void notebook_created(UiEvent *event, int64_t newid, int error, void *userdata) {
     NotebookCreatedResult *result = userdata;
     cxListAdd(result->parent->children, result->notebook);
-    update_sublists(result->window->obj->ctx, result->window->notebooks);
+    note_store_groups_updated();
     free(result);
 }
 
index 998d3b1cd10cd8f9363706c6f50730745bae0ef5..c353186800752c9c44c28b2119c73244e650bf41 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <ui/ui.h>
 #include "application.h"
+#include "store.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -65,6 +66,10 @@ void update_sublists(UiContext *ctx, UiList *sublists);
 
 void* window_notelist_getvalue(void *data, int col);
 
+// note store listener funcs
+void window_store_groups_updated(NoteStore *store, MainWindow *window);
+
+
 void action_notebook_add(UiEvent *event, void *userdata);
 void action_notebook_config(UiEvent *event, void *userdata);