diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-05-20 22:50:50 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-05-20 22:50:50 +0200 |
commit | 05d021e3994fc03341568053371f6f60ec848504 (patch) | |
tree | 5d26cca6c10ec8c8b87ea6997a53750df691fc7d | |
parent | 07bdb5aed83867106b0d7a87969e3164aefee9d6 (diff) |
Added dialog to open chat with custom topic
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | src/ui/account_create.c | 122 | ||||
-rw-r--r-- | src/ui/account_create_dialog.c | 122 | ||||
-rw-r--r-- | src/ui/account_create_dialog.h (renamed from src/ui/account_create.h) | 22 | ||||
-rw-r--r-- | src/ui/accounts.c | 10 | ||||
-rw-r--r-- | src/ui/accounts.h | 4 | ||||
-rw-r--r-- | src/ui/chat_open_dialog.c | 122 | ||||
-rw-r--r-- | src/ui/chat_open_dialog.h | 51 | ||||
-rw-r--r-- | src/ui/chats.c | 50 | ||||
-rw-r--r-- | src/ui/chats.h | 4 |
10 files changed, 360 insertions, 153 deletions
@@ -6,14 +6,16 @@ BINARY = messenger-cli SOURCES = messenger_cli.c\ application.c\ chat.c\ - ui/account_create.c\ + ui/account_create_dialog.c\ ui/accounts.c\ + ui/chat_open_dialog.c\ ui/chats.c\ ui/messages.c HEADERS = application.h\ chat.h\ - ui/account_create.h\ + ui/account_create_dialog.h\ ui/accounts.h\ + ui/chat_open_dialog.h\ ui/chats.h\ ui/messages.h diff --git a/src/ui/account_create.c b/src/ui/account_create.c deleted file mode 100644 index 947317d..0000000 --- a/src/ui/account_create.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - 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/account_create.c - */ - -#include "account_create.h" - -#include <ctype.h> - -#include <gnunet/platform.h> -#include <gnunet/gnunet_chat_lib.h> -#include <gnunet/gnunet_util_lib.h> - -#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_dialog.c b/src/ui/account_create_dialog.c new file mode 100644 index 0000000..70e3fe3 --- /dev/null +++ b/src/ui/account_create_dialog.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 <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/account_create_dialog.c + */ + +#include "account_create_dialog.h" + +#include <ctype.h> + +#include <gnunet/platform.h> +#include <gnunet/gnunet_chat_lib.h> +#include <gnunet/gnunet_util_lib.h> + +#include "../application.h" +#include "../util.h" + +void +account_create_dialog_event(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app, + int key) +{ + switch (key) + { + case 27: + case KEY_EXIT: + { + create_dialog->window = NULL; + break; + } + case KEY_LEFT: + { + create_dialog->name_pos--; + break; + } + case KEY_RIGHT: + { + create_dialog->name_pos++; + break; + } + case '\n': + case KEY_ENTER: + { + if (create_dialog->name_len > 0) + GNUNET_CHAT_account_create(app->chat.handle, create_dialog->name); + + create_dialog->name_len = 0; + create_dialog->window = NULL; + break; + } + case KEY_BACKSPACE: + { + if ((create_dialog->name_pos < create_dialog->name_len) && + (create_dialog->name_pos > 0)) + for (int i = create_dialog->name_pos; i < create_dialog->name_len; i++) + create_dialog->name[i - 1] = create_dialog->name[i]; + + if ((create_dialog->name_pos > 0) && (create_dialog->name_len > 0)) + { + create_dialog->name_pos--; + create_dialog->name_len--; + } + + break; + } + default: + { + if (!isalnum(key)) + break; + + for (int i = create_dialog->name_len - 1; i >= create_dialog->name_pos; i--) + create_dialog->name[i + 1] = create_dialog->name[i]; + + create_dialog->name[create_dialog->name_pos++] = (char) key; + create_dialog->name_len++; + break; + } + } + + if (create_dialog->name_len > 255) + create_dialog->name_len = 255; + + create_dialog->name[create_dialog->name_len] = '\0'; + + if (create_dialog->name_pos < 0) + create_dialog->name_pos = 0; + + if (create_dialog->name_pos > create_dialog->name_len) + create_dialog->name_pos = create_dialog->name_len; +} + +void +account_create_dialog_print(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog, + UNUSED struct MESSENGER_Application *app) +{ + if (!(create_dialog->window)) + return; + + werase(create_dialog->window); + wmove(create_dialog->window, 0, 0); + + wprintw(create_dialog->window, "%s", create_dialog->name); + wmove(create_dialog->window, 0, create_dialog->name_pos); +} diff --git a/src/ui/account_create.h b/src/ui/account_create_dialog.h index 3c06901..69dbb1c 100644 --- a/src/ui/account_create.h +++ b/src/ui/account_create_dialog.h @@ -19,33 +19,33 @@ */ /* * @author Tobias Frisch - * @file ui/account_create.h + * @file ui/account_create_dialog.h */ -#ifndef UI_ACCOUNT_CREATE_H_ -#define UI_ACCOUNT_CREATE_H_ +#ifndef UI_ACCOUNT_CREATE_DIALOG_H_ +#define UI_ACCOUNT_CREATE_DIALOG_H_ #include <stdlib.h> #include <curses.h> struct MESSENGER_Application; -typedef struct UI_ACCOUNT_CREATE_Handle +typedef struct UI_ACCOUNT_CREATE_DIALOG_Handle { WINDOW *window; char name [256]; int name_len; int name_pos; -} UI_ACCOUNT_CREATE_Handle; +} UI_ACCOUNT_CREATE_DIALOG_Handle; void -account_create_event(UI_ACCOUNT_CREATE_Handle *create, - struct MESSENGER_Application *app, - int key); +account_create_dialog_event(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app, + int key); void -account_create_print(UI_ACCOUNT_CREATE_Handle *create, - struct MESSENGER_Application *app); +account_create_dialog_print(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app); -#endif /* UI_ACCOUNT_CREATE_H_ */ +#endif /* UI_ACCOUNT_CREATE_DIALOG_H_ */ diff --git a/src/ui/accounts.c b/src/ui/accounts.c index c3cdb85..377bdc9 100644 --- a/src/ui/accounts.c +++ b/src/ui/accounts.c @@ -49,9 +49,9 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, MESSENGER_Application *app, int key) { - if (accounts->create.window) + if (accounts->create_dialog.window) { - account_create_event(&(accounts->create), app, key); + account_create_dialog_event(&(accounts->create_dialog), app, key); return; } @@ -88,7 +88,7 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, if (accounts->selected) GNUNET_CHAT_connect(app->chat.handle, accounts->selected); else - accounts->create.window = accounts->window; + accounts->create_dialog.window = accounts->window; break; } default: @@ -163,9 +163,9 @@ void accounts_print(UI_ACCOUNTS_Handle *accounts, MESSENGER_Application *app) { - if (accounts->create.window) + if (accounts->create_dialog.window) { - account_create_print(&(accounts->create), app); + account_create_dialog_print(&(accounts->create_dialog), app); return; } diff --git a/src/ui/accounts.h b/src/ui/accounts.h index a48411d..2ca55b5 100644 --- a/src/ui/accounts.h +++ b/src/ui/accounts.h @@ -32,7 +32,7 @@ #include <gnunet/gnunet_chat_lib.h> #include <gnunet/gnunet_util_lib.h> -#include "account_create.h" +#include "account_create_dialog.h" struct MESSENGER_Application; @@ -46,7 +46,7 @@ typedef struct UI_ACCOUNTS_Handle struct GNUNET_CHAT_Account *selected; - UI_ACCOUNT_CREATE_Handle create; + UI_ACCOUNT_CREATE_DIALOG_Handle create_dialog; } UI_ACCOUNTS_Handle; void diff --git a/src/ui/chat_open_dialog.c b/src/ui/chat_open_dialog.c new file mode 100644 index 0000000..4ebe8be --- /dev/null +++ b/src/ui/chat_open_dialog.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 <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/chat_open_dialog.c + */ + +#include "chat_open_dialog.h" + +#include <ctype.h> + +#include <gnunet/platform.h> +#include <gnunet/gnunet_chat_lib.h> +#include <gnunet/gnunet_util_lib.h> + +#include "../application.h" +#include "../util.h" + +void +chat_open_dialog_event(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, + struct MESSENGER_Application *app, + int key) +{ + switch (key) + { + case 27: + case KEY_EXIT: + { + open_dialog->window = NULL; + break; + } + case KEY_LEFT: + { + open_dialog->topic_pos--; + break; + } + case KEY_RIGHT: + { + open_dialog->topic_pos++; + break; + } + case '\n': + case KEY_ENTER: + { + if (open_dialog->topic_len > 0) + GNUNET_CHAT_group_create(app->chat.handle, open_dialog->topic); + + open_dialog->topic_len = 0; + open_dialog->window = NULL; + break; + } + case KEY_BACKSPACE: + { + if ((open_dialog->topic_pos < open_dialog->topic_len) && + (open_dialog->topic_pos > 0)) + for (int i = open_dialog->topic_pos; i < open_dialog->topic_len; i++) + open_dialog->topic[i - 1] = open_dialog->topic[i]; + + if ((open_dialog->topic_pos > 0) && (open_dialog->topic_len > 0)) + { + open_dialog->topic_pos--; + open_dialog->topic_len--; + } + + break; + } + default: + { + if (!isalnum(key)) + break; + + for (int i = open_dialog->topic_len - 1; i >= open_dialog->topic_pos; i--) + open_dialog->topic[i + 1] = open_dialog->topic[i]; + + open_dialog->topic[open_dialog->topic_pos++] = (char) key; + open_dialog->topic_len++; + break; + } + } + + if (open_dialog->topic_len > 255) + open_dialog->topic_len = 255; + + open_dialog->topic[open_dialog->topic_len] = '\0'; + + if (open_dialog->topic_pos < 0) + open_dialog->topic_pos = 0; + + if (open_dialog->topic_pos > open_dialog->topic_len) + open_dialog->topic_pos = open_dialog->topic_len; +} + +void +chat_open_dialog_print(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, + UNUSED struct MESSENGER_Application *app) +{ + if (!(open_dialog->window)) + return; + + werase(open_dialog->window); + wmove(open_dialog->window, 0, 0); + + wprintw(open_dialog->window, "%s", open_dialog->topic); + wmove(open_dialog->window, 0, open_dialog->topic_pos); +} diff --git a/src/ui/chat_open_dialog.h b/src/ui/chat_open_dialog.h new file mode 100644 index 0000000..2adf465 --- /dev/null +++ b/src/ui/chat_open_dialog.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 <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/chat_open_dialog.h + */ + +#ifndef UI_CHAT_OPEN_DIALOG_H_ +#define UI_CHAT_OPEN_DIALOG_H_ + +#include <stdlib.h> +#include <curses.h> + +struct MESSENGER_Application; + +typedef struct UI_CHAT_OPEN_DIALOG_Handle +{ + WINDOW *window; + + char topic [256]; + int topic_len; + int topic_pos; +} UI_CHAT_OPEN_DIALOG_Handle; + +void +chat_open_dialog_event(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, + struct MESSENGER_Application *app, + int key); + +void +chat_open_dialog_print(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, + struct MESSENGER_Application *app); + +#endif /* UI_CHAT_OPEN_DIALOG_H_ */ diff --git a/src/ui/chats.c b/src/ui/chats.c index 991e43b..5d991a7 100644 --- a/src/ui/chats.c +++ b/src/ui/chats.c @@ -61,6 +61,12 @@ chats_event(UI_CHATS_Handle *chats, MESSENGER_Application *app, int key) { + if (chats->open_dialog.window) + { + chat_open_dialog_event(&(chats->open_dialog), app, key); + return; + } + chats->line_index = 0; chats->selected = NULL; @@ -68,7 +74,7 @@ chats_event(UI_CHATS_Handle *chats, app->chat.handle, &_chats_iterate, chats - ); + ) + 1; switch (key) { @@ -108,7 +114,8 @@ chats_event(UI_CHATS_Handle *chats, app->chat.context = chats->selected; } - + else + chats->open_dialog.window = chats->window; break; } default: @@ -137,13 +144,12 @@ chats_event(UI_CHATS_Handle *chats, chats->line_offset = count - 1; } -int -_chats_iterate_print(void *cls, - UNUSED struct GNUNET_CHAT_Handle *handle, - struct GNUNET_CHAT_Group *group) +static int +_chats_print_entry(UI_CHATS_Handle *chats, + char type, + char chat_type, + const char *text) { - UI_CHATS_Handle *chats = cls; - const bool selected = (chats->line_selected == chats->line_index); const int y = chats->line_index - chats->line_offset; @@ -157,24 +163,44 @@ _chats_iterate_print(void *cls, if (y >= height) return GNUNET_NO; - const char *name = GNUNET_CHAT_group_get_name(group); - const int attrs_select = A_BOLD; if (selected) wattron(chats->window, attrs_select); wmove(chats->window, y, 0); - wprintw(chats->window, "%s", name); + + if (chat_type) + wprintw(chats->window, "[%c][%c] %s", type, chat_type, text); + else + wprintw(chats->window, "[%c] %s", type, text); if (selected) wattroff(chats->window, attrs_select); return GNUNET_YES; } +int +_chats_iterate_print(void *cls, + UNUSED struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Group *group) +{ + UI_CHATS_Handle *chats = cls; + + const char *name = GNUNET_CHAT_group_get_name(group); + + return _chats_print_entry(chats, 'x', 'G', name); +} + void chats_print(UI_CHATS_Handle *chats, MESSENGER_Application *app) { + if (chats->open_dialog.window) + { + chat_open_dialog_print(&(chats->open_dialog), app); + return; + } + if (!(chats->window)) return; @@ -186,4 +212,6 @@ chats_print(UI_CHATS_Handle *chats, &_chats_iterate_print, chats ); + + _chats_print_entry(chats, '+', '\0', "Add chat"); } diff --git a/src/ui/chats.h b/src/ui/chats.h index 9ffee88..85a84eb 100644 --- a/src/ui/chats.h +++ b/src/ui/chats.h @@ -32,6 +32,8 @@ #include <gnunet/gnunet_chat_lib.h> #include <gnunet/gnunet_util_lib.h> +#include "chat_open_dialog.h" + struct MESSENGER_Application; typedef struct UI_CHATS_Handle @@ -43,6 +45,8 @@ typedef struct UI_CHATS_Handle int line_selected; struct GNUNET_CHAT_Context *selected; + + UI_CHAT_OPEN_DIALOG_Handle open_dialog; } UI_CHATS_Handle; void |