aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--src/ui/accounts.c118
-rw-r--r--src/ui/accounts.h18
3 files changed, 137 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 000c792..24f771d 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,8 @@ INSTALL_DIR ?= /usr/local/
5BINARY = messenger-cli 5BINARY = messenger-cli
6SOURCES = messenger_cli.c\ 6SOURCES = messenger_cli.c\
7 application.c\ 7 application.c\
8 chat.c 8 chat.c\
9 ui/accounts.c
9HEADERS = 10HEADERS =
10 11
11LIBRARIES = gnunetchat gnunetutil ncurses 12LIBRARIES = gnunetchat gnunetutil ncurses
diff --git a/src/ui/accounts.c b/src/ui/accounts.c
new file mode 100644
index 0000000..067e781
--- /dev/null
+++ b/src/ui/accounts.c
@@ -0,0 +1,118 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/accounts.c
23 */
24
25#include "accounts.h"
26#include "../application.h"
27
28int
29_accounts_iterate(void *cls,
30 UNUSED const struct GNUNET_CHAT_Handle *handle,
31 struct GNUNET_CHAT_Account *account)
32{
33 UI_ACCOUNTS_Handle *accounts = cls;
34
35 if (accounts->line_index++ == accounts->line_selected)
36 accounts->selected = account;
37
38 return GNUNET_NO;
39}
40
41void
42accounts_event(UI_ACCOUNTS_Handle *accounts,
43 struct MESSENGER_Application *app,
44 int key)
45{
46 accounts->line_index = 0;
47 accounts->selected = NULL;
48
49 int count = GNUNET_CHAT_iterate_accounts(
50 app->chat.handle,
51 &_accounts_iterate,
52 accounts
53 );
54
55 switch (key)
56 {
57 case KEY_UP:
58 {
59 accounts->line_selected--;
60 break;
61 }
62 case KEY_DOWN:
63 {
64 accounts->line_selected++;
65 break;
66 }
67 case KEY_ENTER:
68 {
69 if (accounts->selected)
70 GNUNET_CHAT_connect(app->chat.handle, accounts->selected);
71
72 break;
73 }
74 default:
75 if (accounts->line_selected < 0)
76 accounts->line_selected = 0;
77 else if (accounts->line_selected >= count)
78 accounts->line_selected = count - 1;
79 break;
80 }
81}
82
83int
84_accounts_iterate_print(void *cls,
85 UNUSED const struct GNUNET_CHAT_Handle *handle,
86 struct GNUNET_CHAT_Account *account)
87{
88 UI_ACCOUNTS_Handle *accounts = cls;
89
90 const int y = accounts->line_index - accounts->line_offset;
91
92 if (accounts->line_index++ < accounts->line_offset)
93 return GNUNET_YES;
94
95 const int height = getmaxy(accounts->window) - getbegy(accounts->window);
96
97 if (y >= height)
98 return GNUNET_NO;
99
100 const char *name = GNUNET_CHAT_account_get_name(account);
101
102 move(y, 0);
103 wprintw(accounts->window, "%s", name);
104 return GNUNET_YES;
105}
106
107void
108accounts_print(UI_ACCOUNTS_Handle *accounts,
109 struct MESSENGER_Application *app)
110{
111 accounts->line_index = 0;
112
113 GNUNET_CHAT_iterate_accounts(
114 app->chat.handle,
115 &_accounts_iterate_print,
116 accounts
117 );
118}
diff --git a/src/ui/accounts.h b/src/ui/accounts.h
index 1373502..63b0af2 100644
--- a/src/ui/accounts.h
+++ b/src/ui/accounts.h
@@ -28,11 +28,27 @@
28#include <stdlib.h> 28#include <stdlib.h>
29#include <curses.h> 29#include <curses.h>
30 30
31struct UI_ACCOUNTS_Handle 31struct MESSENGER_Application;
32struct GNUNET_CHAT_Account;
33
34typedef struct UI_ACCOUNTS_Handle
32{ 35{
33 WINDOW *window; 36 WINDOW *window;
37
38 int line_index;
39 int line_offset;
40 int line_selected;
41
42 struct GNUNET_CHAT_Account *selected;
34} UI_ACCOUNTS_Handle; 43} UI_ACCOUNTS_Handle;
35 44
45void
46accounts_event(UI_ACCOUNTS_Handle *accounts,
47 struct MESSENGER_Application *app,
48 int key);
36 49
50void
51accounts_print(UI_ACCOUNTS_Handle *accounts,
52 struct MESSENGER_Application *app);
37 53
38#endif /* UI_ACCOUNTS_H_ */ 54#endif /* UI_ACCOUNTS_H_ */