diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-12-30 20:46:49 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-12-30 20:46:49 +0100 |
commit | 97e25d16f6aab66e9e84a06a90e0ffc022bf0d78 (patch) | |
tree | 26473eeece4125be38cc97f141f4dae5fe3c82f7 | |
parent | 697b9292283b1db460e84eeec12e7e121f378a50 (diff) | |
download | anastasis-gtk-97e25d16f6aab66e9e84a06a90e0ffc022bf0d78.tar.gz anastasis-gtk-97e25d16f6aab66e9e84a06a90e0ffc022bf0d78.tar.bz2 anastasis-gtk-97e25d16f6aab66e9e84a06a90e0ffc022bf0d78.zip |
finish 'print' implementation (#7090)
-rw-r--r-- | src/anastasis/anastasis-gtk_handle-print.c | 162 |
1 files changed, 143 insertions, 19 deletions
diff --git a/src/anastasis/anastasis-gtk_handle-print.c b/src/anastasis/anastasis-gtk_handle-print.c index e4961d3..35b0d84 100644 --- a/src/anastasis/anastasis-gtk_handle-print.c +++ b/src/anastasis/anastasis-gtk_handle-print.c @@ -20,7 +20,7 @@ /** * @file src/anastasis/anastasis-gtk_print.c - * @brief Implementation of the "print" button + * @brief Implementation of the "Save as" button for the user attributes * @author Christian Grothoff */ #include <gnunet/platform.h> @@ -31,24 +31,29 @@ #include <gdk-pixbuf/gdk-pixbuf.h> #include "print.h" + /** - * The user clicked the 'print' button to print their personal - * details. Try to print them. + * Generate PDF of the user's personal attributes + * and output it to @a filename. * - * @param button the print button - * @param user_data the builder for the dialog + * @param parent_window the parent window of the button + * @param filename where to store the result */ -void -anastasis_gtk_print_details_button_clicked_cb (GtkButton *button, - gpointer user_data) +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; - (void) button; - (void) user_data; GNUNET_break (0); + ra = json_object_get (AG_redux_state, + "required_attributes"); attr = AG_collect_attributes (false); attrs = json_object_get (attr, "identity_attributes"); @@ -58,17 +63,136 @@ anastasis_gtk_print_details_button_clicked_cb (GtkButton *button, "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[] = { + TALER_JSON_spec_i18n_str ("label", + &tkey), + GNUNET_JSON_spec_end () + }; - json_dumpf (attrs, - stderr, - JSON_INDENT (2)); - /* FIXME: convert keys in 'attrs' to - more nice, human-readable keys! */ - ret = AG_print (attrs, - "test.pdf"); // FIXME: let user specify the filename! - json_decref (attr); + if (GNUNET_OK != + GNUNET_JSON_parse (pos, + spec, + NULL, NULL)) + { + GNUNET_break (0); + continue; + } + found = true; + GNUNET_break (0 == + json_object_set (uattrs, + 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) { - /* FIXME: report failure... */ + /* 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); } |