]> uap-core.de Git - note.git/commitdiff
change attachments list type main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 28 Aug 2026 18:45:03 +0000 (20:45 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 28 Aug 2026 18:45:03 +0000 (20:45 +0200)
application/note/src/attachment.rs [new file with mode: 0644]
application/note/src/main.rs
application/note/src/note.rs
application/note/src/window.rs
ui-rs/src/ui/image.rs
ui/gtk/image.c
ui/ui/image.h

diff --git a/application/note/src/attachment.rs b/application/note/src/attachment.rs
new file mode 100644 (file)
index 0000000..b58dad4
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+pub struct Attachment {
+    pub attachment: entity::attachment::Model,
+    //pub content: entity::attachmentcontent::Model,
+}
+
+impl Attachment {
+    pub fn new(model: &entity::attachment::Model) -> Attachment {
+        Attachment {
+            attachment: model.clone(),
+        }
+    }
+}
\ No newline at end of file
index d820e7306334d14ac630d7155e7feb0a38434eb5..d8ebbb13691e4d82fb6e4aa1443996e6b945174a 100644 (file)
@@ -31,6 +31,7 @@ mod newnotebook;
 mod notebook;
 mod note;
 mod file;
+mod attachment;
 
 use std::env;
 use backend::backend::{Backend, BackendHandle, BroadcastMessage};
index 900cc17dfdc2dfd66ece7f4222a5f4eb8e7ca28c..87acdc7cf1e0ad784fd682b59cd70c48dcff5527 100644 (file)
@@ -25,6 +25,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+
 use std::path::{Path, PathBuf};
 use std::rc::Rc;
 use std::sync::atomic::{AtomicU64, Ordering};
@@ -39,6 +40,7 @@ use ui_rs::ui::*;
 use crate::AppStates;
 use crate::window::NoteTypeTabView;
 use notify::{Event};
+use crate::attachment::Attachment;
 
 static TMP_ID: AtomicU64 = AtomicU64::new(1);
 
@@ -88,7 +90,7 @@ pub struct Note {
     pub info: UiString,
 
     #[bind("note_attachments")]
-    pub attachments: UiList<entity::attachment::Model>
+    pub attachments: UiList<Attachment>
 }
 
 #[ui_actions]
@@ -284,7 +286,7 @@ impl Note {
                     Ok(attachments) => {
                         note.attachments.data_mut().clear();
                         if attachments.len() > 0 {
-                            note.attachments.data_mut().extend(attachments);
+                            note.attachments.data_mut().extend(attachments.iter().map(|a| Attachment::new(a) ));
                             doc.ctx.set_state(AppStates::NoteHasAttachments);
                         } else {
                             doc.ctx.unset_state(AppStates::NoteHasAttachments);
index 82deac64038dbf519c00461e7b53cd09553468ef..06491b565266462de1e3d1bcb07ca4f98efc9fcc 100644 (file)
@@ -33,6 +33,7 @@ use ui_rs::ui::*;
 use crate::{App, AppStates};
 use entity::collection::{CollectionType, Model as Collection, Node};
 use ui_rs::ui::widget::Widget;
+use crate::attachment::Attachment;
 use crate::newnotebook::new_notebook_dialog;
 use crate::notebook::{notelist_getvalue, Notebook, NotebookItem};
 
@@ -288,8 +289,12 @@ pub fn create_window(app: &App, ctx: &AppContext<MainWindow>) -> UiObject<MainWi
                     });
                     hbox!(obj, visibility_states = &[AppStates::NoteHasAttachments], margin = 10, spacing = 4, |obj|{
                         let mut w = scrolledwindow!(obj, |obj|{
-                            obj.itemlist::<entity::attachment::Model>()
+                            obj.itemlist::<Attachment>()
                                 .varname("note_attachments")
+                                .createui(|obj, data, _i|{
+                                    let _imgviewer = obj.imageviewer().create();
+                                    // TODO: set img from attachment preview
+                                })
                                 .create();
                         });
                         w.set_size(-1, 120);
index a97e546b62243182063c91e610f7566b7761c488..35fe5345348dcb6432eb07a060e64e903378545f 100644 (file)
@@ -96,6 +96,45 @@ impl Widget for ImageViewer {
     }
 }
 
+impl ImageViewer {
+    pub fn reset(&self) {
+        unsafe {
+            ui_imageviewer_reset(self.ptr);
+        }
+    }
+
+    pub fn set_autoscale(&self, set: bool) {
+        unsafe {
+            ui_imageviewer_set_autoscale(self.ptr, set as c_int);
+        }
+    }
+
+    pub fn set_adjustwidgetsize(&self, set: bool) {
+        unsafe {
+            ui_imageviewer_set_adjustwidgetsize(self.ptr, set as c_int);
+        }
+    }
+
+    pub fn set_useradjustable(&self, set: bool) {
+        unsafe {
+            ui_imageviewer_set_useradjustable(self.ptr, set as c_int);
+        }
+    }
+
+    pub fn load_file(&self, path: &Path) {
+        let path = CString::new(path.to_string_lossy().as_bytes()).expect("path contains an embedded NUL byte");
+        unsafe {
+            ui_imageviewer_load_file(self.ptr, path.as_ptr() as *const c_char);
+        }
+    }
+
+    pub fn load_data(&self, data: &Vec<u8>) {
+        unsafe {
+            ui_imageviewer_load_data(self.ptr, data.as_ptr() as *const c_void, data.len() as libc::size_t);
+        }
+    }
+}
+
 pub struct ImageViewerBuilder<'a, T> {
     args: *mut UiImageViewerArgs,
     obj: &'a mut toolkit::UiObject<T>,
@@ -398,4 +437,6 @@ unsafe extern "C" {
     fn ui_imageviewer_set_autoscale(widget: *mut c_void, set: c_int);
     fn ui_imageviewer_set_adjustwidgetsize(widget: *mut c_void, set: c_int);
     fn ui_imageviewer_set_useradjustable(widget: *mut c_void, set: c_int);
+    fn ui_imageviewer_load_file(widget: *mut c_void, path: *const c_char);
+    fn ui_imageviewer_load_data(widget: *mut c_void, imgdata: *const c_void, size: libc::size_t);
 }
\ No newline at end of file
index b323cd051214f482b88918284ad2b64459d46f26..5fcb048d2ce451b40539c8ddcce691843bad9f88 100644 (file)
@@ -238,6 +238,48 @@ void ui_imageviewer_set_useradjustable(UIWIDGET w, UiBool set) {
     }
 }
 
+static void imageviewer_set(UiImageViewer *imgviewer, GdkPixbuf *pixbuf) {
+    if(imgviewer->pixbuf) {
+        g_object_unref(imgviewer->pixbuf);
+    }
+    
+    g_object_ref(pixbuf);
+    imgviewer->pixbuf = pixbuf;
+    
+    imageviewer_reset(imgviewer);
+    if(imgviewer->adjustwidgetsize && !imgviewer->autoscale) {
+        int width = gdk_pixbuf_get_width(pixbuf);
+        int height = gdk_pixbuf_get_height(pixbuf);
+        gtk_widget_set_size_request(imgviewer->widget, width, height);
+    }
+    gtk_widget_queue_draw(imgviewer->widget);
+}
+
+void ui_imageviewer_load_file(UIWIDGET w, const char *path) {
+    UiImageViewer *imgviewer = g_object_get_data(G_OBJECT(w), "uiimageviewer");
+    if(imgviewer) {
+        GError *error = NULL;
+        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
+        if(pixbuf) {
+            imageviewer_set(imgviewer, pixbuf);
+        }
+    }
+}
+
+void ui_imageviewer_load_data(UIWIDGET w, const void *imgdata, size_t size) {
+    UiImageViewer *imgviewer = g_object_get_data(G_OBJECT(w), "uiimageviewer");
+    if(imgviewer) {
+        GBytes *bytes = g_bytes_new_static(imgdata, size);
+        GInputStream *in = g_memory_input_stream_new_from_bytes(bytes);
+        GError *error = NULL;
+        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(in, NULL, &error);
+        g_object_unref(in);
+        if(pixbuf) {
+            imageviewer_set(imgviewer, pixbuf);
+        }
+    }
+}
+
 void ui_cairo_draw_image(UiImageViewer *imgviewer, cairo_t *cr, int width, int height) {
     if(!imgviewer->pixbuf) {
         return;
@@ -306,24 +348,9 @@ int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
     }
     
     GdkPixbuf *pixbuf = value;
-    g_object_ref(pixbuf);
-    
     UiImageViewer *imgviewer = g->obj;
     g->value = pixbuf;
-    
-    imageviewer_reset(imgviewer);
-    
-    if(imgviewer->pixbuf) {
-        g_object_unref(imgviewer->pixbuf);
-    }
-    imgviewer->pixbuf = pixbuf;
-    
-    if(imgviewer->adjustwidgetsize && !imgviewer->autoscale) {
-        int width = gdk_pixbuf_get_width(pixbuf);
-        int height = gdk_pixbuf_get_height(pixbuf);
-        gtk_widget_set_size_request(imgviewer->widget, width, height);
-    }
-    gtk_widget_queue_draw(imgviewer->widget);
+    imageviewer_set(imgviewer, pixbuf);
     
     return 0;
 }
index f3498fab98e0b2e48028c243332f936e6d837ca7..e4eee59b2461cc8466573e1401e6f96767c5f30c 100644 (file)
@@ -92,6 +92,8 @@ UIEXPORT void ui_imageviewer_reset(UIWIDGET w);
 UIEXPORT void ui_imageviewer_set_autoscale(UIWIDGET w, UiBool set);
 UIEXPORT void ui_imageviewer_set_adjustwidgetsize(UIWIDGET w, UiBool set);
 UIEXPORT void ui_imageviewer_set_useradjustable(UIWIDGET w, UiBool set);
+UIEXPORT void ui_imageviewer_load_file(UIWIDGET w, const char *path);
+UIEXPORT void ui_imageviewer_load_data(UIWIDGET w, const void *imgdata, size_t size);
 
 UIEXPORT int ui_image_load_file(UiGeneric *obj, const char *path);
 UIEXPORT int ui_image_load_data(UiGeneric *obj, const void *imgdata, size_t size);