]> uap-core.de Git - note.git/commitdiff
implement note_store_save_note_async
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 3 Mar 2025 21:31:28 +0000 (22:31 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 3 Mar 2025 21:31:28 +0000 (22:31 +0100)
application/note.c
application/notebook.c
application/store.c
application/store.h

index 166c0eb12ca0f7a50431c54bdac673e27d89f998..84a613a5a6dbb6c77192cdf7975bab6b67a57fb8 100644 (file)
@@ -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);
     }
 }
index 29828776e7eb14c6a7611562105042fba76c3ac9..895498adfe48575380de6d9ca7ec2ee6f0651889 100644 (file)
@@ -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);
         }
index 3d3da9ed2ce8d9850df8493880e162c7f380944b..97fb59b638b04dbae8a1d8cce1fad02fce8c6236 100644 (file)
 
 #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);
 }
index 376ba6ff981f4e71106c56ce89be07c27352b94f..1c96af7f7ea8b02f7b2ca5c1a35721d8416ba344 100644 (file)
@@ -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
 }