aboutsummaryrefslogtreecommitdiff
path: root/src/ui/invite_contact.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/invite_contact.c')
-rw-r--r--src/ui/invite_contact.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/ui/invite_contact.c b/src/ui/invite_contact.c
new file mode 100644
index 0000000..5b6a71c
--- /dev/null
+++ b/src/ui/invite_contact.c
@@ -0,0 +1,206 @@
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/invite_contact.c
23 */
24
25#include "invite_contact.h"
26
27#include "chat_entry.h"
28#include "contact_entry.h"
29#include "../application.h"
30
31static void
32handle_close_button_click(UNUSED GtkButton *button,
33 gpointer user_data)
34{
35 GtkDialog *dialog = GTK_DIALOG(user_data);
36 gtk_window_close(GTK_WINDOW(dialog));
37}
38
39static void
40handle_contacts_listbox_row_activated(GtkListBox* listbox,
41 GtkListBoxRow* row,
42 gpointer user_data)
43{
44 MESSENGER_Application *app = (MESSENGER_Application*) user_data;
45
46 GtkTextView *text_view = GTK_TEXT_VIEW(
47 g_hash_table_lookup(app->ui.bindings, listbox)
48 );
49
50 struct GNUNET_CHAT_Contact *contact = (struct GNUNET_CHAT_Contact*) (
51 g_hash_table_lookup(app->ui.bindings, row)
52 );
53
54 if ((!contact) || (!GNUNET_CHAT_contact_get_key(contact)) ||
55 (GNUNET_YES == GNUNET_CHAT_contact_is_owned(contact)) ||
56 (!text_view))
57 goto close_dialog;
58
59 struct GNUNET_CHAT_Context *context = g_hash_table_lookup(
60 app->ui.bindings, text_view
61 );
62
63 if (!context)
64 goto close_dialog;
65
66 const struct GNUNET_CHAT_Group *group = GNUNET_CHAT_context_get_group(
67 context
68 );
69
70 if (group)
71 GNUNET_CHAT_group_invite_contact(group, contact);
72
73close_dialog:
74 gtk_window_close(GTK_WINDOW(app->ui.invite_contact.dialog));
75}
76
77static void
78handle_dialog_destroy(UNUSED GtkWidget *window,
79 gpointer user_data)
80{
81 ui_contacts_dialog_cleanup((UI_CONTACTS_Handle*) user_data);
82}
83
84static int
85_iterate_contacts(void *cls,
86 UNUSED struct GNUNET_CHAT_Handle *handle,
87 struct GNUNET_CHAT_Contact *contact)
88{
89 if (GNUNET_YES == GNUNET_CHAT_contact_is_owned(contact))
90 return GNUNET_YES;
91
92 MESSENGER_Application *app = (MESSENGER_Application*) cls;
93
94 const char *title;
95 title = GNUNET_CHAT_contact_get_name(contact);
96
97 const char *key = GNUNET_CHAT_contact_get_key(contact);
98
99 UI_CONTACT_ENTRY_Handle *entry = ui_contact_entry_new();
100 gtk_list_box_prepend(
101 app->ui.invite_contact.contacts_listbox,
102 entry->entry_box
103 );
104
105 if (title)
106 {
107 gtk_label_set_text(entry->title_label, title);
108 hdy_avatar_set_text(entry->entry_avatar, title);
109 }
110
111 if (key)
112 gtk_label_set_text(entry->subtitle_label, key);
113
114 GtkListBoxRow *row = GTK_LIST_BOX_ROW(
115 gtk_widget_get_parent(entry->entry_box)
116 );
117
118 g_hash_table_insert(app->ui.bindings, row, contact);
119
120 app->ui.invite_contact.contact_entries = g_list_append(
121 app->ui.invite_contact.contact_entries,
122 entry
123 );
124
125 return GNUNET_YES;
126}
127
128void
129ui_invite_contact_dialog_init(MESSENGER_Application *app,
130 UI_INVITE_CONTACT_Handle *handle)
131{
132 handle->contact_entries = NULL;
133
134 handle->builder = gtk_builder_new_from_file("resources/ui/invite_contact.ui");
135
136 handle->dialog = GTK_DIALOG(
137 gtk_builder_get_object(handle->builder, "invite_contact_dialog")
138 );
139
140 gtk_window_set_title(
141 GTK_WINDOW(handle->dialog),
142 _("Invite Contact")
143 );
144
145 gtk_window_set_transient_for(
146 GTK_WINDOW(handle->dialog),
147 GTK_WINDOW(app->ui.messenger.main_window)
148 );
149
150 handle->contact_search_entry = GTK_SEARCH_ENTRY(
151 gtk_builder_get_object(handle->builder, "contact_search_entry")
152 );
153
154 handle->contacts_listbox = GTK_LIST_BOX(
155 gtk_builder_get_object(handle->builder, "contacts_listbox")
156 );
157
158 g_signal_connect(
159 handle->contacts_listbox,
160 "row-activated",
161 G_CALLBACK(handle_contacts_listbox_row_activated),
162 app
163 );
164
165 handle->close_button = GTK_BUTTON(
166 gtk_builder_get_object(handle->builder, "close_button")
167 );
168
169 g_signal_connect(
170 handle->close_button,
171 "clicked",
172 G_CALLBACK(handle_close_button_click),
173 handle->dialog
174 );
175
176 g_signal_connect(
177 handle->dialog,
178 "destroy",
179 G_CALLBACK(handle_dialog_destroy),
180 handle
181 );
182
183 GNUNET_CHAT_iterate_contacts(
184 app->chat.messenger.handle,
185 _iterate_contacts,
186 app
187 );
188}
189
190void
191ui_invite_contact_dialog_cleanup(UI_INVITE_CONTACT_Handle *handle)
192{
193 g_object_unref(handle->builder);
194
195 GList *list = handle->contact_entries;
196
197 while (list) {
198 if (list->data)
199 ui_contact_entry_delete((UI_CONTACT_ENTRY_Handle*) list->data);
200
201 list = list->next;
202 }
203
204 if (handle->contact_entries)
205 g_list_free(handle->contact_entries);
206}