From 8fea6d04ff0980f99acfe1d13f0244946f63649d Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 20 Apr 2025 10:57:11 +0200 Subject: [PATCH] fix attachment image loading and pixbuf reference counting --- application/attachment.c | 10 ++++++++++ application/attachment.h | 2 ++ application/gtk-text.c | 4 +++- application/note.c | 10 ++++++++++ ui/gtk/image.c | 2 ++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/application/attachment.c b/application/attachment.c index 85b39ff..2827c22 100644 --- a/application/attachment.c +++ b/application/attachment.c @@ -72,6 +72,7 @@ void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource * img: toolkit image object (for example for gtk this would be GdkPixbuf*) */ void attachment_set_image(Attachment *attachment, void *img) { + ui_image_ref(img); // TODO: replace this with new toolkit function for setting UiGeneric values if(attachment->ui->img->set) { attachment->ui->img->set(attachment->ui->img, img, UI_IMAGE_OBJECT_TYPE); @@ -81,6 +82,15 @@ void attachment_set_image(Attachment *attachment, void *img) { } } +int attachment_set_image_from_data(Attachment *attachment, cxmutstr data) { + int ret = ui_image_load_data(attachment->ui->img, data.ptr, data.length); + if(!ret) { + // we need an extra reference to the image + ui_image_ref(attachment->ui->img->value); + } + return ret; +} + void attachment_set_data(Attachment *attachment, cxmutstr data) { attachment->bin_content = data; } diff --git a/application/attachment.h b/application/attachment.h index 755d944..3776286 100644 --- a/application/attachment.h +++ b/application/attachment.h @@ -46,6 +46,8 @@ void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource void attachment_set_image(Attachment *attachment, void *img); +int attachment_set_image_from_data(Attachment *attachment, cxmutstr data); + void attachment_set_data(Attachment *attachment, cxmutstr data); void attachment_save(UiObject *obj, Attachment *attachment, bool cleanup_content); diff --git a/application/gtk-text.c b/application/gtk-text.c index 875fdbd..070419c 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -302,7 +302,6 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att // create attachment Attachment *attachment = attachment_create(model->note_allocator, note->resource_id, NOTE_ATTACHMENT_IMAGE, util_resource_name(attachment_path)); attachment_create_ui_model(model->ctx, attachment, note); - g_object_ref(pixbuf); attachment_set_image(attachment, pixbuf); if(attachment_path) { @@ -742,6 +741,9 @@ void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList em->serialize = md_serialize_image; g_object_ref(image); g_object_ref(anchor); + + BufferEmbeddedObjects *embedded_objects = g_object_get_data(G_OBJECT(buffer), "embedded"); + cxListAdd(embedded_objects->objects, em); g_object_set_data(G_OBJECT(anchor), "em", em); } else { diff --git a/application/note.c b/application/note.c index b06f0dd..9190234 100644 --- a/application/note.c +++ b/application/note.c @@ -113,6 +113,16 @@ static void note_loading_completed(UiObject *obj, LoadNoteContent *op) { free(op); if(note->model) { + // fill attachment model + CxIterator i = cxListIterator(note->attachments); + cx_foreach(Attachment *, attachment, i) { + ui_list_append(note->model->attachments, attachment); + if(attachment_set_image_from_data(attachment, attachment->bin_content)) { + fprintf(stderr, "Error: cannot open attachment image data\n"); + } + } + ui_list_update(note->model->attachments); + editor_load_markdown(note, wdata->textview, note->content); } note->content_loaded = TRUE; diff --git a/ui/gtk/image.c b/ui/gtk/image.c index 8ce1b4b..ea2f6bb 100644 --- a/ui/gtk/image.c +++ b/ui/gtk/image.c @@ -336,6 +336,7 @@ int ui_image_load_file(UiGeneric *obj, const char *path) { g_object_unref(pixbuf); } else { obj->value = pixbuf; + obj->type = UI_IMAGE_OBJECT_TYPE; } return 0; @@ -356,6 +357,7 @@ UIEXPORT int ui_image_load_data(UiGeneric *obj, const void *imgdata, size_t size g_object_unref(pixbuf); } else { obj->value = pixbuf; + obj->type = UI_IMAGE_OBJECT_TYPE; } return 0; -- 2.43.5