anastasis-gtk_handle-method-post.c (6193B)
1 /* 2 This file is part of anastasis-gtk. 3 Copyright (C) 2020 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-method-post.c 23 * @brief Handle dialogs for security post 24 * @author Christian Grothoff 25 */ 26 #include <gnunet/gnunet_util_lib.h> 27 #include "anastasis_gtk_util.h" 28 #include "anastasis-gtk_action.h" 29 #include "anastasis-gtk_helper.h" 30 #include "anastasis-gtk_handle-identity-changed.h" 31 #include <jansson.h> 32 33 34 /** 35 * Get text of @a widget_name from builder @a b. 36 * 37 * @param b a builder 38 * @param widget_name name of an entry widget of @a b 39 * @return associated text 40 */ 41 static const char * 42 gt (GtkBuilder *b, 43 const char *widget_name) 44 { 45 GtkEntry *q; 46 47 q = GTK_ENTRY (gtk_builder_get_object (b, 48 widget_name)); 49 if (NULL == q) 50 { 51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 52 "Could not find GtkEntry widget `%s'\n", 53 widget_name); 54 } 55 return gtk_entry_get_text (q); 56 } 57 58 59 /** 60 * Function called from the security-post dialog upon completion. 61 * 62 * @param dialog the pseudonym selection dialog 63 * @param response_id response code from the dialog 64 * @param user_data the builder of the dialog 65 */ 66 void 67 anastasis_gtk_b_post_dialog_response_cb (GtkDialog *dialog, 68 gint response_id, 69 gpointer user_data) 70 { 71 GtkBuilder *builder = GTK_BUILDER (user_data); 72 json_t *args; 73 char *addr_s; 74 char *ins; 75 76 if (GTK_RESPONSE_OK != response_id) 77 { 78 gtk_widget_destroy (GTK_WIDGET (dialog)); 79 g_object_unref (G_OBJECT (builder)); 80 return; 81 } 82 GNUNET_asprintf (&ins, 83 _ ("postal address %s"), 84 gt (builder, 85 "anastasis_gtk_b_post_dialog_postcode_entry")); 86 { 87 json_t *addr; 88 89 addr = GNUNET_JSON_PACK ( 90 GNUNET_JSON_pack_string ("full_name", 91 gt (builder, 92 "anastasis_gtk_b_post_dialog_full_name_entry")), 93 GNUNET_JSON_pack_string ("street", 94 gt (builder, 95 "anastasis_gtk_b_post_dialog_street_entry")), 96 GNUNET_JSON_pack_string ("city", 97 gt (builder, 98 "anastasis_gtk_b_post_dialog_city_entry")), 99 GNUNET_JSON_pack_string ("postcode", 100 gt (builder, 101 "anastasis_gtk_b_post_dialog_postcode_entry") 102 ), 103 GNUNET_JSON_pack_string ("country", 104 gt (builder, 105 "anastasis_gtk_b_post_dialog_country_entry")) 106 ); 107 addr_s = json_dumps (addr, 108 JSON_COMPACT | JSON_SORT_KEYS); 109 json_decref (addr); 110 } 111 args = json_pack ("{ s:{s:s, s:o, s:s}}", 112 "authentication_method", 113 "type", 114 "post", 115 "challenge", 116 GNUNET_JSON_from_data (addr_s, 117 strlen (addr_s)), 118 "instructions", 119 ins); 120 free (addr_s); 121 GNUNET_free (ins); 122 gtk_widget_destroy (GTK_WIDGET (dialog)); 123 g_object_unref (G_OBJECT (builder)); 124 AG_freeze (); 125 AG_ra = ANASTASIS_redux_action (AG_redux_state, 126 "add_authentication", 127 args, 128 &AG_action_cb, 129 NULL); 130 json_decref (args); 131 } 132 133 134 void 135 anastasis_gtk_b_post_dialog_entry_changed_cb (GtkEntry *entry, 136 gpointer user_data) 137 { 138 const char *fields[] = { 139 "anastasis_gtk_b_post_dialog_full_name_entry", 140 "anastasis_gtk_b_post_dialog_street_entry", 141 "anastasis_gtk_b_post_dialog_city_entry", 142 "anastasis_gtk_b_post_dialog_postcode_entry", 143 "anastasis_gtk_b_post_dialog_country_entry", 144 NULL 145 }; 146 GtkBuilder *builder = GTK_BUILDER (user_data); 147 bool sensitive = true; 148 149 for (unsigned int i = 0; NULL != fields[i]; i++) 150 { 151 const char *qs; 152 153 qs = gt (builder, 154 fields[i]); 155 if ( (NULL == qs) || 156 (0 == strlen (qs)) ) 157 { 158 sensitive = false; 159 break; 160 } 161 } 162 163 { 164 GtkWidget *button; 165 166 button = GTK_WIDGET (gtk_builder_get_object (builder, 167 "anastasis_gtk_b_post_dialog_btn_ok")); 168 gtk_widget_set_sensitive (button, 169 sensitive); 170 } 171 } 172 173 174 /** 175 * Callback invoked if the the "secure post"-button is clicked. 176 * 177 * @param object 178 * @param user_data unused 179 */ 180 void 181 anastasis_gtk_btn_add_auth_post_clicked_cb (GObject *object, 182 gpointer user_data) 183 { 184 GtkWidget *ad; 185 GtkBuilder *builder; 186 187 builder = ANASTASIS_GTK_get_new_builder ( 188 ANASTASIS_GTK_project_data (), 189 "anastasis_gtk_auth_add_post.glade", 190 NULL); 191 if (NULL == builder) 192 { 193 GNUNET_break (0); 194 return; 195 } 196 ad = GTK_WIDGET (gtk_builder_get_object (builder, 197 "anastasis_gtk_b_post_dialog")); 198 { 199 GtkWidget *toplevel; 200 201 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (object)); 202 gtk_window_set_transient_for (GTK_WINDOW (ad), 203 GTK_WINDOW (toplevel)); 204 gtk_window_present (GTK_WINDOW (ad)); 205 } 206 }