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);
}
#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;
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);
}
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);
}
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
}