kee

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

commit d10488b4fc62f6e6454edfe884df53035b45d9fa
parent ca8ee87db35ce01f48d7f2d5b6edef090222de22
Author: lash <dev@holbrook.no>
Date:   Fri,  3 May 2024 09:05:54 +0100

Move ledger import action handler to window object

Diffstat:
Msrc/gtk/context.h | 2+-
Msrc/gtk/kee-entry-item.c | 14+++++++-------
Msrc/gtk/kee-entry-store.c | 39+--------------------------------------
Msrc/gtk/kee-entry-store.h | 4++--
Msrc/gtk/kee-entry.c | 52+++++++++++++++++++++++++++++++++++++++++++---------
Msrc/gtk/kee-entry.h | 3+++
Msrc/gtk/kee-menu.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/gtk/kee-menu.h | 2++
Msrc/gtk/ui.c | 8+-------
9 files changed, 122 insertions(+), 64 deletions(-)

diff --git a/src/gtk/context.h b/src/gtk/context.h @@ -1,11 +1,11 @@ #ifndef _KEE_CONTEXT #define _KEE_CONTEXT +#include "kee-entry-store.h" #include "settings.h" #include "db.h" #include "camera.h" #include "gpg.h" -#include "kee-entry-store.h" struct kee_context { diff --git a/src/gtk/kee-entry-item.c b/src/gtk/kee-entry-item.c @@ -128,10 +128,10 @@ void kee_entry_item_apply_edit_widget(GtkBox *box, struct kee_entry_item_form_t } -KeeEntryItem* kee_entry_item_import(const char *data, size_t data_len) { - KeeEntryItem *item; - - item = g_object_new(KEE_TYPE_ENTRY_ITEM, "orientation", GTK_ORIENTATION_VERTICAL, NULL); - - return item; -} +//KeeEntryItem* kee_entry_item_import(const char *data, size_t data_len) { +// KeeEntryItem *item; +// +// item = g_object_new(KEE_TYPE_ENTRY_ITEM, "orientation", GTK_ORIENTATION_VERTICAL, NULL); +// +// return item; +//} diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c @@ -6,6 +6,7 @@ #include "kee-entry-store.h" #include "kee-entry.h" +#include "kee-menu.h" // remove when add handler is removed #include "err.h" #include "cadiz.h" #include "ledger.h" @@ -141,41 +142,3 @@ void kee_entry_store_finalize(GObject *go) { free(o->last); } -int kee_entry_store_add(KeeEntryStore *o, GVariant *v) { - int r; - struct kee_ledger_t ledger; - struct kee_ledger_item_t *item; - const char *b; - enum kee_ledger_state_e item_state; - 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; - } - - item = ledger.last_item; - item_state = kee_ledger_item_state(item); - - switch (item_state) { - case KEE_LEDGER_STATE_FINAL: - r = kee_ledger_put(&ledger, o->db); - if (r) { - return ERR_FAIL; - } - break; - case KEE_LEDGER_STATE_RESPONSE: - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "detected response state"); - break; - case KEE_LEDGER_STATE_REQUEST: - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "detected request state, ignoring"); - break; - default: - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "invalid item state after parse"); - return ERR_FAIL; - } - - return ERR_OK; -} diff --git a/src/gtk/kee-entry-store.h b/src/gtk/kee-entry-store.h @@ -2,6 +2,8 @@ #define _GTK_KEE_ENTRY_STORE_H #include <glib-object.h> +#include <gio/gio.h> + #include "db.h" #include "ledger.h" @@ -12,8 +14,6 @@ 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-entry.c b/src/gtk/kee-entry.c @@ -56,6 +56,7 @@ struct _KeeEntry { GtkWidget parent; GtkWidget *display; GtkWidget *edit; + GtkWidget *sign; GtkWidget *entry_list; GtkWidget *showing; int state; @@ -301,7 +302,7 @@ static void kee_entry_setup_edit_widget(KeeEntry *o) { gtk_box_append(GTK_BOX(o->edit), widget); g_signal_connect (widget, "clicked", G_CALLBACK(kee_entry_handle_add), o); } - + static int kee_entry_apply_edit_widget(KeeEntry *o) { kee_entry_setup_edit_widget(o); o->showing = o->edit; @@ -322,6 +323,10 @@ int kee_entry_modeswitch(KeeEntry *o, enum kee_entry_viewmode_e mode) { case KEE_ENTRY_VIEWMODE_EDIT: r = kee_entry_apply_edit_widget(o); break; + case KEE_ENTRY_VIEWMODE_SIGN: + r = kee_entry_apply_display_widget(o); + break; + default: r = kee_entry_apply_display_widget(o); } @@ -363,7 +368,7 @@ static void kee_entry_init_list_widget(KeeEntry *o) { } -int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { +static int process_entry_ledger(KeeEntry *o) { int r; size_t key_len; size_t last_value_length; @@ -375,10 +380,6 @@ int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { key_len = 33; last_value_length = 1024; - r = kee_ledger_parse(&o->ledger, data, data_len); - if (r) { - return ERR_FAIL; - } kee_content_resolve(&o->ledger.content, o->resolver); last_value_length = 2048; @@ -407,15 +408,48 @@ int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { return ERR_FAIL; } } + + + o->state = ENTRYSTATE_LOAD; + + kee_entry_init_list_widget(o); + + return ERR_OK; +} + +/// \todo rename "from" to indicate not return new entry but apply on existing +int kee_entry_from_ledger(KeeEntry *o, struct kee_ledger_t *ledger) { + int r; + + memcpy(&o->ledger, ledger, sizeof(struct kee_ledger_t)); + r = process_entry_ledger(o); + if (r) { + return ERR_FAIL; + } + + memcpy(o->current_id, ledger->digest, DIGEST_LENGTH); + + return ERR_OK; +} + +int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { + int r; + + + r = kee_ledger_parse(&o->ledger, data, data_len); + if (r) { + return ERR_FAIL; + } r = calculate_digest_algo(data, data_len, o->current_id, GCRY_MD_SHA512); if (r) { return ERR_DIGESTFAIL; } - o->state = ENTRYSTATE_LOAD; - - kee_entry_init_list_widget(o); + r = process_entry_ledger(o); + if (r) { + return ERR_FAIL; + } return ERR_OK; } diff --git a/src/gtk/kee-entry.h b/src/gtk/kee-entry.h @@ -7,6 +7,7 @@ #include "db.h" #include "cadiz.h" #include "gpg.h" +#include "ledger.h" G_BEGIN_DECLS @@ -26,6 +27,7 @@ enum kee_entry_viewmode_e { KEE_ENTRY_VIEWMODE_SHORT, KEE_ENTRY_VIEWMODE_FULL, KEE_ENTRY_VIEWMODE_EDIT, + KEE_ENTRY_VIEWMODE_SIGN, }; #define KEE_TYPE_ENTRY kee_entry_get_type() @@ -37,6 +39,7 @@ KeeEntry* kee_entry_new(struct db_ctx *db); void kee_entry_set_resolver(KeeEntry *o, struct Cadiz *resolver); void kee_entry_set_signer(KeeEntry *o, struct gpg_store *gpg); int kee_entry_modeswitch(KeeEntry *o, enum kee_entry_viewmode_e); +int kee_entry_from_ledger(KeeEntry *o, struct kee_ledger_t *ledger); G_END_DECLS diff --git a/src/gtk/kee-menu.c b/src/gtk/kee-menu.c @@ -48,6 +48,64 @@ static void kee_menu_act_new_entry(GAction *act, GVariant *param, KeeMenu *menu) kee_entry_modeswitch(o, KEE_ENTRY_VIEWMODE_EDIT); } +static void kee_menu_act_import_entry(GAction *act, GVariant *param, KeeMenu *menu) { + int r; + struct kee_ledger_t ledger; + const char *b; + enum kee_ledger_state_e item_state; + size_t c; + KeeEntry *entry; + + c = (size_t)g_variant_n_children(param); + b = (const char*)g_variant_get_data(param); + r = kee_ledger_parse_open(&ledger, b, c); + if (r) { + return; + } + + item_state = kee_ledger_item_state(ledger.last_item); + + switch (item_state) { + case KEE_LEDGER_STATE_FINAL: + r = kee_ledger_put(&ledger, &menu->ctx->db); + if (r) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail put entry"); + return; + } + break; + case KEE_LEDGER_STATE_RESPONSE: + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "detected response state"); + entry = kee_entry_new(&menu->ctx->db); + if (entry == NULL) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail create entry"); + return; + } + r = kee_entry_from_ledger(entry, &ledger); + if (r) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail load entry from ledger"); + return; + } + r = kee_entry_modeswitch(entry, KEE_ENTRY_VIEWMODE_SIGN); + if (r) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail set entry widget view mode"); + return; + } + r = kee_menu_set(menu, GTK_WIDGET(entry)); + if (r) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail replace menu entry content"); + return; + } + kee_menu_next(menu, "entry"); + break; + case KEE_LEDGER_STATE_REQUEST: + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "detected request state, ignoring"); + break; + default: + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "invalid item state after parse"); + return; + } +} + //static GParamSpec *kee_props[KEE_N_MENU_PROPS] = {NULL,}; //static guint kee_sigs[KEE_N_MENU_SIGS] = {0,}; @@ -104,6 +162,10 @@ KeeMenu* kee_menu_new(GtkApplication *gapp, struct kee_context *ctx) { gtk_actionable_set_action_name(GTK_ACTIONABLE(butt), "win.new_entry"); g_signal_connect(act, "activate", G_CALLBACK(kee_menu_act_new_entry), o); + act = g_simple_action_new("ledger", g_variant_type_new_array(G_VARIANT_TYPE_BYTE)); + g_action_map_add_action(G_ACTION_MAP(o), G_ACTION(act)); + g_signal_connect(act, "activate", G_CALLBACK(kee_menu_act_import_entry), o); + gtk_window_set_titlebar(GTK_WINDOW(o), GTK_WIDGET(o->head)); gtk_window_set_title (GTK_WINDOW (o), "kee"); diff --git a/src/gtk/kee-menu.h b/src/gtk/kee-menu.h @@ -6,6 +6,8 @@ #include "context.h" +/// \todo rename to kee-win + enum KEE_MENU_PROPS { KEE_N_MENU_PROPS, }; diff --git a/src/gtk/ui.c b/src/gtk/ui.c @@ -86,11 +86,8 @@ 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"); kee_menu_next(win, "unlock"); @@ -101,9 +98,6 @@ void ui_build(GtkApplication *gapp, struct kee_context *ctx) { widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); kee_menu_add(win, "entry", widget); - //widget = g_object(GTK_ORIENTATION_VERTICAL, 0); - //kee_menu_add(win, "item", widget); - trans = g_object_new(KEE_TYPE_TRANSPORT, "orientation", GTK_ORIENTATION_VERTICAL, NULL); kee_menu_add(win, "transport", GTK_WIDGET(trans));