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