#include "window.h"
#include "store.h"
#include "menu.h"
+#include "notebook.h"
#include <unistd.h>
#include <limits.h>
/* ------------------------------- Actions ------------------------------- */
void action_note_new(UiEvent *event, void *data) {
-
+ NotebookModel *notebook = event->document;
+ notebookmodel_new_note(notebook);
}
UiString *title;
UiText *text;
UiGeneric *html;
+
+ bool modified;
};
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
+ }
+}
// 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
}
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) {
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;
+}
void notebookmodel_attach_note(NotebookModel *model, Note *note);
void notebookmodel_detach_current_note(NotebookModel *model);
+void notebookmodel_new_note(NotebookModel *model);
+
#ifdef __cplusplus
}
#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;
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);
+}
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);
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
}