summaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk.c
blob: 3a1e5e52be0e8a658654c28d7f68133f89f171e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
     This file is part of anastasis-gtk.
     Copyright (C) 2020, 2021 Anastasis SARL

     Anastasis is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     Anastasis is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with Anastasis; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/anastasis/anastasis-gtk.c
 * @brief Main function of anastasis-gtk
 * @author Christian Grothoff
 * @author Dennis Neufeld
 */
#include <gnunet/platform.h>
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_action.h"
#include "anastasis-gtk_helper.h"
#include <jansson.h>

/**
 * Handle to our main loop.
 */
struct GNUNET_GTK_MainLoop *AG_ml;

/**
 * Our configuration.
 */
const struct GNUNET_CONFIGURATION_Handle *AG_cfg;

/**
 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
 */
static struct GNUNET_CURL_RescheduleContext *rc;

/**
 * Hash map from UUID hashes to GtkWidgets.
 */
struct GNUNET_CONTAINER_MultiHashMap *AG_entry_attributes;

/**
 * Curl context for communication with taler backend
 */
static struct GNUNET_CURL_Context *ctx;

/**
 * Handle to an ongoing action.
 */
struct ANASTASIS_ReduxAction *AG_ra;

/**
 * Handle to an ongoing background action.
 */
struct ANASTASIS_ReduxAction *AG_long_action;

/**
 * Handle to task to reschedule #AG_long_action.
 */
struct GNUNET_SCHEDULER_Task *AG_long_task;


/**
 * Actual state.
 */
json_t *AG_redux_state;


/**
 * Callback invoked if the the "show animation"-menuitem (Help) is clicked.
 *
 * @param menuitem the object which received the signal.
 * @param user_data user data set when the signal handler was connected.
 */
void
anastasis_gtk_animation_activate_cb (GtkMenuItem *menuitem,
                                     gpointer user_data)
{
  static const struct
  {
    const char *png;
    const char *widget;
  } map[] = {
    { .png = "continent_selection.jpg",
      .widget = "anastasis_gtk_continent_selection_image" },
    { .png = "country_selection.jpg",
      .widget = "anastasis_gtk_country_selection_image" },
    { .png = "user_attributes.png",
      .widget = "anastasis_gtk_user_attributes_image" },
    { .png = "authentication_methods.png",
      .widget = "anastasis_gtk_b_authentication_methods_image" },
    { .png = "policy_confirmation.png",
      .widget = "anastasis_gtk_b_policies_image" },
    { .png = "enter_secret.jpg",
      .widget = "anastasis_gtk_enter_secret_image" },
    { .png = "pay_with_taler.png",
      .widget = "anastasis_gtk_pay_image" },
    { .png = NULL,
      .widget = NULL },
    { .png = NULL,
      .widget = "anastasis_gtk_completed_image" }
  };
  char *path;

  if (gtk_widget_is_visible (GTK_WIDGET (GCG_get_main_window_object (
                                           "anastasis_gtk_illustration_vbox"))))
  {
    AG_hide ("anastasis_gtk_illustration_vbox");
    return;
  }
  AG_show ("anastasis_gtk_illustration_vbox");
  path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
  if (NULL == path)
  {
    GNUNET_break (0);
    return;
  }
  for (unsigned int i = 0; NULL != map[i].png; i++)
  {
    GObject *img;

    img = GCG_get_main_window_object (map[i].widget);
    if (NULL == img)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Could not find widget `%s' to set image path\n",
                  map[i].widget);
    }
    else
    {
      char *ip;

      GNUNET_asprintf (&ip,
                       "%s/share/anastasis/%s",
                       path,
                       map[i].png);
      gtk_image_set_from_file (GTK_IMAGE (img),
                               ip);
      GNUNET_free (ip);
    }
  }
  GNUNET_free (path);
}


void
AG_stop_long_action (void)
{
  if (NULL != AG_long_action)
  {
    ANASTASIS_redux_action_cancel (AG_long_action);
    AG_long_action = NULL;
  }
  if (NULL != AG_long_task)
  {
    GNUNET_SCHEDULER_cancel (AG_long_task);
    AG_long_task = NULL;
  }
}


/**
 * Task run on shutdown.
 *
 * @param cls unused
 */
static void
shutdown_task (void *cls)
{
  (void) cls;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Shutdown initiated\n");
  ANASTASIS_redux_done ();
  if (NULL != AG_ra)
  {
    ANASTASIS_redux_action_cancel (AG_ra);
    AG_ra = NULL;
  }
  AG_stop_long_action ();
  if (NULL != ctx)
  {
    GNUNET_CURL_fini (ctx);
    ctx = NULL;
  }
  if (NULL != rc)
  {
    GNUNET_CURL_gnunet_rc_destroy (rc);
    rc = NULL;
  }
  GNUNET_GTK_main_loop_quit (AG_ml);
  AG_ml = NULL;
  GNUNET_CONTAINER_multihashmap_destroy (AG_entry_attributes);
  AG_entry_attributes = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Shutdown complete\n");
}


/**
 * Callback invoked if the application is supposed to exit.
 *
 * @param object
 * @param user_data unused
 */
void
anastasis_gtk_quit_cb (GObject *object,
                       gpointer user_data)
{
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * User clicked the "quit" button.
 *
 * @param button the button
 * @param user_data unused
 */
void
anastasis_gtk_main_window_quit_button_clicked_cb (GtkButton *button,
                                                  gpointer user_data)
{
  GNUNET_SCHEDULER_shutdown ();
}


void
AG_load (const char *filename)
{
  json_error_t error;
  json_t *in;

  in = json_load_file (filename,
                       JSON_REJECT_DUPLICATES,
                       &error);
  if (NULL == in)
  {
    AG_error ("Failed to parse file `%s' at %d:%d: %s\n",
              filename,
              error.line,
              error.column,
              error.text);
    return;
  }
  AG_action_cb (NULL,
                TALER_EC_NONE,
                in);
  json_decref (in);
}


/**
 * Actual main function run right after GNUnet's scheduler
 * is initialized.  Initializes up GTK and Glade.
 *
 * @param cls NULL
 */
static void
run (void *cls)
{
  GtkWidget *main_window;
  int argc;
  char *const *argv;

  AG_ml = cls;
  AG_entry_attributes = GNUNET_CONTAINER_multihashmap_create (16,
                                                              GNUNET_NO);
  GNUNET_GTK_set_icon_search_path ();
  GNUNET_OS_init (ANASTASIS_project_data_default ());
  GNUNET_GTK_setup_nls ();
  if (GNUNET_OK !=
      GNUNET_GTK_main_loop_build_window (AG_ml,
                                         NULL))
    return;
  AG_cfg = GNUNET_GTK_main_loop_get_configuration (AG_ml);
  GNUNET_GTK_main_loop_get_args (AG_ml,
                                 &argc,
                                 &argv);
  /* setup main window */
  main_window = GTK_WIDGET (
    GCG_get_main_window_object ("anastasis_gtk_main_window"));
  gtk_window_maximize (GTK_WINDOW (main_window));
  /* make GUI visible */
  gtk_widget_show (main_window);
  gtk_window_present (GTK_WINDOW (main_window));
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 NULL);
  /* initialize HTTP client */
  ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
                          &rc);
  rc = GNUNET_CURL_gnunet_rc_create (ctx);
  ANASTASIS_redux_init (ctx);
  if (0 != argc)
    AG_load (argv[0]);
}


/**
 * Main function of anastasis-gtk.
 *
 * @param argc number of arguments
 * @param argv arguments
 * @return 0 on success
 */
int
main (int argc,
      char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  if (GNUNET_OK !=
      GNUNET_GTK_main_loop_start ("anastasis-gtk",
                                  "GTK GUI for Anastasis",
                                  argc,
                                  argv,
                                  options,
                                  "anastasis_gtk_main_window.glade",
                                  &run))
    ret = 1;
  else
    ret = 0;
  return ret;
}


/* end of anastasis-gtk.c */