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) {
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();
}
}
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
if(current_store) {
cxMempoolFree(current_store->mp);
+ store->listener = current_store->listener;
+ } else {
+ store->listener = cxLinkedListCreate(NULL, sizeof(NoteStoreListener));
}
current_store = store;
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;
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();
#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[] = {
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;
}
}
+void window_store_groups_updated(NoteStore *store, MainWindow *window) {
+ update_sublists(window->obj->ctx, window->notebooks);
+}
+
typedef struct NotebookCreatedResult {
MainWindow *window;
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);
}
#include <ui/ui.h>
#include "application.h"
+#include "store.h"
#ifdef __cplusplus
extern "C" {
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);