aboutsummaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_handle-print.c
blob: c64af21d9d03f0d547fd89c70b41905f434d1f6d (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
/*
     This file is part of anastasis-gtk.
     Copyright (C) 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_print.c
 * @brief Implementation of the "Save as" button for the user attributes
 * @author Christian Grothoff
 */
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_attributes.h"
#include "anastasis-gtk_helper.h"
#include <jansson.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "print.h"


/**
 * Generate PDF of the user's personal attributes
 * and output it to @a filename.
 *
 * @param parent_window the parent window of the button
 * @param filename where to store the result
 */
static void
print_to_file (GtkWindow *parent_window,
               const char *filename)
{
  json_t *attr;
  json_t *attrs;
  json_t *uattrs;
  int ret;
  json_t *av;
  json_t *ra;
  const char *key;

  ra = json_object_get (AG_redux_state,
                        "required_attributes");
  attr = AG_collect_attributes (false);
  attrs = json_object_get (attr,
                           "identity_attributes");
  /*
     Example for ATTRS is now:
     "full_name": "Max",
     "birthdate": "1980-09-22",
     "sq_number": "4"
  */
  /* Convert JSON key 'full_name' to (possibly translated) version
     of "Full name" using the "required_attributes" data structure
     in 'ra' */
  uattrs = json_object ();
  json_object_foreach (attrs, key, av)
  {
    json_t *pos;
    size_t off;
    bool found = false;

    json_array_foreach (ra, off, pos)
    {
      const char *name = json_string_value (json_object_get (pos,
                                                             "name"));

      if (NULL == name)
      {
        GNUNET_break (0);
        continue;
      }
      if (0 == strcmp (name,
                       key))
      {
        const char *tkey;
        struct GNUNET_JSON_Specification spec[] = {
          GNUNET_JSON_spec_string ("label",
                                   &tkey),
          GNUNET_JSON_spec_end ()
        };

        if (GNUNET_OK !=
            GNUNET_JSON_parse (pos,
                               spec,
                               NULL, NULL))
        {
          GNUNET_break (0);
          continue;
        }
        found = true;
        GNUNET_break (0 ==
                      json_object_set (uattrs,
                                       dgettext ("anastasis",
                                                 tkey),
                                       av));
      }
    }
    if (! found)
    {
      GNUNET_break (0);
      GNUNET_break (0 ==
                    json_object_set (uattrs,
                                     key,
                                     av));
    }
  }
  json_decref (attrs);

  /* now convert to PDF */
  ret = AG_print (uattrs,
                  filename);
  json_decref (uattrs);
  if (0 != ret)
  {
    /* report something went wrong; alas, we don't have a good
       error message here (yet)...*/
    GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new (parent_window,
                                     flags,
                                     GTK_MESSAGE_ERROR,
                                     GTK_BUTTONS_CLOSE,
                                     _ ("Failed to generate PDF file."));
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
  }
}


/**
 * The user clicked the 'save as' button to save their personal
 * details. Try to save them as a PDF.
 *
 * @param button the button that triggered printing
 * @param user_data the builder for the dialog
 */
void
anastasis_gtk_print_details_button_clicked_cb (GtkWidget *button,
                                               gpointer user_data)
{
  GtkWidget *dialog;
  GtkFileChooser *chooser;
  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
  gint res;
  GtkWindow *parent_window;
  GtkWidget *top;

  (void) user_data;
  top = gtk_widget_get_toplevel (button);
  if (GTK_IS_WINDOW (top))
  {
    parent_window = GTK_WINDOW (top);
  }
  else
  {
    GNUNET_break (0);
    parent_window = NULL;
  }
  dialog = gtk_file_chooser_dialog_new ("Save Personal Details to PDF file",
                                        parent_window,
                                        action,
                                        _ ("_Cancel"),
                                        GTK_RESPONSE_CANCEL,
                                        _ ("_Save"),
                                        GTK_RESPONSE_ACCEPT,
                                        NULL);
  chooser = GTK_FILE_CHOOSER (dialog);
  gtk_file_chooser_set_do_overwrite_confirmation (chooser,
                                                  TRUE);
  gtk_file_chooser_set_current_name (chooser,
                                     _ ("anastasis-personal-details.pdf"));
  res = gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_hide (GTK_WIDGET (dialog));
  if (res == GTK_RESPONSE_ACCEPT)
  {
    char *filename;

    filename = gtk_file_chooser_get_filename (chooser);
    print_to_file (parent_window,
                   filename);
    g_free (filename);
  }
  gtk_widget_destroy (dialog);
}