anastasis-gtk

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

anastasis-gtk_handle-print.c (5892B)


      1 /*
      2      This file is part of anastasis-gtk.
      3      Copyright (C) 2021 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_print.c
     23  * @brief Implementation of the "Save as" button for the user attributes
     24  * @author Christian Grothoff
     25  */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include "anastasis-gtk_attributes.h"
     28 #include "anastasis-gtk_helper.h"
     29 #include <jansson.h>
     30 #include <gdk-pixbuf/gdk-pixbuf.h>
     31 #include "print.h"
     32 
     33 
     34 /**
     35  * Generate PDF of the user's personal attributes
     36  * and output it to @a filename.
     37  *
     38  * @param parent_window the parent window of the button
     39  * @param filename where to store the result
     40  */
     41 static void
     42 print_to_file (GtkWindow *parent_window,
     43                const char *filename)
     44 {
     45   json_t *attr;
     46   json_t *attrs;
     47   json_t *uattrs;
     48   int ret;
     49   json_t *av;
     50   json_t *ra;
     51   const char *key;
     52 
     53   ra = json_object_get (AG_redux_state,
     54                         "required_attributes");
     55   attr = AG_collect_attributes (false);
     56   attrs = json_object_get (attr,
     57                            "identity_attributes");
     58   /*
     59      Example for ATTRS is now:
     60      "full_name": "Max",
     61      "birthdate": "1980-09-22",
     62      "sq_number": "4"
     63   */
     64   /* Convert JSON key 'full_name' to (possibly translated) version
     65      of "Full name" using the "required_attributes" data structure
     66      in 'ra' */
     67   uattrs = json_object ();
     68   json_object_foreach (attrs, key, av)
     69   {
     70     json_t *pos;
     71     size_t off;
     72     bool found = false;
     73 
     74     json_array_foreach (ra, off, pos)
     75     {
     76       const char *name = json_string_value (json_object_get (pos,
     77                                                              "name"));
     78 
     79       if (NULL == name)
     80       {
     81         GNUNET_break (0);
     82         continue;
     83       }
     84       if (0 == strcmp (name,
     85                        key))
     86       {
     87         const char *tkey;
     88         struct GNUNET_JSON_Specification spec[] = {
     89           GNUNET_JSON_spec_string ("label",
     90                                    &tkey),
     91           GNUNET_JSON_spec_end ()
     92         };
     93 
     94         if (GNUNET_OK !=
     95             GNUNET_JSON_parse (pos,
     96                                spec,
     97                                NULL, NULL))
     98         {
     99           GNUNET_break (0);
    100           continue;
    101         }
    102         found = true;
    103         GNUNET_break (0 ==
    104                       json_object_set (uattrs,
    105                                        dgettext ("anastasis",
    106                                                  tkey),
    107                                        av));
    108       }
    109     }
    110     if (! found)
    111     {
    112       GNUNET_break (0);
    113       GNUNET_break (0 ==
    114                     json_object_set (uattrs,
    115                                      key,
    116                                      av));
    117     }
    118   }
    119   json_decref (attrs);
    120 
    121   /* now convert to PDF */
    122   ret = AG_print (uattrs,
    123                   filename);
    124   json_decref (uattrs);
    125   if (0 != ret)
    126   {
    127     /* report something went wrong; alas, we don't have a good
    128        error message here (yet)...*/
    129     GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
    130     GtkWidget *dialog;
    131 
    132     dialog = gtk_message_dialog_new (parent_window,
    133                                      flags,
    134                                      GTK_MESSAGE_ERROR,
    135                                      GTK_BUTTONS_CLOSE,
    136                                      _ ("Failed to generate PDF file."));
    137     gtk_dialog_run (GTK_DIALOG (dialog));
    138     gtk_widget_destroy (dialog);
    139   }
    140 }
    141 
    142 
    143 /**
    144  * The user clicked the 'save as' button to save their personal
    145  * details. Try to save them as a PDF.
    146  *
    147  * @param button the button that triggered printing
    148  * @param user_data the builder for the dialog
    149  */
    150 void
    151 anastasis_gtk_print_details_button_clicked_cb (GtkWidget *button,
    152                                                gpointer user_data)
    153 {
    154   GtkWidget *dialog;
    155   GtkFileChooser *chooser;
    156   GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
    157   gint res;
    158   GtkWindow *parent_window;
    159   GtkWidget *top;
    160 
    161   (void) user_data;
    162   top = gtk_widget_get_toplevel (button);
    163   if (GTK_IS_WINDOW (top))
    164   {
    165     parent_window = GTK_WINDOW (top);
    166   }
    167   else
    168   {
    169     GNUNET_break (0);
    170     parent_window = NULL;
    171   }
    172   dialog = gtk_file_chooser_dialog_new ("Save Personal Details to PDF file",
    173                                         parent_window,
    174                                         action,
    175                                         _ ("_Cancel"),
    176                                         GTK_RESPONSE_CANCEL,
    177                                         _ ("_Save"),
    178                                         GTK_RESPONSE_ACCEPT,
    179                                         NULL);
    180   chooser = GTK_FILE_CHOOSER (dialog);
    181   gtk_file_chooser_set_do_overwrite_confirmation (chooser,
    182                                                   TRUE);
    183   gtk_file_chooser_set_current_name (chooser,
    184                                      _ ("anastasis-personal-details.pdf"));
    185   res = gtk_dialog_run (GTK_DIALOG (dialog));
    186   gtk_widget_hide (GTK_WIDGET (dialog));
    187   if (res == GTK_RESPONSE_ACCEPT)
    188   {
    189     char *filename;
    190 
    191     filename = gtk_file_chooser_get_filename (chooser);
    192     print_to_file (parent_window,
    193                    filename);
    194     g_free (filename);
    195   }
    196   gtk_widget_destroy (dialog);
    197 }