From 390da8bc412eeb3bbef7cbcf59b3ecb6c92233a4 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 5 May 2025 21:05:31 +0200 Subject: [PATCH] use new Note struct in case of Resource --- application/application.c | 4 +-- application/application.h | 6 ++-- application/attachment.c | 18 +++++------ application/attachment.h | 6 ++-- application/editor.c | 2 +- application/editor.h | 4 +-- application/gtk-text.c | 6 ++-- application/note.c | 65 +++++++++++++++++++------------------- application/note.h | 12 +++---- application/notebook.c | 25 +++++++++------ application/notebook.h | 4 +-- application/store.c | 24 +++++++------- application/store.h | 8 ++--- application/store_sqlite.c | 2 +- application/types.c | 1 + application/types.h | 12 ++++--- application/window.c | 7 ++-- 17 files changed, 108 insertions(+), 98 deletions(-) diff --git a/application/application.c b/application/application.c index 2420342..df3096c 100644 --- a/application/application.c +++ b/application/application.c @@ -143,8 +143,8 @@ static void delete_result(UiEvent *event, void *data) { void action_note_delete(UiEvent *event, void *data) { NotebookModel *notebook = event->document; - Resource *note = notebook->current_note; - cxmutstr msg = cx_asprintf("Delete note %s?", note_get_title(note)); + Note *note = notebook->current_note; + cxmutstr msg = cx_asprintf("Delete note %s?", note_get_title(note->resource)); ui_dialog( event->obj, .title = "Delete Note", diff --git a/application/application.h b/application/application.h index a70fab8..6fb9817 100644 --- a/application/application.h +++ b/application/application.h @@ -117,7 +117,7 @@ struct NotebookModel { /* * currently attached note */ - Resource *current_note; + Note *current_note; /* * some actions might trigger unwanted selection events @@ -163,7 +163,7 @@ typedef struct AttachmentModel { UiContext *ctx; const CxAllocator *note_allocator; - Resource *parent_note; + Note *parent_note; UiGeneric *img; } AttachmentModel; @@ -171,7 +171,7 @@ typedef struct AttachmentModel { typedef struct AttachmentWindow { UiObject *obj; - Resource *resource; + Note *note; UiList *attachments; int current_index; diff --git a/application/attachment.c b/application/attachment.c index be271fd..c5cf9ba 100644 --- a/application/attachment.c +++ b/application/attachment.c @@ -48,7 +48,7 @@ Attachment* attachment_create( return attachment; } -void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource *resource) { +void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Note *note) { if(attachment->ui) { return; } @@ -61,9 +61,9 @@ void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource model->img = ui_generic_new(ctx, NULL); } model->ctx = ctx; - model->note_allocator = resource->model->note_allocator; + model->note_allocator = note->model->note_allocator; - model->parent_note = resource; + model->parent_note = note; } /* @@ -172,12 +172,12 @@ void action_attachment_clicked(UiEvent *event, void *userdata) { * selected_attachment (optional): if non null, this attachment will be pre- * selected in the window */ -UiObject* attachment_window_create(Resource *resource, Attachment *selected_attachment) { - cxmutstr title = cx_asprintf("%s - attachments", note_get_title(resource)); +UiObject* attachment_window_create(Note *note, Attachment *selected_attachment) { + cxmutstr title = cx_asprintf("%s - attachments", note_get_title(note->resource)); UiObject *obj = ui_simple_window(title.ptr, NULL); free(title.ptr); - AttachmentWindow *wdata = attachment_window_create_data(obj, resource); + AttachmentWindow *wdata = attachment_window_create_data(obj, note); obj->window = wdata; // create UI @@ -218,17 +218,17 @@ static void update_attachments(AttachmentWindow *wdata, NoteModel *model) { /* * creates and initializes the AttachmentWindow window data object */ -AttachmentWindow* attachment_window_create_data(UiObject *obj, Resource *resource) { +AttachmentWindow* attachment_window_create_data(UiObject *obj, Note *note) { AttachmentWindow *wdata = ui_malloc(obj->ctx, sizeof(AttachmentWindow)); memset(wdata, 0, sizeof(AttachmentWindow)); wdata->obj = obj; - wdata->resource = resource; + wdata->note = note; wdata->attachments = ui_list_new(obj->ctx, NULL); wdata->image = ui_generic_new(obj->ctx, NULL); wdata->current_index = -1; - update_attachments(wdata, resource->model); + update_attachments(wdata, note->model); return wdata; } diff --git a/application/attachment.h b/application/attachment.h index 3776286..b738fb7 100644 --- a/application/attachment.h +++ b/application/attachment.h @@ -42,7 +42,7 @@ Attachment* attachment_create( AttachmentType type, const char *name); -void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource *resource); +void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Note *note); void attachment_set_image(Attachment *attachment, void *img); @@ -62,9 +62,9 @@ void attachment_item(UiObject *obj, int index, void *elm, void *userdata); void action_attachment_clicked(UiEvent *event, void *userdata); -UiObject* attachment_window_create(Resource *resource, Attachment *selected_attachment); +UiObject* attachment_window_create(Note *note, Attachment *selected_attachment); -AttachmentWindow* attachment_window_create_data(UiObject *obj, Resource *resource); +AttachmentWindow* attachment_window_create_data(UiObject *obj, Note *note); void attachment_window_set_image(AttachmentWindow *wdata, Attachment *attachment); diff --git a/application/editor.c b/application/editor.c index a1fedec..00da427 100644 --- a/application/editor.c +++ b/application/editor.c @@ -36,7 +36,7 @@ #include // url encoding/decoding -void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown) { +void editor_load_markdown(Note *note, UIWIDGET textview, cxmutstr markdown) { UiText *text = note->model->text; // make sure the textbuf is initialized editor_init_textbuf(text); diff --git a/application/editor.h b/application/editor.h index 41c03dd..7069a50 100644 --- a/application/editor.h +++ b/application/editor.h @@ -118,7 +118,7 @@ struct MDActiveStyles { void editor_init(UiText *text); -void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown); +void editor_load_markdown(Note *note, UIWIDGET textview, cxmutstr markdown); MDDoc* parse_markdown(cxstring markdown); void mddoc_free(MDDoc *doc); @@ -133,7 +133,7 @@ MDDocLinear mddoc_linearization(MDDoc *doc); void editor_global_init(); void editor_init_textview(UiObject *obj, UIWIDGET textview); void editor_init_textbuf(UiText *text); -void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles); +void editor_apply_styles(Note *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles); cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a); UiBool editor_set_style(UiText *text, const char *style, UiBool enabled); diff --git a/application/gtk-text.c b/application/gtk-text.c index f876044..cd896fd 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -364,7 +364,7 @@ static void editor_insert_image(NoteEditor *editor, Attachment *attachment, GtkT static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *attachment_path) { MainWindow *wdata = editor->obj->window; - Resource *note = wdata->current_notebook->current_note; + Note *note = wdata->current_notebook->current_note; NoteModel *model = note->model; // create attachment @@ -837,7 +837,7 @@ void init_tagtable(GtkTextTagTable *table) { /* * Applies all styles from the MDDocStyleSection list to the text buffer */ -void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles) { +void editor_apply_styles(Note *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles) { GtkTextBuffer *buffer = text->data1; GtkTextTagTable *tagtable = gtk_text_buffer_get_tag_table(buffer); NoteEditor *editor = g_object_get_data(G_OBJECT(textview), "editor"); @@ -878,7 +878,7 @@ void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList GtkTextIter iter; gtk_text_buffer_get_iter_at_offset(buffer, &iter, sec->pos); if(sec->length == MDDocStyleSection_IMAGE) { - Attachment *attachment = note_get_attachment(note, sec->link); + Attachment *attachment = note_get_attachment(note->resource, sec->link); if(attachment && attachment->ui->img->value) { editor_insert_image(editor, attachment, &iter); } else { diff --git a/application/note.c b/application/note.c index ffa0d94..e19cd27 100644 --- a/application/note.c +++ b/application/note.c @@ -87,34 +87,32 @@ NoteModel* notemodel_create(const CxAllocator *note_allocator) { return model; } -void notemodel_set_note(NoteModel *model, Resource *note) { +void notemodel_set_note(NoteModel *model, Note *note) { note->model = model; - ui_set(model->title, note->displayname); + ui_set(model->title, note->resource->displayname); - if(note->content_loaded) { + if(note->resource->content_loaded) { // TODO: when multiple note types are implemented, check contenttype // and set model->text, model->html or something else - if(!note->contenttype) { - ui_set(model->text, note->content.ptr); - } + ui_set(model->text, note->resource->content.ptr); } } typedef struct LoadNoteContent { - Resource *note; + Note *note; UiBool content; UiBool attachments; } LoadNoteContent; static void note_loading_completed(UiObject *obj, LoadNoteContent *op) { - Resource *note = op->note; + Note *note = op->note; MainWindow *wdata = obj->window; free(op); if(note->model) { // fill attachment model - CxIterator i = cxListIterator(note->attachments); + CxIterator i = cxListIterator(note->resource->attachments); cx_foreach(Attachment *, attachment, i) { ui_list_append(note->model->attachments, attachment); if(attachment_set_image_from_data(attachment, attachment->content)) { @@ -127,14 +125,14 @@ static void note_loading_completed(UiObject *obj, LoadNoteContent *op) { ui_set_group(obj->ctx, APP_STATE_NOTE_HAS_ATTACHMENTS); } - editor_load_markdown(note, wdata->textview, note->content); + editor_load_markdown(note, wdata->textview, note->resource->content); } - note->content_loaded = TRUE; + note->resource->content_loaded = TRUE; } static void note_content_loaded(UiEvent *event, cxmutstr result, void *userdata) { LoadNoteContent *op = userdata; - op->note->content = result; + op->note->resource->content = result; printf("note content: %s\n", result.ptr); op->content = TRUE; if(op->attachments) { @@ -146,8 +144,8 @@ static void note_attachments_loaded(UiEvent *event, int error, void *userdata) { LoadNoteContent *op = userdata; op->attachments = TRUE; - if(op->note->attachments) { - CxIterator i = cxListIterator(op->note->attachments); + if(op->note->resource->attachments) { + CxIterator i = cxListIterator(op->note->resource->attachments); cx_foreach(Attachment *, attachment, i) { attachment_create_ui_model(op->note->model->ctx, attachment, op->note); if(attachment->content.length > 0) { @@ -162,7 +160,7 @@ static void note_attachments_loaded(UiEvent *event, int error, void *userdata) { } } -void note_load_content(UiObject *obj, NotebookModel *notebook, Resource *note) { +void note_load_content(UiObject *obj, NotebookModel *notebook, Note *note) { LoadNoteContent *op = malloc(sizeof(LoadNoteContent)); op->note = note; op->content = FALSE; @@ -172,28 +170,28 @@ void note_load_content(UiObject *obj, NotebookModel *notebook, Resource *note) { note_store_load_note_attachments_async(obj, note, note_attachments_loaded, op); } -void note_add_attachment(Resource *note, Attachment *attachment) { - if(!note->attachments) { - note->attachments = cxArrayListCreate(note->model->note_allocator, NULL, CX_STORE_POINTERS, 8); +void note_add_attachment(Note *note, Attachment *attachment) { + if(!note->resource->attachments) { + note->resource->attachments = cxArrayListCreate(note->model->note_allocator, NULL, CX_STORE_POINTERS, 8); } - cxListAdd(note->attachments, attachment); + cxListAdd(note->resource->attachments, attachment); } -void note_save(UiObject *obj, NotebookModel *notebook, Resource *note) { +void note_save(UiObject *obj, NotebookModel *notebook, Note *note) { NoteModel *m = note->model; char *title = ui_get(m->title); const CxAllocator *a = notebook->current_notes_pool->allocator; - cxFree(a, note->displayname); - note->displayname = cx_strdup_a(a, cx_str(title)).ptr; + cxFree(a, note->resource->displayname); + note->resource->displayname = cx_strdup_a(a, cx_str(title)).ptr; - cxFree(a, note->nodename); - note->nodename = cx_strdup_a(a, cx_str(title)).ptr; + cxFree(a, note->resource->nodename); + note->resource->nodename = cx_strdup_a(a, cx_str(title)).ptr; cxmutstr content = editor_get_markdown(m->text, a); - cxFree(a, note->content.ptr); - note->content = content; + cxFree(a, note->resource->content.ptr); + note->resource->content = content; if(note->resource_id == 0) { // new note @@ -202,8 +200,8 @@ void note_save(UiObject *obj, NotebookModel *notebook, Resource *note) { note_store_save_note_async(obj, note, NULL, NULL); } - if(note->attachments) { - CxIterator i = cxListIterator(note->attachments); + if(note->resource->attachments) { + CxIterator i = cxListIterator(note->resource->attachments); cx_foreach(Attachment *, attachment, i) { attachment_save(obj, attachment, TRUE); } @@ -257,7 +255,7 @@ void note_insert_list(NoteModel *note, UiBool ordered) { -void note_update_title(NotebookModel *notebook, Resource *note) { +void note_update_title(NotebookModel *notebook, Note *note) { int index = notebookmode_get_note_index(notebook, note); if(index < 0) { return; @@ -266,8 +264,8 @@ void note_update_title(NotebookModel *notebook, Resource *note) { m->modified = TRUE; char *title = ui_get(m->title); - cxFree(m->note_allocator, note->displayname); - note->displayname = cx_strdup_a(m->note_allocator, cx_str(title)).ptr; + cxFree(m->note_allocator, note->resource->displayname); + note->resource->displayname = cx_strdup_a(m->note_allocator, cx_str(title)).ptr; notebook->notes->update(notebook->notes, index); } @@ -276,7 +274,9 @@ const char* note_get_title(Resource *note) { return note->displayname ? note->displayname : note->nodename; } -void note_destroy(const CxAllocator *a, Resource *note) { +void note_destroy(const CxAllocator *a, Note *note) { + // TODO + /* cxFree(a, note->nodename); cxFree(a, note->displayname); cxFree(a, note->contenttype); @@ -285,6 +285,7 @@ void note_destroy(const CxAllocator *a, Resource *note) { if(note->model) { // TODO: destroy model->context } + */ } Attachment* note_get_attachment(Resource *note, const char *path) { diff --git a/application/note.h b/application/note.h index f3024fa..190d1d5 100644 --- a/application/note.h +++ b/application/note.h @@ -44,14 +44,14 @@ typedef struct TextNoteParagraphStyles { NoteModel *notemodel_current(UiObject *obj); NoteModel* notemodel_create(const CxAllocator *note_allocator); -void notemodel_set_note(NoteModel *model, Resource *note); +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, Resource *note); +void note_load_content(UiObject *obj, NotebookModel *notebook, Note *note); -void note_add_attachment(Resource *note, Attachment *attachment); +void note_add_attachment(Note *note, Attachment *attachment); -void note_save(UiObject *obj, NotebookModel *notebook, Resource *note); +void note_save(UiObject *obj, NotebookModel *notebook, Note *note); void note_update_current_style(NoteModel *note, MDActiveStyles *style); @@ -62,10 +62,10 @@ void note_text_style_set_underline(NoteModel *note, UiBool enabled); void note_text_style_set_code(NoteModel *note, UiBool enabled); void note_insert_list(NoteModel *note, UiBool ordered); -void note_update_title(NotebookModel *notebook, Resource *note); +void note_update_title(NotebookModel *notebook, Note *note); const char* note_get_title(Resource *note); -void note_destroy(const CxAllocator *a, Resource *note); +void note_destroy(const CxAllocator *a, Note *note); Attachment* note_get_attachment(Resource *note, const char *path); diff --git a/application/notebook.c b/application/notebook.c index 5b92dd3..966a61c 100644 --- a/application/notebook.c +++ b/application/notebook.c @@ -44,7 +44,7 @@ NotebookModel* notebookmodel_create() { return model; } -static void notelist_select_note(NotebookModel *model, Resource *note) { +static void notelist_select_note(NotebookModel *model, Note *note) { CxList *list = model->notes->data; // UiList uses CxList internally list->collection.cmpfunc = cx_cmp_ptr; int index = (int)cxListFind(list, note); @@ -100,7 +100,7 @@ static void notebook_loaded(UiEvent *event, AsyncListResult *result, void *data) if(result->list) { ui_list_clear(model->notes); CxIterator i = cxListIterator(result->list); - cx_foreach(Resource *, note, i) { + cx_foreach(Note *, note, i) { ui_list_append(model->notes, note); } ui_list_update(model->notes); @@ -115,7 +115,7 @@ void notebookmodel_reload(UiObject *obj, NotebookModel *model) { } -void notebookmodel_attach_note(NotebookModel *model, Resource *note) { +void notebookmodel_attach_note(NotebookModel *model, Note *note) { // TODO: enable this optimization when possible // currently, a reattaching a notebook will still have current_note // but the UI binding will not be enabled @@ -151,7 +151,7 @@ void notebookmodel_attach_note(NotebookModel *model, Resource *note) { ui_set_group(model->window->obj->ctx, APP_STATE_NOTE_HAS_ATTACHMENTS); } - if(!note->content_loaded) { + if(!note->resource->content_loaded) { note_load_content(model->window->obj, model, note); } @@ -160,7 +160,7 @@ void notebookmodel_attach_note(NotebookModel *model, Resource *note) { void notebookmodel_detach_current_note(NotebookModel *model) { if(model->current_note) { - Resource *current_note = model->current_note; + Note *current_note = model->current_note; // TODO: model->modified doesnt work yet, remove || 1 when it works if(current_note->model->modified || 1) { note_save(model->window->obj, model, model->current_note); @@ -197,10 +197,15 @@ Resource* notebookmodel_get_note_by_id(NotebookModel *model, int64_t note_id, si void notebookmodel_new_note(NotebookModel *model) { - Resource *new_note = cxMalloc(model->current_notes_pool->allocator, sizeof(Resource)); - memset(new_note, 0, sizeof(Resource)); + Note *new_note = cxMalloc(model->current_notes_pool->allocator, sizeof(Note)); + memset(new_note, 0, sizeof(Note)); - new_note->parent_id = model->collection->resource_id; + Resource *new_resource = cxMalloc(model->current_notes_pool->allocator, sizeof(Resource)); + memset(new_resource, 0, sizeof(Resource)); + + new_note->resource = new_resource; + + new_resource->parent_id = model->collection->resource_id; notebookmodel_attach_note(model, new_note); new_note->model->modified = TRUE; // initialize note content @@ -213,7 +218,7 @@ void notebookmodel_new_note(NotebookModel *model) { typedef struct NoteDeleteOp { NotebookModel *notebook; - Resource *note; + Note *note; } NoteDeleteOp; static void note_deleted(UiEvent *event, int error, void *userdata) { @@ -284,7 +289,7 @@ void notebookmodel_add2navstack(NotebookModel *model) { */ } -int notebookmode_get_note_index(NotebookModel *model, Resource *note) { +int notebookmode_get_note_index(NotebookModel *model, Note *note) { CxList *list = model->notes->data; size_t index = cxListFind(list, note); return cxListIndexValid(list, index) ? index : -1; diff --git a/application/notebook.h b/application/notebook.h index e031863..3524016 100644 --- a/application/notebook.h +++ b/application/notebook.h @@ -46,7 +46,7 @@ void notebookmodel_set_collection(NotebookModel *model, Resource *collection); void notebookmodel_reload(UiObject *obj, NotebookModel *model); -void notebookmodel_attach_note(NotebookModel *model, Resource *note); +void notebookmodel_attach_note(NotebookModel *model, Note *note); void notebookmodel_detach_current_note(NotebookModel *model); Resource* notebookmodel_get_note_by_id(NotebookModel *model, int64_t note_id, size_t *index); @@ -56,7 +56,7 @@ void notebookmodel_delete_note(NotebookModel *model); void notebookmodel_add2navstack(NotebookModel *model); -int notebookmode_get_note_index(NotebookModel *model, Resource *note); +int notebookmode_get_note_index(NotebookModel *model, Note *note); #ifdef __cplusplus diff --git a/application/store.c b/application/store.c index 6b92ff6..1400393 100644 --- a/application/store.c +++ b/application/store.c @@ -56,7 +56,7 @@ "select * from cols\n" \ "order by path;" -#define SQL_NOTEBOOK_GET_NOTES "select r.resource_id, r.parent_id, r.nodename, r.displayname, r.lastmodified, r.creationdate, r.iscollection, r.contenttype from resources r inner join notes n on r.resource_id = n.resource_id where parent_id = ? ;" +#define SQL_NOTEBOOK_GET_NOTES "select n.*, NULL as [__resources__resource_id], r.resource_id, r.parent_id, r.nodename, r.displayname, r.lastmodified, r.creationdate, r.iscollection, r.contenttype from resources r inner join notes n on r.resource_id = n.resource_id where parent_id = ? ;" #define SQL_NOTE_GET_CONTENT "select content from resources where resource_id = ? ;" @@ -371,7 +371,7 @@ CxList* note_store_get_notes(const CxAllocator *a, int64_t parent_collection_id) DBUQuery *q = connection->createQuery(connection, NULL); dbuQuerySetSQL(q, SQL_NOTEBOOK_GET_NOTES); dbuQuerySetParamInt64(q, 1, parent_collection_id); - DBUObjectBuilder *builder = dbuObjectBuilder(resource_class, q, a); + DBUObjectBuilder *builder = dbuObjectBuilder(note_class, q, a); CxList *notes = dbuObjectBuilderGetList(builder); dbuObjectBuilderDestroy(builder); return notes; @@ -487,7 +487,7 @@ void note_store_get_note_content_async(UiObject *obj, const CxAllocator *a, int6 typedef struct SaveNoteJob { - Resource *note; + Note *note; execresult_func resultcb; void *userdata; int error; @@ -501,7 +501,7 @@ static void uithr_save_note_finished(UiEvent *event, SaveNoteJob *job) { } static int qthr_new_note(SaveNoteJob *job) { - Resource *n = job->note; + Resource *n = job->note->resource; DBUQuery *q = connection->createQuery(connection, NULL); dbuQuerySetSQL(q, SQL_NOTE_RESOURCE_NEW); dbuQuerySetParamInt64(q, 1, n->parent_id); @@ -525,7 +525,7 @@ static int qthr_new_note(SaveNoteJob *job) { dbuQuerySetParamInt64(q2, 2, 0); if(dbuQueryExec(q2)) { job->error = 3; - } + } // TODO: save note_id in the Note object dbuQueryFree(q2); } } else { @@ -538,7 +538,7 @@ static int qthr_new_note(SaveNoteJob *job) { return 0; } -void note_store_new_note_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata) { +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; @@ -549,7 +549,7 @@ void note_store_new_note_async(UiObject *obj, Resource *note, execresult_func re static int qthr_save_note(SaveNoteJob *job) { - Resource *n = job->note; + Resource *n = job->note->resource; DBUQuery *q = connection->createQuery(connection, NULL); dbuQuerySetSQL(q, SQL_NOTE_SAVE); dbuQuerySetParamString(q, 1, cx_str(n->displayname)); @@ -570,7 +570,7 @@ static int qthr_save_note(SaveNoteJob *job) { return 0; } -void note_store_save_note_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata) { +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; @@ -612,7 +612,7 @@ static void uithr_delete_finished(UiEvent *event, DeleteNoteJob *job) { free(job); } -void note_store_delete_async(UiObject *obj, Resource *note, UiBool move_to_trash, execresult_func resultcb, void *userdata) { +void note_store_delete_async(UiObject *obj, Note *note, UiBool move_to_trash, execresult_func resultcb, void *userdata) { DeleteNoteJob *job = malloc(sizeof(DeleteNoteJob)); job->note_id = note->resource_id; job->move_to_trash = move_to_trash; @@ -703,7 +703,7 @@ void note_store_save_attachment_async(UiObject *obj, Attachment *attachment, exe typedef struct LoadAttachmentsJob { - Resource *note; + Note *note; CxMempool *temp_mp; CxList *result; execresult_func resultcb; @@ -733,7 +733,7 @@ static void uithr_load_attachments_finished(UiEvent *event, LoadAttachmentsJob * if(job->result) { CxMempool *note_mp = job->note->model->note_allocator->data; cxMempoolTransfer(job->temp_mp, note_mp); - job->note->attachments = job->result; + job->note->resource->attachments = job->result; } cxMempoolFree(job->temp_mp); @@ -743,7 +743,7 @@ static void uithr_load_attachments_finished(UiEvent *event, LoadAttachmentsJob * free(job); } -void note_store_load_note_attachments_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata) { +void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata) { LoadAttachmentsJob *job = malloc(sizeof(LoadAttachmentsJob)); job->note = note; job->temp_mp = NULL; diff --git a/application/store.h b/application/store.h index 84405b3..4314efd 100644 --- a/application/store.h +++ b/application/store.h @@ -87,14 +87,14 @@ void note_store_get_notes_async(UiObject *obj, int64_t parent_resource_id, listr 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, Resource *note, execresult_func resultcb, void *userdata); -void note_store_save_note_async(UiObject *obj, Resource *note, execresult_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); -void note_store_delete_async(UiObject *obj, Resource *note, UiBool move_to_trash, execresult_func resultcb, void *userdata); +void note_store_delete_async(UiObject *obj, Note *note, UiBool move_to_trash, execresult_func resultcb, void *userdata); void note_store_save_attachment_async(UiObject *obj, Attachment *attachment, execresult_func resultcb, void *userdata); -void note_store_load_note_attachments_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata); +void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresult_func resultcb, void *userdata); #ifdef __cplusplus } diff --git a/application/store_sqlite.c b/application/store_sqlite.c index fc2cc0d..5f9bf57 100644 --- a/application/store_sqlite.c +++ b/application/store_sqlite.c @@ -76,7 +76,7 @@ "type integer, " \ "status text, " \ "targetdate integer, " \ - "foreign key (resource_id) references resources(resource_id) " \ + "foreign key (resource_id) references resources(resource_id) on delete cascade " \ ");" #define SQL_CREATE_TABLE_ATTACHMENTS "create table attachments( " \ "attachment_id integer primary key, " \ diff --git a/application/types.c b/application/types.c index b4f6a77..57db2a7 100644 --- a/application/types.c +++ b/application/types.c @@ -69,6 +69,7 @@ void register_types() { dbuClassAdd(note_class, Note, type); dbuClassAdd(note_class, Note, status); dbuClassAdd(note_class, Note, targetdate); + dbuClassAddObj(note_class, "resource_id", offsetof(Note, resource), resource_class); repository_class = dbuRegisterClass(ctx, "repositories", Repository, repository_id); dbuClassAdd(repository_class, Repository, name); diff --git a/application/types.h b/application/types.h index 431e7a9..9692cad 100644 --- a/application/types.h +++ b/application/types.h @@ -99,11 +99,6 @@ struct Resource { * included in a Notes query result */ bool content_loaded; - - /* - * non-db member, UI model - */ - NoteModel *model; }; struct Notebook { @@ -119,6 +114,13 @@ struct Note { int type; char *status; time_t targetdate; + + Resource *resource; + + /* + * non-db member, UI model + */ + NoteModel *model; }; typedef enum AttachmentType { diff --git a/application/window.c b/application/window.c index dfd9912..a4552e6 100644 --- a/application/window.c +++ b/application/window.c @@ -203,10 +203,11 @@ void update_sublists(UiContext *ctx, UiList *sublists) { } void* window_notelist_getvalue(void *data, int col) { - Resource *note = data; + Note *note = data; + Resource *resource = note->resource; switch(col) { case 0: { - return note->displayname ? note->displayname : note->nodename; + return resource->displayname ? resource->displayname : resource->nodename; } case 1: { //return note->lastmodified ? strdup(note->lastmodified) : NULL; @@ -370,7 +371,7 @@ static int select_note(UiEvent *event) { return 0; } - Resource *note = ui_list_get(notebook->notes, sel->rows[0]); + Note *note = ui_list_get(notebook->notes, sel->rows[0]); notebookmodel_attach_note(notebook, note); return 1; -- 2.43.5