From: Olaf Wintermann Date: Sat, 29 Mar 2025 13:10:58 +0000 (+0100) Subject: use new Attachment type for note attachments X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=02802e7d5a397c51fcd4a34c82450ff2b73fb95d;p=note.git use new Attachment type for note attachments --- diff --git a/application/Makefile b/application/Makefile index 0a94c71..0549e3e 100644 --- a/application/Makefile +++ b/application/Makefile @@ -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)) diff --git a/application/application.h b/application/application.h index 48d5e2e..23ff571 100644 --- a/application/application.h +++ b/application/application.h @@ -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 index 0000000..3bb2f52 --- /dev/null +++ b/application/attachment.c @@ -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 index 0000000..fcc2d5e --- /dev/null +++ b/application/attachment.h @@ -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 */ + diff --git a/application/gtk-text.c b/application/gtk-text.c index dfd65b0..3794565 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -29,6 +29,7 @@ #include "gtk-text.h" #include "editor.h" #include "note.h" +#include "attachment.h" #include #include @@ -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); } diff --git a/application/types.h b/application/types.h index 3946d73..dc5eefa 100644 --- a/application/types.h +++ b/application/types.h @@ -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; diff --git a/application/window.c b/application/window.c index 440a480..85333e1 100644 --- a/application/window.c +++ b/application/window.c @@ -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 #include @@ -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; diff --git a/application/window.h b/application/window.h index 7959b5b..1e32b55 100644 --- a/application/window.h +++ b/application/window.h @@ -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);