]> uap-core.de Git - note.git/commitdiff
implement AddNote button
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 25 Feb 2025 21:10:56 +0000 (22:10 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 25 Feb 2025 21:10:56 +0000 (22:10 +0100)
application/application.c
application/application.h
application/note.c
application/note.h
application/notebook.c
application/notebook.h
application/store.c
application/store.h

index f72724e9c0f48ffb93956b8c9956f7458efbad2f..bef70d5cbd27e18f5cfd01ed2f5c03e02d9b2055 100644 (file)
@@ -30,6 +30,7 @@
 #include "window.h"
 #include "store.h"
 #include "menu.h"
+#include "notebook.h"
 
 #include <unistd.h>
 #include <limits.h>
@@ -101,5 +102,6 @@ void application_startup(UiEvent *event, void *data) {
 /* ------------------------------- Actions ------------------------------- */
 
 void action_note_new(UiEvent *event, void *data) {
-    
+    NotebookModel *notebook = event->document;
+    notebookmodel_new_note(notebook);
 }
index 21cfd86c0071966d03bc2a342381f2d9f4a195ff..8fbe46a3e3a8056a995a42347e30826c54ed4ec3 100644 (file)
@@ -110,6 +110,8 @@ struct NoteModel {
     UiString *title;
     UiText *text;
     UiGeneric *html;
+    
+    bool modified;
 };
    
 
index 4f3c913a865fdab4ab93653305531af708c0b09c..166c0eb12ca0f7a50431c54bdac673e27d89f998 100644 (file)
@@ -73,3 +73,23 @@ static void note_content_loaded(UiEvent *event, cxmutstr result, void *userdata)
 void note_load_content(UiObject *obj, NotebookModel *notebook, Note *note) {
     note_store_get_note_content_async(obj, notebook->current_notes_pool->allocator, note->note_id, note_content_loaded, note);
 }
+
+void note_save(UiObject *obj, NotebookModel *notebook, Note *note) {
+    NoteModel *m = note->model;
+    char *title = ui_get(m->title);
+    char *content = ui_get(m->text);
+    const CxAllocator *a = notebook->current_notes_pool->allocator;
+    
+    cxFree(a, note->title);
+    note->title = cx_strdup_a(a, cx_str(title)).ptr;
+    
+    cxFree(a, note->content.ptr);
+    note->content = cx_strdup_a(a, cx_str(content));
+    
+    if(note->note_id == 0) {
+        // new note
+        note_store_new_note_async(obj, note, NULL, NULL);
+    } else {
+        // TODO: save note
+    }
+}
index e1242b43b2652247fb5a99d38762004aa0c8838b..30a003bbd4e564aa207a82577a0e205955ffde05 100644 (file)
@@ -41,6 +41,8 @@ void notemodel_set_note(NoteModel *model, Note *note);
 // TODO: the interface is weird, but we need the NotebookModel for the allocator
 void note_load_content(UiObject *obj, NotebookModel *notebook, Note *note);
 
+void note_save(UiObject *obj, NotebookModel *notebook, Note *note);
+
 
 #ifdef __cplusplus
 }
index cd5c636dcb8cf93b9855e750c44b23358a48ca0f..a0c5a8117fc5f2ad7dc5adbf7aebcb342d6f5f4c 100644 (file)
@@ -56,6 +56,10 @@ void notebookmodel_detach(NotebookModel *model) {
         ui_detach_document2(model->window->obj->ctx, model);
         model->window->current_notebook = NULL;
     }
+    
+    if(model->current_note && model->current_note->model->modified) {
+        note_save(model->window->obj, model, model->current_note);
+    }
 }
 
 void notebookmodel_set_collection(NotebookModel *model, Collection *collection) {
@@ -115,3 +119,13 @@ void notebookmodel_detach_current_note(NotebookModel *model) {
         model->current_note = NULL;
     }
 }
+
+
+void notebookmodel_new_note(NotebookModel *model) {
+    Note *new_note = cxMalloc(model->current_notes_pool->allocator, sizeof(Note));
+    memset(new_note, 0, sizeof(Note));
+    
+    new_note->parent_id = model->collection->collection_id;
+    notebookmodel_attach_note(model, new_note);
+    new_note->model->modified = TRUE;
+}
index 084d909ed7c9fcd07a1747ea7669ca50584c73f1..8c8c50c25a7f205d94e752d6f3ea2f8b263746a6 100644 (file)
@@ -49,6 +49,8 @@ void notebookmodel_reload(UiObject *obj, NotebookModel *model);
 void notebookmodel_attach_note(NotebookModel *model, Note *note);
 void notebookmodel_detach_current_note(NotebookModel *model);
 
+void notebookmodel_new_note(NotebookModel *model);
+
 
 #ifdef __cplusplus
 }
index 3ddcd624b92be29cd928da8da95f2f087d0aa9f5..3d3da9ed2ce8d9850df8493880e162c7f380944b 100644 (file)
@@ -59,6 +59,8 @@
 
 #define SQL_NOTE_GET_CONTENT "select content, bin_content from notes where note_id = ? ;"
 
+#define SQL_NOTE_NEW "insert into notes(parent_id, name, title, lastmodified, creationdate, content) values (?, ?, ?, datetime(), datetime(), ?) returning note_id;"
+
 static DBUConnection *connection;
 
 static UiThreadpool *queue;
@@ -460,3 +462,58 @@ void note_store_get_note_content_async(UiObject *obj, const CxAllocator *a, int6
     job->userdata = userdata;
     ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_get_note_content, job, (ui_callback)uithr_get_note_content_finished, job);
 }
+
+
+
+
+typedef struct SaveNoteJob {
+    Note *note;
+    execresult_func resultcb;
+    void *userdata;
+    int error;
+} SaveNoteJob;
+
+static void uithr_new_note_finished(UiEvent *event, SaveNoteJob *job) {
+    if(job->resultcb) {
+        job->resultcb(event, job->error, job->userdata);
+    }
+    free(job);
+}
+
+static int qthr_new_note(SaveNoteJob *job) {
+    Note *n = job->note;
+    DBUQuery *q = connection->createQuery(connection, NULL);
+    dbuQuerySetSQL(q, SQL_NOTE_NEW);
+    dbuQuerySetParamInt64(q, 1, n->parent_id);
+    dbuQuerySetParamString(q, 2, cx_str(n->title));
+    dbuQuerySetParamString(q, 3, cx_str(n->title));
+    dbuQuerySetParamString(q, 4, cx_strcast(n->content));
+    if(dbuQueryExec(q)) {
+        job->error = 1;
+    } else {
+        DBUResult *result = dbuQueryGetResult(q);
+        if(result->hasData(result)) {
+            cxstring new_id_str = result->getText(result, 0);
+            if(new_id_str.ptr) {
+                long long new_id = 0;
+                cx_strtoll(new_id_str, &new_id, 10);
+                n->note_id = new_id;
+            }
+        } else {
+            job->error = 2;
+        }
+        result->free(result);
+    }
+    dbuQueryFree(q);
+    
+    return 0;
+}
+
+void note_store_new_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_new_note, job, (ui_callback)uithr_new_note_finished, job);
+}
index 9a49f35483cb921073ed4b46611efc72e00fbc5e..376ba6ff981f4e71106c56ce89be07c27352b94f 100644 (file)
@@ -53,6 +53,8 @@ typedef struct AsyncListResult {
 typedef void (*listresult_func)(UiEvent *event, AsyncListResult *result, void *userdata);
 typedef void (*stringresult_func)(UiEvent *event, cxmutstr result, void *userdata);
 
+typedef void (*execresult_func)(UiEvent *event, 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);
@@ -84,6 +86,8 @@ void note_store_get_notes_async(UiObject *obj, int64_t parent_collection_id, lis
 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);
+
 
 #ifdef __cplusplus
 }