aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-05-19 23:58:50 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-05-19 23:58:50 +0200
commit07bdb5aed83867106b0d7a87969e3164aefee9d6 (patch)
treedad1cff377357531e0dc86892a06f0db6b97b49b
parent31f06baed66c24e82e3cf50326ef66ec2b8ab8c6 (diff)
downloadmessenger-cli-07bdb5aed83867106b0d7a87969e3164aefee9d6.tar.gz
messenger-cli-07bdb5aed83867106b0d7a87969e3164aefee9d6.zip
Added account creation as option to the list
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--Makefile2
-rw-r--r--src/ui/account_create.c122
-rw-r--r--src/ui/account_create.h51
-rw-r--r--src/ui/accounts.c45
-rw-r--r--src/ui/accounts.h4
5 files changed, 213 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index b15dffe..84d5942 100644
--- a/Makefile
+++ b/Makefile
@@ -6,11 +6,13 @@ BINARY = messenger-cli
6SOURCES = messenger_cli.c\ 6SOURCES = messenger_cli.c\
7 application.c\ 7 application.c\
8 chat.c\ 8 chat.c\
9 ui/account_create.c\
9 ui/accounts.c\ 10 ui/accounts.c\
10 ui/chats.c\ 11 ui/chats.c\
11 ui/messages.c 12 ui/messages.c
12HEADERS = application.h\ 13HEADERS = application.h\
13 chat.h\ 14 chat.h\
15 ui/account_create.h\
14 ui/accounts.h\ 16 ui/accounts.h\
15 ui/chats.h\ 17 ui/chats.h\
16 ui/messages.h 18 ui/messages.h
diff --git a/src/ui/account_create.c b/src/ui/account_create.c
new file mode 100644
index 0000000..947317d
--- /dev/null
+++ b/src/ui/account_create.c
@@ -0,0 +1,122 @@
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/account_create.c
23 */
24
25#include "account_create.h"
26
27#include <ctype.h>
28
29#include <gnunet/platform.h>
30#include <gnunet/gnunet_chat_lib.h>
31#include <gnunet/gnunet_util_lib.h>
32
33#include "../application.h"
34#include "../util.h"
35
36void
37account_create_event(UI_ACCOUNT_CREATE_Handle *create,
38 struct MESSENGER_Application *app,
39 int key)
40{
41 switch (key)
42 {
43 case 27:
44 case KEY_EXIT:
45 {
46 create->window = NULL;
47 break;
48 }
49 case KEY_LEFT:
50 {
51 create->name_pos--;
52 break;
53 }
54 case KEY_RIGHT:
55 {
56 create->name_pos++;
57 break;
58 }
59 case '\n':
60 case KEY_ENTER:
61 {
62 if (create->name_len > 0)
63 GNUNET_CHAT_account_create(app->chat.handle, create->name);
64
65 create->name_len = 0;
66 create->window = NULL;
67 break;
68 }
69 case KEY_BACKSPACE:
70 {
71 if ((create->name_pos < create->name_len) &&
72 (create->name_pos > 0))
73 for (int i = create->name_pos; i < create->name_len; i++)
74 create->name[i - 1] = create->name[i];
75
76 if ((create->name_pos > 0) && (create->name_len > 0))
77 {
78 create->name_pos--;
79 create->name_len--;
80 }
81
82 break;
83 }
84 default:
85 {
86 if (!isalnum(key))
87 break;
88
89 for (int i = create->name_len - 1; i >= create->name_pos; i--)
90 create->name[i + 1] = create->name[i];
91
92 create->name[create->name_pos++] = (char) key;
93 create->name_len++;
94 break;
95 }
96 }
97
98 if (create->name_len > 255)
99 create->name_len = 255;
100
101 create->name[create->name_len] = '\0';
102
103 if (create->name_pos < 0)
104 create->name_pos = 0;
105
106 if (create->name_pos > create->name_len)
107 create->name_pos = create->name_len;
108}
109
110void
111account_create_print(UI_ACCOUNT_CREATE_Handle *create,
112 UNUSED struct MESSENGER_Application *app)
113{
114 if (!(create->window))
115 return;
116
117 werase(create->window);
118 wmove(create->window, 0, 0);
119
120 wprintw(create->window, "%s", create->name);
121 wmove(create->window, 0, create->name_pos);
122}
diff --git a/src/ui/account_create.h b/src/ui/account_create.h
new file mode 100644
index 0000000..3c06901
--- /dev/null
+++ b/src/ui/account_create.h
@@ -0,0 +1,51 @@
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/account_create.h
23 */
24
25#ifndef UI_ACCOUNT_CREATE_H_
26#define UI_ACCOUNT_CREATE_H_
27
28#include <stdlib.h>
29#include <curses.h>
30
31struct MESSENGER_Application;
32
33typedef struct UI_ACCOUNT_CREATE_Handle
34{
35 WINDOW *window;
36
37 char name [256];
38 int name_len;
39 int name_pos;
40} UI_ACCOUNT_CREATE_Handle;
41
42void
43account_create_event(UI_ACCOUNT_CREATE_Handle *create,
44 struct MESSENGER_Application *app,
45 int key);
46
47void
48account_create_print(UI_ACCOUNT_CREATE_Handle *create,
49 struct MESSENGER_Application *app);
50
51#endif /* UI_ACCOUNT_CREATE_H_ */
diff --git a/src/ui/accounts.c b/src/ui/accounts.c
index e933d5d..c3cdb85 100644
--- a/src/ui/accounts.c
+++ b/src/ui/accounts.c
@@ -49,6 +49,12 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
49 MESSENGER_Application *app, 49 MESSENGER_Application *app,
50 int key) 50 int key)
51{ 51{
52 if (accounts->create.window)
53 {
54 account_create_event(&(accounts->create), app, key);
55 return;
56 }
57
52 accounts->line_index = 0; 58 accounts->line_index = 0;
53 accounts->selected = NULL; 59 accounts->selected = NULL;
54 60
@@ -56,7 +62,7 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
56 app->chat.handle, 62 app->chat.handle,
57 &_accounts_iterate, 63 &_accounts_iterate,
58 accounts 64 accounts
59 ); 65 ) + 1;
60 66
61 switch (key) 67 switch (key)
62 { 68 {
@@ -81,7 +87,8 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
81 { 87 {
82 if (accounts->selected) 88 if (accounts->selected)
83 GNUNET_CHAT_connect(app->chat.handle, accounts->selected); 89 GNUNET_CHAT_connect(app->chat.handle, accounts->selected);
84 90 else
91 accounts->create.window = accounts->window;
85 break; 92 break;
86 } 93 }
87 default: 94 default:
@@ -110,13 +117,11 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
110 accounts->line_offset = count - 1; 117 accounts->line_offset = count - 1;
111} 118}
112 119
113int 120static int
114_accounts_iterate_print(void *cls, 121_accounts_print_entry(UI_ACCOUNTS_Handle *accounts,
115 UNUSED const struct GNUNET_CHAT_Handle *handle, 122 char type,
116 struct GNUNET_CHAT_Account *account) 123 const char *text)
117{ 124{
118 UI_ACCOUNTS_Handle *accounts = cls;
119
120 const bool selected = (accounts->line_selected == accounts->line_index); 125 const bool selected = (accounts->line_selected == accounts->line_index);
121 const int y = accounts->line_index - accounts->line_offset; 126 const int y = accounts->line_index - accounts->line_offset;
122 127
@@ -130,24 +135,40 @@ _accounts_iterate_print(void *cls,
130 if (y >= height) 135 if (y >= height)
131 return GNUNET_NO; 136 return GNUNET_NO;
132 137
133 const char *name = GNUNET_CHAT_account_get_name(account);
134
135 const int attrs_select = A_BOLD; 138 const int attrs_select = A_BOLD;
136 139
137 if (selected) wattron(accounts->window, attrs_select); 140 if (selected) wattron(accounts->window, attrs_select);
138 141
139 wmove(accounts->window, y, 0); 142 wmove(accounts->window, y, 0);
140 wprintw(accounts->window, "%s", name); 143 wprintw(accounts->window, "[%c] %s", selected? type : ' ', text);
141 144
142 if (selected) wattroff(accounts->window, attrs_select); 145 if (selected) wattroff(accounts->window, attrs_select);
143 146
144 return GNUNET_YES; 147 return GNUNET_YES;
145} 148}
146 149
150int
151_accounts_iterate_print(void *cls,
152 UNUSED const struct GNUNET_CHAT_Handle *handle,
153 struct GNUNET_CHAT_Account *account)
154{
155 UI_ACCOUNTS_Handle *accounts = cls;
156
157 const char *name = GNUNET_CHAT_account_get_name(account);
158
159 return _accounts_print_entry(accounts, 'x', name);
160}
161
147void 162void
148accounts_print(UI_ACCOUNTS_Handle *accounts, 163accounts_print(UI_ACCOUNTS_Handle *accounts,
149 MESSENGER_Application *app) 164 MESSENGER_Application *app)
150{ 165{
166 if (accounts->create.window)
167 {
168 account_create_print(&(accounts->create), app);
169 return;
170 }
171
151 if (!(accounts->window)) 172 if (!(accounts->window))
152 return; 173 return;
153 174
@@ -159,4 +180,6 @@ accounts_print(UI_ACCOUNTS_Handle *accounts,
159 &_accounts_iterate_print, 180 &_accounts_iterate_print,
160 accounts 181 accounts
161 ); 182 );
183
184 _accounts_print_entry(accounts, '+', "Add account");
162} 185}
diff --git a/src/ui/accounts.h b/src/ui/accounts.h
index 285f97e..a48411d 100644
--- a/src/ui/accounts.h
+++ b/src/ui/accounts.h
@@ -32,6 +32,8 @@
32#include <gnunet/gnunet_chat_lib.h> 32#include <gnunet/gnunet_chat_lib.h>
33#include <gnunet/gnunet_util_lib.h> 33#include <gnunet/gnunet_util_lib.h>
34 34
35#include "account_create.h"
36
35struct MESSENGER_Application; 37struct MESSENGER_Application;
36 38
37typedef struct UI_ACCOUNTS_Handle 39typedef struct UI_ACCOUNTS_Handle
@@ -43,6 +45,8 @@ typedef struct UI_ACCOUNTS_Handle
43 int line_selected; 45 int line_selected;
44 46
45 struct GNUNET_CHAT_Account *selected; 47 struct GNUNET_CHAT_Account *selected;
48
49 UI_ACCOUNT_CREATE_Handle create;
46} UI_ACCOUNTS_Handle; 50} UI_ACCOUNTS_Handle;
47 51
48void 52void