summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-04-17 23:31:37 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-04-17 23:31:37 +0200
commitd75a06e86407204e302fe3a49d8e2c137d1fbe2b (patch)
treec9bcc9673d31bad8dc0716ed1d6f223fcc0a43e9
parent85e489d74a931a1bbc809933b53e593da0f880a7 (diff)
Added very basic messages list
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--Makefile12
-rw-r--r--src/application.h7
-rw-r--r--src/chat.c82
-rw-r--r--src/chat.h4
-rw-r--r--src/messenger_cli.c84
-rw-r--r--src/ui/messages.c80
-rw-r--r--src/ui/messages.h64
7 files changed, 202 insertions, 131 deletions
diff --git a/Makefile b/Makefile
index 24f771d..237681e 100644
--- a/Makefile
+++ b/Makefile
@@ -4,10 +4,14 @@ INSTALL_DIR ?= /usr/local/
BINARY = messenger-cli
SOURCES = messenger_cli.c\
- application.c\
- chat.c\
- ui/accounts.c
-HEADERS =
+ application.c\
+ chat.c\
+ ui/accounts.c\
+ ui/messages.c
+HEADERS = application.h\
+ chat.h\
+ ui/accounts.h\
+ ui/messages.h
LIBRARIES = gnunetchat gnunetutil ncurses
diff --git a/src/application.h b/src/application.h
index 16909c9..0eeb37e 100644
--- a/src/application.h
+++ b/src/application.h
@@ -30,7 +30,9 @@
#include "chat.h"
#include "util.h"
-//#include "ui/accounts.h"
+
+#include "ui/accounts.h"
+#include "ui/messages.h"
typedef struct MESSENGER_Application
{
@@ -40,6 +42,9 @@ typedef struct MESSENGER_Application
int status;
MESSENGER_Chat chat;
+
+ UI_ACCOUNTS_Handle accounts;
+ UI_MESSAGES_Handle messages;
} MESSENGER_Application;
void
diff --git a/src/chat.c b/src/chat.c
index c4b67b2..4645c6b 100644
--- a/src/chat.c
+++ b/src/chat.c
@@ -25,36 +25,24 @@
#include "chat.h"
#include "application.h"
-#include "ui/accounts.h"
-UI_ACCOUNTS_Handle accounts;
-
-int lc = 0;
-
-static int
-_chat_message(UNUSED void *cls,
- UNUSED struct GNUNET_CHAT_Context *context,
- UNUSED const struct GNUNET_CHAT_Message *message)
+static void
+_chat_refresh(MESSENGER_Application *app)
{
- // MESSENGER_Application *app = cls;
-
- enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(
- message
+ const struct GNUNET_CHAT_Account *account = GNUNET_CHAT_get_connected(
+ app->chat.handle
);
- move(lc++, 50);
- printw("TEST %d", (int) kind);
+ app->accounts.window = NULL;
+ app->messages.window = NULL;
- return GNUNET_YES;
-}
+ if (!account)
+ app->accounts.window = stdscr;
+ else if (app->chat.context)
+ app->messages.window = stdscr;
-static void
-_chat_refresh(MESSENGER_Application *app)
-{
- // TODO
-
- accounts.window = stdscr;
- accounts_print(&accounts, app);
+ accounts_print(&(app->accounts), app);
+ messages_print(&(app->messages), app);
}
static int
@@ -64,19 +52,44 @@ _chat_event(MESSENGER_Application *app,
if (key < 0)
goto refresh;
+ const struct GNUNET_CHAT_Account *account = GNUNET_CHAT_get_connected(
+ app->chat.handle
+ );
+
if ('q' == key)
return 1;
- move(lc++, 0);
- printw("KEY %d", key);
+ if (!account)
+ accounts_event(&(app->accounts), app, key);
+ else if (app->chat.context)
+ messages_event(&(app->messages), app, key);
+ else
+ {
+ struct GNUNET_CHAT_Group *test = GNUNET_CHAT_group_create(
+ app->chat.handle,
+ "test"
+ );
- accounts_event(&accounts, app, key);
+ app->chat.context = GNUNET_CHAT_group_get_context(test);
+ }
refresh:
_chat_refresh(app);
return 0;
}
+int lc = 0;
+
+static int
+_chat_message(void *cls,
+ UNUSED struct GNUNET_CHAT_Context *context,
+ UNUSED const struct GNUNET_CHAT_Message *message)
+{
+ MESSENGER_Application *app = cls;
+ _chat_event(app, KEY_RESIZE);
+ return GNUNET_YES;
+}
+
static void
_chat_idle(void *cls)
{
@@ -107,11 +120,12 @@ chat_start(MESSENGER_Chat *chat,
{
chat->handle = GNUNET_CHAT_start(
cfg,
- "appdir",
&_chat_message,
app
);
+ chat->context = NULL;
+
chat->idle = GNUNET_SCHEDULER_add_now(
&_chat_idle,
app
@@ -127,18 +141,6 @@ chat_stop(MESSENGER_Chat *chat)
chat->idle = NULL;
}
- if (chat->key)
- {
- GNUNET_SCHEDULER_cancel(chat->key);
- chat->key = NULL;
- }
-
- if (chat->fdset)
- {
- GNUNET_NETWORK_fdset_destroy(chat->fdset);
- chat->fdset = NULL;
- }
-
GNUNET_CHAT_stop(chat->handle);
chat->handle = NULL;
}
diff --git a/src/chat.h b/src/chat.h
index 8248da3..0635336 100644
--- a/src/chat.h
+++ b/src/chat.h
@@ -34,8 +34,8 @@ struct MESSENGER_Application;
typedef struct MESSENGER_Chat
{
struct GNUNET_CHAT_Handle *handle;
- struct GNUNET_NETWORK_FDSet *fdset;
- struct GNUNET_SCHEDULER_Task *key;
+ struct GNUNET_CHAT_Context *context;
+
struct GNUNET_SCHEDULER_Task *idle;
} MESSENGER_Chat;
diff --git a/src/messenger_cli.c b/src/messenger_cli.c
index 0c9fda2..1c68b9a 100644
--- a/src/messenger_cli.c
+++ b/src/messenger_cli.c
@@ -24,90 +24,6 @@
#include "application.h"
-/*static void
-run (void *cls, char* const* args,
- const char *cfgfile,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
-{
- struct GNUNET_CHAT_Handle *handle = GNUNET_CHAT_start(
- cfg,
- "appdir",
- NULL, NULL
- );
-
- initscr();
- noecho();
-
- int bx, by, mx, my;
- getbegyx(stdscr, by, bx);
- getmaxyx(stdscr, my, mx);
-
- WINDOW *win = newwin(15, 30, by + (my - by - 15) / 2, bx + (mx - bx - 30) / 2);
- keypad(win, TRUE);
-
- int selected = 0;
-
- int c;
- do {
- getbegyx(stdscr, by, bx);
- getmaxyx(stdscr, my, mx);
-
- int x = bx + (mx - bx - 30) / 2;
- int y = by + (my - by - 15) / 2;
-
- int w = 30;
- int h = 15;
-
- if (mx - x < w)
- w = (mx - x > 0? mx - x : 0);
-
- if (my - y < h)
- h = (my - y > 0? my - y : 0);
-
- if (w * h > 0)
- {
- mvwin(win, y, x);
- wresize(win, h, w);
-
- werase(win);
- box(win, 0, 0);
-
- wmove(win, 1, 1);
- wprintw(win, "%d %d, %d %d | %d %d", bx, by, mx, my, c, KEY_DOWN);
-
- const int attrs_select = A_BOLD;
-
- for (int i = 0; i < 5; i++) {
- if (i == selected) wattron(win, attrs_select);
-
- wmove(win, i+2, 1);
- wprintw(win, "Option %d", i+1);
-
- if (i == selected) wattroff(win, attrs_select);
- }
-
- wmove(win, 7, 1);
- c = wgetch(win);
- }
- else
- {
- c = getch();
- }
-
- if (KEY_UP == c) selected = (selected > 0? selected - 1 : 0);
- else if (KEY_DOWN == c) selected = (selected < 4? selected + 1 : 4);
-
- clear();
- refresh();
- } while (c != 'q');
-
- delwin(win);
-
- endwin();
-
- GNUNET_CHAT_stop(handle);
-}*/
-
int
main (int argc, char** argv)
{
diff --git a/src/ui/messages.c b/src/ui/messages.c
new file mode 100644
index 0000000..780dd45
--- /dev/null
+++ b/src/ui/messages.c
@@ -0,0 +1,80 @@
+/*
+ This file is part of GNUnet.
+ Copyright (C) 2022 GNUnet e.V.
+
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
+ */
+/*
+ * @author Tobias Frisch
+ * @file ui/messages.c
+ */
+
+#include "messages.h"
+
+#include "../application.h"
+
+void
+messages_event(UI_MESSAGES_Handle *messages,
+ struct MESSENGER_Application *app,
+ int key)
+{
+ // TODO
+}
+
+int
+_messages_iterate_print(void *cls,
+ struct GNUNET_CHAT_Context *context,
+ const struct GNUNET_CHAT_Message *message)
+{
+ UI_MESSAGES_Handle *messages = cls;
+
+ const int y = messages->line_index++;
+
+ enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message);
+ const char *text = GNUNET_CHAT_message_get_text(message);
+
+ struct GNUNET_TIME_Absolute timestamp = GNUNET_CHAT_message_get_timestamp(
+ message
+ );
+
+ wmove(messages->window, y, 0);
+ wprintw(
+ messages->window,
+ "%s | [%d]: %s",
+ GNUNET_TIME_absolute2s(timestamp),
+ (int) kind,
+ text
+ );
+
+ return GNUNET_YES;
+}
+
+void
+messages_print(UI_MESSAGES_Handle *messages,
+ struct MESSENGER_Application *app)
+{
+ if (!(messages->window))
+ return;
+
+ struct GNUNET_CHAT_Context *context = app->chat.context;
+
+ messages->line_index = 0;
+ GNUNET_CHAT_context_iterate_messages(
+ context,
+ _messages_iterate_print,
+ messages
+ );
+}
diff --git a/src/ui/messages.h b/src/ui/messages.h
new file mode 100644
index 0000000..305ad01
--- /dev/null
+++ b/src/ui/messages.h
@@ -0,0 +1,64 @@
+/*
+ This file is part of GNUnet.
+ Copyright (C) 2022 GNUnet e.V.
+
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
+ */
+/*
+ * @author Tobias Frisch
+ * @file ui/messages.h
+ */
+
+#ifndef UI_MESSAGES_H_
+#define UI_MESSAGES_H_
+
+#include <stdlib.h>
+#include <curses.h>
+
+#include <gnunet/platform.h>
+#include <gnunet/gnunet_chat_lib.h>
+#include <gnunet/gnunet_util_lib.h>
+
+struct MESSENGER_Application;
+
+typedef struct UI_MESSAGES_List
+{
+ const struct GNUNET_CHAT_Message *message;
+
+ struct UI_MESSAGES_List *prev;
+ struct UI_MESSAGES_List *next;
+} UI_MESSAGES_List;
+
+typedef struct UI_MESSAGES_Handle
+{
+ WINDOW *window;
+
+ UI_MESSAGES_List *head;
+ UI_MESSAGES_List *tail;
+
+ int line_index;
+} UI_MESSAGES_Handle;
+
+void
+messages_event(UI_MESSAGES_Handle *messages,
+ struct MESSENGER_Application *app,
+ int key);
+
+void
+messages_print(UI_MESSAGES_Handle *messages,
+ struct MESSENGER_Application *app);
+
+#endif /* UI_MESSAGES_H_ */