static int notebooklist_move_pos0 = 0;
static int notebooklist_move_pos1 = 0;
-static void* reslist_getvalue(void *data, int col) {
- Resource *resource = data;
+static void* reslist_getvalue(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) {
+ NotebookConfigDialog *wdata = userdata;
+ Resource *resource = elm;
switch(col) {
case 0: {
+ if(resource == wdata->selected_group && wdata->selected_group_name) {
+ return wdata->selected_group_name;
+ } else if(resource == wdata->selected_notebook && wdata->selected_notebook_name) {
+ return wdata->selected_notebook_name;
+ }
return resource->displayname ? resource->displayname : resource->nodename;
}
case 1: {
return repo->name;
}
+void nbconfig_update_list(NotebookConfigDialog *wdata, CxList *list, int index) {
+ if(list == wdata->groups) {
+ ui_list_update_row(wdata->tab1_groups, index);
+ ui_list_update_row(wdata->tab2_groups, index);
+ } else if(list == wdata->notebooks) {
+ ui_list_update_row(wdata->tab2_notebooks, index);
+ }
+}
+
void nbconfig_update_lists(NotebookConfigDialog *wdata) {
ui_list_clear(wdata->tab1_groups);
ui_list_clear(wdata->tab2_groups);
static void nbconfig_tab2_load_notebook(NotebookConfigDialog *wdata, Resource *res) {
Notebook *nb = res->notebook;
- CxHashKey key = cx_hash_key(&res->resource_id, sizeof(res->resource_id));
- Resource *parent = cxMapGet(wdata->notebook_parents, key);
ui_set(wdata->tab2_notebook_name, res->nodename);
- if(parent) {
- size_t index = cxListFind(wdata->groups, parent);
- ui_list_setselection(wdata->tab2_groups, index);
+ CxIterator i = cxListIterator(wdata->groups);
+ cx_foreach(Resource *, parent, i) {
+ if(parent->resource_id == res->parent_id) {
+ ui_list_setselection(wdata->tab2_groups, (int)i.index);
+ break;
+ }
}
wdata->selected_notebook = 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);
- }
- 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");
+ fprintf(stderr, "Error: note_store_save_notebook_async failed\n");
}
}
return;
}
- Resource *dup = cxMapGet(nbconfig->current_names, res->nodename);
+ Resource *dup = cxMapGet(nbconfig->current_names, nbconfig->selected_group_name);
if(dup) {
- fprintf(stderr, "Name %s already in use\n", res->nodename);
+ fprintf(stderr, "Name %s already in use\n", nbconfig->selected_group_name);
// TODO: show error in UI
- // TODO: restore old name
return;
}
- if(res->resource_id == 0) {
- note_store_new_notebook_async(application_global_obj(), res, group_created, res);
- } else {
- note_store_update_resource_async(application_global_obj(), res, group_saved, res);
- note_store_groups_updated();
- }
+ NoteStore *store = note_store_get();
+ cxFree(store->mp->allocator, nbconfig->selected_group->nodename);
+ nbconfig->selected_group->nodename = cx_strdup_a(store->mp->allocator, nbconfig->selected_group_name).ptr;
+
+ note_store_save_notebook_async(application_global_obj(), res, 0, group_saved, res);
}
static void nbconfig_grouplist_onselect(UiEvent *event, void *userdata) {
return;
}
NotebookConfigDialog *wdata = event->window;
- NoteStore *store = note_store_get();
+ UiContext *ctx = wdata->obj->ctx;
wdata->valuechange = TRUE;
if(wdata->selected_group) {
char *input = ui_get(wdata->tab1_group_name);
- cxFree(store->mp->allocator, wdata->selected_group->nodename);
- wdata->selected_group->nodename = cx_strdup_a(store->mp->allocator, cx_str(input)).ptr;
+ ui_free(ctx, wdata->selected_group_name);
+ wdata->selected_group_name = ui_strdup(ctx, input);
- UiListSelection sel = ui_list_getselection(wdata->tab1_groups);
- nbconfig_update_lists(wdata); // TODO: update only single row
- if(sel.count > 0) {
- ui_list_setselection(wdata->tab1_groups, sel.rows[0]);
- }
- ui_listselection_free(sel);
+ nbconfig_update_list(wdata, wdata->groups, cxListFind(wdata->groups, wdata->selected_group));
Resource *dup = cxMapGet(wdata->current_names, input);
if(dup) {
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(store, 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");
+ fprintf(stderr, "Error: note_store_save_notebook_async failed\n");
}
}
return;
}
+ // check if the notebook name is unique
+ Resource *dup = cxMapGet(nbconfig->current_names, nbconfig->selected_notebook_name);
+ if(dup) {
+ fprintf(stderr, "Name %s already in use\n", nbconfig->selected_notebook_name);
+ // TODO: show error in UI
+ return;
+ }
+
+ // update name
+ NoteStore *store = note_store_get();
+ cxFree(store->mp->allocator, nbconfig->selected_notebook->nodename);
+ nbconfig->selected_notebook->nodename = cx_strdup_a(store->mp->allocator, nbconfig->selected_notebook_name).ptr;
+
+ // update parent
UiListSelection sel = ui_list_getselection(nbconfig->tab2_groups);
if(sel.count == 0) {
return; // shouldn't happen, maybe remove this when ui_list_getselectedindex exists
return; //shouldn't happen
}
ui_listselection_free(sel);
+ int64_t prev_parent_id = res->parent_id;
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_update_resource_async(application_global_obj(), res, notebook_saved, res);
- note_store_groups_updated();
- }
+ note_store_save_notebook_async(application_global_obj(), res, prev_parent_id, notebook_saved, res);
}
static void nbconfig_notebooklist_onselect(UiEvent *event, void *userdata) {
if(nbconfig_list_activate_event(event, wdata->tab2_notebooks, (void**)&res)) {
return;
}
- nbconfig_tab2_load_notebook(wdata, res);
-
- wdata->selected_group = res;
+ wdata->selected_notebook = res;
+ nbconfig_tab2_load_notebook(wdata, res);
nbconfig_build_current_names_map(wdata, res);
}
static void nbconfig_notebooklist_add(UiEvent *event, void *userdata) {
NotebookConfigDialog *wdata = event->window;
NoteStore *store = note_store_get();
+ UiContext *ctx = wdata->obj->ctx;
+
+ Resource *parent = cxListAt(wdata->groups, 0);
Resource *notebook = cxCalloc(store->mp->allocator, 1, sizeof(Resource));
notebook->notebook = cxCalloc(store->mp->allocator, 1, sizeof(Notebook));
-
- if(wdata->selected_notebook) {
- // TODO
+ if(parent) {
+ notebook->parent_id = parent->resource_id;
}
+ cxListAdd(wdata->notebooks, notebook);
wdata->selected_notebook = notebook;
+ ui_free(ctx, wdata->selected_notebook_name);
+ wdata->selected_notebook_name = ui_strdup(ctx, "New");
+ nbconfig_update_lists(wdata);
- ui_set(wdata->tab2_notebook_name, "");
+ ui_set(wdata->tab2_notebook_name, "New");
ui_list_setselection(wdata->tab2_repositories, 0);
ui_list_setselection(wdata->tab2_types, 0);
}
}
NotebookConfigDialog *wdata = event->window;
- NoteStore *store = note_store_get();
+ UiContext *ctx = wdata->obj->ctx;
wdata->valuechange = TRUE;
if(wdata->selected_notebook) {
- UiBool add_notebook = FALSE;
- if(!wdata->selected_notebook->nodename) {
- add_notebook = TRUE;
- } else {
- cxFree(store->mp->allocator, wdata->selected_notebook->nodename);
- }
+ char *input = ui_get(wdata->tab2_notebook_name);
+ ui_free(ctx, wdata->selected_notebook_name);
+ wdata->selected_notebook_name = ui_strdup(ctx, input);
- wdata->selected_notebook->nodename = cx_strdup_a(store->mp->allocator, cx_str(ui_get(wdata->tab2_notebook_name))).ptr;
+ nbconfig_update_list(wdata, wdata->notebooks, cxListFind(wdata->notebooks, wdata->selected_notebook));
- if(add_notebook) {
- cxListAdd(wdata->notebooks, wdata->selected_notebook);
+ Resource *dup = cxMapGet(wdata->current_names, input);
+ if(dup) {
+ fprintf(stderr, "Name %s already in use\n", input);
+ // TODO: show error in UI
}
- nbconfig_update_lists(wdata); // TODO: update only single row
}
wdata->valuechange = FALSE;
wdata->repositories = cxArrayListCreate(a, CX_STORE_POINTERS, 32);
wdata->groups = cxArrayListCreate(a, CX_STORE_POINTERS, 32);
wdata->notebooks = cxArrayListCreate(a, CX_STORE_POINTERS, 32);
- wdata->notebook_parents = cxHashMapCreate(a, CX_STORE_POINTERS, 32);
wdata->current_names = cxHashMapCreate(a, CX_STORE_POINTERS, 16);
CxIterator k = cxListIterator(res->children);
cx_foreach(Resource *, nb, k) {
cxListAdd(wdata->notebooks, nb);
- CxHashKey key = cx_hash_key(&nb->resource_id, sizeof(nb->resource_id));
- cxMapPut(wdata->notebook_parents, key, res);
- if(!notebook1) {
- notebook1 = nb;
- }
}
}
nbconfig_update_lists(wdata);
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, .onselection = nbconfig_grouplist_onselect);
+ ui_listview(obj, .list = wdata->tab1_groups, .getvalue2 = reslist_getvalue, .getvalue2data = wdata, .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, .onselection = nbconfig_notebooklist_onselect);
+ ui_listview(obj, .list = wdata->tab2_notebooks, .getvalue2 = reslist_getvalue, .getvalue2data = wdata, .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);
ui_grid(obj, .columnspacing = 10, .rowspacing = 10, .fill = TRUE, .def_vfill = TRUE) {
ui_rlabel(obj, .label = "Group");
- ui_dropdown(obj, .list = wdata->tab2_groups, .getvalue = reslist_getvalue);
+ ui_dropdown(obj, .list = wdata->tab2_groups, .getvalue2 = reslist_getvalue, .getvalue2data = wdata);
ui_newline(obj);
ui_rlabel(obj, .label = "Name");