From: Olaf Wintermann Date: Sun, 27 Jul 2025 10:52:27 +0000 (+0200) Subject: add note_store_delete_empty_collection_async function X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=d94127bc4311fe538021945a288e924b6661170e;p=note.git add note_store_delete_empty_collection_async function --- diff --git a/application/store.c b/application/store.c index c70071a..f19aa57 100644 --- a/application/store.c +++ b/application/store.c @@ -78,7 +78,9 @@ #define SQL_NOTE_MOVE_TO_TRASH "update resources set parent_id = ? where resource_id = ? ;" -#define SQL_NOTE_DELETE "delete from resources where resource_id = ? ;" +#define SQL_RESOURCE_DELETE "delete from resources where resource_id = ? ;" + +#define SQL_RESOURCE_COUNT_CHILDREN "select count(*) from resources where parent_id = ? ;" #define SQL_ATTACHMENT_RESOURCE_NEW \ "insert into resources(parent_id, nodename, lastmodified, creationdate, content) values " \ @@ -391,6 +393,20 @@ NoteStore* note_store_get() { return current_store; } +static int64_t resource_get_children(int64_t resource_id) { + DBUQuery *q = connection->createQuery(connection, NULL); + dbuQuerySetSQL(q, SQL_RESOURCE_COUNT_CHILDREN); + dbuQuerySetParamInt64(q, 1, resource_id); + if(dbuQueryExec(q)) { + return -1; + } + DBUResult *result = dbuQueryGetResult(q); + int64_t intResult = -1; + dbuResultAsInt64(result, &intResult); + dbuQueryFree(q); + return intResult; +} + CxList* note_store_get_notes(const CxAllocator *a, int64_t parent_collection_id) { DBUQuery *q = connection->createQuery(connection, NULL); @@ -628,7 +644,7 @@ static int qthr_delete_note(DeleteNoteJob *job) { dbuQuerySetParamInt64(q, 1, current_store->trash->resource_id); dbuQuerySetParamInt64(q, 2, job->note_id); } else { - dbuQuerySetSQL(q, SQL_NOTE_DELETE); + dbuQuerySetSQL(q, SQL_RESOURCE_DELETE); dbuQuerySetParamInt64(q, 1, job->note_id); } if(dbuQueryExec(q)) { @@ -846,3 +862,54 @@ void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresu job->error = 0; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_new_notebook, job, (ui_callback)uithr_new_notebook_finished, job); } + + +typedef struct ExecJob { + execresult_func resultcb; + void *userdata; + int64_t id1; + int64_t id2; + int error; +} ExecJob; + +static void uithr_execjob_finished(UiEvent *event, ExecJob *job) { + job->resultcb(event, job->error, job->userdata); + free(job); +} + +static void qthr_delete_empty_collection(ExecJob *job) { + // only delete the resource when there are no children + int64_t nchildren = resource_get_children(job->id1); + if(nchildren > 0) { + job->error = 1; + return; + } + if(nchildren < 0) { + job->error = 2; // db error + return; + } + + + DBUQuery *q = connection->createQuery(connection, NULL); + dbuQuerySetSQL(q, SQL_RESOURCE_DELETE); + dbuQuerySetParamInt64(q, 1, job->id1); + if(dbuQueryExec(q)) { + job->error = 3; + } + dbuQueryFree(q); +} + +void note_store_delete_empty_collection_async( + UiObject *obj, + Resource *res, + execresult_func resultcb, + void *userdata) +{ + ExecJob *job = malloc(sizeof(ExecJob)); + job->resultcb = resultcb; + job->userdata = userdata; + job->error = 0; + job->id1 = res->resource_id; + job->id2 = 0; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_empty_collection, job, (ui_callback)uithr_execjob_finished, job); +} diff --git a/application/store.h b/application/store.h index 1c1ba6d..a319dca 100644 --- a/application/store.h +++ b/application/store.h @@ -100,6 +100,26 @@ void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresul void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresult_func resultcb, void *userdata); +void note_store_delete_empty_collection_async( + UiObject *obj, + Resource *res, + execresult_func resultcb, + void *userdata); + +void note_store_delete_collection_and_move_children_async( + UiObject *obj, + Resource *res, + int64_t new_parent_id, + execresult_func resultcb, + void *userdata); + +void note_store_notebook_swap_position( + UiObject *obj, + Notebook *nb1, + Notebook *nb2, + execresult_func resultcb, + void *userdata); + #ifdef __cplusplus } #endif