diff options
Diffstat (limited to 'src/ui/accounts.c')
-rw-r--r-- | src/ui/accounts.c | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/ui/accounts.c b/src/ui/accounts.c new file mode 100644 index 0000000..c55c009 --- /dev/null +++ b/src/ui/accounts.c | |||
@@ -0,0 +1,190 @@ | |||
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/accounts.c | ||
23 | */ | ||
24 | |||
25 | #include "accounts.h" | ||
26 | |||
27 | #include "account_entry.h" | ||
28 | #include "../application.h" | ||
29 | |||
30 | static void | ||
31 | handle_close_button_click(UNUSED GtkButton *button, | ||
32 | gpointer user_data) | ||
33 | { | ||
34 | GtkDialog *dialog = GTK_DIALOG(user_data); | ||
35 | gtk_window_close(GTK_WINDOW(dialog)); | ||
36 | } | ||
37 | |||
38 | static gboolean | ||
39 | _open_new_account_dialog(gpointer user_data) | ||
40 | { | ||
41 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
42 | |||
43 | ui_new_account_dialog_init(app, &(app->ui.new_account)); | ||
44 | |||
45 | gtk_widget_show(GTK_WIDGET(app->ui.new_account.dialog)); | ||
46 | return FALSE; | ||
47 | } | ||
48 | |||
49 | static void | ||
50 | handle_accounts_listbox_row_activated(UNUSED GtkListBox* listbox, | ||
51 | GtkListBoxRow* row, | ||
52 | gpointer user_data) | ||
53 | { | ||
54 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
55 | |||
56 | if (!gtk_list_box_row_get_selectable(row)) | ||
57 | { | ||
58 | g_idle_add(G_SOURCE_FUNC(_open_new_account_dialog), app); | ||
59 | goto close_dialog; | ||
60 | } | ||
61 | |||
62 | struct GNUNET_CHAT_Account *account = (struct GNUNET_CHAT_Account*) ( | ||
63 | g_hash_table_lookup(app->ui.bindings, row) | ||
64 | ); | ||
65 | |||
66 | if (!account) | ||
67 | goto close_dialog; | ||
68 | |||
69 | // TODO | ||
70 | |||
71 | close_dialog: | ||
72 | gtk_window_close(GTK_WINDOW(app->ui.accounts.dialog)); | ||
73 | } | ||
74 | |||
75 | static void | ||
76 | handle_dialog_destroy(UNUSED GtkWidget *window, | ||
77 | gpointer user_data) | ||
78 | { | ||
79 | ui_accounts_dialog_cleanup((UI_ACCOUNTS_Handle*) user_data); | ||
80 | } | ||
81 | |||
82 | static int | ||
83 | _iterate_accounts(void *cls, | ||
84 | const struct GNUNET_CHAT_Handle *handle, | ||
85 | struct GNUNET_CHAT_Account *account) | ||
86 | { | ||
87 | MESSENGER_Application *app = (MESSENGER_Application*) cls; | ||
88 | UI_MESSENGER_Handle *ui = &(app->ui.messenger); | ||
89 | |||
90 | const gchar *name = GNUNET_CHAT_account_get_name(account); | ||
91 | |||
92 | UI_ACCOUNT_ENTRY_Handle *entry = ui_account_entry_new(app); | ||
93 | |||
94 | hdy_avatar_set_text(entry->entry_avatar, name); | ||
95 | gtk_label_set_text(entry->entry_label, name); | ||
96 | |||
97 | gtk_list_box_prepend(ui->accounts_listbox, entry->entry_box); | ||
98 | |||
99 | GtkListBoxRow *row = GTK_LIST_BOX_ROW( | ||
100 | gtk_widget_get_parent(entry->entry_box) | ||
101 | ); | ||
102 | |||
103 | g_hash_table_insert(ui->bindings, row, account); | ||
104 | |||
105 | ui.accounts.account_entries = g_list_append( | ||
106 | ui.accounts.account_entries, | ||
107 | entry | ||
108 | ); | ||
109 | |||
110 | return GNUNET_YES; | ||
111 | } | ||
112 | |||
113 | void | ||
114 | ui_accounts_dialog_init(MESSENGER_Application *app, | ||
115 | UI_ACCOUNTS_Handle *handle) | ||
116 | { | ||
117 | handle->account_entries = NULL; | ||
118 | handle->bindings = app->ui.bindings; | ||
119 | |||
120 | handle->builder = gtk_builder_new_from_resource( | ||
121 | application_get_resource_path(app, "ui/accounts.ui") | ||
122 | ); | ||
123 | |||
124 | handle->dialog = GTK_DIALOG( | ||
125 | gtk_builder_get_object(handle->builder, "accounts_dialog") | ||
126 | ); | ||
127 | |||
128 | gtk_window_set_title( | ||
129 | GTK_WINDOW(handle->dialog), | ||
130 | _("Contacts") | ||
131 | ); | ||
132 | |||
133 | gtk_window_set_transient_for( | ||
134 | GTK_WINDOW(handle->dialog), | ||
135 | GTK_WINDOW(app->ui.messenger.main_window) | ||
136 | ); | ||
137 | |||
138 | handle->accounts_listbox = GTK_LIST_BOX( | ||
139 | gtk_builder_get_object(handle->builder, "accounts_listbox") | ||
140 | ); | ||
141 | |||
142 | g_signal_connect( | ||
143 | handle->accounts_listbox, | ||
144 | "row-activated", | ||
145 | G_CALLBACK(handle_accounts_listbox_row_activated), | ||
146 | app | ||
147 | ); | ||
148 | |||
149 | handle->close_button = GTK_BUTTON( | ||
150 | gtk_builder_get_object(handle->builder, "close_button") | ||
151 | ); | ||
152 | |||
153 | g_signal_connect( | ||
154 | handle->close_button, | ||
155 | "clicked", | ||
156 | G_CALLBACK(handle_close_button_click), | ||
157 | handle->dialog | ||
158 | ); | ||
159 | |||
160 | g_signal_connect( | ||
161 | handle->dialog, | ||
162 | "destroy", | ||
163 | G_CALLBACK(handle_dialog_destroy), | ||
164 | handle | ||
165 | ); | ||
166 | |||
167 | GNUNET_CHAT_iterate_accounts( | ||
168 | app->chat.messenger.handle, | ||
169 | _iterate_accounts, | ||
170 | app | ||
171 | ); | ||
172 | } | ||
173 | |||
174 | void | ||
175 | ui_accounts_dialog_cleanup(UI_ACCOUNTS_Handle *handle) | ||
176 | { | ||
177 | g_object_unref(handle->builder); | ||
178 | |||
179 | GList *list = handle->account_entries; | ||
180 | |||
181 | while (list) { | ||
182 | if (list->data) | ||
183 | ui_account_entry_delete((UI_ACCOUNT_ENTRY_Handle*) list->data); | ||
184 | |||
185 | list = list->next; | ||
186 | } | ||
187 | |||
188 | if (handle->account_entries) | ||
189 | g_list_free(handle->account_entries); | ||
190 | } | ||