summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2022-08-19 10:51:24 +0200
committerxengineering <me@xengineering.eu>2022-08-19 12:13:49 +0200
commit43aa02ccbd484d8c7cecec7c38c446847b34e007 (patch)
tree5e34779ee75c8788760d74a9f73a01575c335f54 /data.c
parent4c848f4085cd174d1ad54169632a95023774717b (diff)
downloadlimox-43aa02ccbd484d8c7cecec7c38c446847b34e007.tar
limox-43aa02ccbd484d8c7cecec7c38c446847b34e007.tar.zst
limox-43aa02ccbd484d8c7cecec7c38c446847b34e007.zip
Implement new datastructures
Diffstat (limited to 'data.c')
-rw-r--r--data.c95
1 files changed, 53 insertions, 42 deletions
diff --git a/data.c b/data.c
index c0a0592..afa4616 100644
--- a/data.c
+++ b/data.c
@@ -5,36 +5,34 @@
#include <stdio.h>
#include "gui.h"
+#include "data.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;
+static roster_item_t* roster = NULL;
-typedef struct _roster_item_t {
- const char* name;
- const char* jid;
- subscription_t sub;
- void* widget; // the roster item in the contact list view
- void* page_widget; // the chat page corresponding to this roster item
- struct _roster_item_t* next;
-} roster_item_t;
+void data_add_roster_item(const char* jid, const char* subscription,
+ const char* name) {
-// the RAM-stored data payload
-static roster_item_t* roster = NULL;
+ // FIXME check if roster item already exists and handle updates
+ // allocate datastructures
+ roster_item_t* item = malloc(sizeof(roster_item_t));
+ chat_t* chat = malloc(sizeof(chat_t));
-void add_roster_item(const char* jid, const char* subscription, const char* name) {
+ // initialize chat
+ chat->messages = NULL;
+ gui_add_chat_widget(chat);
- // parse roster item
- roster_item_t* item = malloc(sizeof(roster_item_t));
- item->name = name;
- item->jid = jid;
+ // initialize roster item
+ if (name == NULL) {
+ item->name = NULL;
+ } else {
+ item->name = malloc(sizeof(char)*strlen(name));
+ strcpy(item->name, name);
+ }
+ item->jid = malloc(sizeof(char)*strlen(jid));
+ strcpy(item->jid, jid);
if (strcmp(subscription, "none") == 0) {
item->sub = SUB_NONE;
} else if (strcmp(subscription, "to") == 0) {
@@ -47,7 +45,9 @@ void add_roster_item(const char* jid, const char* subscription, const char* name
printf("Invalid subscription '%s'!\n", subscription);
return;
}
+ item->chat = chat;
item->next = NULL;
+ gui_add_roster_item_widget(item);
// add item to roster datastructure
if (roster == NULL) {
@@ -59,29 +59,40 @@ void add_roster_item(const char* jid, const char* subscription, const char* name
current->next = item;
}
- // notify GUI
- item->widget = gui_add_roster_item(jid, subscription, name);
- item->page_widget = gui_add_chat();
-
}
-void* data_get_chat_page(void* roster_item_widget) {
-
- if (roster == NULL) {
- printf("Empty roster!\n");
- exit(1);
- } else {
- roster_item_t* current = roster;
- while (1) {
- if (current->widget == roster_item_widget) {
- return current->page_widget;
- }
- if (current->next == NULL) {
- printf("Could not find matching roster item!\n");
- exit(1);
- }
- current = current->next;
+void data_add_incoming_message(const char* sender_jid, const char* content) {
+
+ // find correct chat
+ chat_t* chat = NULL;
+ roster_item_t* i;
+ int bare_len; // length of bare JID
+ for (i = roster; i != NULL; i=i->next) {
+ bare_len = strlen(i->jid);
+ if (strncmp(i->jid, sender_jid, bare_len) == 0) {
+ chat = i->chat;
+ break;
}
}
+ if (chat == NULL) {
+ printf("Could not find chat for message from '%s'!\n", sender_jid);
+ return;
+ }
+
+ // initialize message_t
+ message_t* msg = malloc(sizeof(message_t));
+ msg->sender_jid = malloc(sizeof(char)*strlen(sender_jid));
+ strcpy(msg->sender_jid, sender_jid);
+ msg->content = malloc(sizeof(char)*strlen(content));
+ strcpy(msg->content, content);
+ msg->next = NULL;
+ gui_add_message_widget(msg, i->chat);
+
+ // find pointer to next message of chat
+ message_t* m;
+ for (m = chat->messages; m != NULL; m=m->next);
+
+ // append message to chat
+ m = msg;
}