kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 30e79688141ecfb749a735365dfe8d5f0fee0da2
parent 16c20e6b722caba35cf3c41d796f7bd316a46b59
Author: lash <dev@holbrook.no>
Date:   Thu,  2 May 2024 15:21:41 +0100

Enable ledger transport from import to entry store

Diffstat:
Msrc/gtk/kee-entry-store.c | 21+++++++++++++++++++++
Msrc/gtk/kee-entry-store.h | 3+++
Msrc/gtk/kee-import.c | 61++++++++++++++++++++++++++++++++++++++++---------------------
Msrc/gtk/ui.c | 3+++
Msrc/ledger.c | 1+
5 files changed, 68 insertions(+), 21 deletions(-)

diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c @@ -140,3 +140,24 @@ void kee_entry_store_finalize(GObject *go) { free(o->resolver.locator); free(o->last); } + +int kee_entry_store_add(KeeEntryStore *o, GVariant *v) { + int r; + struct kee_ledger_t ledger; + const char *b; + size_t c; + + c = (size_t)g_variant_n_children(v); + b = (const char*)g_variant_get_data(v); + r = kee_ledger_parse_open(&ledger, b, c); + if (r) { + return r; + } + + r = kee_ledger_put(&ledger, o->db); + if (r) { + return ERR_FAIL; + } + + return ERR_OK; +} diff --git a/src/gtk/kee-entry-store.h b/src/gtk/kee-entry-store.h @@ -3,6 +3,7 @@ #include <glib-object.h> #include "db.h" +#include "ledger.h" G_BEGIN_DECLS @@ -11,6 +12,8 @@ G_DECLARE_FINAL_TYPE(KeeEntryStore, kee_entry_store, KEE, ENTRY_STORE, GObject); KeeEntryStore* kee_entry_store_new(struct db_ctx *db); void kee_entry_store_set_resolve(KeeEntryStore *o, const char *locator); +//int kee_entry_store_add(KeeEntryStore *o, struct kee_ledger_t *ledger); +int kee_entry_store_add(KeeEntryStore *o, GVariant *v); G_END_DECLS diff --git a/src/gtk/kee-import.c b/src/gtk/kee-import.c @@ -4,7 +4,8 @@ #include "kee-import.h" #include "kee-menu.h" -#include "kee-entry-list.h" +//#include "kee-entry-list.h" +#include "kee-entry-store.h" #include "camera.h" #include "scan.h" #include "err.h" @@ -30,6 +31,7 @@ struct _KeeImport { GtkStack *stack; GtkBox *viewbox; GtkWidget *toggler_text; + GtkTextBuffer *import_content; }; G_DEFINE_TYPE(KeeImport, kee_import, GTK_TYPE_BOX); @@ -99,6 +101,7 @@ static void kee_import_init(KeeImport *o) { o->viewbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)); /// \todo remember to reset this o->cmd_accept = 0xff; + o->import_content = gtk_text_buffer_new(NULL); } static void kee_import_handle_camera_change(GtkDropDown *chooser, GParamSpec *spec, KeeImport *import) { @@ -142,28 +145,38 @@ static int check_cmd(KeeImport *o, char cmd) { } -static int import_ledger(KeeImport *o, const char *in, size_t in_len) { - int r; - struct kee_ledger_t ledger; - - r = kee_ledger_parse_open(&ledger, in, in_len); - if (r) { - return r; - } - - return ERR_OK; -} +//static int import_ledger(KeeImport *o, const char *in, size_t in_len) { +// int r; +// struct kee_ledger_t ledger; +// +// r = kee_ledger_parse_open(&ledger, in, in_len); +// if (r) { +// return r; +// } +// +// return ERR_OK; +//} -static void kee_import_handle_import_data_accept(KeeImport *o, GString *v, GtkStack *stack) { +/// \todo too long, split up +static void kee_import_handle_import_data_accept(GtkActionable *actn, void *null, KeeImport *o) { int r; char *s; size_t c; struct kee_transport_t trans; + GtkTextIter start; + GtkTextIter end; char b[1024]; + GVariant *v; + GAction *act; - s = (char*)v->str; + gtk_text_buffer_get_start_iter(o->import_content, &start); + gtk_text_buffer_get_end_iter(o->import_content, &end); + s = gtk_text_buffer_get_text(o->import_content, &start, &end, false); + + //s = (char*)v->str; - c = strlen(s) + 1; + //c = strlen(s) + 1; + c = strlen(s); //r = kee_transport_import(&trans, KEE_TRANSPORT_BASE64, s, c); r = kee_transport_single(&trans, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0); if (r) { @@ -184,7 +197,9 @@ static void kee_import_handle_import_data_accept(KeeImport *o, GString *v, GtkSt switch(*trans.cmd) { case KEE_CMD_LEDGER: - r = import_ledger(o, b, c); + v = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, b, c, 1); + act = g_action_map_lookup_action(G_ACTION_MAP(o->win), "ledger"); + g_action_activate(act, v); break; default: r = ERR_FAIL; @@ -276,12 +291,14 @@ static GtkWidget* kee_import_build_import_text(KeeImport *o, GtkStack *stack) { GtkWidget *butt; GAction *act; GtkApplication *gapp; + GtkTextBuffer *buf; box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gapp = gtk_window_get_application(GTK_WINDOW(o->win)); - txt = GTK_TEXT_VIEW(gtk_text_view_new()); + //txt = GTK_TEXT_VIEW(gtk_text_view_new()); + txt = GTK_TEXT_VIEW(gtk_text_view_new_with_buffer(o->import_content)); gtk_widget_set_vexpand(GTK_WIDGET(txt), true); gtk_box_append(GTK_BOX(box), GTK_WIDGET(txt)); @@ -293,11 +310,13 @@ static GtkWidget* kee_import_build_import_text(KeeImport *o, GtkStack *stack) { gtk_actionable_set_action_name(GTK_ACTIONABLE(butt), "app.import_data_accept"); gtk_box_append(GTK_BOX(box), butt); - g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_text), gtk_text_view_get_buffer(txt)); - g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_accept), stack); + buf = gtk_text_view_get_buffer(txt); + g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_text), buf); + //g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_accept), stack); g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_focus), stack); //g_signal_connect(o, "data_available", G_CALLBACK(kee_import_handle_import_data_check), butt); - //g_signal_connect(butt, "clicked", G_CALLBACK(kee_import_handle_import_data_check), butt); + //g_signal_connect(butt, "clicked", G_CALLBACK(kee_import_handle_import_data_accept), o); + g_signal_connect(act, "activate", G_CALLBACK(kee_import_handle_import_data_accept), o); return box; } @@ -411,7 +430,7 @@ static gboolean kee_import_scan_code_handler(GstBus *bus, GstMessage *msg, gpoin break; } strctr = gst_message_get_structure(msg); - /// \todo remove newline in result + /// \todo segfaults occasionally, find out how to persist the buffer across signal code = gst_structure_get_string(strctr, "symbol"); code_str = g_string_new(code); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "message %s: %d (%s) - decoded: %s", src, msg->type, gst_message_type_get_name(msg->type), code); diff --git a/src/gtk/ui.c b/src/gtk/ui.c @@ -86,6 +86,9 @@ void ui_build(GtkApplication *gapp, struct kee_context *ctx) { g_signal_connect (widget, "unlock", G_CALLBACK(ui_handle_unlock), win); widget = kee_entry_list_new(G_LIST_MODEL(ctx->entry_store), win); + act = g_simple_action_new("ledger", g_variant_type_new_array(G_VARIANT_TYPE_BYTE)); + g_action_map_add_action(G_ACTION_MAP(win), G_ACTION(act)); + g_signal_connect(act, "activate", G_CALLBACK(kee_entry_store_add), win); kee_menu_add(win, "view", widget); kee_menu_next(win, "view"); diff --git a/src/ledger.c b/src/ledger.c @@ -944,6 +944,7 @@ static size_t db_key(enum DbKey pfx, struct timespec *ts, char *out, size_t offs return offset + 1 + c; } +/// \todo atomic put for ledger and items!! int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { int r; size_t c;