From e3130275f64edd7e97a7f4561a7b867c25b7a79a Mon Sep 17 00:00:00 2001 From: Jakub Kucharski Date: Mon, 8 Feb 2016 21:03:03 +0100 Subject: [PATCH] glib: Added document property setters & simplified getters --- glib/poppler-document.cc | 326 ++++++++++++++++++++++++++++++++++------------- glib/poppler-document.h | 19 +++ glib/poppler-private.h | 4 +- 3 files changed, 259 insertions(+), 90 deletions(-) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 61d92e8..39de5a7 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -1,6 +1,8 @@ /* poppler-document.cc: glib wrapper for poppler * Copyright (C) 2005, Red Hat, Inc. * + * Copyright (C) 2016 Jakub Kucharski + * * This program 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 2, or (at your option) @@ -711,6 +713,10 @@ poppler_document_find_dest (PopplerDocument *document, char *_poppler_goo_string_to_utf8(GooString *s) { + if (s == NULL) { + return NULL; + } + char *result; if (s->hasUnicodeMarker()) { @@ -737,39 +743,27 @@ char *_poppler_goo_string_to_utf8(GooString *s) return result; } -static gchar * -info_dict_get_string (Dict *info_dict, const gchar *key) +static GooString * +_poppler_goo_string_from_utf8(const gchar *src) { - Object obj; - GooString *goo_value; - gchar *result; - - if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) { - obj.free (); + if (src == NULL) { return NULL; } - goo_value = obj.getString (); - result = _poppler_goo_string_to_utf8(goo_value); - obj.free (); + gsize outlen; - return result; -} + gchar *utf16 = g_convert (src, -1, "UTF-16BE", "UTF-8", NULL, &outlen, NULL); + GooString *result = new GooString (utf16, outlen); + g_free (utf16); -static time_t -info_dict_get_date (Dict *info_dict, const gchar *key) -{ - Object obj; - time_t result; + assert(result->getLength() != 0); - if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) { - obj.free (); - return (time_t)-1; + if (!result->hasUnicodeMarker()) { + result->insert(0, 0xff); + result->insert(0, 0xfe); } - if (!_poppler_convert_pdf_date_to_gtime (obj.getString (), &result)) - result = (time_t)-1; - obj.free (); + assert((*result).getLength() != 0); return result; } @@ -879,17 +873,31 @@ poppler_document_get_pdf_version (PopplerDocument *document, gchar * poppler_document_get_title (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Title"); - obj.free (); + GooString *goo_title = document->doc->getDocInfoTitle(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_title); + delete goo_title; - return retval; + return utf8; +} + +/** + * poppler_document_set_title: + * @document: A #PopplerDocument + * @title: A new title + * + * Sets the document's title. If @title is NULL or its length is 0, Title entry + * is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_title (PopplerDocument *document, const gchar *title) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_title = _poppler_goo_string_from_utf8(title); + document->doc->setDocInfoTitle(goo_title); } /** @@ -906,17 +914,31 @@ poppler_document_get_title (PopplerDocument *document) gchar * poppler_document_get_author (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Author"); - obj.free (); + GooString *goo_author = document->doc->getDocInfoAuthor(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_author); + delete goo_author; - return retval; + return utf8; +} + +/** + * poppler_document_set_author: + * @document: A #PopplerDocument + * @author: A new author + * + * Sets the document's author. If @author is NULL or its length is 0, Author + * entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_author (PopplerDocument *document, const gchar *author) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_author = _poppler_goo_string_from_utf8(author); + document->doc->setDocInfoAuthor(goo_author); } /** @@ -933,17 +955,31 @@ poppler_document_get_author (PopplerDocument *document) gchar * poppler_document_get_subject (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Subject"); - obj.free (); + GooString *goo_subject = document->doc->getDocInfoSubject(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_subject); + delete goo_subject; - return retval; + return utf8; +} + +/** + * poppler_document_set_subject: + * @document: A #PopplerDocument + * @subject: A new subject + * + * Sets the document's subject. If @subject is NULL or its length is 0, Subject + * entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_subject (PopplerDocument *document, const gchar *subject) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_subject = _poppler_goo_string_from_utf8(subject); + document->doc->setDocInfoSubject(goo_subject); } /** @@ -960,17 +996,31 @@ poppler_document_get_subject (PopplerDocument *document) gchar * poppler_document_get_keywords (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Keywords"); - obj.free (); + GooString *goo_keywords = document->doc->getDocInfoKeywords(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_keywords); + delete goo_keywords; - return retval; + return utf8; +} + +/** + * poppler_document_set_keywords: + * @document: A #PopplerDocument + * @keywords: New keywords + * + * Sets the document's keywords. If @keywords is NULL or its length is 0, + * Keywords entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_keywords (PopplerDocument *document, const gchar *keywords) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_keywords = _poppler_goo_string_from_utf8(keywords); + document->doc->setDocInfoKeywords(goo_keywords); } /** @@ -989,17 +1039,31 @@ poppler_document_get_keywords (PopplerDocument *document) gchar * poppler_document_get_creator (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Creator"); - obj.free (); + GooString *goo_creator = document->doc->getDocInfoCreator(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_creator); + delete goo_creator; - return retval; + return utf8; +} + +/** + * poppler_document_set_creator: + * @document: A #PopplerDocument + * @creator: A new creator + * + * Sets the document's creator. If @creator is NULL or its length is 0, Creator + * entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_creator (PopplerDocument *document, const gchar *creator) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_creator = _poppler_goo_string_from_utf8(creator); + document->doc->setDocInfoCreator(goo_creator); } /** @@ -1018,17 +1082,31 @@ poppler_document_get_creator (PopplerDocument *document) gchar * poppler_document_get_producer (PopplerDocument *document) { - Object obj; - gchar *retval = NULL; - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_string (obj.getDict(), "Producer"); - obj.free (); + GooString *goo_producer = document->doc->getDocInfoProducer(); + gchar *utf8 = _poppler_goo_string_to_utf8(goo_producer); + delete goo_producer; - return retval; + return utf8; +} + +/** + * poppler_document_set_producer: + * @document: A #PopplerDocument + * @producer: A new producer + * + * Sets the document's producer. If @producer is NULL or its length is 0, + * Producer entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_producer (PopplerDocument *document, const gchar *producer) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *goo_producer = _poppler_goo_string_from_utf8(producer); + document->doc->setDocInfoProducer(goo_producer); } /** @@ -1044,17 +1122,37 @@ poppler_document_get_producer (PopplerDocument *document) time_t poppler_document_get_creation_date (PopplerDocument *document) { - Object obj; - time_t retval = (time_t)-1; + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), -1); - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), (time_t)-1); + GooString *str = document->doc->getDocInfoCreatDate(); + if (str == NULL) { + return -1; + } - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_date (obj.getDict(), "CreationDate"); - obj.free (); + time_t date; + gboolean success = _poppler_convert_pdf_date_to_gtime (str, &date); + delete str; - return retval; + return (success) ? date : -1; +} + +/** + * poppler_document_set_creation_date: + * @document: A #PopplerDocument + * @creation_date: A new creation date + * + * Sets the document's creation date. If @creation_date is -1, CreationDate + * entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_creation_date (PopplerDocument *document, + time_t creation_date) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *str = _poppler_convert_gtime_to_pdf_date (creation_date); + document->doc->setDocInfoCreatDate (str); } /** @@ -1070,17 +1168,52 @@ poppler_document_get_creation_date (PopplerDocument *document) time_t poppler_document_get_modification_date (PopplerDocument *document) { - Object obj; - time_t retval = (time_t)-1; + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), -1); - g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), (time_t)-1); + GooString *str = document->doc->getDocInfoModDate(); + if (str == NULL) { + return -1; + } - document->doc->getDocInfo (&obj); - if (obj.isDict ()) - retval = info_dict_get_date (obj.getDict(), "ModDate"); - obj.free (); + time_t date; + gboolean success = _poppler_convert_pdf_date_to_gtime (str, &date); + delete str; - return retval; + return (success) ? date : -1; +} + +/** + * poppler_document_set_modification_date: + * @document: A #PopplerDocument + * @modification_date: A new modification date + * + * Sets the document's modification date. If @modification_date is -1, ModDate + * entry is removed from the document's Info dictionary. + * + * Since: 0.41 + **/ +void +poppler_document_set_modification_date (PopplerDocument *document, + time_t modification_date) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + GooString *str = _poppler_convert_gtime_to_pdf_date (modification_date); + document->doc->setDocInfoModDate (str); +} + +/** + * poppler_document_remove_doc_info: + * @document: A #PopplerDocument + * + * Removes the document's Info dictionary + * + * Since: 0.41 + **/ +void +poppler_document_remove_doc_info (PopplerDocument *document) +{ + g_return_if_fail (POPPLER_IS_DOCUMENT (document)); + document->doc->removeDocInfo(); } /** @@ -2698,6 +2831,23 @@ poppler_document_get_form_field (PopplerDocument *document, return NULL; } +GooString * +_poppler_convert_gtime_to_pdf_date (time_t gdate) { + if (gdate == -1) { + return NULL; + } + + struct tm tm; + gmtime_r(&gdate, &tm); + + char str[24]; + sprintf(str, "D:%04d%02d%02d%02d%02d%02d+00'00'", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + GooString *goo_str = _poppler_goo_string_from_utf8(str); + + return goo_str; +} + gboolean _poppler_convert_pdf_date_to_gtime (GooString *date, time_t *gdate) diff --git a/glib/poppler-document.h b/glib/poppler-document.h index a34e88c..ec35c2b 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -1,6 +1,8 @@ /* poppler-document.h: glib interface to poppler * Copyright (C) 2004, Red Hat, Inc. * + * Copyright (C) 2016 Jakub Kucharski + * * This program 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 2, or (at your option) @@ -201,13 +203,30 @@ void poppler_document_get_pdf_version (PopplerDocument *doc guint *major_version, guint *minor_version); gchar *poppler_document_get_title (PopplerDocument *document); +void poppler_document_set_title (PopplerDocument *document, + const gchar *title); gchar *poppler_document_get_author (PopplerDocument *document); +void poppler_document_set_author (PopplerDocument *document, + const gchar *author); gchar *poppler_document_get_subject (PopplerDocument *document); +void poppler_document_set_subject (PopplerDocument *document, + const gchar *subject); gchar *poppler_document_get_keywords (PopplerDocument *document); +void poppler_document_set_keywords (PopplerDocument *document, + const gchar *keywords); gchar *poppler_document_get_creator (PopplerDocument *document); +void poppler_document_set_creator (PopplerDocument *document, + const gchar *creator); gchar *poppler_document_get_producer (PopplerDocument *document); +void poppler_document_set_producer (PopplerDocument *document, + const gchar *producer); time_t poppler_document_get_creation_date (PopplerDocument *document); +void poppler_document_set_creation_date (PopplerDocument *document, + time_t creation_date); time_t poppler_document_get_modification_date (PopplerDocument *document); +void poppler_document_set_modification_date (PopplerDocument *document, + time_t modification_date); +void poppler_document_remove_doc_info (PopplerDocument *document); gboolean poppler_document_is_linearized (PopplerDocument *document); PopplerPageLayout poppler_document_get_page_layout (PopplerDocument *document); PopplerPageMode poppler_document_get_page_mode (PopplerDocument *document); diff --git a/glib/poppler-private.h b/glib/poppler-private.h index 9abdd7c..7a3ce71 100644 --- a/glib/poppler-private.h +++ b/glib/poppler-private.h @@ -136,8 +136,8 @@ PopplerAnnot *_poppler_annot_circle_new (Annot *annot); PopplerAnnot *_poppler_annot_square_new (Annot *annot); char *_poppler_goo_string_to_utf8(GooString *s); -gboolean _poppler_convert_pdf_date_to_gtime (GooString *date, - time_t *gdate); +GooString *_poppler_convert_gtime_to_pdf_date (time_t gdate); +gboolean _poppler_convert_pdf_date_to_gtime (GooString *date, time_t *gdate); /* * A convenience macro for boxed type implementations, which defines a -- 2.7.1