diff options
Diffstat (limited to 'src/anastasis')
-rw-r--r-- | src/anastasis/.gitignore | 1 | ||||
-rw-r--r-- | src/anastasis/Makefile.am | 19 | ||||
-rw-r--r-- | src/anastasis/anastasis-gtk.h | 4 | ||||
-rw-r--r-- | src/anastasis/anastasis-gtk_handle-challenge-code.c | 2 | ||||
-rw-r--r-- | src/anastasis/anastasis-gtk_handle-main-window-forward-clicked.c | 5 | ||||
-rw-r--r-- | src/anastasis/anastasis-gtk_handle-print.c | 29 | ||||
-rw-r--r-- | src/anastasis/print.c | 158 | ||||
-rw-r--r-- | src/anastasis/print.h | 45 | ||||
-rw-r--r-- | src/anastasis/test-print.c | 45 |
9 files changed, 301 insertions, 7 deletions
diff --git a/src/anastasis/.gitignore b/src/anastasis/.gitignore index e4b8b25..18dd486 100644 --- a/src/anastasis/.gitignore +++ b/src/anastasis/.gitignore @@ -1 +1,2 @@ anastasis-gtk +test-print diff --git a/src/anastasis/Makefile.am b/src/anastasis/Makefile.am index 110682c..cf0bbcf 100644 --- a/src/anastasis/Makefile.am +++ b/src/anastasis/Makefile.am @@ -62,7 +62,8 @@ anastasis_gtk_SOURCES = \ if HPDF_ENABLED anastasis_gtk_SOURCES += \ - anastasis-gtk_handle-print.c + anastasis-gtk_handle-print.c \ + print.c else anastasis_gtk_SOURCES += \ anastasis-gtk_handle-noprint.c @@ -97,3 +98,19 @@ anastasis_gtk_LDADD += \ anastasis_gtk_CFLAGS += \ $(HPDF_CFLAGS) endif + + +if HPDF_ENABLED +noinst_PROGRAMS = \ + test-print + +test_print_SOURCES = \ + test-print.c \ + print.c +test_print_LDADD = \ + $(HPDF_LIBS) \ + -lgnunetutil \ + -ljansson +test_print_CFLAGS = \ + $(HPDF_CFLAGS) +endif diff --git a/src/anastasis/anastasis-gtk.h b/src/anastasis/anastasis-gtk.h index 27d23f3..5151383 100644 --- a/src/anastasis/anastasis-gtk.h +++ b/src/anastasis/anastasis-gtk.h @@ -19,8 +19,8 @@ */ /** - * @file src/include/anastasis-gtk_helper.h - * @brief Definition of helpers. + * @file src/anastasis/anastasis-gtk.h + * @brief Definitions of the main Gtk GUI. * @author Christian Grothoff * @author Dennis Neufeld */ diff --git a/src/anastasis/anastasis-gtk_handle-challenge-code.c b/src/anastasis/anastasis-gtk_handle-challenge-code.c index 4f42023..6aeeb6f 100644 --- a/src/anastasis/anastasis-gtk_handle-challenge-code.c +++ b/src/anastasis/anastasis-gtk_handle-challenge-code.c @@ -48,8 +48,6 @@ anastasis_gtk_c_code_dialog_response_cb (GtkDialog *dialog, const char *qs; json_t *args; unsigned long long pin; - char dummy; - char s[16]; if (GTK_RESPONSE_OK != response_id) { diff --git a/src/anastasis/anastasis-gtk_handle-main-window-forward-clicked.c b/src/anastasis/anastasis-gtk_handle-main-window-forward-clicked.c index 0e3c238..8ce8e3b 100644 --- a/src/anastasis/anastasis-gtk_handle-main-window-forward-clicked.c +++ b/src/anastasis/anastasis-gtk_handle-main-window-forward-clicked.c @@ -23,6 +23,9 @@ * @author Christian Grothoff * @author Dennis Neufeld */ +#include "anastasis_gtk_config.h" +#define HAVE_USED_CONFIG_H 1 + #include <gnunet/platform.h> #include <gnunet/gnunet_util_lib.h> #include "anastasis-gtk_action.h" @@ -200,7 +203,7 @@ question_certainty (void) GtkWidget *toplevel; GtkWidget *widget; -#if ! HPDF_ENABLED +#if ! HAVE_HPDF_H gtk_widget_hide ( GTK_WIDGET ( gtk_builder_get_object ( diff --git a/src/anastasis/anastasis-gtk_handle-print.c b/src/anastasis/anastasis-gtk_handle-print.c index 5b72c39..e4961d3 100644 --- a/src/anastasis/anastasis-gtk_handle-print.c +++ b/src/anastasis/anastasis-gtk_handle-print.c @@ -25,10 +25,11 @@ */ #include <gnunet/platform.h> #include <gnunet/gnunet_util_lib.h> +#include "anastasis-gtk_attributes.h" #include "anastasis-gtk_helper.h" #include <jansson.h> #include <gdk-pixbuf/gdk-pixbuf.h> - +#include "print.h" /** * The user clicked the 'print' button to print their personal @@ -41,7 +42,33 @@ void anastasis_gtk_print_details_button_clicked_cb (GtkButton *button, gpointer user_data) { + json_t *attr; + json_t *attrs; + int ret; + (void) button; (void) user_data; GNUNET_break (0); + attr = AG_collect_attributes (false); + attrs = json_object_get (attr, + "identity_attributes"); + /* + Example for ATTRS is now: + "full_name": "Max", + "birthdate": "1980-09-22", + "sq_number": "4" + */ + + 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 (0 != ret) + { + /* FIXME: report failure... */ + } } diff --git a/src/anastasis/print.c b/src/anastasis/print.c new file mode 100644 index 0000000..6b4a4c5 --- /dev/null +++ b/src/anastasis/print.c @@ -0,0 +1,158 @@ +/* + This file is part of anastasis-gtk. + Copyright (C) 2021 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + Anastasis is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Anastasis; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +/** + * @file src/anastasis/print.c + * @brief Printing helper. + * @author Christian Grothoff + */ + +#include <gnunet/platform.h> +#include <setjmp.h> +#include <hpdf.h> +#include <gnunet/gnunet_util_lib.h> +#include "print.h" + +static jmp_buf env; + + +static void +error_handler (HPDF_STATUS error_no, + HPDF_STATUS detail_no, + void *user_data) +{ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "error_no=%04X, detail_no=%u\n", + (HPDF_UINT) error_no, + (HPDF_UINT) detail_no); + longjmp (env, + 1); +} + + +int +AG_print (json_t *user, + const char *fname) +{ + const char *page_title = "GNU Anastasis Identity Sheet"; + const char *type; + json_t *jvalue; + HPDF_Doc pdf; + HPDF_Font font; + HPDF_Page page; + float tw; + float fsize; + + pdf = HPDF_New (error_handler, NULL); + if (! pdf) + { + printf ("error: cannot create PdfDoc object\n"); + return 1; + } + + if (setjmp (env)) + { + HPDF_Free (pdf); + return 1; + } + + /* set compression mode */ + HPDF_SetCompressionMode (pdf, + HPDF_COMP_ALL); + /* create default-font */ + font = HPDF_GetFont (pdf, + "Helvetica", + NULL); + /* add a new page object. */ + page = HPDF_AddPage (pdf); + + /* print the lines of the page. */ + HPDF_Page_SetLineWidth (page, 1); + HPDF_Page_Rectangle (page, + 50, 50, + HPDF_Page_GetWidth (page) - 100, + HPDF_Page_GetHeight (page) - 110); + HPDF_Page_Stroke (page); + + /* print the title of the page (with positioning center). */ + HPDF_Page_SetFontAndSize (page, + font, + 24); + tw = HPDF_Page_TextWidth (page, + page_title); + HPDF_Page_BeginText (page); + HPDF_Page_TextOut (page, + (HPDF_Page_GetWidth (page) - tw) / 2, + HPDF_Page_GetHeight (page) - 50, + page_title); + HPDF_Page_EndText (page); + + HPDF_Page_BeginText (page); + HPDF_Page_MoveTextPos (page, 60, HPDF_Page_GetHeight (page) - 60); + + /* + * font size + */ + fsize = 18; + json_object_foreach (user, type, jvalue) + { + char *val; + int len; + + /* print the description. */ + HPDF_Page_MoveTextPos (page, 0, -12); + HPDF_Page_SetFontAndSize (page, font, 10); + HPDF_Page_ShowText (page, type); + + /* set the position of the text. */ + HPDF_Page_MoveTextPos (page, 10, -5 - fsize); + val = GNUNET_strdup (json_string_value (jvalue)); + do { + /* set style and size of font. */ + HPDF_Page_SetFontAndSize (page, font, fsize); + /* measure the number of characters which included in the page. */ + len = HPDF_Page_MeasureText (page, + val, + HPDF_Page_GetWidth (page) - 130, + HPDF_FALSE, + NULL); + if ( (len < strlen (val)) && + (fsize > 10) ) + fsize--; + else + break; + } while (1); + /* truncate the text. */ + val[len] = 0x00; + HPDF_Page_ShowText (page, val); + GNUNET_free (val); + HPDF_Page_MoveTextPos (page, -10, -25 - fsize); + } + + HPDF_Page_EndText (page); + + /* save the document to a file */ + HPDF_SaveToFile (pdf, fname); + + /* clean up */ + HPDF_Free (pdf); + + return 0; +} diff --git a/src/anastasis/print.h b/src/anastasis/print.h new file mode 100644 index 0000000..142f9f5 --- /dev/null +++ b/src/anastasis/print.h @@ -0,0 +1,45 @@ +/* + This file is part of anastasis-gtk. + Copyright (C) 2021 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + Anastasis is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Anastasis; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +/** + * @file src/anastasis/print.h + * @brief Definition of printing helper. + * @author Christian Grothoff + */ + +#ifndef PRINT_H +#define PRINT_H + +#include <jansson.h> + + +/** + * Generate a PDF file with the key-value strings + * from @a user and write it to @a fname. + * + * @param user user attribute data to output + * @param fname filename to write the PDF to (will overwrite) + * @return 0 on success + */ +int +AG_print (json_t *user, + const char *fname); + +#endif diff --git a/src/anastasis/test-print.c b/src/anastasis/test-print.c new file mode 100644 index 0000000..c27c5ce --- /dev/null +++ b/src/anastasis/test-print.c @@ -0,0 +1,45 @@ +/* + This file is part of anastasis-gtk. + Copyright (C) 2021 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + Anastasis is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Anastasis; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +/** + * @file src/anastasis/test-print.c + * @brief Program to test printing helper. + * @author Christian Grothoff + */ +#include "print.h" + + +int +main (int argc, + char **argv) +{ + json_t *user; + int ret; + + user = json_pack ("{s:s,s:s,s:s}", + "key1", "value1", + "key2", + "superloooooooooooooooooooooooooooooooooooooooooooooooooongvaaaaluevalue2", + "key3", "value3"); + ret = AG_print (user, + "test-print.pdf"); + json_decref (user); + return ret; +} |