anastasis-gtk

Demonstrator GUI for Anastasis
Log | Files | Refs | README | LICENSE

anastasis-gtk_attributes.c (6659B)


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