From a80c8a8bcf84570de680149f2769edfd12c2b88b Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 11 Jan 2026 13:39:05 +0100 Subject: [PATCH] add NoteStoreListener, implement notebook tree update after groups are added --- application/nbconfig.c | 6 +++++- application/store.c | 31 +++++++++++++++++++++++++++++++ application/store.h | 29 +++++++++++++++++++++++++++++ application/window.c | 15 ++++++++++++++- application/window.h | 5 +++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/application/nbconfig.c b/application/nbconfig.c index 596ca5b..3f7ff55 100644 --- a/application/nbconfig.c +++ b/application/nbconfig.c @@ -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(); } } diff --git a/application/store.c b/application/store.c index 45a7f25..a521e16 100644 --- a/application/store.c +++ b/application/store.c @@ -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; diff --git a/application/store.h b/application/store.h index d60dacf..8d62f2a 100644 --- a/application/store.h +++ b/application/store.h @@ -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(); diff --git a/application/window.c b/application/window.c index 4103013..53cc0f3 100644 --- a/application/window.c +++ b/application/window.c @@ -39,10 +39,14 @@ #include #include +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); } diff --git a/application/window.h b/application/window.h index 998d3b1..c353186 100644 --- a/application/window.h +++ b/application/window.h @@ -31,6 +31,7 @@ #include #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); -- 2.47.3