diff options
Diffstat (limited to 'src/anastasis/anastasis-gtk_handle-add-provider.c')
-rw-r--r-- | src/anastasis/anastasis-gtk_handle-add-provider.c | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/src/anastasis/anastasis-gtk_handle-add-provider.c b/src/anastasis/anastasis-gtk_handle-add-provider.c new file mode 100644 index 0000000..8bf68e9 --- /dev/null +++ b/src/anastasis/anastasis-gtk_handle-add-provider.c | |||
@@ -0,0 +1,244 @@ | |||
1 | /* | ||
2 | This file is part of anastasis-gtk. | ||
3 | Copyright (C) 2022 Anastasis SARL | ||
4 | |||
5 | Anastasis is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | Anastasis 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with Anastasis; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/anastasis/anastasis-gtk_handle-add-provider.c | ||
23 | * @brief Dialog to add a provider during recovery | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include <gnunet/platform.h> | ||
27 | #include <gnunet/gnunet_util_lib.h> | ||
28 | #include "anastasis-gtk_action.h" | ||
29 | #include "anastasis-gtk_helper.h" | ||
30 | #include <jansson.h> | ||
31 | #include <microhttpd.h> | ||
32 | |||
33 | /** | ||
34 | * State data for the provider, NULL if none is available. | ||
35 | */ | ||
36 | static json_t *pstate; | ||
37 | |||
38 | /** | ||
39 | * Current /config operation, or NULL if none. | ||
40 | */ | ||
41 | static struct ANASTASIS_ConfigOperation *co; | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Function called with the result of a /config request. | ||
46 | * Note that an HTTP status of #MHD_HTTP_OK is no guarantee | ||
47 | * that @a acfg is non-NULL. @a acfg is non-NULL only if | ||
48 | * the server provided an acceptable response. | ||
49 | * | ||
50 | * @param cls closure with our `GtkBuilder *` | ||
51 | * @param http_status the HTTP status | ||
52 | * @param acfg configuration obtained, NULL if we could not parse it | ||
53 | */ | ||
54 | static void | ||
55 | config_cb (void *cls, | ||
56 | unsigned int http_status, | ||
57 | const struct ANASTASIS_Config *acfg) | ||
58 | { | ||
59 | GtkBuilder *builder = GTK_BUILDER (cls); | ||
60 | GtkWidget *button; | ||
61 | json_t *methods_list; | ||
62 | GtkLabel *l; | ||
63 | |||
64 | co = NULL; | ||
65 | l = GTK_LABEL (gtk_builder_get_object (builder, | ||
66 | "error_label")); | ||
67 | if ( (MHD_HTTP_OK != http_status) || | ||
68 | (NULL == acfg) ) | ||
69 | { | ||
70 | char *msg; | ||
71 | |||
72 | if (0 == http_status) | ||
73 | GNUNET_asprintf (&msg, | ||
74 | "Provider URL invalid (no response)"); | ||
75 | else | ||
76 | GNUNET_asprintf (&msg, | ||
77 | "Provider URL invalid (HTTP status %u)", | ||
78 | http_status); | ||
79 | gtk_widget_show (GTK_WIDGET (l)); | ||
80 | gtk_label_set_text (l, | ||
81 | msg); | ||
82 | free (msg); | ||
83 | return; | ||
84 | } | ||
85 | methods_list = json_array (); | ||
86 | GNUNET_assert (NULL != methods_list); | ||
87 | for (unsigned int i = 0; i<acfg->methods_length; i++) | ||
88 | { | ||
89 | struct ANASTASIS_AuthorizationMethodConfig *method = &acfg->methods[i]; | ||
90 | json_t *mj = GNUNET_JSON_PACK ( | ||
91 | GNUNET_JSON_pack_string ("type", | ||
92 | method->type), | ||
93 | TALER_JSON_pack_amount ("usage_fee", | ||
94 | &method->usage_fee)); | ||
95 | |||
96 | GNUNET_assert (0 == | ||
97 | json_array_append_new (methods_list, | ||
98 | mj)); | ||
99 | } | ||
100 | pstate = GNUNET_JSON_PACK ( | ||
101 | GNUNET_JSON_pack_array_steal ("methods", | ||
102 | methods_list), | ||
103 | TALER_JSON_pack_amount ("annual_fee", | ||
104 | &acfg->annual_fee), | ||
105 | TALER_JSON_pack_amount ("truth_upload_fee", | ||
106 | &acfg->truth_upload_fee), | ||
107 | TALER_JSON_pack_amount ("liability_limit", | ||
108 | &acfg->liability_limit), | ||
109 | GNUNET_JSON_pack_string ("currency", | ||
110 | acfg->currency), | ||
111 | GNUNET_JSON_pack_string ("business_name", | ||
112 | acfg->business_name), | ||
113 | GNUNET_JSON_pack_uint64 ("storage_limit_in_megabytes", | ||
114 | acfg->storage_limit_in_megabytes), | ||
115 | GNUNET_JSON_pack_data_auto ("salt", | ||
116 | &acfg->salt), | ||
117 | GNUNET_JSON_pack_uint64 ("http_status", | ||
118 | http_status)); | ||
119 | button = GTK_WIDGET (gtk_builder_get_object (builder, | ||
120 | "add_button")); | ||
121 | gtk_widget_set_sensitive (button, | ||
122 | true); | ||
123 | gtk_widget_hide (GTK_WIDGET (l)); | ||
124 | } | ||
125 | |||
126 | |||
127 | /** | ||
128 | * Function called when the user edits the URL in the "add_provider" | ||
129 | * dialog. Updates the visibility of the 'apply/add' button. | ||
130 | * | ||
131 | * @param entry the entry that changed | ||
132 | * @param user_data our `GtkBuilder *` | ||
133 | */ | ||
134 | void | ||
135 | add_provider_url_entry_changed_cb (GtkEntry *entry, | ||
136 | gpointer user_data) | ||
137 | { | ||
138 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
139 | GtkWidget *button; | ||
140 | const char *url; | ||
141 | |||
142 | json_decref (pstate); | ||
143 | pstate = NULL; | ||
144 | if (NULL != co) | ||
145 | { | ||
146 | ANASTASIS_config_cancel (co); | ||
147 | co = NULL; | ||
148 | } | ||
149 | button = GTK_WIDGET (gtk_builder_get_object (builder, | ||
150 | "add_button")); | ||
151 | gtk_widget_set_sensitive (button, | ||
152 | false); | ||
153 | url = gtk_entry_get_text (entry); | ||
154 | if ( (0 == strncasecmp (url, | ||
155 | "http://", | ||
156 | strlen ("http://"))) || | ||
157 | (0 == strncasecmp (url, | ||
158 | "https://", | ||
159 | strlen ("https://"))) ) | ||
160 | { | ||
161 | co = ANASTASIS_get_config (AG_ctx, | ||
162 | url, | ||
163 | &config_cb, | ||
164 | builder); | ||
165 | GNUNET_break (NULL != co); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | |||
170 | /** | ||
171 | * Function called from the edit-provider dialog upon completion. | ||
172 | * | ||
173 | * @param dialog the pseudonym selection dialog | ||
174 | * @param response_id response code from the dialog | ||
175 | * @param user_data the builder of the dialog | ||
176 | */ | ||
177 | void | ||
178 | add_provider_dialog_response_cb (GtkDialog *dialog, | ||
179 | gint response_id, | ||
180 | gpointer user_data) | ||
181 | { | ||
182 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
183 | GtkEntry *entry; | ||
184 | const char *url; | ||
185 | |||
186 | if (NULL != co) | ||
187 | { | ||
188 | ANASTASIS_config_cancel (co); | ||
189 | co = NULL; | ||
190 | } | ||
191 | if (GTK_RESPONSE_APPLY != response_id) | ||
192 | { | ||
193 | gtk_widget_destroy (GTK_WIDGET (dialog)); | ||
194 | g_object_unref (G_OBJECT (builder)); | ||
195 | json_decref (pstate); | ||
196 | pstate = NULL; | ||
197 | return; | ||
198 | } | ||
199 | entry = GTK_ENTRY (gtk_builder_get_object (builder, | ||
200 | "url_entry")); | ||
201 | url = gtk_entry_get_text (entry); | ||
202 | ANASTASIS_policy_discovery_more (AG_pd, | ||
203 | url, | ||
204 | pstate); | ||
205 | gtk_widget_destroy (GTK_WIDGET (dialog)); | ||
206 | g_object_unref (G_OBJECT (builder)); | ||
207 | json_decref (pstate); | ||
208 | pstate = NULL; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Callback invoked if the the "Add"-provider button is clicked. | ||
214 | * | ||
215 | * @param object | ||
216 | * @param user_data unused | ||
217 | */ | ||
218 | void | ||
219 | anastasis_gtk_add_provider_button_clicked_cb (GtkButton *object, | ||
220 | gpointer user_data) | ||
221 | { | ||
222 | GtkWidget *ad; | ||
223 | GtkBuilder *builder; | ||
224 | GtkWidget *toplevel; | ||
225 | |||
226 | if (NULL == AG_pd) | ||
227 | { | ||
228 | GNUNET_break (0); | ||
229 | return; | ||
230 | } | ||
231 | builder = GNUNET_GTK_get_new_builder ("anastasis_gtk_add_provider.glade", | ||
232 | NULL); | ||
233 | if (NULL == builder) | ||
234 | { | ||
235 | GNUNET_break (0); | ||
236 | return; | ||
237 | } | ||
238 | ad = GTK_WIDGET (gtk_builder_get_object (builder, | ||
239 | "add_provider_dialog")); | ||
240 | toplevel = gtk_widget_get_toplevel (GTK_WIDGET (object)); | ||
241 | gtk_window_set_transient_for (GTK_WINDOW (ad), | ||
242 | GTK_WINDOW (toplevel)); | ||
243 | gtk_window_present (GTK_WINDOW (ad)); | ||
244 | } | ||