return current_store;
}
-Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id) {
+Resource* note_store_get_notebook_by_resource_id(NoteStore *store, int64_t resource_id) {
if(!store || !store->root) {
return NULL;
}
return NULL;
}
+Resource* note_store_get_notebook_by_notebook_id(NoteStore *store, int64_t notebook_id) {
+ if(!store || !store->root) {
+ return NULL;
+ }
+
+ CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS);
+ cxListAdd(stack, current_store->root);
+
+ while(cxListSize(stack) > 0) {
+ Resource *parent = NULL;
+ cxListRemoveAndGet(stack, 0, &parent);
+ CxIterator i = cxListIterator(parent->children);
+ cx_foreach(Resource *, res, i) {
+ if(res->notebook && res->notebook->notebook_id == notebook_id) {
+ return res;
+ }
+ if(res->children) {
+ cxListAdd(stack, res);
+ }
+ }
+ }
+ cxListFree(stack);
+
+ return NULL;
+}
+
int64_t note_store_count_children(int64_t resource_id) {
DBUQuery *q = connection->createQuery(connection, NULL);
dbuQuerySetSQL(q, SQL_RESOURCE_COUNT_CHILDREN);
if(job->prev_parent_id != 0 && job->prev_parent_id != job->res->parent_id) {
// move resource to new parent
NoteStore *store = note_store_get();
- Resource *old_parent = note_store_get_notebook_by_id(store, job->prev_parent_id);
- Resource *new_parent = note_store_get_notebook_by_id(store, job->res->parent_id);
+ Resource *old_parent = note_store_get_notebook_by_resource_id(store, job->prev_parent_id);
+ Resource *new_parent = note_store_get_notebook_by_resource_id(store, job->res->parent_id);
if(old_parent && new_parent && old_parent->children) {
size_t i = cxListFind(old_parent->children, job->res);
cxListRemove(old_parent->children, i);
if(!error) {
NoteStore *store = job->store;
job->res->resource_id = newid;
- Resource *parent = note_store_get_notebook_by_id(store, job->res->parent_id);
+ Resource *parent = note_store_get_notebook_by_resource_id(store, job->res->parent_id);
if(parent) {
if(!parent->children) {
parent->children = cxArrayListCreate(store->mp->allocator, CX_STORE_POINTERS, 8);
return 0;
}
+static void uithr_swap_positions_finished(UiEvent *event, ExecJob *job) {
+ Resource *res1 = note_store_get_notebook_by_notebook_id(current_store, job->id1);
+ Resource *res2 = note_store_get_notebook_by_notebook_id(current_store, job->id2);
+ // res1 && res2 should always exist and the parent should be the same
+ if(res1 && res2 && res1->parent_id == res2->parent_id) {
+ Resource *parent = note_store_get_notebook_by_resource_id(current_store, res1->parent_id);
+ if(parent) {
+ size_t index1 = cxListFind(parent->children, res1);
+ size_t index2 = cxListFind(parent->children, res2);
+ cxListSwap(parent->children, index1, index2);
+ note_store_groups_updated();
+ }
+ }
+
+ if(job->resultcb) {
+ job->resultcb(event, job->error, job->userdata);
+ }
+ free(job);
+}
+
void note_store_notebook_swap_position_async(
UiObject *obj,
Notebook *nb1,
job->flag = 0;
job->id1 = nb1->notebook_id;
job->id2 = nb2->notebook_id;
- ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_execjob_finished, job);
+ ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_swap_positions_finished, job);
}
CX_TEST_ASSERT(store && store->root && store->root->children);
CX_TEST_ASSERT(cxListSize(store->root->children) == numchildren + 1);
- res0 = note_store_get_notebook_by_id(store, resource_id);
+ res0 = note_store_get_notebook_by_resource_id(store, resource_id);
CX_TEST_ASSERT(res0);
CX_TEST_ASSERT(res0->resource_id == resource_id);
CX_TEST_ASSERT(!cx_strcmp(res0->nodename, "test_save_notebook_res0"));
CX_TEST_ASSERT(store && store->root && store->root->children);
CX_TEST_ASSERT(cxListSize(store->root->children) == numchildren);
- res0 = note_store_get_notebook_by_id(store, resource_id);
+ res0 = note_store_get_notebook_by_resource_id(store, resource_id);
CX_TEST_ASSERT(res0);
CX_TEST_ASSERT(!cx_strcmp(res0->nodename, "test_save_notebook_update0"));
store = note_store_get();
CX_TEST_ASSERT(store && store->root && store->root->children);
- Resource *test_res0 = note_store_get_notebook_by_id(store, group1_id);
- Resource *test_res1 = note_store_get_notebook_by_id(store, group2_id);
+ Resource *test_res0 = note_store_get_notebook_by_resource_id(store, group1_id);
+ Resource *test_res1 = note_store_get_notebook_by_resource_id(store, group2_id);
CX_TEST_ASSERT(test_res0 && test_res1);
CX_TEST_ASSERT(!cx_strcmp(test_res0->nodename, "movetest_group1"));
CxIterator i = cxListIterator(store->root->children);
cx_foreach(Resource *, res, i) {
- Resource *xres = note_store_get_notebook_by_id(store, res->resource_id);
+ Resource *xres = note_store_get_notebook_by_resource_id(store, res->resource_id);
CX_TEST_ASSERT(res == xres);
}
}
store = note_store_get();
CX_TEST_ASSERT(store);
- Resource *res0_deleted = note_store_get_notebook_by_id(store, res0_id);
+ Resource *res0_deleted = note_store_get_notebook_by_resource_id(store, res0_id);
CX_TEST_ASSERT(res0_deleted == NULL);
// test delete-fail
store = note_store_get();
CX_TEST_ASSERT(store);
- Resource *res1_not_deleted = note_store_get_notebook_by_id(store, res1_id);
+ Resource *res1_not_deleted = note_store_get_notebook_by_resource_id(store, res1_id);
CX_TEST_ASSERT(res1_not_deleted != NULL);
// cleanup
store = note_store_get();
CX_TEST_ASSERT(store);
- Resource *res1_deleted = note_store_get_notebook_by_id(store, res1_id);
+ Resource *res1_deleted = note_store_get_notebook_by_resource_id(store, res1_id);
CX_TEST_ASSERT(res1_deleted == NULL);
int64_t nchildren = note_store_count_children(res1_id);