commit a69f943fb17c11b560b242774273368f05ef211d
parent 1ef98807249886bf8ea47472edc8f86af3e7fb98
Author: lash <dev@holbrook.no>
Date: Mon, 22 Apr 2024 17:20:22 +0100
Enable item deserialize on store seek
Diffstat:
7 files changed, 104 insertions(+), 67 deletions(-)
diff --git a/src/gtk/kee-entry-item-store.c b/src/gtk/kee-entry-item-store.c
@@ -13,7 +13,6 @@ typedef struct {
struct _KeeEntryItemStore {
GObject parent;
- const char *current_id;
struct db_ctx *db;
int last_idx;
int last_state;
@@ -24,6 +23,7 @@ struct _KeeEntryItemStore {
char *last_value;
size_t last_value_length;
struct Cadiz resolver;
+ struct kee_ledger_t *ledger;
};
@@ -60,23 +60,23 @@ static guint kee_entry_item_store_get_n_items(GListModel *list) {
static gpointer kee_entry_item_store_get_item(GListModel *list, guint index) {
-// int r;
+ int r;
KeeEntryItem *o;
KeeEntryItemStore *store;
//
// //kee_entry_load(o, list->db);
store = KEE_ENTRY_ITEM_STORE(list);
- o = kee_entry_item_new(store->db);
+ o = kee_entry_item_new(store->db, store->ledger);
kee_entry_item_set_resolver(o, &store->resolver);
kee_entry_item_store_seek(store, index);
-// //kee_entry_deserialize(o, store->last_key, 9, store->last_value, store->last_value_length);
-// r = kee_entry_deserialize(o, store->last_value, store->last_value_length);
-// if (r) {
-// return NULL;
-// }
-//
+ //kee_entry_deserialize(o, store->last_key, 9, store->last_value, store->last_value_length);
+ r = kee_entry_item_deserialize(o, store->last_value, store->last_value_length);
+ if (r) {
+ return NULL;
+ }
+
// //return o;
-// kee_entry_apply_list_item_widget(o);
+ kee_entry_item_apply_list_item_widget(o);
//
// return o;
return o;
@@ -88,15 +88,17 @@ static void kee_entry_item_store_iface_init(GListModelInterface *ifc) {
ifc->get_item = kee_entry_item_store_get_item;
}
-KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, const char *current_id) {
+KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger) {
KeeEntryItemStore *o;
o = g_object_new(KEE_TYPE_ENTRY_ITEM_STORE, NULL);
o->db = db;
- o->last = calloc(2048, 1);
+ o->last = calloc(4096, 1);
o->last_digest = o->last + DB_KEY_SIZE_LIMIT;
+ o->last_key = o->last;
+ o->last_value = o->last_digest + 64;
o->last_value_length = 1024;
- o->current_id = current_id;
+ o->ledger = ledger;
o->last_count = kee_entry_item_store_seek(o, INT_MAX);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "max key index is: %d", o->last_idx - 1);
@@ -109,44 +111,51 @@ static int kee_entry_item_store_seek(KeeEntryItemStore *o, int idx) {
int r;
int i;
size_t key_len;
- size_t entry_key_len;
- char *mem[4096];
- char *last_key;
+ //size_t entry_key_len;
+ size_t cmp_key_len;
+ //char *mem[4096];
+ //char *last_key;
+ char mem[DB_KEY_SIZE_LIMIT];
+ char *cmp_key = (char*)mem;
char *entry_key;
- char *last_value;
- size_t last_value_length;
- char out[1024];
- size_t out_len;
-
- entry_key_len = 65;
- key_len = entry_key_len + 8;
- last_key = (char*)mem;
- entry_key = last_key + 128;
- last_value = entry_key + 128;
- *last_key = DbKeyLedgerEntry;
- memcpy(last_key+1, o->current_id, key_len - 1);
- memcpy(entry_key, last_key, entry_key_len);
+ //char *last_value;
+ //size_t last_value_length;
+ //char out[1024];
+ //size_t out_len;
+
+ cmp_key_len = 65;
+ key_len = cmp_key_len + 8;
+ //o.last_key = (char*)mem;
+ //entry_key = last_key + 128;
+ //last_value = entry_key + 128;
+ //*last_key = DbKeyLedgerEntry;
+ *cmp_key = DbKeyLedgerEntry;
+ //memcpy(last_key+1, o->ledger->digest, key_len - 1);
+ memcpy(cmp_key+1, o->ledger->digest, key_len - 1);
+ //memcpy(entry_key, last_key, entry_key_len);
+ memcpy(o->last_key, cmp_key, cmp_key_len);
i = 0;
while (i <= idx) {
o->last_idx = i;
- last_value_length = 2048;
- r = db_next(o->db, DbKeyLedgerEntry, &last_key, &key_len, &last_value, &last_value_length);
+ o->last_value_length = 2048;
+ //r = db_next(o->db, DbKeyLedgerEntry, &last_key, &key_len, &last_value, &last_value_length);
+ r = db_next(o->db, DbKeyLedgerEntry, &cmp_key, &key_len, &o->last_value, &o->last_value_length);
if (r) {
break;
}
- if (memcmp(last_key, entry_key, entry_key_len)) {
+ //if (memcmp(last_key, entry_key, entry_key_len)) {
+ if (memcmp(cmp_key, o->last_key, cmp_key_len)) {
break;
}
- out_len = 1024;
- //r = kee_entry_deserialize_item(o, last_value, last_value_length, out, &out_len);
- //if (r) {
- // g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!");
- //} else {
- // g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding entry: %s", out);
- // i++;
- // gtk_string_list_append(list, out);
- //}
+// out_len = 1024;
+// r = kee_entry_item_deserialize(o, last_value, last_value_length);
+// if (r) {
+// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!");
+// } else {
+// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding entry: %s", out);
+// i++;
+// }
i++;
//o->alice_credit_balance += o->
}
diff --git a/src/gtk/kee-entry-item-store.h b/src/gtk/kee-entry-item-store.h
@@ -3,13 +3,14 @@
#include <glib-object.h>
#include "db.h"
+#include "ledger.h"
G_BEGIN_DECLS
#define KEE_TYPE_ENTRY_ITEM_STORE kee_entry_item_store_get_type()
G_DECLARE_FINAL_TYPE(KeeEntryItemStore, kee_entry_item_store, KEE, ENTRY_ITEM_STORE, GObject);
-KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, const char *current_id);
+KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger);
void kee_entry_item_store_set_resolve(KeeEntryItemStore *o, const char *locator);
G_END_DECLS
diff --git a/src/gtk/kee-entry-item.c b/src/gtk/kee-entry-item.c
@@ -18,7 +18,8 @@ struct _KeeEntryItem {
GtkWidget parent;
int state;
char header[1024];
- struct kee_ledger_t ledger;
+ struct kee_ledger_t *ledger;
+ struct kee_ledger_item_t *item;
struct Cadiz *resolver;
int alice_credit_delta;
int bob_credit_delta;
@@ -30,22 +31,27 @@ struct _KeeEntryItem {
G_DEFINE_TYPE(KeeEntryItem, kee_entry_item, GTK_TYPE_BOX);
void kee_entry_item_handle_setup(GtkListItemFactory* o, GtkListItem *item) {
- GtkWidget *label;
+ GtkWidget *box;
- label = gtk_label_new(NULL);
- gtk_list_item_set_child(item, label);
+ box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+ gtk_list_item_set_child(item, box);
}
void kee_entry_item_handle_bind(GtkListItemFactory *o, GtkListItem *item) {
- GtkWidget *label;
- GtkStringObject *s;
-
- label = gtk_list_item_get_child(item);
- s = gtk_list_item_get_item(item);
- gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s));
+ //GtkWidget *label;
+ GtkWidget *box;
+ GtkWidget *box_item;
+ //GtkStringObject *s;
+
+ box = gtk_list_item_get_child(item);
+ //s = gtk_list_item_get_item(item);
+ box_item = gtk_list_item_get_item(item);
+ //gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s));
+ //gtk_label_set_label(GTK_LABEL(label), GTK_LABEL(s));
+ gtk_box_append(GTK_BOX(box), box_item);
+
}
-
static void kee_entry_item_dispose(GObject *o) {
}
@@ -67,27 +73,40 @@ void kee_entry_item_set_resolver(KeeEntryItem *o, struct Cadiz *resolver) {
o->resolver = resolver;
}
-static int kee_entry_item_deserialize(KeeEntryItem *o, const char *data, size_t data_len, char *out, size_t *out_len) {
- struct kee_ledger_item_t *item;
-
- item = kee_ledger_parse_item(&o->ledger, data, data_len);
- if (item == NULL) {
+int kee_entry_item_deserialize(KeeEntryItem *o, const char *data, size_t data_len) {
+ o->item = kee_ledger_parse_item(o->ledger, data, data_len);
+ if (o->item == NULL) {
return ERR_FAIL;
}
- kee_content_resolve(&item->content, o->resolver);
+ kee_content_resolve(&o->item->content, o->resolver);
- if (item->content.flags & KEE_CONTENT_RESOLVED_SUBJECT) {
- strcpy(out, item->content.subject);
+ if (o->item->content.flags & KEE_CONTENT_RESOLVED_SUBJECT) {
+ strcpy(o->header, o->item->content.subject);
} else {
- strcpy(out, "(no subject)");
+ strcpy(o->header, "(no subject)");
}
return ERR_OK;
}
-KeeEntryItem* kee_entry_item_new(struct db_ctx *db) {
+KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger) {
KeeEntryItem *o;
o = KEE_ENTRY_ITEM(g_object_new(KEE_TYPE_ENTRY_ITEM, "orientation", GTK_ORIENTATION_VERTICAL, NULL));
o->db = db;
+ o->ledger = ledger;
return o;
}
+
+void kee_entry_item_apply_list_item_widget(KeeEntryItem *o) {
+ GtkWidget *widget;
+
+ //if (o->state) {
+ // g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "entry must be loaded first");
+ // return;
+ //}
+
+ //sprintf(o->header, "%s [%s]\n%s (%s)", o->ledger.content.subject, o->ledger.uoa, o->bob_dn.cn, o->bob_dn.uid);
+ widget = gtk_label_new(o->header);
+ gtk_box_append(GTK_BOX(o), widget);
+ return;
+}
diff --git a/src/gtk/kee-entry-item.h b/src/gtk/kee-entry-item.h
@@ -6,6 +6,7 @@
#include "db.h"
#include "cadiz.h"
+#include "ledger.h"
G_BEGIN_DECLS
@@ -20,10 +21,12 @@ enum KEE_ENTRY_ITEM_PROPS {
#define KEE_TYPE_ENTRY_ITEM kee_entry_item_get_type()
G_DECLARE_FINAL_TYPE(KeeEntryItem, kee_entry_item, KEE, ENTRY_ITEM, GtkBox);
-KeeEntryItem* kee_entry_item_new(struct db_ctx *db);
+KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger);
void kee_entry_item_handle_setup(GtkListItemFactory* o, GtkListItem *item);
void kee_entry_item_handle_bind(GtkListItemFactory *o, GtkListItem *item);
void kee_entry_item_set_resolver(KeeEntryItem *o, struct Cadiz *resolver);
+int kee_entry_item_deserialize(KeeEntryItem *o, const char *data, size_t data_len);
+void kee_entry_item_apply_list_item_widget(KeeEntryItem *o);
G_END_DECLS
diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c
@@ -236,7 +236,7 @@ void kee_entry_apply_display_widget(KeeEntry *o) {
g_signal_connect(factory, "setup", G_CALLBACK(kee_entry_item_handle_setup), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(kee_entry_item_handle_bind), NULL);
- model = kee_entry_item_store_new(o->db, o->current_id);
+ model = kee_entry_item_store_new(o->db, &o->ledger);
kee_entry_item_store_set_resolve(model, "./testdata_resource");
sel = gtk_single_selection_new(G_LIST_MODEL(model));
widget = gtk_list_view_new(GTK_SELECTION_MODEL(sel), GTK_LIST_ITEM_FACTORY(factory));
@@ -245,12 +245,11 @@ void kee_entry_apply_display_widget(KeeEntry *o) {
return;
}
-
void kee_entry_apply_entry(KeeEntry *target, KeeEntry *orig) {
target->db = orig->db;
memcpy(target->current_id, orig->current_id, 128);
target->resolver = orig->resolver;
- target->ledger = orig->ledger;
+ memcpy(&target->ledger, &orig->ledger, sizeof(struct kee_ledger_t));
target->state = orig->state;
target->bob_dn = orig->bob_dn;
return;
diff --git a/src/ledger.c b/src/ledger.c
@@ -373,6 +373,11 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_
return 1;
}
+ r = calculate_digest_algo(data, data_len, (char*)ledger->digest, GCRY_MD_SHA512);
+ if (r) {
+ return 1;
+ }
+
return ERR_OK;
}
diff --git a/src/ledger.h b/src/ledger.h
@@ -25,6 +25,7 @@ struct kee_ledger_item_t {
};
struct kee_ledger_t {
+ const char digest[64];
struct kee_ledger_item_t *last_item;
char pubkey_alice[32];
char pubkey_bob[32];