kee

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

commit 754eba98c8d281aa32cc867b533e933a0ce4a657
parent c7299f44d096c2ca61e48417d80ba34109074876
Author: lash <dev@holbrook.no>
Date:   Wed, 24 Apr 2024 21:38:44 +0100

Consolidate context object, simplify gtk keystore wrapper

Diffstat:
Msrc/gtk/context.c | 11+++++++++--
Msrc/gtk/context.h | 3++-
Msrc/gtk/kee-key.c | 70++++------------------------------------------------------------------
Msrc/gtk/kee-key.h | 7+++----
Msrc/gtk/main.c | 8+-------
Msrc/gtk/ui.c | 5+----
6 files changed, 20 insertions(+), 84 deletions(-)

diff --git a/src/gtk/context.c b/src/gtk/context.c @@ -5,13 +5,20 @@ #include "camera.h" #include "err.h" #include "gpg.h" +#include "db.h" -int kee_context_new(struct kee_context *ctx, struct kee_settings *settings) { +int kee_context_init(struct kee_context *ctx, struct kee_settings *settings) { + unsigned char *v; + memset(ctx, 0, sizeof(struct kee_context)); ctx->state = 1; ctx->settings = settings; - + db_connect(&ctx->db, "./testdata_mdb"); + v = settings_get(ctx->settings, SETTINGS_KEY); + gpg_store_init(&ctx->gpg, (char*)v); + ctx->entry_store = kee_entry_store_new(&ctx->db); + kee_entry_store_set_resolve(ctx->entry_store, "./testdata_resource"); return ERR_OK; } diff --git a/src/gtk/context.h b/src/gtk/context.h @@ -13,11 +13,12 @@ struct kee_context { struct kee_settings *settings; struct kee_camera_devices camera_devices; struct db_ctx db; + struct gpg_store gpg; KeeEntryStore *entry_store; int state; }; -int kee_context_new(struct kee_context *ctx, struct kee_settings *settings); +int kee_context_init(struct kee_context *ctx, struct kee_settings *settings); int kee_context_state(struct kee_context *ctx); void kee_context_free(struct kee_context *ctx); diff --git a/src/gtk/kee-key.c b/src/gtk/kee-key.c @@ -19,36 +19,8 @@ struct _KeeKey { G_DEFINE_TYPE(KeeKey, kee_key, GTK_TYPE_BOX); -static GParamSpec *kee_props[KEE_N_KEY_PROPS] = {NULL,}; static guint kee_sigs[KEE_N_KEY_SIGS] = {0,}; - -static void kee_key_set_property(GObject *oo, guint property_id, const GValue *value, GParamSpec *pspec) { - KeeKey *o = KEE_KEY(oo); - const gchar *s; - - switch((enum KEE_KEY_PROPS)property_id) { - case KEE_P_KEY_STORE_PATH: - s = g_value_get_string(value); - gpg_store_init(o->gpg, (char*)s); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(oo, property_id, pspec); - } -} - -static void kee_key_get_property(GObject *oo, guint property_id, GValue *value, GParamSpec *pspec) { - KeeKey *o = KEE_KEY(oo); - - switch((enum KEE_KEY_PROPS)property_id) { - case KEE_P_KEY_STORE: - g_value_set_pointer(value, o->gpg); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(oo, property_id, pspec); - } -} - static void kee_key_class_init(KeeKeyClass *kls) { GObjectClass *o = G_OBJECT_CLASS(kls); @@ -62,50 +34,17 @@ static void kee_key_class_init(KeeKeyClass *kls) { G_TYPE_NONE, 0, NULL); - -// kee_sigs[KEE_S_KEY_UNLOCKED] = g_signal_new("sign", -// G_TYPE_FROM_CLASS(o), -// G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, -// 0, -// NULL, -// NULL, -// NULL, -// G_TYPE_NONE, -// 1, -// NULL); - - o->set_property = kee_key_set_property; - o->get_property = kee_key_get_property; - - kee_props[KEE_P_KEY_STORE_PATH] = g_param_spec_string( - "keystore_path", - "Keystore path", - "GPG keystore interface path initializer", - ".", - G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); - - - kee_props[KEE_P_KEY_STORE] = g_param_spec_pointer( - "keystore", - "Keystore", - "GPG keystore interface", - G_PARAM_READABLE); - - g_object_class_install_properties(o, KEE_N_KEY_PROPS, kee_props); } static void kee_key_init(KeeKey *o) { - o->gpg = malloc(sizeof(struct gpg_store)); } static void kee_key_finalize(KeeKey *o) { - free(o->gpg); } static void kee_key_handle_unlock_click(GtkWidget *button, KeeKey *o) { int r; GtkEntryBuffer *buf; - struct gpg_store *gpg; GValue v = G_VALUE_INIT; char passphrase[1024]; @@ -114,9 +53,7 @@ static void kee_key_handle_unlock_click(GtkWidget *button, KeeKey *o) { strcpy(passphrase, gtk_entry_buffer_get_text(buf)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "click"); - g_object_get_property(G_OBJECT(o), "keystore", &v); - gpg = g_value_get_pointer(&v); - r = gpg_store_check(gpg, passphrase); + r = gpg_store_check(o->gpg, passphrase); if (r) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "wrong passphrase"); return; @@ -126,13 +63,14 @@ static void kee_key_handle_unlock_click(GtkWidget *button, KeeKey *o) { gtk_entry_buffer_delete_text(buf, 0, gtk_entry_buffer_get_length(buf)); } -KeeKey* kee_key_new(const char *key_path) { +KeeKey* kee_key_new(struct gpg_store *gpg) { KeeKey *o; GtkWidget *entry; GtkWidget *button; GtkEntryBuffer *buf; - o = g_object_new(KEE_TYPE_KEY, "keystore_path", key_path, "orientation", GTK_ORIENTATION_VERTICAL, NULL); + o = g_object_new(KEE_TYPE_KEY, "orientation", GTK_ORIENTATION_VERTICAL, NULL); + o->gpg = gpg; entry = gtk_entry_new(); gtk_box_append(GTK_BOX(o), entry); diff --git a/src/gtk/kee-key.h b/src/gtk/kee-key.h @@ -4,26 +4,25 @@ #include <glib-object.h> #include <gtk/gtk.h> +#include "gpg.h" + G_BEGIN_DECLS #define KEE_TYPE_KEY kee_key_get_type() G_DECLARE_FINAL_TYPE(KeeKey, kee_key, KEE, KEY, GtkBox) enum KEE_KEY_PROPS { - KEE_P_KEY_STORE_PATH = 1, - KEE_P_KEY_STORE, KEE_N_KEY_PROPS, }; enum KEE_KEY_SIGS { KEE_S_KEY_UNLOCKED, -// KEE_S_KEY_WANT_SIGN, KEE_N_KEY_SIGS, }; G_END_DECLS -KeeKey* kee_key_new(const char *key_path); +KeeKey* kee_key_new(struct gpg_store *gpg); const char *kee_key_get_fingerprint(KeeKey *o, char *fingerprint); #endif // _GTK_KEE_KEY_H diff --git a/src/gtk/main.c b/src/gtk/main.c @@ -34,7 +34,6 @@ int main(int argc, char **argv) { struct kee_settings settings; GtkApplication *gapp; struct kee_context ctx; - KeeEntryStore *store; gtk_init(); gst_init(0, NULL); @@ -44,16 +43,11 @@ int main(int argc, char **argv) { settings_new_from_xdg(&settings); settings_init(&settings); - r = kee_context_new(&ctx, &settings); + r = kee_context_init(&ctx, &settings); if (r) { return r; } - db_connect(&ctx.db, "./testdata_mdb"); - store = kee_entry_store_new(&ctx.db); - kee_entry_store_set_resolve(store, "./testdata_resource"); - ctx.entry_store = store; - g_signal_connect (gapp, "startup", G_CALLBACK (startup), &ctx); g_signal_connect (gapp, "activate", G_CALLBACK (activate), &ctx); g_signal_connect (gapp, "shutdown", G_CALLBACK (deactivate), NULL); diff --git a/src/gtk/ui.c b/src/gtk/ui.c @@ -30,7 +30,6 @@ static void ui_handle_unlock(KeeKey *o, KeeMenu *menu) { kee_menu_prev(menu); } - //static GtkWidget* ui_build_view(KeeMenu *menu) { // GtkListItemFactory *factory; // GtkSelectionModel *sel; @@ -53,12 +52,10 @@ void ui_build(GtkApplication *app, struct kee_context *ctx) { GtkWidget *widget; KeeMenu *win; KeeImport *import; - struct kee_settings *settings; win = kee_menu_new(app); - settings = ctx->settings; - widget = GTK_WIDGET(kee_key_new(settings_get(settings, SETTINGS_KEY))); + widget = GTK_WIDGET(kee_key_new(&ctx->gpg)); kee_menu_add(win, "unlock", widget); g_signal_connect (widget, "unlock", G_CALLBACK(ui_handle_unlock), win);