gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis-gtk] branch master updated: finish 'print' implementati


From: gnunet
Subject: [taler-anastasis-gtk] branch master updated: finish 'print' implementation (#7090)
Date: Thu, 30 Dec 2021 20:46:52 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis-gtk.

The following commit(s) were added to refs/heads/master by this push:
     new 97e25d1  finish 'print' implementation (#7090)
97e25d1 is described below

commit 97e25d16f6aab66e9e84a06a90e0ffc022bf0d78
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Thu Dec 30 20:46:49 2021 +0100

    finish 'print' implementation (#7090)
---
 src/anastasis/anastasis-gtk_handle-print.c | 162 +++++++++++++++++++++++++----
 1 file 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);
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]