]> uap-core.de Git - note.git/commitdiff
store attachment reference in resources/notes, prepare for attachment_save
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 6 Apr 2025 20:37:32 +0000 (22:37 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 6 Apr 2025 20:37:32 +0000 (22:37 +0200)
application/attachment.c
application/attachment.h
application/gtk-text.c
application/note.c
application/note.h
application/types.h

index a64c6fce326f62a1387e4bcccea457e87297120e..edacc1e6df10d1040c86669bdbf2c1fe9cf6c259 100644 (file)
@@ -31,6 +31,8 @@
 #include "note.h"
 
 #include <cx/printf.h>
+#include <cx/buffer.h>
+#include <cx/streams.h>
 
 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
index ea6ba615be5a4f40b20e90fec6d87ef4d890a085..36c09055fd7f975fb0f34d9010867b35c78cc985 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "application.h"
 
+#include <cx/string.h>
+
 #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
  */
index 07118002b6db17227d11ae9798cf2c4bf305a041..471d8837de6124306953fab890b561d96f411711 100644 (file)
@@ -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);
 }
index e8306e4a5b4c353acbc569ddd12f7a7cfc41c14f..1b2f9a4ad9ce6ef6793f5d7883ccf3bd314b82f2 100644 (file)
@@ -32,6 +32,8 @@
 #include "editor.h"
 #include "notebook.h"
 
+#include <cx/array_list.h>
+
 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) {
index b3b559b06914be2242e4a141dd36b08bfecf8efe..04524f47a0dbaa7dcb90004186480ebfaba10ac7 100644 (file)
@@ -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);
index dc5eefaecf0a3823a3031ea051267b624575340d..5214794e6d70aab267f31079d253d7b81589575a 100644 (file)
@@ -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;
 };