diff options
Diffstat (limited to 'src/ui/chat_open_dialog.c')
-rw-r--r-- | src/ui/chat_open_dialog.c | 122 |
1 files changed, 122 insertions, 0 deletions
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 @@ | |||
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/chat_open_dialog.c | ||
23 | */ | ||
24 | |||
25 | #include "chat_open_dialog.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 | |||
36 | void | ||
37 | chat_open_dialog_event(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, | ||
38 | struct MESSENGER_Application *app, | ||
39 | int key) | ||
40 | { | ||
41 | switch (key) | ||
42 | { | ||
43 | case 27: | ||
44 | case KEY_EXIT: | ||
45 | { | ||
46 | open_dialog->window = NULL; | ||
47 | break; | ||
48 | } | ||
49 | case KEY_LEFT: | ||
50 | { | ||
51 | open_dialog->topic_pos--; | ||
52 | break; | ||
53 | } | ||
54 | case KEY_RIGHT: | ||
55 | { | ||
56 | open_dialog->topic_pos++; | ||
57 | break; | ||
58 | } | ||
59 | case '\n': | ||
60 | case KEY_ENTER: | ||
61 | { | ||
62 | if (open_dialog->topic_len > 0) | ||
63 | GNUNET_CHAT_group_create(app->chat.handle, open_dialog->topic); | ||
64 | |||
65 | open_dialog->topic_len = 0; | ||
66 | open_dialog->window = NULL; | ||
67 | break; | ||
68 | } | ||
69 | case KEY_BACKSPACE: | ||
70 | { | ||
71 | if ((open_dialog->topic_pos < open_dialog->topic_len) && | ||
72 | (open_dialog->topic_pos > 0)) | ||
73 | for (int i = open_dialog->topic_pos; i < open_dialog->topic_len; i++) | ||
74 | open_dialog->topic[i - 1] = open_dialog->topic[i]; | ||
75 | |||
76 | if ((open_dialog->topic_pos > 0) && (open_dialog->topic_len > 0)) | ||
77 | { | ||
78 | open_dialog->topic_pos--; | ||
79 | open_dialog->topic_len--; | ||
80 | } | ||
81 | |||
82 | break; | ||
83 | } | ||
84 | default: | ||
85 | { | ||
86 | if (!isalnum(key)) | ||
87 | break; | ||
88 | |||
89 | for (int i = open_dialog->topic_len - 1; i >= open_dialog->topic_pos; i--) | ||
90 | open_dialog->topic[i + 1] = open_dialog->topic[i]; | ||
91 | |||
92 | open_dialog->topic[open_dialog->topic_pos++] = (char) key; | ||
93 | open_dialog->topic_len++; | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | if (open_dialog->topic_len > 255) | ||
99 | open_dialog->topic_len = 255; | ||
100 | |||
101 | open_dialog->topic[open_dialog->topic_len] = '\0'; | ||
102 | |||
103 | if (open_dialog->topic_pos < 0) | ||
104 | open_dialog->topic_pos = 0; | ||
105 | |||
106 | if (open_dialog->topic_pos > open_dialog->topic_len) | ||
107 | open_dialog->topic_pos = open_dialog->topic_len; | ||
108 | } | ||
109 | |||
110 | void | ||
111 | chat_open_dialog_print(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, | ||
112 | UNUSED struct MESSENGER_Application *app) | ||
113 | { | ||
114 | if (!(open_dialog->window)) | ||
115 | return; | ||
116 | |||
117 | werase(open_dialog->window); | ||
118 | wmove(open_dialog->window, 0, 0); | ||
119 | |||
120 | wprintw(open_dialog->window, "%s", open_dialog->topic); | ||
121 | wmove(open_dialog->window, 0, open_dialog->topic_pos); | ||
122 | } | ||