From: Olaf Wintermann Date: Fri, 9 May 2025 19:32:06 +0000 (+0200) Subject: add note_store_new_notebook_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=c8ec21bb383607dde0526df9d9a9bdc243eeb563;p=note.git add note_store_new_notebook_async --- diff --git a/application/store.c b/application/store.c index 2c82211..3841288 100644 --- a/application/store.c +++ b/application/store.c @@ -82,6 +82,8 @@ "inner join resources r on a.attachment_resource_id = r.resource_id " \ "where parent_resource_id = ? order by attachment_id;" +#define SQL_NOTEBOOK_NEW "insert into resources(parent_id, nodename, iscollection) values (?, ?, 1) returning resource_id;" + static DBUConnection *connection; static UiThreadpool *queue; @@ -761,3 +763,49 @@ void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresul job->userdata = userdata; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_load_attachments, job, (ui_callback)uithr_load_attachments_finished, job); } + + + + + +typedef struct CreateNotebookJob { + Resource *resource; + createresult_func resultcb; + void *userdata; + int error; +} CreateNotebookJob; + +static int qthr_new_notebook(CreateNotebookJob *job) { + DBUQuery *q = connection->createQuery(connection, NULL); + dbuQuerySetSQL(q, SQL_NOTEBOOK_NEW); + dbuQuerySetParamInt64(q, 1, job->resource->parent_id); + dbuQuerySetParamString(q, 2, cx_str(job->resource->nodename)); + if(dbuQueryExec(q)) { + job->error = 1; + } else { + DBUResult *result = dbuQueryGetResult(q); + int64_t new_resource_id; + if(!dbuResultAsInt64(result, &job->resource->resource_id)) { + job->error = 2; + } + } + dbuQueryFree(q); + + return 0; +} + +static void uithr_new_notebook_finished(UiEvent *event, CreateNotebookJob *job) { + if(job->resultcb) { + job->resultcb(event, job->resource->resource_id, job->error, job->userdata); + } + free(job); +} + +void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresult_func resultcb, void *userdata) { + CreateNotebookJob *job = malloc(sizeof(CreateNotebookJob)); + job->resource = notebook; + job->resultcb = resultcb; + job->userdata = userdata; + job->error = 0; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_new_notebook, job, (ui_callback)uithr_new_notebook_finished, job); +} diff --git a/application/store.h b/application/store.h index 4314efd..1c1ba6d 100644 --- a/application/store.h +++ b/application/store.h @@ -56,6 +56,8 @@ typedef void (*stringresult_func)(UiEvent *event, cxmutstr result, void *userdat typedef void (*execresult_func)(UiEvent *event, int error, void *userdata); +typedef void (*createresult_func)(UiEvent *event, int64_t newid, int error, void *userdata); + int init_note_store(); CxList* note_store_get_user_settings(const CxAllocator *a, const char *host, const char *user, const char *profile); @@ -96,6 +98,8 @@ void note_store_save_attachment_async(UiObject *obj, Attachment *attachment, exe void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata); +void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresult_func resultcb, void *userdata); + #ifdef __cplusplus } #endif diff --git a/application/window.c b/application/window.c index 175129c..4f4d893 100644 --- a/application/window.c +++ b/application/window.c @@ -279,6 +279,7 @@ static void action_nnd_new_group(UiEvent *event, void *userdata) { static void action_nnd_button(UiEvent *event, void *userdata) { if(event->intval == 1) { // add + } // 4: cancel ui_close(event->obj); }