From: Olaf Wintermann Date: Mon, 3 Mar 2025 21:31:28 +0000 (+0100) Subject: implement note_store_save_note_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=7fdc4557152e1a6e7ce807ccb185343a4153a0b0;p=note.git implement note_store_save_note_async --- diff --git a/application/note.c b/application/note.c index 166c0eb..84a613a 100644 --- a/application/note.c +++ b/application/note.c @@ -89,7 +89,9 @@ void note_save(UiObject *obj, NotebookModel *notebook, Note *note) { if(note->note_id == 0) { // new note note_store_new_note_async(obj, note, NULL, NULL); + ui_list_append(notebook->notes, note); + ui_list_update(notebook->notes); } else { - // TODO: save note + note_store_save_note_async(obj, note, NULL, NULL); } } diff --git a/application/notebook.c b/application/notebook.c index 2982877..895498a 100644 --- a/application/notebook.c +++ b/application/notebook.c @@ -140,9 +140,13 @@ void notebookmodel_attach_note(NotebookModel *model, Note *note) { void notebookmodel_detach_current_note(NotebookModel *model) { if(model->current_note) { - if(model->current_note->model->modified) { + // TODO: model->modified doesnt work yet, remove || 1 when it works + if(model->current_note->model->modified || 1) { note_save(model->window->obj, model, model->current_note); } + // TODO: in theory ui_detach_document2 should save the state + // of all vars, but it seems this doesn't work + // without note_save, the content is not saved (internally) if(model->current_note->model) { ui_detach_document2(model->ctx, model->current_note->model); } diff --git a/application/store.c b/application/store.c index 3d3da9e..97fb59b 100644 --- a/application/store.c +++ b/application/store.c @@ -61,6 +61,11 @@ #define SQL_NOTE_NEW "insert into notes(parent_id, name, title, lastmodified, creationdate, content) values (?, ?, ?, datetime(), datetime(), ?) returning note_id;" +#define SQL_NOTE_SAVE "update notes set name = ? ," \ + "title = ? ," \ + "content = ? " \ + "where note_id = ? ;" + static DBUConnection *connection; static UiThreadpool *queue; @@ -473,7 +478,7 @@ typedef struct SaveNoteJob { int error; } SaveNoteJob; -static void uithr_new_note_finished(UiEvent *event, SaveNoteJob *job) { +static void uithr_save_note_finished(UiEvent *event, SaveNoteJob *job) { if(job->resultcb) { job->resultcb(event, job->error, job->userdata); } @@ -515,5 +520,37 @@ void note_store_new_note_async(UiObject *obj, Note *note, execresult_func result job->resultcb = resultcb; job->userdata = userdata; job->error = 0; - ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_new_note, job, (ui_callback)uithr_new_note_finished, job); + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_new_note, job, (ui_callback)uithr_save_note_finished, job); +} + + +static int qthr_save_note(SaveNoteJob *job) { + Note *n = job->note; + DBUQuery *q = connection->createQuery(connection, NULL); + dbuQuerySetSQL(q, SQL_NOTE_SAVE); + dbuQuerySetParamString(q, 1, cx_str(n->title)); + dbuQuerySetParamString(q, 2, cx_str(n->title)); + dbuQuerySetParamString(q, 3, cx_strcast(n->content)); + dbuQuerySetParamInt64(q, 4, n->note_id); + if(dbuQueryExec(q)) { + job->error = 1; + } else { + DBUResult *result = dbuQueryGetResult(q); + if(!result->isOk(result)) { + job->error = 2; + } + result->free(result); + } + dbuQueryFree(q); + + return 0; +} + +void note_store_save_note_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata) { + SaveNoteJob *job = malloc(sizeof(SaveNoteJob)); + job->note = note; + job->resultcb = resultcb; + job->userdata = userdata; + job->error = 0; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_save_note, job, (ui_callback)uithr_save_note_finished, job); } diff --git a/application/store.h b/application/store.h index 376ba6f..1c96af7 100644 --- a/application/store.h +++ b/application/store.h @@ -87,7 +87,7 @@ cxmutstr note_store_get_note_content(const CxAllocator *a, int64_t note_id); void note_store_get_note_content_async(UiObject *obj, const CxAllocator *a, int64_t note_id, stringresult_func resultcb, void *userdata); void note_store_new_note_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata); - +void note_store_save_note_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata); #ifdef __cplusplus }