diff options
author | xengineering <me@xengineering.eu> | 2022-08-17 11:39:16 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2022-08-17 11:39:16 +0200 |
commit | d82f48718bc6c69d734d0306867b866efc2f9719 (patch) | |
tree | 6f0d3798018af0c41729810c55546b89450e16f3 | |
parent | 94cca91f9c0036a5ceb95ff4099d2cdc3d9265af (diff) | |
download | limox-d82f48718bc6c69d734d0306867b866efc2f9719.tar limox-d82f48718bc6c69d734d0306867b866efc2f9719.tar.zst limox-d82f48718bc6c69d734d0306867b866efc2f9719.zip |
Implement datastructure for roster
-rw-r--r-- | data.c | 65 | ||||
-rw-r--r-- | data.h | 1 | ||||
-rw-r--r-- | gtk.c | 4 | ||||
-rw-r--r-- | gui.h | 2 | ||||
-rw-r--r-- | limox.c | 10 | ||||
-rw-r--r-- | meson.build | 2 |
6 files changed, 77 insertions, 7 deletions
@@ -0,0 +1,65 @@ + + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +#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 + +} @@ -0,0 +1 @@ +void add_roster_item(const char* jid, const char* subscription, const char* name); @@ -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 } @@ -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); @@ -11,6 +11,7 @@ #include <gui.h> #include <limox.h> +#include <data.h> // 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]) |