summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Crain <jason@inspiresomeone.us>2020-01-03 19:05:57 -0700
committerAlbert Astals Cid <aacid@kde.org>2020-08-21 12:16:40 +0200
commitb13cd962b59e65846e3a9b18eee8a8b999b0291a (patch)
treeff57072426184cc3c97a96e9bb0dc71e0e7e9918
parentc146da765689968ceb09921928152d5ecd5b0956 (diff)
glib: Deprecate PopplerDocument date properties
PopplerDocument's creation-date and mod-date properties are 32-bit unix times, a.k.a. GTime, and will overflow in 2038. Deprecate these properties and replace with creation-datetime and mod-datetime, which are GDateTime instead, and add accessor functions. Fixes #765
-rw-r--r--glib/demo/info.cc12
-rw-r--r--glib/poppler-document.cc161
-rw-r--r--glib/poppler-document.h8
-rw-r--r--glib/poppler-private.h1
-rw-r--r--glib/reference/poppler-sections.txt4
5 files changed, 178 insertions, 8 deletions
diff --git a/glib/demo/info.cc b/glib/demo/info.cc
index f29bf98e..c3d0d767 100644
--- a/glib/demo/info.cc
+++ b/glib/demo/info.cc
@@ -134,7 +134,7 @@ GtkWidget *pgd_info_create_widget(PopplerDocument *document)
gchar *perm_id;
gchar *up_id;
gboolean linearized;
- GTime creation_date, mod_date;
+ GDateTime *creation_date, *mod_date;
GEnumValue *enum_value;
PopplerBackend backend;
PopplerPageLayout layout;
@@ -143,8 +143,8 @@ GtkWidget *pgd_info_create_widget(PopplerDocument *document)
PopplerViewerPreferences view_prefs;
gint row = 0;
- g_object_get(document, "title", &title, "format", &format, "author", &author, "subject", &subject, "keywords", &keywords, "creation-date", &creation_date, "mod-date", &mod_date, "creator", &creator, "producer", &producer, "linearized",
- &linearized, "page-mode", &mode, "page-layout", &layout, "permissions", &permissions, "viewer-preferences", &view_prefs, "metadata", &metadata, NULL);
+ g_object_get(document, "title", &title, "format", &format, "author", &author, "subject", &subject, "keywords", &keywords, "creation-datetime", &creation_date, "mod-datetime", &mod_date, "creator", &creator, "producer", &producer,
+ "linearized", &linearized, "page-mode", &mode, "page-layout", &layout, "permissions", &permissions, "viewer-preferences", &view_prefs, "metadata", &metadata, NULL);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
@@ -200,12 +200,14 @@ GtkWidget *pgd_info_create_widget(PopplerDocument *document)
pgd_table_add_property(GTK_GRID(table), "<b>Linearized:</b>", linearized ? "Yes" : "No", &row);
- str = pgd_format_date(creation_date);
+ str = g_date_time_format(creation_date, "%c");
pgd_table_add_property(GTK_GRID(table), "<b>Creation Date:</b>", str, &row);
+ g_clear_pointer(&creation_date, g_date_time_unref);
g_free(str);
- str = pgd_format_date(mod_date);
+ str = g_date_time_format(mod_date, "%c");
pgd_table_add_property(GTK_GRID(table), "<b>Modification Date:</b>", str, &row);
+ g_clear_pointer(&mod_date, g_date_time_unref);
g_free(str);
enum_value = g_enum_get_value((GEnumClass *)g_type_class_peek(POPPLER_TYPE_PAGE_MODE), mode);
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 85326240..e91fe342 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -87,7 +87,9 @@ enum
PROP_METADATA,
PROP_PRINT_SCALING,
PROP_PRINT_DUPLEX,
- PROP_PRINT_N_COPIES
+ PROP_PRINT_N_COPIES,
+ PROP_CREATION_DATETIME,
+ PROP_MOD_DATETIME
};
static void poppler_document_layers_free(PopplerDocument *document);
@@ -1430,6 +1432,50 @@ void poppler_document_set_creation_date(PopplerDocument *document, time_t creati
}
/**
+ * poppler_document_get_creation_date_time:
+ * @document: A #PopplerDocument
+ *
+ * Returns the date the document was created as a #GDateTime
+ *
+ * Returns: (nullable): the date the document was created, or %NULL
+ *
+ * Since: 20.09.0
+ **/
+GDateTime *poppler_document_get_creation_date_time(PopplerDocument *document)
+{
+ g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), nullptr);
+
+ GooString *str = document->doc->getDocInfoCreatDate();
+
+ if (!str)
+ return nullptr;
+
+ return _poppler_convert_pdf_date_to_date_time(str);
+}
+
+/**
+ * poppler_document_set_creation_date_time:
+ * @document: A #PopplerDocument
+ * @creation_datetime: (nullable): A new creation #GDateTime
+ *
+ * Sets the document's creation date. If @creation_datetime is %NULL,
+ * CreationDate entry is removed from the document's Info dictionary.
+ *
+ * Since: 20.09.0
+ **/
+void poppler_document_set_creation_date_time(PopplerDocument *document, GDateTime *creation_datetime)
+{
+ g_return_if_fail(POPPLER_IS_DOCUMENT(document));
+
+ GooString *str = nullptr;
+
+ if (creation_datetime)
+ str = _poppler_convert_date_time_to_pdf_date(creation_datetime);
+
+ document->doc->setDocInfoCreatDate(str);
+}
+
+/**
* poppler_document_get_modification_date:
* @document: A #PopplerDocument
*
@@ -1474,6 +1520,50 @@ void poppler_document_set_modification_date(PopplerDocument *document, time_t mo
}
/**
+ * poppler_document_get_modification_date_time:
+ * @document: A #PopplerDocument
+ *
+ * Returns the date the document was most recently modified as a #GDateTime
+ *
+ * Returns: (nullable): the date the document was modified, or %NULL
+ *
+ * Since: 20.09.0
+ **/
+GDateTime *poppler_document_get_modification_date_time(PopplerDocument *document)
+{
+ g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), nullptr);
+
+ GooString *str = document->doc->getDocInfoModDate();
+
+ if (!str)
+ return nullptr;
+
+ return _poppler_convert_pdf_date_to_date_time(str);
+}
+
+/**
+ * poppler_document_set_modification_date_time:
+ * @document: A #PopplerDocument
+ * @modification_datetime: (nullable): A new modification #GDateTime
+ *
+ * Sets the document's modification date. If @modification_datetime is %NULL,
+ * ModDate entry is removed from the document's Info dictionary.
+ *
+ * Since: 20.09.0
+ **/
+void poppler_document_set_modification_date_time(PopplerDocument *document, GDateTime *modification_datetime)
+{
+ g_return_if_fail(POPPLER_IS_DOCUMENT(document));
+
+ GooString *str = nullptr;
+
+ if (modification_datetime)
+ str = _poppler_convert_date_time_to_pdf_date(modification_datetime);
+
+ document->doc->setDocInfoModDate(str);
+}
+
+/**
* poppler_document_is_linearized:
* @document: A #PopplerDocument
*
@@ -1949,9 +2039,15 @@ static void poppler_document_get_property(GObject *object, guint prop_id, GValue
case PROP_CREATION_DATE:
g_value_set_int(value, poppler_document_get_creation_date(document));
break;
+ case PROP_CREATION_DATETIME:
+ g_value_take_boxed(value, poppler_document_get_creation_date_time(document));
+ break;
case PROP_MOD_DATE:
g_value_set_int(value, poppler_document_get_modification_date(document));
break;
+ case PROP_MOD_DATETIME:
+ g_value_take_boxed(value, poppler_document_get_modification_date_time(document));
+ break;
case PROP_LINEARIZED:
g_value_set_boolean(value, poppler_document_is_linearized(document));
break;
@@ -2023,9 +2119,15 @@ static void poppler_document_set_property(GObject *object, guint prop_id, const
case PROP_CREATION_DATE:
poppler_document_set_creation_date(document, g_value_get_int(value));
break;
+ case PROP_CREATION_DATETIME:
+ poppler_document_set_creation_date_time(document, (GDateTime *)g_value_get_boxed(value));
+ break;
case PROP_MOD_DATE:
poppler_document_set_modification_date(document, g_value_get_int(value));
break;
+ case PROP_MOD_DATETIME:
+ poppler_document_set_modification_date_time(document, (GDateTime *)g_value_get_boxed(value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
@@ -2106,15 +2208,39 @@ static void poppler_document_class_init(PopplerDocumentClass *klass)
* PopplerDocument:creation-date:
*
* The date the document was created as seconds since the Epoch, or -1
+ *
+ * Deprecated: 20.09.0: This will overflow in 2038. Use creation-datetime
+ * instead.
+ */
+ g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_CREATION_DATE,
+ g_param_spec_int("creation-date", "Creation Date", "The date and time the document was created", -1, G_MAXINT, -1, (GParamFlags)(G_PARAM_READWRITE | G_PARAM_DEPRECATED)));
+
+ /**
+ * PopplerDocument:creation-datetime:
+ * The #GDateTime the document was created.
+ *
+ * Since: 20.09.0
*/
- g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_CREATION_DATE, g_param_spec_int("creation-date", "Creation Date", "The date and time the document was created", -1, G_MAXINT, -1, G_PARAM_READWRITE));
+ g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_CREATION_DATETIME, g_param_spec_boxed("creation-datetime", "Creation DateTime", "The date and time the document was created", G_TYPE_DATE_TIME, G_PARAM_READWRITE));
/**
* PopplerDocument:mod-date:
*
* The date the document was most recently modified as seconds since the Epoch, or -1
+ *
+ * Deprecated: 20.09.0: This will overflow in 2038. Use mod-datetime instead.
*/
- g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_MOD_DATE, g_param_spec_int("mod-date", "Modification Date", "The date and time the document was modified", -1, G_MAXINT, -1, G_PARAM_READWRITE));
+ g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_MOD_DATE,
+ g_param_spec_int("mod-date", "Modification Date", "The date and time the document was modified", -1, G_MAXINT, -1, (GParamFlags)(G_PARAM_READWRITE | G_PARAM_DEPRECATED)));
+
+ /**
+ * PopplerDocument:mod-datetime:
+ *
+ * The #GDateTime the document was most recently modified.
+ *
+ * Since: 20.09.0
+ */
+ g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_MOD_DATETIME, g_param_spec_boxed("mod-datetime", "Modification DateTime", "The date and time the document was modified", G_TYPE_DATE_TIME, G_PARAM_READWRITE));
/**
* PopplerDocument:linearized:
@@ -3345,3 +3471,32 @@ GDateTime *_poppler_convert_pdf_date_to_date_time(const GooString *date)
return date_time;
}
+
+/**
+ * _poppler_convert_date_time_to_pdf_date:
+ * @datetime: a #GDateTime
+ *
+ * Converts a #GDateTime to a PDF date.
+ *
+ * Returns: The converted date
+ **/
+GooString *_poppler_convert_date_time_to_pdf_date(GDateTime *datetime)
+{
+ int offset_min;
+ gchar *date_str;
+ GooString *out_str;
+
+ offset_min = g_date_time_get_utc_offset(datetime) / 1000000 / 60;
+ date_str = g_date_time_format(datetime, "D:%Y%m%d%H%M%S");
+
+ if (offset_min == 0) {
+ out_str = GooString::format("{0:s}Z", date_str);
+ } else {
+ char tz = offset_min > 0 ? '+' : '-';
+
+ out_str = GooString::format("{0:s}{1:c}{2:02d}'{3:02d}'", date_str, tz, offset_min / 60, offset_min % 60);
+ }
+
+ g_free(date_str);
+ return out_str;
+}
diff --git a/glib/poppler-document.h b/glib/poppler-document.h
index fd5acac9..5d19b52c 100644
--- a/glib/poppler-document.h
+++ b/glib/poppler-document.h
@@ -345,10 +345,18 @@ time_t poppler_document_get_creation_date(PopplerDocument *document);
POPPLER_PUBLIC
void poppler_document_set_creation_date(PopplerDocument *document, time_t creation_date);
POPPLER_PUBLIC
+GDateTime *poppler_document_get_creation_date_time(PopplerDocument *document);
+POPPLER_PUBLIC
+void poppler_document_set_creation_date_time(PopplerDocument *document, GDateTime *creation_datetime);
+POPPLER_PUBLIC
time_t poppler_document_get_modification_date(PopplerDocument *document);
POPPLER_PUBLIC
void poppler_document_set_modification_date(PopplerDocument *document, time_t modification_date);
POPPLER_PUBLIC
+GDateTime *poppler_document_get_modification_date_time(PopplerDocument *document);
+POPPLER_PUBLIC
+void poppler_document_set_modification_date_time(PopplerDocument *document, GDateTime *modification_datetime);
+POPPLER_PUBLIC
gboolean poppler_document_is_linearized(PopplerDocument *document);
POPPLER_PUBLIC
PopplerPageLayout poppler_document_get_page_layout(PopplerDocument *document);
diff --git a/glib/poppler-private.h b/glib/poppler-private.h
index 5495def6..d41b59c6 100644
--- a/glib/poppler-private.h
+++ b/glib/poppler-private.h
@@ -141,6 +141,7 @@ const PDFRectangle *_poppler_annot_get_cropbox(PopplerAnnot *poppler_annot);
char *_poppler_goo_string_to_utf8(const GooString *s);
gboolean _poppler_convert_pdf_date_to_gtime(const GooString *date, time_t *gdate);
GDateTime *_poppler_convert_pdf_date_to_date_time(const GooString *date);
+GooString *_poppler_convert_date_time_to_pdf_date(GDateTime *datetime);
/*
* A convenience macro for boxed type implementations, which defines a
diff --git a/glib/reference/poppler-sections.txt b/glib/reference/poppler-sections.txt
index dc2be923..900b1f9f 100644
--- a/glib/reference/poppler-sections.txt
+++ b/glib/reference/poppler-sections.txt
@@ -160,12 +160,14 @@ poppler_document_find_dest
poppler_document_get_attachments
poppler_document_get_author
poppler_document_get_creation_date
+poppler_document_get_creation_date_time
poppler_document_get_creator
poppler_document_get_form_field
poppler_document_get_id
poppler_document_get_keywords
poppler_document_get_metadata
poppler_document_get_modification_date
+poppler_document_get_modification_date_time
poppler_document_get_n_attachments
poppler_document_get_n_pages
poppler_document_get_page
@@ -199,9 +201,11 @@ poppler_document_save
poppler_document_save_a_copy
poppler_document_set_author
poppler_document_set_creation_date
+poppler_document_set_creation_date_time
poppler_document_set_creator
poppler_document_set_keywords
poppler_document_set_modification_date
+poppler_document_set_modification_date_time
poppler_document_set_producer
poppler_document_set_subject
poppler_document_set_title