size_t index = cxListFind(wdata->groups, parent);
ui_list_setselection(wdata->tab2_groups, index);
}
+
+ wdata->selected_notebook = res;
}
static int nbconfig_list_activate_event(UiEvent *event, UiList *list, void **listelm) {
return 0;
}
-static void nbconfig_notebooklist_activate(UiEvent *event, void *userdata) {
- NotebookConfigDialog *wdata = event->window;
- Resource *res;
- if(nbconfig_list_activate_event(event, wdata->tab2_notebooks, (void**)&res)) {
- return;
- }
- nbconfig_tab2_load_notebook(wdata, res);
-}
static void group_created(UiEvent *event, int64_t newid, int error, void *userdata) {
Resource *res = userdata;
NoteStore *store = note_store_get();
if(store->root) {
if(!store->root->children) {
- store->root->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);;
+ store->root->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);
}
cxListAdd(store->root->children, res);
note_store_groups_updated();
}
if(res->resource_id == 0) {
- note_store_new_resource_async(nbconfig->obj, res, group_created, res);
+ note_store_new_notebook_async(application_global_obj(), res, group_created, res);
} else {
- note_store_save_resource_async(nbconfig->obj, res, group_saved, res);
+ note_store_save_resource_async(application_global_obj(), res, group_saved, res);
note_store_groups_updated();
}
}
-static void nbconfig_grouplist_activate(UiEvent *event, void *userdata) {
+static void nbconfig_grouplist_onselect(UiEvent *event, void *userdata) {
NotebookConfigDialog *wdata = event->window;
+ if(wdata->valuechange) {
+ return;
+ }
Resource *res;
if(nbconfig_list_activate_event(event, wdata->tab1_groups, (void**)&res)) {
return;
NotebookConfigDialog *wdata = event->window;
NoteStore *store = note_store_get();
+ wdata->valuechange = TRUE;
+
if(wdata->selected_group) {
cxFree(store->mp->allocator, wdata->selected_group->nodename);
wdata->selected_group->nodename = cx_strdup_a(store->mp->allocator, cx_str(ui_get(wdata->tab1_group_name))).ptr;
}
ui_listselection_free(sel);
}
+
+ wdata->valuechange = FALSE;
+}
+
+static void notebook_created(UiEvent *event, int64_t newid, int error, void *userdata) {
+ NoteStore *store = note_store_get();
+ Resource *res = userdata;
+ Resource *parent = note_store_get_notebook_by_id(res->parent_id);
+ if(parent) {
+ if(!parent->children) {
+ parent->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);
+ }
+ cxListAdd(parent->children, res);
+ note_store_groups_updated();
+ }
+}
+
+static void notebook_saved(UiEvent *event, int error, void *userdata) {
+ if(error) {
+ fprintf(stderr, "Error: note_store_save_resource failed\n");
+ }
+}
+
+static void nbconfig_notebook_save(NotebookConfigDialog *nbconfig) {
+ Resource *res = nbconfig->selected_notebook;
+ if(!res) {
+ return;
+ }
+
+ UiListSelection sel = ui_list_getselection(nbconfig->tab2_groups);
+ if(sel.count == 0) {
+ return; // shouldn't happen, maybe remove this when ui_list_getselectedindex exists
+ }
+ Resource *parent = ui_list_get(nbconfig->tab2_groups, sel.rows[0]);
+ if(!parent) {
+ return; //shouldn't happen
+ }
+ ui_listselection_free(sel);
+ res->parent_id = parent->resource_id;
+
+ if(res->resource_id == 0) {
+ note_store_new_notebook_async(application_global_obj(), res, notebook_created, res);
+ } else {
+ note_store_save_resource_async(application_global_obj(), res, notebook_saved, res);
+ note_store_groups_updated();
+ }
+}
+
+static void nbconfig_notebooklist_onselect(UiEvent *event, void *userdata) {
+ NotebookConfigDialog *wdata = event->window;
+ if(wdata->valuechange) {
+ return;
+ }
+
+ nbconfig_notebook_save(wdata);
+
+ Resource *res;
+ if(nbconfig_list_activate_event(event, wdata->tab2_notebooks, (void**)&res)) {
+ return;
+ }
+ nbconfig_tab2_load_notebook(wdata, res);
+
+ wdata->selected_group = res;
}
static void nbconfig_notebooklist_add(UiEvent *event, void *userdata) {
Resource *notebook = cxCalloc(store->mp->allocator, 1, sizeof(Resource));
notebook->notebook = cxCalloc(store->mp->allocator, 1, sizeof(Notebook));
- if(wdata->new_notebook) {
+ if(wdata->selected_notebook) {
// TODO
}
- wdata->new_notebook = notebook;
+ wdata->selected_notebook = notebook;
ui_set(wdata->tab2_notebook_name, "");
ui_list_setselection(wdata->tab2_repositories, 0);
NotebookConfigDialog *wdata = event->window;
NoteStore *store = note_store_get();
- if(wdata->new_notebook) {
+ wdata->valuechange = TRUE;
+
+ if(wdata->selected_notebook) {
UiBool add_notebook = FALSE;
- if(!wdata->new_notebook->nodename) {
+ if(!wdata->selected_notebook->nodename) {
add_notebook = TRUE;
} else {
- cxFree(store->mp->allocator, wdata->new_notebook->nodename);
+ cxFree(store->mp->allocator, wdata->selected_notebook->nodename);
}
- wdata->new_notebook->nodename = cx_strdup_a(store->mp->allocator, cx_str(ui_get(wdata->tab2_notebook_name))).ptr;
+ wdata->selected_notebook->nodename = cx_strdup_a(store->mp->allocator, cx_str(ui_get(wdata->tab2_notebook_name))).ptr;
if(add_notebook) {
- cxListAdd(wdata->notebooks, wdata->new_notebook);
+ cxListAdd(wdata->notebooks, wdata->selected_notebook);
}
nbconfig_update_lists(wdata); // TODO: update only single row
}
+
+ wdata->valuechange = FALSE;
}
static void nbconfig_notebooklist_delete(UiEvent *event, void *userdata) {
ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_FOLDER, nbconfig_repolist_dir_selected, wdata);
}
+static void nbconfig_close(UiEvent *event, void *userdata) {
+ NotebookConfigDialog *wdata = event->window;
+ nbconfig_group_save(wdata);
+ nbconfig_notebook_save(wdata);
+}
+
void notebook_config_dialog(void) {
NoteStore *store = note_store_get();
// TODO: check store->root and show different dialog, when root is missing
UiObject *obj = ui_simple_window("Notebooks");
const CxAllocator *a = ui_allocator(obj->ctx);
+ ui_context_closefunc(obj->ctx, nbconfig_close, NULL);
NotebookConfigDialog *wdata = ui_malloc(obj->ctx, sizeof(NotebookConfigDialog));
memset(wdata, 0, sizeof(NotebookConfigDialog));
ui_tab(obj, "Groups") {
ui_hbox(obj, .margin = 10, .spacing = 10, .fill = TRUE) {
ui_vbox(obj, .fill = FALSE) {
- ui_listview(obj, .list = wdata->tab1_groups, .getvalue = reslist_getvalue, .fill = TRUE, .onactivate = nbconfig_grouplist_activate);
+ ui_listview(obj, .list = wdata->tab1_groups, .getvalue = reslist_getvalue, .fill = TRUE, .onselection = nbconfig_grouplist_onselect);
ui_hbox(obj, .fill = FALSE) {
ui_button(obj, .icon = UI_ICON_NEW_FOLDER, .onclick = nbconfig_grouplist_add);
ui_button(obj, .icon = UI_ICON_DELETE, .onclick = nbconfig_grouplist_delete);
ui_tab(obj, "Notebooks") {
ui_hbox(obj, .margin = 10, .spacing = 10, .fill = TRUE) {
ui_vbox(obj) {
- ui_listview(obj, .list = wdata->tab2_notebooks, .getvalue = reslist_getvalue, .fill = TRUE, .onactivate = nbconfig_notebooklist_activate);
+ ui_listview(obj, .list = wdata->tab2_notebooks, .getvalue = reslist_getvalue, .fill = TRUE, .onselection = nbconfig_notebooklist_onselect);
ui_hbox(obj) {
ui_button(obj, .icon = UI_ICON_NEW_FOLDER, .onclick = nbconfig_notebooklist_add);
ui_button(obj, .icon = UI_ICON_DELETE, .onclick = nbconfig_notebooklist_delete);