]> uap-core.de Git - note.git/commitdiff
fix attachment image loading and pixbuf reference counting
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 08:57:11 +0000 (10:57 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 08:57:11 +0000 (10:57 +0200)
application/attachment.c
application/attachment.h
application/gtk-text.c
application/note.c
ui/gtk/image.c

index 85b39ff2afc8711657c4a081b0dad1ab9f61eef6..2827c225c27ebf72bb332c775ec6161d10dd0697 100644 (file)
@@ -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;
 }
index 755d944169956382417018d7851d849a5e5fb1f6..377628626aba53c8fc7c7edf8297a9dbfa4dd495 100644 (file)
@@ -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);
index 875fdbdb9965549ba58817d06c0a67fd2dc27b59..070419c263a42960e0d719b2ea3991438adfd74b 100644 (file)
@@ -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 {
index b06f0dd2fba2d3c7e855ff35fdfc4824e530b12b..91902345ca82d27fc82373f8a7244eec19c3b827 100644 (file)
@@ -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;
index 8ce1b4bdce5cc95029be51b47ce7e7cbeee49799..ea2f6bbaa7d988500c45a8d0dd218b31f4c43024 100644 (file)
@@ -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;