aboutsummaryrefslogtreecommitdiff
path: root/src/ui/new_group.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/new_group.c')
-rw-r--r--src/ui/new_group.c353
1 files changed, 353 insertions, 0 deletions
diff --git a/src/ui/new_group.c b/src/ui/new_group.c
new file mode 100644
index 0000000..ea19371
--- /dev/null
+++ b/src/ui/new_group.c
@@ -0,0 +1,353 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021 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/new_group.c
23 */
24
25#include "new_platform.h"
26
27#include "contact_entry.h"
28#include "../application.h"
29
30static void
31_open_new_group(GtkEntry *entry,
32 GtkListBox *listbox,
33 MESSENGER_Application *app)
34{
35 const gchar *name = gtk_entry_get_text(entry);
36
37 struct GNUNET_CHAT_Group *group = GNUNET_CHAT_group_create(
38 app->chat.messenger.handle,
39 NULL
40 );
41
42 if ((name) && (strlen(name) > 0))
43 GNUNET_CHAT_group_set_name(group, name);
44
45 GList *selected = gtk_list_box_get_selected_rows(listbox);
46
47 while (selected)
48 {
49 if (selected->data)
50 {
51 GtkListBoxRow *row = GTK_LIST_BOX_ROW(selected->data);
52
53 struct GNUNET_CHAT_Contact* contact = g_hash_table_lookup(
54 app->ui.bindings, row
55 );
56
57 GNUNET_CHAT_group_invite_contact(group, contact);
58 }
59
60 selected = selected->next;
61 }
62}
63
64static void
65handle_group_entry_changed(GtkEditable *editable,
66 gpointer user_data)
67{
68 HdyAvatar *avatar = HDY_AVATAR(user_data);
69 GtkEntry *entry = GTK_ENTRY(editable);
70
71 hdy_avatar_set_text(avatar, gtk_entry_get_text(entry));
72}
73
74static void
75_go_page_details(UI_NEW_GROUP_Handle *handle)
76{
77 gtk_stack_set_visible_child(handle->stack, handle->details_box);
78
79 gtk_widget_hide(GTK_WIDGET(handle->previous_button));
80 gtk_widget_hide(GTK_WIDGET(handle->confirm_button));
81
82 gtk_widget_show(GTK_WIDGET(handle->cancel_button));
83 gtk_widget_show(GTK_WIDGET(handle->next_button));
84}
85
86static void
87_go_page_contacts(UI_NEW_GROUP_Handle *handle)
88{
89 gtk_stack_set_visible_child(handle->stack, handle->contacts_box);
90
91 gtk_widget_hide(GTK_WIDGET(handle->cancel_button));
92 gtk_widget_hide(GTK_WIDGET(handle->next_button));
93
94 gtk_widget_show(GTK_WIDGET(handle->previous_button));
95 gtk_widget_show(GTK_WIDGET(handle->confirm_button));
96}
97
98static void
99handle_group_entry_activate(UNUSED GtkEntry *entry,
100 gpointer user_data)
101{
102 MESSENGER_Application *app = (MESSENGER_Application*) user_data;
103
104 _go_page_contacts(&(app->ui.new_group));
105}
106
107static void
108handle_cancel_button_click(UNUSED GtkButton *button,
109 gpointer user_data)
110{
111 GtkDialog *dialog = GTK_DIALOG(user_data);
112 gtk_window_close(GTK_WINDOW(dialog));
113}
114
115static void
116handle_previous_button_click(UNUSED GtkButton *button,
117 gpointer user_data)
118{
119 _go_page_details((UI_NEW_GROUP_Handle*) user_data);
120}
121
122static void
123handle_next_button_click(UNUSED GtkButton *button,
124 gpointer user_data)
125{
126 _go_page_contacts((UI_NEW_GROUP_Handle*) user_data);
127}
128
129static void
130handle_confirm_button_click(UNUSED GtkButton *button,
131 gpointer user_data)
132{
133 MESSENGER_Application *app = (MESSENGER_Application*) user_data;
134
135 _open_new_group(
136 app->ui.new_group.group_entry,
137 app->ui.new_group.contacts_listbox,
138 app
139 );
140
141 gtk_window_close(GTK_WINDOW(app->ui.new_group.dialog));
142}
143
144static void
145handle_dialog_destroy(UNUSED GtkWidget *window,
146 gpointer user_data)
147{
148 ui_new_group_dialog_cleanup((UI_NEW_GROUP_Handle*) user_data);
149}
150
151static int
152_iterate_clear_contacts(UNUSED void *cls,
153 UNUSED struct GNUNET_CHAT_Handle *handle,
154 struct GNUNET_CHAT_Contact *contact)
155{
156 GNUNET_CHAT_contact_set_user_pointer(contact, NULL);
157 return GNUNET_YES;
158}
159
160static int
161_iterate_contacts(void *cls,
162 UNUSED struct GNUNET_CHAT_Handle *handle,
163 struct GNUNET_CHAT_Contact *contact)
164{
165 MESSENGER_Application *app = (MESSENGER_Application*) cls;
166
167 if (GNUNET_CHAT_contact_get_user_pointer(contact))
168 return GNUNET_YES;
169
170 const char *title;
171 title = GNUNET_CHAT_contact_get_name(contact);
172
173 const char *key = GNUNET_CHAT_contact_get_key(contact);
174
175 UI_CONTACT_ENTRY_Handle *entry = ui_contact_entry_new();
176 gtk_list_box_prepend(
177 app->ui.new_group.contacts_listbox,
178 entry->entry_box
179 );
180
181 GNUNET_CHAT_contact_set_user_pointer(contact, entry);
182
183 if (title)
184 {
185 gtk_label_set_text(entry->title_label, title);
186 hdy_avatar_set_text(entry->entry_avatar, title);
187 }
188
189 if (key)
190 gtk_label_set_text(entry->subtitle_label, key);
191
192 GtkListBoxRow *row = GTK_LIST_BOX_ROW(
193 gtk_widget_get_parent(entry->entry_box)
194 );
195
196 g_hash_table_insert(app->ui.bindings, row, contact);
197
198 app->ui.new_group.contact_entries = g_list_append(
199 app->ui.new_group.contact_entries,
200 entry
201 );
202
203 return GNUNET_YES;
204}
205
206void
207ui_new_group_dialog_init(MESSENGER_Application *app,
208 UI_NEW_GROUP_Handle *handle)
209{
210 handle->contact_entries = g_list_alloc();
211
212 handle->builder = gtk_builder_new_from_file("resources/ui/new_group.ui");
213
214 handle->dialog = GTK_DIALOG(
215 gtk_builder_get_object(handle->builder, "new_group_dialog")
216 );
217
218 gtk_window_set_title(
219 GTK_WINDOW(handle->dialog),
220 "New Group"
221 );
222
223 gtk_window_set_transient_for(
224 GTK_WINDOW(handle->dialog),
225 GTK_WINDOW(app->ui.messenger.main_window)
226 );
227
228 handle->stack = GTK_STACK(
229 gtk_builder_get_object(handle->builder, "new_group_stack")
230 );
231
232 handle->details_box = GTK_WIDGET(
233 gtk_builder_get_object(handle->builder, "details_box")
234 );
235
236 handle->contacts_box = GTK_WIDGET(
237 gtk_builder_get_object(handle->builder, "contacts_box")
238 );
239
240 handle->group_avatar = HDY_AVATAR(
241 gtk_builder_get_object(handle->builder, "group_avatar")
242 );
243
244 handle->group_avatar_file = GTK_FILE_CHOOSER_BUTTON(
245 gtk_builder_get_object(handle->builder, "group_avatar_file")
246 );
247
248 handle->group_entry = GTK_ENTRY(
249 gtk_builder_get_object(handle->builder, "group_entry")
250 );
251
252 g_signal_connect(
253 handle->group_entry,
254 "changed",
255 G_CALLBACK(handle_group_entry_changed),
256 handle->group_avatar
257 );
258
259 g_signal_connect(
260 handle->group_entry,
261 "activate",
262 G_CALLBACK(handle_group_entry_activate),
263 app
264 );
265
266 handle->contact_search_entry = GTK_SEARCH_ENTRY(
267 gtk_builder_get_object(handle->builder, "contact_search_entry")
268 );
269
270 handle->contacts_listbox = GTK_LIST_BOX(
271 gtk_builder_get_object(handle->builder, "contacts_listbox")
272 );
273
274 handle->cancel_button = GTK_BUTTON(
275 gtk_builder_get_object(handle->builder, "cancel_button")
276 );
277
278 g_signal_connect(
279 handle->cancel_button,
280 "clicked",
281 G_CALLBACK(handle_cancel_button_click),
282 handle->dialog
283 );
284
285 handle->previous_button = GTK_BUTTON(
286 gtk_builder_get_object(handle->builder, "previous_button")
287 );
288
289 g_signal_connect(
290 handle->previous_button,
291 "clicked",
292 G_CALLBACK(handle_previous_button_click),
293 handle
294 );
295
296 handle->next_button = GTK_BUTTON(
297 gtk_builder_get_object(handle->builder, "next_button")
298 );
299
300 g_signal_connect(
301 handle->next_button,
302 "clicked",
303 G_CALLBACK(handle_next_button_click),
304 handle
305 );
306
307 handle->confirm_button = GTK_BUTTON(
308 gtk_builder_get_object(handle->builder, "confirm_button")
309 );
310
311 g_signal_connect(
312 handle->confirm_button,
313 "clicked",
314 G_CALLBACK(handle_confirm_button_click),
315 app
316 );
317
318 g_signal_connect(
319 handle->dialog,
320 "destroy",
321 G_CALLBACK(handle_dialog_destroy),
322 handle
323 );
324
325 GNUNET_CHAT_iterate_contacts(
326 app->chat.messenger.handle,
327 _iterate_clear_contacts,
328 NULL
329 );
330
331 GNUNET_CHAT_iterate_contacts(
332 app->chat.messenger.handle,
333 _iterate_contacts,
334 app
335 );
336}
337
338void
339ui_new_group_dialog_cleanup(UI_NEW_GROUP_Handle *handle)
340{
341 g_object_unref(handle->builder);
342
343 GList *list = handle->contact_entries;
344
345 while (list) {
346 if (list->data)
347 ui_contact_entry_delete((UI_CONTACT_ENTRY_Handle*) list->data);
348
349 list = list->next;
350 }
351
352 g_list_free(handle->contact_entries);
353}