kee

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

commit 1633c87b8b96e2b8437d7e1c1149812ed2c5550a
parent 2c2c8dbd9abeea9a1e9a2701bc6600b9f9d1c97a
Author: lash <dev@holbrook.no>
Date:   Fri,  8 Mar 2024 14:34:12 +0000

Wrap ui in gobject context

Diffstat:
Msrc/gtk/kee-uicontext.c | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/gtk/kee-uicontext.h | 7+++++++
Msrc/gtk/main.c | 14++++++--------
Msrc/gtk/menu.c | 18+++++++++++-------
Msrc/gtk/ui.c | 5+++++
Msrc/gtk/ui.h | 10++++++++++
6 files changed, 80 insertions(+), 15 deletions(-)

diff --git a/src/gtk/kee-uicontext.c b/src/gtk/kee-uicontext.c @@ -8,10 +8,14 @@ typedef struct { } KeeUicontextPrivate; +/** + * \todo parent can be gapplication object? + */ struct _KeeUicontext { GObject parent; struct ui_container *ui; struct kee_context *ctx; + GApplication *app; }; G_DEFINE_TYPE(KeeUicontext, kee_uicontext, G_TYPE_OBJECT) @@ -28,6 +32,7 @@ static void kee_uicontext_set_property(GObject *oo, guint property_id, const GVa break; case UI_CONTAINER: o->ui = g_value_get_pointer(value); + o->app = (GApplication*)o->ui->gapp; break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(oo, property_id, pspec); @@ -36,6 +41,18 @@ static void kee_uicontext_set_property(GObject *oo, guint property_id, const GVa } +static void kee_uicontext_get_property(GObject *oo, guint property_id, GValue *value, GParamSpec *pspec) { + KeeUicontext *o = KEE_UICONTEXT(oo); + switch ((enum KEE_PROPS) property_id) { + case GAPP: + g_value_set_pointer(value, o->app); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(oo, property_id, pspec); + break; + } +} + static void kee_uicontext_class_init(KeeUicontextClass *kls) { GObjectClass *o = G_OBJECT_CLASS(kls); @@ -52,6 +69,8 @@ static void kee_uicontext_class_init(KeeUicontextClass *kls) { ); o->set_property = kee_uicontext_set_property; + o->get_property = kee_uicontext_get_property; + kee_props[CORE_CONTEXT] = g_param_spec_pointer( "core_context", "Core context", @@ -62,6 +81,12 @@ static void kee_uicontext_class_init(KeeUicontextClass *kls) { "Ui Container", "UI container to connect", G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); + kee_props[GAPP] = g_param_spec_pointer( + "app", + "Gapplication object", + "Gapplication object attached to ui", + G_PARAM_READABLE); + g_object_class_install_properties(o, KEE_N_PROPS, kee_props); } @@ -72,5 +97,21 @@ static void kee_uicontext_init(KeeUicontext *self) { } void kee_uicontext_scanstart(KeeUicontext *o) { + if (KEE_UI_STATE_IS_SCANNING(o->ui)) { + return; + } g_signal_emit(o, kee_sigs[SCAN_WANT], 0); } + +KeeState kee_uicontext_state(KeeUicontext *o) { + KeeState state; + + state.ui = o->ui->state; + state.ctx = o->ctx->state; + + return state; +} + +GApplication* kee_uicontext_get_application(KeeUicontext *o) { + return o->app; +} diff --git a/src/gtk/kee-uicontext.h b/src/gtk/kee-uicontext.h @@ -5,6 +5,11 @@ G_BEGIN_DECLS +typedef struct { + int ui; + int ctx; +} KeeState; + enum KEE_SIGS { SCAN_WANT, KEE_N_SIGS, @@ -13,6 +18,7 @@ enum KEE_SIGS { enum KEE_PROPS { CORE_CONTEXT = 1, UI_CONTAINER, + GAPP, KEE_N_PROPS, }; @@ -21,6 +27,7 @@ G_DECLARE_FINAL_TYPE(KeeUicontext, kee_uicontext, KEE, UICONTEXT, GObject) KeeUicontext* kee_uicontext_new(void); void kee_uicontext_scanstart(KeeUicontext *o); +KeeState kee_uicontext_state(KeeUicontext *o); G_END_DECLS diff --git a/src/gtk/main.c b/src/gtk/main.c @@ -2,11 +2,11 @@ #include <gtk/gtk.h> #include <glib-object.h> +#include "kee-uicontext.h" #include "ui.h" #include "context.h" #include "menu.h" #include "settings.h" -#include "kee-uicontext.h" static void startup(GtkApplication *app, gpointer user_data) { @@ -18,12 +18,10 @@ static void activate(GtkApplication *app, gpointer user_data) { } static void deactivate(GtkApplication *app, gpointer user_data) { + g_object_unref(user_data); ui_free(user_data); } -static void tmpscan (KeeUicontext *o, gpointer v) { - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo in scan want"); -} int main(int argc, char **argv) { int r; @@ -38,19 +36,19 @@ int main(int argc, char **argv) { kee_context_new(&ctx, &ui); uctx = g_object_new(KEE_TYPE_UICONTEXT, "ui_container", &ui, "core_context", &ctx, NULL); + g_object_set(&uctx, "app", ui.gapp, NULL); settings_new_from_xdg(&ctx.settings); settings_init(&ctx.settings); db_connect(&ctx.db, "./testdata_mdb"); - g_signal_connect (ui.gapp, "startup", G_CALLBACK (startup), &ui); + g_signal_connect (ui.gapp, "startup", G_CALLBACK (startup), uctx); g_signal_connect (ui.gapp, "activate", G_CALLBACK (activate), &ui); - g_signal_connect (ui.gapp, "shutdown", G_CALLBACK (deactivate), &ui); - g_signal_connect (uctx, "scan_want", G_CALLBACK( tmpscan ), NULL); + g_signal_connect (ui.gapp, "shutdown", G_CALLBACK (deactivate), uctx); + //g_signal_connect (uctx, "scan_want", G_CALLBACK( tmpscan ), NULL); r = g_application_run (G_APPLICATION (ui.gapp), argc, argv); - kee_uicontext_scanstart(uctx); g_object_unref(ui.gapp); return r; diff --git a/src/gtk/menu.c b/src/gtk/menu.c @@ -1,6 +1,6 @@ #include <gtk/gtk.h> -#include "ui.h" +#include "kee-uicontext.h" static void act_scan(GSimpleAction *act, GVariant *param, GApplication *app) { @@ -15,12 +15,16 @@ static void act_quit(GSimpleAction *act, GVariant *param, GApplication *app) { g_application_quit(app); } -void menu_setup(struct ui_container *ui) { +//void menu_setup(struct ui_container *ui) { +void menu_setup(KeeUicontext *ui) { GMenu *menu_bar; GMenu *menu; GMenuItem *menu_item; GMenuItem *menu_item_menu; GSimpleAction *act; + GApplication *gapp; + + g_object_get(ui, "app", &gapp, NULL); menu_bar = g_menu_new(); menu_item_menu = g_menu_item_new("Menu", NULL); @@ -34,12 +38,12 @@ void menu_setup(struct ui_container *ui) { g_object_unref(menu_item); act = g_simple_action_new("quit", NULL); - g_action_map_add_action(G_ACTION_MAP(ui->gapp), G_ACTION(act)); - g_signal_connect(act, "activate", G_CALLBACK(act_quit), ui->gapp); + g_action_map_add_action(G_ACTION_MAP(gapp), G_ACTION(act)); + g_signal_connect(act, "activate", G_CALLBACK(act_quit), gapp); act = g_simple_action_new("scan", NULL); - g_action_map_add_action(G_ACTION_MAP(ui->gapp), G_ACTION(act)); - g_signal_connect(act, "activate", G_CALLBACK(act_scan), ui->gapp); + g_action_map_add_action(G_ACTION_MAP(gapp), G_ACTION(act)); + g_signal_connect(act, "activate", G_CALLBACK(act_scan), gapp); g_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu)); g_object_unref(menu); @@ -47,6 +51,6 @@ void menu_setup(struct ui_container *ui) { g_menu_append_item(menu_bar, menu_item_menu); g_object_unref(menu_item_menu); - gtk_application_set_menubar(GTK_APPLICATION(ui->gapp), G_MENU_MODEL(menu_bar)); + gtk_application_set_menubar(GTK_APPLICATION(gapp), G_MENU_MODEL(menu_bar)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "set up menus"); } diff --git a/src/gtk/ui.c b/src/gtk/ui.c @@ -107,3 +107,8 @@ void ui_free(struct ui_container *ui) { //g_object_unref(ui->unlock); //g_object_unref(ui->win); } + +int ui_state_change(struct ui_container *ui, int set, int reset) { + ui->state |= set; + return ui->state; +} diff --git a/src/gtk/ui.h b/src/gtk/ui.h @@ -5,6 +5,14 @@ #include "scan.h" + +typedef enum { + KEE_UI_INIT = 1, + KEE_UI_SCANNING = 2, +} UiState; + +#define KEE_UI_STATE_IS_SCANNING(c) c->state & KEE_UI_SCANNING + struct ui_container { GtkApplication *gapp; GtkApplicationWindow *win; @@ -12,10 +20,12 @@ struct ui_container { GListModel *front_list; GtkListView *front_view; struct kee_scanner scan; + int state; }; int ui_init(struct ui_container *ui); void ui_build(GtkApplication *app, struct ui_container *ui); +int ui_state_change(struct ui_container *ui, int set, int reset); void ui_free(struct ui_container *ui); #endif // _UI_H