aboutsummaryrefslogtreecommitdiff
path: root/src/ui/accounts.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/accounts.c')
-rw-r--r--src/ui/accounts.c118
1 files changed, 118 insertions, 0 deletions
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}