anastasis-gtk_attributes.c (6369B)
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 * @file src/anastasis/anastasis-gtk_handle-identity-changed.c 22 * @brief 23 * @author Christian Grothoff 24 * @author Dennis Neufeld 25 */ 26 #include <gnunet/gnunet_util_lib.h> 27 #include "anastasis-gtk_helper.h" 28 #include "anastasis-gtk_action.h" 29 #include "anastasis-gtk_attributes.h" 30 #include "anastasis-gtk_handle-identity-changed.h" 31 32 33 static json_t * 34 extract_entry (GtkWidget *entry) 35 { 36 const gchar *txt; 37 38 txt = gtk_entry_get_text (GTK_ENTRY (entry)); 39 if ( (NULL == txt) || 40 (0 == strlen (txt)) ) 41 return NULL; 42 return json_string (txt); 43 } 44 45 46 static json_t * 47 extract_cal (GtkWidget *cal) 48 { 49 guint day = 0; 50 guint month = 0; 51 guint year = 0; 52 char txt[12]; 53 54 gtk_calendar_get_date (GTK_CALENDAR (cal), 55 &year, 56 &month, 57 &day); 58 GNUNET_snprintf (txt, 59 sizeof (txt), 60 "%04u-%02u-%02u", 61 (unsigned int) year, 62 (unsigned int) month + 1, 63 (unsigned int) day); 64 return json_string (txt); 65 } 66 67 68 json_t * 69 AG_collect_attributes (bool partial) 70 { 71 static struct 72 { 73 const char *type; 74 json_t * (*extract)(GtkWidget *w); 75 } e_map [] = { 76 { .type = "string", 77 .extract = &extract_entry }, 78 { .type = "date", 79 .extract = &extract_cal }, 80 { .type = NULL, 81 .extract = NULL } 82 }; 83 const json_t *id_attributes; 84 json_t *result; 85 size_t index; 86 json_t *id_attr; 87 88 id_attributes = json_object_get (AG_redux_state, 89 "required_attributes"); 90 GNUNET_assert (NULL != id_attributes); 91 result = json_object (); 92 GNUNET_assert (NULL != result); 93 json_array_foreach (id_attributes, index, id_attr) 94 { 95 json_t *val = NULL; 96 GtkWidget *w; 97 const char *attr_name; 98 const char *attr_type; 99 const char *attr_uuid; 100 bool optional = false; 101 struct GNUNET_JSON_Specification spec[] = { 102 GNUNET_JSON_spec_mark_optional ( 103 GNUNET_JSON_spec_bool ("optional", 104 &optional), 105 NULL), 106 GNUNET_JSON_spec_string ("type", 107 &attr_type), 108 GNUNET_JSON_spec_string ("name", 109 &attr_name), 110 GNUNET_JSON_spec_string ("uuid", 111 &attr_uuid), 112 GNUNET_JSON_spec_end () 113 }; 114 struct GNUNET_HashCode uh; 115 116 GNUNET_assert (GNUNET_OK == 117 GNUNET_JSON_parse (id_attr, 118 spec, 119 NULL, NULL)); 120 GNUNET_CRYPTO_hash (attr_uuid, 121 strlen (attr_uuid), 122 &uh); 123 w = GNUNET_CONTAINER_multihashmap_get (AG_entry_attributes, 124 &uh); 125 if (NULL == w) 126 { 127 if (partial) 128 continue; 129 json_decref (result); 130 return NULL; 131 } 132 for (unsigned int i = 0; NULL != e_map[i].type; i++) 133 { 134 if (0 != strcmp (e_map[i].type, 135 attr_type)) 136 continue; 137 val = e_map[i].extract (w); 138 break; 139 } 140 if (NULL == val) 141 { 142 if (partial) 143 continue; 144 if (optional) 145 continue; 146 AG_mark_invalid_input (attr_name); 147 json_decref (result); 148 return NULL; 149 } 150 GNUNET_assert (0 == 151 json_object_set_new (result, 152 attr_name, 153 val)); 154 } 155 GNUNET_assert (0 == 156 json_object_set_new (result, 157 "application_id", 158 json_string (AG_application_id))); 159 return GNUNET_JSON_PACK ( 160 GNUNET_JSON_pack_object_steal ("identity_attributes", 161 result)); 162 } 163 164 165 /** 166 * Import string value into a GtkEntry. 167 * 168 * @param w should be a GtkEntry 169 * @param value should be a string value 170 */ 171 static void 172 import_entry (GtkWidget *w, 173 const json_t *value) 174 { 175 GNUNET_break (json_is_string (value)); 176 gtk_entry_set_text (GTK_ENTRY (w), 177 json_string_value (value)); 178 } 179 180 181 /** 182 * Import date value into a GtkCalendar. 183 * 184 * @param w should be a GtkCalendar 185 * @param value should be a date value 186 */ 187 static void 188 import_cal (GtkWidget *w, 189 const json_t *value) 190 { 191 const char *s; 192 guint day; 193 guint month; 194 guint year; 195 char dummy; 196 197 s = json_string_value (value); 198 if (NULL == s) 199 { 200 GNUNET_break (0); 201 return; 202 } 203 if (3 != 204 sscanf (s, 205 "%04u-%02u-%02u%c", 206 &year, 207 &month, 208 &day, 209 &dummy)) 210 { 211 GNUNET_break (0); 212 return; 213 } 214 gtk_calendar_select_day (GTK_CALENDAR (w), 215 day); 216 gtk_calendar_select_month (GTK_CALENDAR (w), 217 month - 1, 218 year); 219 } 220 221 222 void 223 AG_import_attribute_data (GtkWidget *w, 224 const char *type, 225 const json_t *value) 226 { 227 static struct 228 { 229 const char *type; 230 void (*import)(GtkWidget *w, 231 const json_t *value); 232 } i_map [] = { 233 { .type = "string", 234 .import = &import_entry }, 235 { .type = "date", 236 .import = &import_cal }, 237 { .type = NULL, 238 .import = NULL } 239 }; 240 241 for (unsigned int i = 0; NULL != i_map[i].type; i++) 242 { 243 if (0 != strcmp (i_map[i].type, 244 type)) 245 continue; 246 i_map[i].import (w, 247 value); 248 return; 249 } 250 251 }