anastasis-gtk

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

anastasis-gtk_handle-add-provider.c (7966B)


      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/gnunet_util_lib.h>
     27 #include "anastasis_gtk_util.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 acfg configuration obtained, NULL if we could not parse it
     52  */
     53 static void
     54 config_cb (void *cls,
     55            const struct ANASTASIS_Config *acfg)
     56 {
     57   GtkBuilder *builder = GTK_BUILDER (cls);
     58   GtkWidget *button;
     59   json_t *methods_list;
     60   GtkLabel *l;
     61 
     62   co = NULL;
     63   l = GTK_LABEL (gtk_builder_get_object (builder,
     64                                          "error_label"));
     65   if (MHD_HTTP_OK != acfg->http_status)
     66   {
     67     char *msg;
     68 
     69     if (0 == acfg->http_status)
     70       GNUNET_asprintf (&msg,
     71                        "Provider URL invalid (no response)");
     72     else
     73       GNUNET_asprintf (&msg,
     74                        "Provider URL invalid (HTTP status %u)",
     75                        acfg->http_status);
     76     gtk_widget_show (GTK_WIDGET (l));
     77     gtk_label_set_text (l,
     78                         msg);
     79     free (msg);
     80     return;
     81   }
     82   methods_list = json_array ();
     83   GNUNET_assert (NULL != methods_list);
     84   for (unsigned int i = 0; i<acfg->details.ok.methods_length; i++)
     85   {
     86     const struct ANASTASIS_AuthorizationMethodConfig *method
     87       = &acfg->details.ok.methods[i];
     88     json_t *mj = GNUNET_JSON_PACK (
     89       GNUNET_JSON_pack_string ("type",
     90                                method->type),
     91       TALER_JSON_pack_amount ("usage_fee",
     92                               &method->usage_fee));
     93 
     94     GNUNET_assert (0 ==
     95                    json_array_append_new (methods_list,
     96                                           mj));
     97   }
     98   pstate = GNUNET_JSON_PACK (
     99     GNUNET_JSON_pack_array_steal ("methods",
    100                                   methods_list),
    101     TALER_JSON_pack_amount ("annual_fee",
    102                             &acfg->details.ok.annual_fee),
    103     TALER_JSON_pack_amount ("truth_upload_fee",
    104                             &acfg->details.ok.truth_upload_fee),
    105     TALER_JSON_pack_amount ("liability_limit",
    106                             &acfg->details.ok.liability_limit),
    107     GNUNET_JSON_pack_string ("business_name",
    108                              acfg->details.ok.business_name),
    109     GNUNET_JSON_pack_string ("status",
    110                              "enabled"),
    111     GNUNET_JSON_pack_uint64 ("http_status",
    112                              MHD_HTTP_OK),
    113     GNUNET_JSON_pack_uint64 ("storage_limit_in_megabytes",
    114                              acfg->details.ok.storage_limit_in_megabytes),
    115     GNUNET_JSON_pack_data_auto ("provider_salt",
    116                                 &acfg->details.ok.provider_salt),
    117     GNUNET_JSON_pack_uint64 ("http_status",
    118                              acfg->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   json_t *ap;
    186 
    187   if (NULL != co)
    188   {
    189     ANASTASIS_config_cancel (co);
    190     co = NULL;
    191   }
    192   if (GTK_RESPONSE_APPLY != response_id)
    193   {
    194     gtk_widget_destroy (GTK_WIDGET (dialog));
    195     g_object_unref (G_OBJECT (builder));
    196     json_decref (pstate);
    197     pstate = NULL;
    198     return;
    199   }
    200   if (NULL == pstate)
    201   {
    202     GNUNET_break (0);
    203     return;
    204   }
    205   ap = json_object_get (AG_redux_state,
    206                         "authentication_providers");
    207   if (NULL == ap)
    208   {
    209     GNUNET_break (0);
    210     return;
    211   }
    212   entry = GTK_ENTRY (gtk_builder_get_object (builder,
    213                                              "url_entry"));
    214   url = gtk_entry_get_text (entry);
    215   ANASTASIS_policy_discovery_more (AG_pd,
    216                                    url,
    217                                    pstate);
    218   GNUNET_break (0 ==
    219                 json_object_set_new (ap,
    220                                      url,
    221                                      pstate));
    222   pstate = NULL;
    223   gtk_widget_destroy (GTK_WIDGET (dialog));
    224   g_object_unref (G_OBJECT (builder));
    225 }
    226 
    227 
    228 /**
    229  * Callback invoked if the the "Add"-provider button is clicked.
    230  *
    231  * @param object
    232  * @param user_data unused
    233  */
    234 void
    235 anastasis_gtk_add_provider_button_clicked_cb (GtkButton *object,
    236                                               gpointer user_data)
    237 {
    238   GtkWidget *ad;
    239   GtkBuilder *builder;
    240   GtkWidget *toplevel;
    241 
    242   if (NULL == AG_pd)
    243   {
    244     GNUNET_break (0);
    245     return;
    246   }
    247   builder = ANASTASIS_GTK_get_new_builder (
    248     ANASTASIS_GTK_project_data (),
    249     "anastasis_gtk_add_provider.glade",
    250     NULL);
    251   if (NULL == builder)
    252   {
    253     GNUNET_break (0);
    254     return;
    255   }
    256   ad = GTK_WIDGET (gtk_builder_get_object (builder,
    257                                            "add_provider_dialog"));
    258   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (object));
    259   gtk_window_set_transient_for (GTK_WINDOW (ad),
    260                                 GTK_WINDOW (toplevel));
    261   gtk_window_present (GTK_WINDOW (ad));
    262 }