kee

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

commit 8784cb438cbe5baef1e049f35a00996f394ed27d
parent f5906b8e0a0621af88bd6446dfc614b40197027d
Author: lash <dev@holbrook.no>
Date:   Wed, 28 Feb 2024 15:05:21 +0000

Factor out menu creation, introduce context backend object

Diffstat:
MMakefile | 4++--
Msrc/aux/Makefile | 3+++
Msrc/aux/varint/Makefile | 3+++
Asrc/context.c | 7+++++++
Msrc/context.h | 9++++++++-
Msrc/gtk/Makefile | 2+-
Msrc/gtk/main.c | 72++++++++++++++++++++----------------------------------------------------
Asrc/gtk/menu.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/gtk/menu.h | 8++++++++
Msrc/gtk/ui.c | 13++++++++++---
Msrc/gtk/ui.h | 2++
Msrc/settings.c | 22++++++++++++++++++++++
12 files changed, 134 insertions(+), 59 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,12 +9,12 @@ subs: glade make -C src glade: - gtk4-builder-tool simplify --3to4 glade.ui > src/main.ui + gtk4-builder-tool simplify --3to4 glade.ui > src/gtk/main.ui clean: make -C src clean make -C src/gtk clean - rm -vf src/aux/varint/*.o + make -C src/aux clean run: gtk all G_MESSAGES_DEBUG=all ./src/gtk/a.out diff --git a/src/aux/Makefile b/src/aux/Makefile @@ -1,2 +1,5 @@ all: make -C varint + +clean: + make -C varint clean diff --git a/src/aux/varint/Makefile b/src/aux/varint/Makefile @@ -1,2 +1,5 @@ all: $(CC) -c varint.c -o varint.o + +clean: + rm -vf *.o diff --git a/src/context.c b/src/context.c @@ -0,0 +1,7 @@ +#include <string.h> +#include "context.h" + +void kee_context_new(struct kee_context *ctx, void *front) { + memset(ctx, 0, sizeof(struct kee_context)); + ctx->front = front; +} diff --git a/src/context.h b/src/context.h @@ -1,8 +1,15 @@ #ifndef _KEE_CONTEXT #define _KEE_CONTEXT -struct kee_context { +#include "settings.h" +#include "db.h" +struct kee_context { + void *front; + struct kee_settings settings; + struct db_ctx db; }; +void kee_context_new(struct kee_context *ctx, void *front); + #endif // _KEE_CONTEXT diff --git a/src/gtk/Makefile b/src/gtk/Makefile @@ -2,7 +2,7 @@ OBJS := $(patsubst %.c,%.o,$(filter-out main.c,$(wildcard *.c))) LINKOBJS := $(wildcard ../*.o) $(OBJS) ../aux/varint/varint.o INCLUDES := `pkg-config --cflags gtk4` -I.. CFLAGS += $(INCLUDES) -g3 -Wall -LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt` -lb64 +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir` -lb64 LDFLAGS += $(LIBS) all: resource $(OBJS) diff --git a/src/gtk/main.c b/src/gtk/main.c @@ -1,51 +1,13 @@ #include <string.h> #include <gtk/gtk.h> -#include "ui.h" -#include "db.h" - -static void act_scan(GSimpleAction *act, GVariant *param, GApplication *app) { - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "scan clicked"); -} +#include "ui.h" +#include "context.h" +#include "menu.h" -static void act_quit(GSimpleAction *act, GVariant *param, GApplication *app) { - g_application_quit(app); -} static void startup(GtkApplication *app, gpointer user_data) { - GMenu *menu_bar; - GMenu *menu; - GMenuItem *menu_item; - GMenuItem *menu_item_menu; - GSimpleAction *act; - - menu_bar = g_menu_new(); - menu_item_menu = g_menu_item_new("Menu", NULL); - menu = g_menu_new(); - menu_item = g_menu_item_new("Scan", "app.scan"); - g_menu_append_item(menu, menu_item); - g_object_unref(menu_item); - - menu_item = g_menu_item_new("Quit", "app.quit"); - g_menu_append_item(menu, menu_item); - g_object_unref(menu_item); - - act = g_simple_action_new("quit", NULL); - g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act)); - g_signal_connect(act, "activate", G_CALLBACK(act_quit), app); - - act = g_simple_action_new("scan", NULL); - g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act)); - g_signal_connect(act, "activate", G_CALLBACK(act_scan), app); - - g_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu)); - g_object_unref(menu); - - g_menu_append_item(menu_bar, menu_item_menu); - g_object_unref(menu_item_menu); - - gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menu_bar)); - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "set up menus"); + menu_setup(user_data); } static void activate(GtkApplication *app, gpointer user_data) { @@ -63,18 +25,24 @@ static void deactivate(GtkApplication *app, gpointer user_data) { } int main(int argc, char **argv) { - struct ui_container ui; int r; - GtkApplication *app; - struct db_ctx db; + struct kee_context ctx; + struct ui_container ui; + + + r = ui_init(&ui); + if (r) { + return r; + } + + kee_context_new(&ctx, &ui); + db_connect(&ctx.db, "./testdata_mdb"); - db_connect(&db, "./testdata_mdb"); + g_signal_connect (ui.gapp, "startup", G_CALLBACK (startup), &ui); + g_signal_connect (ui.gapp, "activate", G_CALLBACK (activate), &ui); + g_signal_connect (ui.gapp, "shutdown", G_CALLBACK (deactivate), &ui); - app = gtk_application_new ("no.holbrook.example.Buidler", G_APPLICATION_DEFAULT_FLAGS); - g_signal_connect (app, "startup", G_CALLBACK (startup), &ui); - g_signal_connect (app, "activate", G_CALLBACK (activate), &ui); - g_signal_connect (app, "shutdown", G_CALLBACK (deactivate), &ui); - r = g_application_run (G_APPLICATION (app), argc, argv); - g_object_unref(app); + r = g_application_run (G_APPLICATION (ui.gapp), argc, argv); + g_object_unref(ui.gapp); return r; } diff --git a/src/gtk/menu.c b/src/gtk/menu.c @@ -0,0 +1,48 @@ +#include <gtk/gtk.h> + +#include "ui.h" + + +static void act_scan(GSimpleAction *act, GVariant *param, GApplication *app) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "scan clicked"); +} + +static void act_quit(GSimpleAction *act, GVariant *param, GApplication *app) { + g_application_quit(app); +} + +void menu_setup(struct ui_container *ui) { + GMenu *menu_bar; + GMenu *menu; + GMenuItem *menu_item; + GMenuItem *menu_item_menu; + GSimpleAction *act; + + menu_bar = g_menu_new(); + menu_item_menu = g_menu_item_new("Menu", NULL); + menu = g_menu_new(); + menu_item = g_menu_item_new("Scan", "app.scan"); + g_menu_append_item(menu, menu_item); + g_object_unref(menu_item); + + menu_item = g_menu_item_new("Quit", "app.quit"); + g_menu_append_item(menu, menu_item); + 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); + + 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_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu)); + g_object_unref(menu); + + 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)); + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "set up menus"); +} diff --git a/src/gtk/menu.h b/src/gtk/menu.h @@ -0,0 +1,8 @@ +#ifndef _KEE_GTK_MENU_H +#define _KEE_GTK_MENU_H + +#include "ui.h" + +void menu_setup(struct ui_container *ui); + +#endif // _KEE_GTK_MENU_H diff --git a/src/gtk/ui.c b/src/gtk/ui.c @@ -1,9 +1,16 @@ #include <gtk/gtk.h> + #include "ui.h" +#include "err.h" + -/*** - * \todo change file to resource - */ +int ui_init(struct ui_container *ui) { + ui->gapp = gtk_application_new ("org.defalsify.Kee", G_APPLICATION_DEFAULT_FLAGS); + if (ui->gapp == NULL) { + return ERR_FAIL; + } + return ERR_OK; +} static void new_item(GtkListItemFactory *factory, GtkListItem *item, gpointer user_data) { } diff --git a/src/gtk/ui.h b/src/gtk/ui.h @@ -4,12 +4,14 @@ #include <gtk/gtk.h> struct ui_container { + GtkApplication *gapp; GtkApplicationWindow *win; GtkStack *stack; GListModel *front_list; GtkListView *front_view; }; +int ui_init(struct ui_container *ui); void ui_build(GtkApplication *app, struct ui_container *ui); void ui_free(struct ui_container *ui); diff --git a/src/settings.c b/src/settings.c @@ -1,3 +1,4 @@ +#include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> @@ -8,11 +9,28 @@ #include "debug.h" #include "settings.h" +#define KEE_SETTINGS_DATANAME "kee" +#define KEE_SETTINGS_CAP 4096 /** * \todo make xdg optional */ int settings_new_from_xdg(struct kee_settings *z) { + xdgHandle xdg; + const char *s; + + xdgInitHandle(&xdg); + + memset(z, 0, sizeof(struct kee_settings)); + + z->data = malloc(KEE_SETTINGS_CAP); + + s = xdgDataHome(&xdg); + sprintf((char*)z->data, "%s/%s", s, KEE_SETTINGS_DATANAME); + + s = xdgRuntimeDirectory(&xdg); + sprintf((char*)z->run, "%s/%s", s, KEE_SETTINGS_DATANAME); + return ERR_OK; } @@ -65,3 +83,7 @@ int settings_set(struct kee_settings *z, enum SettingsType typ, unsigned char* v } return ERR_OK; } + +void settings_free(struct kee_settings *z) { + free(z->data); +}