anastasis-gtk

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

anastasis-gtk_handle-policy-version-changed.c (6227B)


      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-policy-version-changed.c
     22  * @brief
     23  * @author Christian Grothoff
     24  */
     25 #include <gnunet/gnunet_util_lib.h>
     26 #include "anastasis_gtk_util.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 #include <jansson.h>
     32 
     33 
     34 /**
     35  * Function called with the results of #ANASTASIS_redux_action.
     36  *
     37  * @param cls closure
     38  * @param error_code Error code
     39  * @param response new state as result or config information of a provider
     40  */
     41 static void
     42 change_action_cb (void *cls,
     43                   enum TALER_ErrorCode error_code,
     44                   json_t *response)
     45 {
     46   (void) cls;
     47   AG_ra = NULL;
     48   if (TALER_EC_NONE != error_code)
     49   {
     50     AG_error ("Error: %s (%d)\n",
     51               TALER_ErrorCode_get_hint (error_code),
     52               error_code);
     53     AG_insensitive ("anastasis_gtk_main_window_forward_button");
     54     return;
     55   }
     56   AG_action_cb (NULL,
     57                 TALER_EC_NONE,
     58                 response);
     59 }
     60 
     61 
     62 /**
     63  * Download the version the user selected from the provider they named.
     64  */
     65 static void
     66 change_version (void)
     67 {
     68   GtkSpinButton *sb;
     69   GtkEntry *ge;
     70   gint version;
     71   const char *provider_url;
     72 
     73   sb = GTK_SPIN_BUTTON (GCG_get_main_window_object (
     74                           "anastasis_gtk_policy_version_spin_button"));
     75   ge = GTK_ENTRY (GCG_get_main_window_object (
     76                     "anastasis_gtk_provider_url_entry"));
     77   provider_url = gtk_editable_get_text (GTK_EDITABLE (ge));
     78   if (! ( ( (0 == strncasecmp (provider_url,
     79                                "https://",
     80                                strlen ("https://"))) &&
     81             (strlen (provider_url) >= strlen ("https://X/")) ) ||
     82           ( (0 == strncasecmp (provider_url,
     83                                "http://",
     84                                strlen ("http://"))) &&
     85             (strlen (provider_url) >= strlen ("http://X/")) ) ) )
     86   {
     87     AG_error ("Notice: URL must begin with 'http://' or 'https://'.");
     88     AG_insensitive ("anastasis_gtk_main_window_forward_button");
     89     return;
     90   }
     91   if ( (0 == strlen (provider_url)) ||
     92        ('/' != provider_url[strlen (provider_url) - 1]) )
     93   {
     94     AG_error ("Notice: URL must end with '/'.");
     95     AG_insensitive ("anastasis_gtk_main_window_forward_button");
     96     return;
     97   }
     98   version = gtk_spin_button_get_value_as_int (sb);
     99 
    100   {
    101     json_t *args;
    102 
    103     args = GNUNET_JSON_PACK (
    104       GNUNET_JSON_pack_uint64 ("version",
    105                                version),
    106       GNUNET_JSON_pack_string ("provider_url",
    107                                provider_url));
    108     AG_ra = ANASTASIS_redux_action (AG_redux_state,
    109                                     "change_version",
    110                                     args,
    111                                     &change_action_cb,
    112                                     NULL);
    113     json_decref (args);
    114   }
    115 }
    116 
    117 
    118 /**
    119  * The user answered the question whether they accept losing their challenge
    120  * solving progress.
    121  *
    122  * @param source the warning dialog
    123  * @param res the result of the user's choice, button 0 to proceed
    124  * @param user_data unused
    125  */
    126 static void
    127 reset_warning_response_cb (GObject *source,
    128                            GAsyncResult *res,
    129                            gpointer user_data)
    130 {
    131   (void) user_data;
    132   if (0 ==
    133       gtk_alert_dialog_choose_finish (GTK_ALERT_DIALOG (source),
    134                                       res,
    135                                       NULL))
    136   {
    137     change_version ();
    138     return;
    139   }
    140   /* user aborted: call action to reset view */
    141   {
    142     json_t *cp = json_incref (AG_redux_state);
    143 
    144     AG_action_cb (NULL,
    145                   TALER_EC_NONE,
    146                   cp);
    147     json_decref (cp);
    148   }
    149 }
    150 
    151 
    152 /**
    153  * The version or provider URL was edited by the user. Try to
    154  * download the specified version from the specified provider.
    155  */
    156 static void
    157 update_policy (void)
    158 {
    159   if (AG_in_action)
    160     return;
    161   if (NULL != AG_ra)
    162   {
    163     ANASTASIS_redux_action_cancel (AG_ra);
    164     AG_ra = NULL;
    165   }
    166   if (NULL !=
    167       json_object_get (AG_redux_state,
    168                        "challenge_feedback"))
    169   {
    170     GtkRoot *toplevel;
    171     GtkAlertDialog *diag;
    172     const char *buttons[] = {
    173       _ ("_OK"),
    174       _ ("_Cancel"),
    175       NULL
    176     };
    177 
    178     toplevel = gtk_widget_get_root (
    179       GTK_WIDGET (GCG_get_main_window_object (
    180                     "anastasis_gtk_main_window")));
    181     diag = gtk_alert_dialog_new (
    182       "%s",
    183       _ ("This action will reset all of your challenge solving progress!"));
    184     gtk_alert_dialog_set_buttons (diag,
    185                                   buttons);
    186     gtk_alert_dialog_set_cancel_button (diag,
    187                                         1);
    188     gtk_alert_dialog_set_default_button (diag,
    189                                          0);
    190     gtk_alert_dialog_choose (diag,
    191                              GTK_WINDOW (toplevel),
    192                              NULL,
    193                              &reset_warning_response_cb,
    194                              NULL);
    195     g_object_unref (diag);
    196     return;
    197   }
    198   change_version ();
    199 }
    200 
    201 
    202 void
    203 anastasis_gtk_policy_version_spin_button_changed_cb (GtkEditable *entry,
    204                                                      gpointer user_data)
    205 {
    206   update_policy ();
    207 }
    208 
    209 
    210 void
    211 anastasis_gtk_provider_url_entry_changed_cb (GtkEditable *entry,
    212                                              gpointer user_data)
    213 {
    214   update_policy ();
    215 }