From 07bdb5aed83867106b0d7a87969e3164aefee9d6 Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Thu, 19 May 2022 23:58:50 +0200 Subject: Added account creation as option to the list Signed-off-by: TheJackiMonster --- Makefile | 2 + src/ui/account_create.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/account_create.h | 51 ++++++++++++++++++++ src/ui/accounts.c | 45 +++++++++++++----- src/ui/accounts.h | 4 ++ 5 files changed, 213 insertions(+), 11 deletions(-) create mode 100644 src/ui/account_create.c create mode 100644 src/ui/account_create.h diff --git a/Makefile b/Makefile index b15dffe..84d5942 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,13 @@ BINARY = messenger-cli SOURCES = messenger_cli.c\ application.c\ chat.c\ + ui/account_create.c\ ui/accounts.c\ ui/chats.c\ ui/messages.c HEADERS = application.h\ chat.h\ + ui/account_create.h\ ui/accounts.h\ ui/chats.h\ 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 @@ +/* + 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 . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/account_create.c + */ + +#include "account_create.h" + +#include + +#include +#include +#include + +#include "../application.h" +#include "../util.h" + +void +account_create_event(UI_ACCOUNT_CREATE_Handle *create, + struct MESSENGER_Application *app, + int key) +{ + switch (key) + { + case 27: + case KEY_EXIT: + { + create->window = NULL; + break; + } + case KEY_LEFT: + { + create->name_pos--; + break; + } + case KEY_RIGHT: + { + create->name_pos++; + break; + } + case '\n': + case KEY_ENTER: + { + if (create->name_len > 0) + GNUNET_CHAT_account_create(app->chat.handle, create->name); + + create->name_len = 0; + create->window = NULL; + break; + } + case KEY_BACKSPACE: + { + if ((create->name_pos < create->name_len) && + (create->name_pos > 0)) + for (int i = create->name_pos; i < create->name_len; i++) + create->name[i - 1] = create->name[i]; + + if ((create->name_pos > 0) && (create->name_len > 0)) + { + create->name_pos--; + create->name_len--; + } + + break; + } + default: + { + if (!isalnum(key)) + break; + + for (int i = create->name_len - 1; i >= create->name_pos; i--) + create->name[i + 1] = create->name[i]; + + create->name[create->name_pos++] = (char) key; + create->name_len++; + break; + } + } + + if (create->name_len > 255) + create->name_len = 255; + + create->name[create->name_len] = '\0'; + + if (create->name_pos < 0) + create->name_pos = 0; + + if (create->name_pos > create->name_len) + create->name_pos = create->name_len; +} + +void +account_create_print(UI_ACCOUNT_CREATE_Handle *create, + UNUSED struct MESSENGER_Application *app) +{ + if (!(create->window)) + return; + + werase(create->window); + wmove(create->window, 0, 0); + + wprintw(create->window, "%s", create->name); + wmove(create->window, 0, create->name_pos); +} 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 @@ +/* + 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 . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/account_create.h + */ + +#ifndef UI_ACCOUNT_CREATE_H_ +#define UI_ACCOUNT_CREATE_H_ + +#include +#include + +struct MESSENGER_Application; + +typedef struct UI_ACCOUNT_CREATE_Handle +{ + WINDOW *window; + + char name [256]; + int name_len; + int name_pos; +} UI_ACCOUNT_CREATE_Handle; + +void +account_create_event(UI_ACCOUNT_CREATE_Handle *create, + struct MESSENGER_Application *app, + int key); + +void +account_create_print(UI_ACCOUNT_CREATE_Handle *create, + struct MESSENGER_Application *app); + +#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, MESSENGER_Application *app, int key) { + if (accounts->create.window) + { + account_create_event(&(accounts->create), app, key); + return; + } + accounts->line_index = 0; accounts->selected = NULL; @@ -56,7 +62,7 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, app->chat.handle, &_accounts_iterate, accounts - ); + ) + 1; switch (key) { @@ -81,7 +87,8 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, { if (accounts->selected) GNUNET_CHAT_connect(app->chat.handle, accounts->selected); - + else + accounts->create.window = accounts->window; break; } default: @@ -110,13 +117,11 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, accounts->line_offset = count - 1; } -int -_accounts_iterate_print(void *cls, - UNUSED const struct GNUNET_CHAT_Handle *handle, - struct GNUNET_CHAT_Account *account) +static int +_accounts_print_entry(UI_ACCOUNTS_Handle *accounts, + char type, + const char *text) { - UI_ACCOUNTS_Handle *accounts = cls; - const bool selected = (accounts->line_selected == accounts->line_index); const int y = accounts->line_index - accounts->line_offset; @@ -130,24 +135,40 @@ _accounts_iterate_print(void *cls, if (y >= height) return GNUNET_NO; - const char *name = GNUNET_CHAT_account_get_name(account); - const int attrs_select = A_BOLD; if (selected) wattron(accounts->window, attrs_select); wmove(accounts->window, y, 0); - wprintw(accounts->window, "%s", name); + wprintw(accounts->window, "[%c] %s", selected? type : ' ', text); if (selected) wattroff(accounts->window, attrs_select); return GNUNET_YES; } +int +_accounts_iterate_print(void *cls, + UNUSED const struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Account *account) +{ + UI_ACCOUNTS_Handle *accounts = cls; + + const char *name = GNUNET_CHAT_account_get_name(account); + + return _accounts_print_entry(accounts, 'x', name); +} + void accounts_print(UI_ACCOUNTS_Handle *accounts, MESSENGER_Application *app) { + if (accounts->create.window) + { + account_create_print(&(accounts->create), app); + return; + } + if (!(accounts->window)) return; @@ -159,4 +180,6 @@ accounts_print(UI_ACCOUNTS_Handle *accounts, &_accounts_iterate_print, accounts ); + + _accounts_print_entry(accounts, '+', "Add account"); } 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 @@ #include #include +#include "account_create.h" + struct MESSENGER_Application; typedef struct UI_ACCOUNTS_Handle @@ -43,6 +45,8 @@ typedef struct UI_ACCOUNTS_Handle int line_selected; struct GNUNET_CHAT_Account *selected; + + UI_ACCOUNT_CREATE_Handle create; } UI_ACCOUNTS_Handle; void -- cgit v1.2.3