]> uap-core.de Git - note.git/commitdiff
use new Attachment type for note attachments
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 29 Mar 2025 13:10:58 +0000 (14:10 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 29 Mar 2025 13:10:58 +0000 (14:10 +0100)
application/Makefile
application/application.h
application/attachment.c [new file with mode: 0644]
application/attachment.h [new file with mode: 0644]
application/gtk-text.c
application/types.h
application/window.c
application/window.h

index 0a94c71b9e896de55982eb4ca4ab09b4e60f0f98..0549e3e4a0f500a63ba36f84b64e45497668a8b8 100644 (file)
@@ -40,6 +40,7 @@ SRC += store_sqlite.c
 SRC += notebook.c
 SRC += note.c
 SRC += editor.c
+SRC += attachment.c
 SRC += $(APP_PLATFORM_SRC)
 
 OBJ = $(SRC:%.c=../build/application/%$(OBJ_EXT))
index 48d5e2e4a3d6b1fce0088bd4ad28cc8550353798..23ff571968b3f73450c3d86bf6450a39f50ee004 100644 (file)
@@ -158,11 +158,6 @@ struct NoteModel {
     
     bool modified;
 };
-   
-typedef enum AttachmentType {
-    NOTE_ATTACHMENT_FILE = 0,
-    NOTE_ATTACHMENT_IMAGE
-} AttachmentType;
 
 typedef struct AttachmentModel {
     UiContext *ctx;
@@ -170,13 +165,7 @@ typedef struct AttachmentModel {
     
     Resource *parent_note;
     
-    int64_t resource_id;
-    AttachmentType type;
-    char *name;
-    char *temp_path;
-    char *temp_data;
-    
-    void *data;
+    UiGeneric *img;
 } AttachmentModel;
 
 void application_init();
diff --git a/application/attachment.c b/application/attachment.c
new file mode 100644 (file)
index 0000000..3bb2f52
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 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.
+ */
+
+#include "attachment.h"
+
+Attachment* attachment_create(
+        const CxAllocator *a,
+        int64_t parent,
+        AttachmentType type,
+        const char *name)
+{
+    Attachment *attachment = cxCalloc(a, 1, sizeof(Attachment));
+    attachment->parent_resource_id = parent;
+    attachment->name = cx_strdup_a(a, cx_str(name)).ptr;
+    attachment->type = type;
+    return attachment;
+}
+
+void attachment_create_ui_model(UiContext *ctx, Attachment *attachment) {
+    if(attachment->ui) {
+        return;
+    }
+    
+    const CxAllocator *a = ui_allocator(ctx);
+    AttachmentModel *model = cxCalloc(a, 1, sizeof(AttachmentModel));
+    attachment->ui = model;
+    
+    if(attachment->type == NOTE_ATTACHMENT_IMAGE) {
+        model->img = ui_generic_new(ctx, NULL);
+    }
+    model->ctx = ctx;
+}
+
+void attachment_set_image(Attachment *attachment, void *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);
+    } else {
+        attachment->ui->img->value = img;
+        attachment->ui->img->type = UI_IMAGE_OBJECT_TYPE;
+    }
+}
+
+
+
+void attachment_item(UiObject *obj, int index, void *elm, void *userdata) {
+    Attachment *attachment = elm;
+    
+    if(attachment->type == NOTE_ATTACHMENT_IMAGE) {
+        ui_imageviewer(obj, .value = attachment->ui->img, .scrollarea = FALSE);
+    }
+}
diff --git a/application/attachment.h b/application/attachment.h
new file mode 100644 (file)
index 0000000..fcc2d5e
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 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.
+ */
+#ifndef ATTACHMENT_H
+#define ATTACHMENT_H
+
+#include "application.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+Attachment* attachment_create(
+        const CxAllocator *a,
+        int64_t parent,
+        AttachmentType type,
+        const char *name);
+
+void attachment_create_ui_model(UiContext *ctx, Attachment *attachment);
+
+void attachment_set_image(Attachment *attachment, void *img);
+
+/*
+ * create UI for an attachment item
+ */
+void attachment_item(UiObject *obj, int index, void *elm, void *userdata);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ATTACHMENT_H */
+
index dfd65b052cbf81530dc25a038e872b1c123d1ae6..379456588ae3a0c9bde26945695e24df290a93b1 100644 (file)
@@ -29,6 +29,7 @@
 #include "gtk-text.h"
 #include "editor.h"
 #include "note.h"
+#include "attachment.h"
 
 #include <cx/buffer.h>
 #include <cx/array_list.h>
@@ -268,6 +269,17 @@ void md_serialize_image(EmbeddedWidget *em, CxBuffer *out) {
 }
 
 static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *attachment_path) {
+    MainWindow *wdata = editor->obj->window;
+    Resource *note = wdata->current_notebook->current_note;
+    NoteModel *model = note->model;
+    
+    // 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);
+    g_object_ref(pixbuf);
+    attachment_set_image(attachment, pixbuf);
+    
+    // insert image
     GtkTextView *textview = GTK_TEXT_VIEW(editor->textview);
     GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
     
@@ -284,14 +296,14 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att
     gtk_widget_set_size_request(image, width, height);
     
     gtk_text_view_add_child_at_anchor(textview, image, anchor);
-    
+      
     // remember widget and store a reference in the textbuffer
     // TODO: we need a cleanup strategy
     EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget));
     em->widget = image;
     em->anchor = anchor;
-    em->name = attachment_path ? strdup(util_resource_name(attachment_path)) : NULL;
-    em->path = attachment_path;
+    //em->name = attachment_path ? strdup(util_resource_name(attachment_path)) : NULL;
+    //em->path = attachment_path;
     em->data1 = pixbuf;
     em->data2 = NULL;
     em->serialize = md_serialize_image;
@@ -304,12 +316,7 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att
     cxListAdd(embedded_objects->objects, em);
     
     // add attachment to note
-    MainWindow *wdata = editor->obj->window;
-    Resource *note = wdata->current_notebook->current_note;
-    NoteModel *model = note->model;
-    
-    // TODO: this is just a test
-    ui_list_append(model->attachments, em);
+    ui_list_append(model->attachments, attachment);
     ui_list_update(model->attachments);
 }
 
index 3946d732d5b8d239f02ed7bc82bb01f827787ba5..dc5eefaecf0a3823a3031ea051267b624575340d 100644 (file)
@@ -37,8 +37,9 @@ extern "C" {
 #endif
     
     
-typedef struct NotebookModel NotebookModel;
-typedef struct NoteModel     NoteModel;
+typedef struct NotebookModel   NotebookModel;
+typedef struct NoteModel       NoteModel;
+typedef struct AttachmentModel AttachmentModel;
     
 
 typedef struct UserSettings UserSettings;
@@ -104,17 +105,25 @@ struct Resource {
     NoteModel  *model;
 };
 
+typedef enum AttachmentType {
+    NOTE_ATTACHMENT_FILE = 0,
+    NOTE_ATTACHMENT_IMAGE
+} AttachmentType;
+
 struct Attachment {
-    // db fields
-    
+    // db
     int64_t    attachment_id;
     int64_t    attachment_resource_id;
     int64_t    parent_resource_id;
     int        type;
     
-    // temp fields
+    // temp (from table resources)
     char       *name;
     cxmutstr   bin_content;
+    bool       content_loaded;
+    
+    // ui
+    AttachmentModel *ui;
 };
 
 extern DBUClass *usersettings_class;
index 440a48041e68c827464ecefb8fd0052d389f7cca..85333e1cf39c331e3cb1367c26f10ba228d0544c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2024 Olaf Wintermann. All rights reserved.
+ * Copyright 2025 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:
@@ -33,6 +33,7 @@
 #include "editor.h"
 #include "note.h"
 #include "menu.h"
+#include "attachment.h"
 
 #include <cx/array_list.h>
 #include <cx/hash_map.h>
@@ -104,10 +105,6 @@ void window_create() {
     ui_show(obj);
 }
 
-void attachment_item(UiObject *obj, int index, void *elm, void *userdata) {
-    ui_button(obj, .label = "Attachment Dummy");
-}
-
 MainWindow* window_init_data(UiObject *obj) {
     MainWindow *wdata = ui_calloc(obj->ctx, 1, sizeof(MainWindow));
     obj->window = wdata;
index 7959b5b082c9f35bf75a2cbd30b34641ee7e95e8..1e32b55077db4d3c6435a3093bd8c1914f75e02f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2024 Olaf Wintermann. All rights reserved.
+ * Copyright 2025 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:
@@ -52,8 +52,6 @@ void update_sublists(UiContext *ctx, UiList *sublists);
 
 void* window_notelist_getvalue(void *data, int col);
 
-void attachment_item(UiObject *obj, int index, void *elm, void *userdata);
-
 void action_notebook_selected(UiEvent *event, void *userdata);
 void action_note_selected(UiEvent *event, void *userdata);
 void action_note_activated(UiEvent *event, void *userdata);