aboutsummaryrefslogtreecommitdiff
path: root/src/ui/members.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/members.c')
-rw-r--r--src/ui/members.c217
1 files changed, 217 insertions, 0 deletions
diff --git a/src/ui/members.c b/src/ui/members.c
new file mode 100644
index 0000000..c500d58
--- /dev/null
+++ b/src/ui/members.c
@@ -0,0 +1,217 @@
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/members.c
23 */
24
25#include "members.h"
26
27#include "../application.h"
28#include "../util.h"
29
30void
31members_event(UI_MEMBERS_Handle *members,
32 struct MESSENGER_Application *app,
33 int key)
34{
35 members->line_index = 0;
36 members->selected = NULL;
37
38 int count = 0;
39
40 UI_MEMBERS_List *element = members->head;
41 while (element)
42 {
43 count++;
44
45 element = element->next;
46 }
47
48 switch (key)
49 {
50 case 27:
51 case KEY_EXIT:
52 case '\t':
53 {
54 app->chat.show_members = FALSE;
55 break;
56 }
57 case KEY_UP:
58 {
59 members->line_selected--;
60 break;
61 }
62 case KEY_DOWN:
63 {
64 members->line_selected++;
65 break;
66 }
67 case '\n':
68 case KEY_ENTER:
69 {
70 if (members->selected)
71 {
72 // TODO
73 }
74
75 break;
76 }
77 default:
78 break;
79 }
80
81 if (members->line_selected < 0)
82 members->line_selected = 0;
83 else if (members->line_selected >= count)
84 members->line_selected = count - 1;
85
86 if (!(members->window))
87 return;
88
89 const int height = getmaxy(members->window) - getbegy(members->window);
90 const int y = members->line_selected - members->line_offset;
91
92 if (y < 0)
93 members->line_offset += y;
94 else if (y + 1 >= height)
95 members->line_offset += y + 1 - height;
96
97 if (members->line_offset < 0)
98 members->line_offset = 0;
99 else if (members->line_offset >= count)
100 members->line_offset = count - 1;
101}
102
103static void
104_members_iterate_print(UI_MEMBERS_Handle *members,
105 const struct GNUNET_CHAT_Contact *contact)
106{
107 const bool selected = (members->line_selected == members->line_index);
108 const int y = members->line_index - members->line_offset;
109
110 members->line_index++;
111
112 if (y < 0)
113 return;
114
115 const int height = getmaxy(members->window) - getbegy(members->window);
116
117 if (y >= height)
118 return;
119
120 const int attrs_select = A_BOLD;
121
122 if (selected) wattron(members->window, attrs_select);
123
124 wmove(members->window, y, 0);
125
126 const char *name = GNUNET_CHAT_contact_get_name(contact);
127 wprintw(members->window, "%s", name);
128
129 if (selected) wattroff(members->window, attrs_select);
130}
131
132void
133members_print(UI_MEMBERS_Handle *members)
134{
135 if (!(members->window))
136 return;
137
138 members->line_index = 0;
139 werase(members->window);
140
141 UI_MEMBERS_List *element = members->head;
142 while (element)
143 {
144 _members_iterate_print(members, element->contact);
145
146 element = element->next;
147 }
148}
149
150void
151members_clear(UI_MEMBERS_Handle *members)
152{
153 UI_MEMBERS_List *element;
154 while (members->head)
155 {
156 element = members->head;
157
158 GNUNET_CONTAINER_DLL_remove(
159 members->head,
160 members->tail,
161 element
162 );
163
164 GNUNET_free(element);
165 }
166
167 members->line_selected = 0;
168}
169
170bool
171members_add(UI_MEMBERS_Handle *members,
172 const struct GNUNET_CHAT_Contact *contact)
173{
174 UI_MEMBERS_List *element = members->head;
175 while (element)
176 {
177 if (element->contact == contact)
178 break;
179
180 element = element->next;
181 }
182
183 if (element)
184 return FALSE;
185
186 element = GNUNET_new(UI_MEMBERS_List);
187 element->contact = contact;
188
189 GNUNET_CONTAINER_DLL_insert_tail(
190 members->head,
191 members->tail,
192 element
193 );
194
195 return TRUE;
196}
197
198void
199members_remove(UI_MEMBERS_Handle *members,
200 const struct GNUNET_CHAT_Contact *contact)
201{
202 UI_MEMBERS_List *element = members->head;
203 while (element)
204 {
205 if (element->contact == contact)
206 break;
207
208 element = element->next;
209 }
210
211 if (element)
212 GNUNET_CONTAINER_DLL_remove(
213 members->head,
214 members->tail,
215 element
216 );
217}