From 8364a318c890c1f025593c2994dea604f4bd14a4 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 6 Apr 2025 22:37:32 +0200 Subject: [PATCH] store attachment reference in resources/notes, prepare for attachment_save --- application/attachment.c | 26 ++++++++++++++++++++++++++ application/attachment.h | 9 +++++++++ application/gtk-text.c | 8 ++++++++ application/note.c | 16 ++++++++++++++++ application/note.h | 2 ++ application/types.h | 13 ++++++++++++- 6 files changed, 73 insertions(+), 1 deletion(-) diff --git a/application/attachment.c b/application/attachment.c index a64c6fc..edacc1e 100644 --- a/application/attachment.c +++ b/application/attachment.c @@ -31,6 +31,8 @@ #include "note.h" #include +#include +#include Attachment* attachment_create( const CxAllocator *a, @@ -77,6 +79,30 @@ void attachment_set_image(Attachment *attachment, void *img) { } } +void attachment_set_data(Attachment *attachment, cxmutstr data) { + attachment->bin_content = data; +} + +void attachment_save(UiObject *obj, Attachment *attachment) { + +} + + +cxmutstr attachment_file_load(const CxAllocator *a, const char *path) { + FILE *f = fopen(path, "rb"); + if(!f) { + return (cxmutstr){NULL, 0}; + } + + CxBuffer buf; + cxBufferInit(&buf, NULL, 1024*128, a, CX_BUFFER_FREE_CONTENTS); + + cx_stream_copy(f, &buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); + fclose(f); + + return cx_mutstrn(buf.space, buf.size); +} + /* * itemlist create_ui function diff --git a/application/attachment.h b/application/attachment.h index ea6ba61..36c0905 100644 --- a/application/attachment.h +++ b/application/attachment.h @@ -30,6 +30,8 @@ #include "application.h" +#include + #ifdef __cplusplus extern "C" { #endif @@ -44,6 +46,13 @@ void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource void attachment_set_image(Attachment *attachment, void *img); +void attachment_set_data(Attachment *attachment, cxmutstr data); + +void attachment_save(UiObject *obj, Attachment *attachment); + +// TODO: move this function to a separate file +cxmutstr attachment_file_load(const CxAllocator *a, const char *path); + /* * create UI for an attachment item */ diff --git a/application/gtk-text.c b/application/gtk-text.c index 0711800..471d883 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -305,6 +305,13 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att g_object_ref(pixbuf); attachment_set_image(attachment, pixbuf); + if(attachment_path) { + cxmutstr data = attachment_file_load(model->note_allocator, attachment_path); + if(data.ptr) { + attachment_set_data(attachment, data); + } + } + // insert image GtkTextView *textview = GTK_TEXT_VIEW(editor->textview); GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview); @@ -337,6 +344,7 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att cxListAdd(embedded_objects->objects, em); // add attachment to note + note_add_attachment(note, attachment); ui_list_append(model->attachments, attachment); ui_list_update(model->attachments); } diff --git a/application/note.c b/application/note.c index e8306e4..1b2f9a4 100644 --- a/application/note.c +++ b/application/note.c @@ -32,6 +32,8 @@ #include "editor.h" #include "notebook.h" +#include + static TextNoteParagraphStyles paragraph_styles[] = { { "Paragraph", EDITOR_STYLE_PARAGRAPH }, { "Code", EDITOR_STYLE_CODE_BLOCK }, @@ -113,6 +115,13 @@ void note_load_content(UiObject *obj, NotebookModel *notebook, Resource *note) { note_store_get_note_content_async(obj, notebook->current_notes_pool->allocator, note->resource_id, note_content_loaded, note); } +void note_add_attachment(Resource *note, Attachment *attachment) { + if(!note->attachments) { + note->attachments = cxArrayListCreate(note->model->note_allocator, NULL, CX_STORE_POINTERS, 8); + } + cxListAdd(note->attachments, attachment); +} + void note_save(UiObject *obj, NotebookModel *notebook, Resource *note) { NoteModel *m = note->model; @@ -140,6 +149,13 @@ void note_save(UiObject *obj, NotebookModel *notebook, Resource *note) { } else { note_store_save_note_async(obj, note, NULL, NULL); } + + if(note->attachments) { + CxIterator i = cxListIterator(note->attachments); + cx_foreach(Attachment *, attachment, i) { + attachment_save(obj, attachment); + } + } } void note_update_current_style(NoteModel *note, MDActiveStyles *style) { diff --git a/application/note.h b/application/note.h index b3b559b..04524f4 100644 --- a/application/note.h +++ b/application/note.h @@ -49,6 +49,8 @@ void notemodel_set_note(NoteModel *model, Resource *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_add_attachment(Resource *note, Attachment *attachment); + void note_save(UiObject *obj, NotebookModel *notebook, Resource *note); void note_update_current_style(NoteModel *note, MDActiveStyles *style); diff --git a/application/types.h b/application/types.h index dc5eefa..5214794 100644 --- a/application/types.h +++ b/application/types.h @@ -92,13 +92,18 @@ struct Resource { cxmutstr bin_content; char *created_by; + /* + * type: Attachment* + */ + CxList *attachments; + /* * indicates, whether content or bin_content was queried * content_loaded is not a column in the Notes table, however it can be * included in a Notes query result */ bool content_loaded; - + /* * non-db member, UI model */ @@ -122,6 +127,12 @@ struct Attachment { cxmutstr bin_content; bool content_loaded; + // is the attachment content already stored persistently + bool content_saved; + + // is the attachment persistently connected to a note + bool saved; + // ui AttachmentModel *ui; }; -- 2.43.5