anastasis-gtk

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

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


      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_helper.h"
     27 #include "anastasis-gtk_action.h"
     28 #include "anastasis-gtk_attributes.h"
     29 #include "anastasis-gtk_handle-identity-changed.h"
     30 #include <jansson.h>
     31 
     32 
     33 /**
     34  * Function called with the results of #ANASTASIS_redux_action.
     35  *
     36  * @param cls closure
     37  * @param error_code Error code
     38  * @param response new state as result or config information of a provider
     39  */
     40 static void
     41 change_action_cb (void *cls,
     42                   enum TALER_ErrorCode error_code,
     43                   json_t *response)
     44 {
     45   (void) cls;
     46   AG_ra = NULL;
     47   if (TALER_EC_NONE != error_code)
     48   {
     49     AG_error ("Error: %s (%d)\n",
     50               TALER_ErrorCode_get_hint (error_code),
     51               error_code);
     52     AG_insensitive ("anastasis_gtk_main_window_forward_button");
     53     return;
     54   }
     55   AG_action_cb (NULL,
     56                 TALER_EC_NONE,
     57                 response);
     58 }
     59 
     60 
     61 /**
     62  * The version or provider URL was edited by the user. Try to
     63  * download the specified version from the specified provider.
     64  */
     65 static void
     66 update_policy (void)
     67 {
     68   GtkSpinButton *sb;
     69   GtkEntry *ge;
     70   gint version;
     71   const char *provider_url;
     72   GtkWidget *toplevel;
     73 
     74   if (AG_in_action)
     75     return;
     76   toplevel = gtk_widget_get_toplevel (
     77     GTK_WIDGET (GCG_get_main_window_object (
     78                   "anastasis_gtk_main_window")));
     79   if (NULL != AG_ra)
     80   {
     81     ANASTASIS_redux_action_cancel (AG_ra);
     82     AG_ra = NULL;
     83   }
     84 
     85   if (NULL !=
     86       json_object_get (AG_redux_state,
     87                        "challenge_feedback"))
     88   {
     89     GtkWidget *diag;
     90     gint ret;
     91 
     92     diag = gtk_message_dialog_new (
     93       GTK_WINDOW (toplevel),
     94       GTK_DIALOG_MODAL,
     95       GTK_MESSAGE_QUESTION,
     96       GTK_BUTTONS_OK_CANCEL,
     97       _ ("This action will reset all of your challenge solving progress!"));
     98     ret = gtk_dialog_run (GTK_DIALOG (diag));
     99     gtk_widget_destroy (diag);
    100     switch (ret)
    101     {
    102     case GTK_RESPONSE_OK:
    103       break;
    104     default:
    105       {
    106         /* call action to reset view */
    107         json_t *cp = json_incref (AG_redux_state);
    108 
    109         AG_action_cb (NULL,
    110                       TALER_EC_NONE,
    111                       cp);
    112         json_decref (cp);
    113       }
    114       /* user aborted */
    115       return;
    116     }
    117   }
    118 
    119   sb = GTK_SPIN_BUTTON (GCG_get_main_window_object (
    120                           "anastasis_gtk_policy_version_spin_button"));
    121   ge = GTK_ENTRY (GCG_get_main_window_object (
    122                     "anastasis_gtk_provider_url_entry"));
    123   provider_url = gtk_entry_get_text (ge);
    124   if (! ( ( (0 == strncasecmp (provider_url,
    125                                "https://",
    126                                strlen ("https://"))) &&
    127             (strlen (provider_url) >= strlen ("https://X/")) ) ||
    128           ( (0 == strncasecmp (provider_url,
    129                                "http://",
    130                                strlen ("http://"))) &&
    131             (strlen (provider_url) >= strlen ("http://X/")) ) ) )
    132   {
    133     AG_error ("Notice: URL must begin with 'http://' or 'https://'.");
    134     AG_insensitive ("anastasis_gtk_main_window_forward_button");
    135     return;
    136   }
    137   if ( (0 == strlen (provider_url)) ||
    138        ('/' != provider_url[strlen (provider_url) - 1]) )
    139   {
    140     AG_error ("Notice: URL must end with '/'.");
    141     AG_insensitive ("anastasis_gtk_main_window_forward_button");
    142     return;
    143   }
    144   version = gtk_spin_button_get_value_as_int (sb);
    145 
    146   {
    147     json_t *args;
    148 
    149     args = GNUNET_JSON_PACK (
    150       GNUNET_JSON_pack_uint64 ("version",
    151                                version),
    152       GNUNET_JSON_pack_string ("provider_url",
    153                                provider_url));
    154     AG_ra = ANASTASIS_redux_action (AG_redux_state,
    155                                     "change_version",
    156                                     args,
    157                                     &change_action_cb,
    158                                     NULL);
    159     json_decref (args);
    160   }
    161 }
    162 
    163 
    164 void
    165 anastasis_gtk_policy_version_spin_button_changed_cb (GtkEditable *entry,
    166                                                      gpointer user_data)
    167 {
    168   update_policy ();
    169 }
    170 
    171 
    172 void
    173 anastasis_gtk_provider_url_entry_changed_cb (GtkEditable *entry,
    174                                              gpointer user_data)
    175 {
    176   update_policy ();
    177 }