anastasis-gtk_handle-print.c (6047B)
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_util.h" 28 #include "anastasis-gtk_attributes.h" 29 #include "anastasis-gtk_helper.h" 30 #include <jansson.h> 31 #include <gdk-pixbuf/gdk-pixbuf.h> 32 #include "print.h" 33 34 35 /** 36 * Generate PDF of the user's personal attributes 37 * and output it to @a filename. 38 * 39 * @param parent_window the parent window of the button 40 * @param filename where to store the result 41 */ 42 static void 43 print_to_file (GtkWindow *parent_window, 44 const char *filename) 45 { 46 json_t *attr; 47 json_t *attrs; 48 json_t *uattrs; 49 int ret; 50 json_t *av; 51 json_t *ra; 52 const char *key; 53 54 ra = json_object_get (AG_redux_state, 55 "required_attributes"); 56 attr = AG_collect_attributes (false); 57 attrs = json_object_get (attr, 58 "identity_attributes"); 59 /* 60 Example for ATTRS is now: 61 "full_name": "Max", 62 "birthdate": "1980-09-22", 63 "sq_number": "4" 64 */ 65 /* Convert JSON key 'full_name' to (possibly translated) version 66 of "Full name" using the "required_attributes" data structure 67 in 'ra' */ 68 uattrs = json_object (); 69 json_object_foreach (attrs, key, av) 70 { 71 json_t *pos; 72 size_t off; 73 bool found = false; 74 75 if (0 == strcmp (key, 76 "application_id")) 77 continue; /* not an attribute the user entered */ 78 json_array_foreach (ra, off, pos) 79 { 80 const char *name = json_string_value (json_object_get (pos, 81 "name")); 82 83 if (NULL == name) 84 { 85 GNUNET_break (0); 86 continue; 87 } 88 if (0 == strcmp (name, 89 key)) 90 { 91 const char *tkey; 92 struct GNUNET_JSON_Specification spec[] = { 93 GNUNET_JSON_spec_string ("label", 94 &tkey), 95 GNUNET_JSON_spec_end () 96 }; 97 98 if (GNUNET_OK != 99 GNUNET_JSON_parse (pos, 100 spec, 101 NULL, NULL)) 102 { 103 GNUNET_break (0); 104 continue; 105 } 106 found = true; 107 GNUNET_break (0 == 108 json_object_set (uattrs, 109 dgettext ("anastasis", 110 tkey), 111 av)); 112 } 113 } 114 if (! found) 115 { 116 GNUNET_break (0); 117 GNUNET_break (0 == 118 json_object_set (uattrs, 119 key, 120 av)); 121 } 122 } 123 json_decref (attr); /* 'attrs' is borrowed from 'attr' */ 124 125 /* now convert to PDF */ 126 ret = AG_print (uattrs, 127 filename); 128 json_decref (uattrs); 129 if (0 != ret) 130 { 131 /* report something went wrong; alas, we don't have a good 132 error message here (yet)...*/ 133 GtkAlertDialog *dialog; 134 135 dialog = gtk_alert_dialog_new ("%s", 136 _ ("Failed to generate PDF file.")); 137 gtk_alert_dialog_show (dialog, 138 parent_window); 139 g_object_unref (dialog); 140 } 141 } 142 143 144 /** 145 * Function called once the user picked the file to print to. 146 * 147 * @param source the file dialog 148 * @param res the result of the user's choice 149 * @param user_data the window the dialog belongs to 150 */ 151 static void 152 print_dialog_response_cb (GObject *source, 153 GAsyncResult *res, 154 gpointer user_data) 155 { 156 GtkWindow *parent_window = user_data; 157 GFile *file; 158 char *filename; 159 160 file = gtk_file_dialog_save_finish (GTK_FILE_DIALOG (source), 161 res, 162 NULL); 163 if (NULL == file) 164 return; /* user aborted */ 165 filename = ANASTASIS_GTK_file_get_path_utf8 (file); 166 g_object_unref (file); 167 if (NULL == filename) 168 return; 169 print_to_file (parent_window, 170 filename); 171 GNUNET_free (filename); 172 } 173 174 175 /** 176 * The user clicked the 'save as' button to save their personal 177 * details. Try to save them as a PDF. 178 * 179 * @param button the button that triggered printing 180 * @param user_data the builder for the dialog 181 */ 182 void 183 anastasis_gtk_print_details_button_clicked_cb (GtkWidget *button, 184 gpointer user_data) 185 { 186 GtkFileDialog *dialog; 187 GtkWindow *parent_window; 188 GtkRoot *top; 189 190 (void) user_data; 191 top = gtk_widget_get_root (button); 192 if (GTK_IS_WINDOW (top)) 193 { 194 parent_window = GTK_WINDOW (top); 195 } 196 else 197 { 198 GNUNET_break (0); 199 parent_window = NULL; 200 } 201 dialog = gtk_file_dialog_new (); 202 gtk_file_dialog_set_title (dialog, 203 _ ("Save Personal Details to PDF file")); 204 gtk_file_dialog_set_initial_name (dialog, 205 _ ("anastasis-personal-details.pdf")); 206 gtk_file_dialog_save (dialog, 207 parent_window, 208 NULL, 209 &print_dialog_response_cb, 210 parent_window); 211 g_object_unref (dialog); 212 }