From d82f48718bc6c69d734d0306867b866efc2f9719 Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 17 Aug 2022 11:39:16 +0200 Subject: Implement datastructure for roster --- data.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ data.h | 1 + gtk.c | 4 +++- gui.h | 2 +- limox.c | 10 ++++++---- meson.build | 2 +- 6 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 data.c create mode 100644 data.h diff --git a/data.c b/data.c new file mode 100644 index 0000000..e9c4039 --- /dev/null +++ b/data.c @@ -0,0 +1,65 @@ + + +#include +#include +#include + +#include "gui.h" + + +// https://www.rfc-editor.org/rfc/rfc6121#section-2.1.2.5 +typedef enum { + SUB_NONE, + SUB_TO, + SUB_FROM, + SUB_BOTH +} subscription_t; + +typedef struct _roster_item_t { + const char* name; + const char* jid; + subscription_t sub; + void* widget; // GUI widget as generic void* + struct _roster_item_t* next; +} roster_item_t; + + +// the RAM-stored data payload +static roster_item_t* roster = NULL; + + +void add_roster_item(const char* jid, const char* subscription, const char* name) { + + // parse roster item + roster_item_t* item = malloc(sizeof(roster_item_t)); + item->name = name; + item->jid = jid; + if (strcmp(subscription, "none") == 0) { + item->sub = SUB_NONE; + } else if (strcmp(subscription, "to") == 0) { + item->sub = SUB_TO; + } else if (strcmp(subscription, "from") == 0) { + item->sub = SUB_FROM; + } else if (strcmp(subscription, "both") == 0) { + item->sub = SUB_BOTH; + } else { + printf("Invalid subscription '%s'!\n", subscription); + return; + } + item->next = NULL; + + // add item to roster datastructure + if (roster == NULL) { + roster = item; + } else { + roster_item_t* current = roster; + while (current->next != NULL) // loop until end of linked list + current = current->next; + current->next = item; + } + + // notify GUI + void* widget = gui_add_roster_item(jid, subscription, name); + item->widget = widget; // adds pointer to GUI widget to roster_item_t + +} diff --git a/data.h b/data.h new file mode 100644 index 0000000..7536227 --- /dev/null +++ b/data.h @@ -0,0 +1 @@ +void add_roster_item(const char* jid, const char* subscription, const char* name); diff --git a/gtk.c b/gtk.c index 47caac0..19af543 100644 --- a/gtk.c +++ b/gtk.c @@ -269,11 +269,13 @@ void gui_add_message(const char* sender_jid, const char* content) { printf("Received from %s:\n%s\n", sender_jid, content); } -void gui_add_roster_item(const char* jid, const char* sub, const char* name) { +void* gui_add_roster_item(const char* jid, const char* sub, const char* name) { if (name) { printf("roster item: %s, %s, sub:%s\n", name, jid, sub); } else { printf("roster item: (no name), %s, sub:%s\n", jid, sub); } + + return NULL; // TODO return pointer to GTK widget } diff --git a/gui.h b/gui.h index ab59d3d..4cb1fd2 100644 --- a/gui.h +++ b/gui.h @@ -7,4 +7,4 @@ void gui_disconnected(void); void gui_suspended(void); void gui_resumed(void); void gui_add_message(const char* sender_jid, const char* content); -void gui_add_roster_item(const char* jid, const char* sub, const char* name); +void* gui_add_roster_item(const char* jid, const char* sub, const char* name); diff --git a/limox.c b/limox.c index 123ca53..5bdaa7b 100644 --- a/limox.c +++ b/limox.c @@ -11,6 +11,7 @@ #include #include +#include // the state of limox @@ -66,9 +67,9 @@ static int roster_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, for (xmpp_stanza_t* item = xmpp_stanza_get_children(query); item; item = xmpp_stanza_get_next(item)) { - gui_add_roster_item(xmpp_stanza_get_attribute(item, "jid"), - xmpp_stanza_get_attribute(item, "subscription"), - xmpp_stanza_get_attribute(item, "name")); + add_roster_item(xmpp_stanza_get_attribute(item, "jid"), + xmpp_stanza_get_attribute(item, "subscription"), + xmpp_stanza_get_attribute(item, "name")); } return 1; @@ -207,7 +208,8 @@ void limox_disconnect(void) { if (conn != NULL && xmpp_conn_is_connected(conn)) { xmpp_disconnect(conn); while (xmpp_conn_is_connected(conn)) { - xmpp_run_once(ctx, 200); + xmpp_run_once(ctx, 200); // TODO avoid with additional + // state "disconnecting" } if (!xmpp_conn_release(conn)) { printf("DEBUG: Could not free connection!\n"); diff --git a/meson.build b/meson.build index 34a33e4..a16a3c0 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ project('LimoX', 'c') gtkdep = dependency('gtk4') strophedep = dependency('libstrophe') -executable('limox', ['main.c', 'gtk.c', 'limox.c'], dependencies : [gtkdep, strophedep]) +executable('limox', ['main.c', 'gtk.c', 'limox.c', 'data.c'], dependencies : [gtkdep, strophedep]) -- cgit v1.2.3-70-g09d2