[evince/wip/gpoo/fix-warnings: 1/5] Use GDateTime for handling document properties




commit 6d95afa372a732d8212145e7469462161fece3f2
Author: Germán Poo-Caamaño <gpoo gnome org>
Date:   Sat Oct 16 14:57:25 2021 -0300

    Use GDateTime for handling document properties
    
    * GTime is deprecated as it not 2038-year safe
    * Add deprecation information to no break the API (yet)

 backend/pdf/ev-poppler.c        | 92 +++++++++++++++++------------------------
 libdocument/ev-document-info.h  |  4 +-
 libdocument/ev-document.c       |  6 ++-
 properties/ev-properties-view.c |  8 ++--
 4 files changed, 49 insertions(+), 61 deletions(-)
---
diff --git a/backend/pdf/ev-poppler.c b/backend/pdf/ev-poppler.c
index 351d57bc..a53ff31d 100644
--- a/backend/pdf/ev-poppler.c
+++ b/backend/pdf/ev-poppler.c
@@ -839,50 +839,6 @@ pdf_document_get_subject_from_metadata (xmlXPathContextPtr xpathCtx)
        return pdf_document_get_localized_object_from_metadata (xpathCtx, SUBJECT);
 }
 
-static void
-pdf_document_get_dates_from_metadata (GTime *result, xmlXPathContextPtr xpathCtx)
-{
-       xmlChar *modifydate = NULL;
-       xmlChar *createdate = NULL;
-       xmlChar *metadate = NULL;
-       char    *datestr;
-       GTimeVal  tmp_time;
-
-       /* reads modify date */
-       modifydate = pdf_document_get_xmptag_from_path (xpathCtx, MOD_DATE);
-       /* reads pdf create date */
-       createdate = pdf_document_get_xmptag_from_path (xpathCtx, CREATE_DATE);
-       /* reads pdf metadata date */
-       metadate = pdf_document_get_xmptag_from_path (xpathCtx, META_DATE);
-
-       if (modifydate != NULL) {
-               /* return buffer */
-               datestr = g_strdup_printf ("%s", modifydate);
-               g_time_val_from_iso8601 (datestr, &tmp_time);
-               result[0] = (GTime)tmp_time.tv_sec;
-               g_free (datestr);
-       }
-
-       if (createdate != NULL) {
-               datestr = g_strdup_printf ("%s", createdate);
-               g_time_val_from_iso8601 (datestr, &tmp_time);
-               result[1] = (GTime)tmp_time.tv_sec;
-               g_free (datestr);
-       }
-
-       if (metadate != NULL) {
-               datestr = g_strdup_printf ("%s", metadate);
-               g_time_val_from_iso8601 (datestr, &tmp_time);
-               result[2] = (GTime)tmp_time.tv_sec;
-               g_free (datestr);
-       }
-
-       /* Cleanup */
-       xmlFree (modifydate);
-       xmlFree (createdate);
-       xmlFree (metadate);
-}
-
 static EvDocumentLicense *
 pdf_document_get_license_from_metadata (xmlXPathContextPtr xpathCtx)
 {
@@ -951,7 +907,11 @@ pdf_document_parse_metadata (const gchar    *metadata,
        gchar             *subject;
        gchar             *creatortool;
        gchar             *producer;
-       GTime              dates[3] = {0};
+       gchar             *datestr;
+       gchar             *modified_date;
+       gchar             *creation_date;
+       gchar             *metadata_date;
+       GDateTime         *dt = NULL;
 
        doc = xmlParseMemory (metadata, strlen (metadata));
        if (doc == NULL)
@@ -963,12 +923,21 @@ pdf_document_parse_metadata (const gchar    *metadata,
                return;         /* invalid xpath context */
        }
 
-       pdf_document_get_dates_from_metadata (dates, xpathCtx);
+       /* reads pdf metadata date */
+       metadata_date = (gchar *)pdf_document_get_xmptag_from_path (xpathCtx, META_DATE);
+       if (metadata_date != NULL) {
+               datestr = g_strdup_printf ("%s", metadata_date);
+               dt = g_date_time_new_from_iso8601 (datestr, NULL);
+               g_free (datestr);
+               g_free (metadata_date);
+       }
+
        /* From PDF spec, if the PDF modified date is newer than metadata date,
         * it indicates that the file was edited by a non-XMP aware software.
         * Then, the information dictionary is considered authoritative and the
         * XMP metadata should not be displayed. */
-       if (dates[2] >= info->modified_date) {
+       if (info->modified_date == NULL || dt == NULL ||
+           ! (g_date_time_compare (dt, info->modified_date) == -1)) {
 
                fmt = pdf_document_get_format_from_metadata (xpathCtx);
                if (fmt != NULL) {
@@ -1012,15 +981,32 @@ pdf_document_parse_metadata (const gchar    *metadata,
                        info->producer = producer;
                }
 
-               if (dates[0] != 0)
-                       info->modified_date = dates[0];
+               /* reads modify date */
+               modified_date = (gchar *)pdf_document_get_xmptag_from_path (xpathCtx, MOD_DATE);
+               if (modified_date != NULL) {
+                       g_clear_pointer (&info->modified_date, g_date_time_unref);
+
+                       datestr = g_strdup_printf ("%s", modified_date);
+                       info->modified_date = g_date_time_new_from_iso8601 (datestr, NULL);
+                       g_free (datestr);
+                       g_free (modified_date);
+               }
+
+               /* reads pdf create date */
+               creation_date = (gchar *)pdf_document_get_xmptag_from_path (xpathCtx, CREATE_DATE);
+               if (creation_date != NULL) {
+                       g_clear_pointer (&info->creation_date, g_date_time_unref);
 
-               if (dates[1] != 0)
-                       info->creation_date = dates[1];
+                       datestr = g_strdup_printf ("%s", creation_date);
+                       info->creation_date = g_date_time_new_from_iso8601 (datestr, NULL);
+                       g_free (datestr);
+                       g_free (creation_date);
+               }
        }
 
        info->license = pdf_document_get_license_from_metadata (xpathCtx);
 
+       g_clear_pointer (&dt, g_date_time_unref);
        xmlXPathFreeContext (xpathCtx);
        xmlFreeDoc (doc);
 }
@@ -1071,8 +1057,8 @@ pdf_document_get_info (EvDocument *document)
                      "permissions", &permissions,
                      "creator", &(info->creator),
                      "producer", &(info->producer),
-                     "creation-date", &(info->creation_date),
-                     "mod-date", &(info->modified_date),
+                     "creation-datetime", &(info->creation_date),
+                     "mod-datetime", &(info->modified_date),
                      "linearized", &linearized,
                      "metadata", &metadata,
                      NULL);
diff --git a/libdocument/ev-document-info.h b/libdocument/ev-document-info.h
index ebeeea36..d05e48cc 100644
--- a/libdocument/ev-document-info.h
+++ b/libdocument/ev-document-info.h
@@ -124,8 +124,8 @@ struct _EvDocumentInfo
        char *producer;
        char *linearized;
         char *security;
-       GTime creation_date;
-       GTime modified_date;
+       GDateTime *creation_date;
+       GDateTime *modified_date;
        EvDocumentLayout layout;
        EvDocumentMode mode;
        guint ui_hints;
diff --git a/libdocument/ev-document.c b/libdocument/ev-document.c
index 1cd6af5c..3751f28a 100644
--- a/libdocument/ev-document.c
+++ b/libdocument/ev-document.c
@@ -1187,8 +1187,8 @@ ev_document_info_copy (EvDocumentInfo *info)
        copy->producer = g_strdup (info->producer);
        copy->linearized = g_strdup (info->linearized);
        
-       copy->creation_date = info->creation_date;
-       copy->modified_date = info->modified_date;
+       copy->creation_date = g_date_time_add (info->creation_date, 0);
+       copy->modified_date = g_date_time_add (info->modified_date, 0);
        copy->layout = info->layout;
        copy->mode = info->mode;
        copy->ui_hints = info->ui_hints;
@@ -1216,6 +1216,8 @@ ev_document_info_free (EvDocumentInfo *info)
        g_free (info->producer);
        g_free (info->linearized);
        g_free (info->security);
+       g_clear_pointer (&(info->creation_date), g_date_time_unref);
+       g_clear_pointer (&(info->modified_date), g_date_time_unref);
        ev_document_license_free (info->license);
 
        g_free (info);
diff --git a/properties/ev-properties-view.c b/properties/ev-properties-view.c
index 3e34e23b..e964af3c 100644
--- a/properties/ev-properties-view.c
+++ b/properties/ev-properties-view.c
@@ -392,19 +392,19 @@ ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo
                set_property (properties, GTK_GRID (grid), CREATOR_PROPERTY, info->creator, &row);
        }
        if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
-               if (info->creation_date == -1) {
+               if (info->creation_date == NULL) {
                        set_property (properties, GTK_GRID (grid), CREATION_DATE_PROPERTY, NULL, &row);
                } else {
-                       text = ev_document_misc_format_date (info->creation_date);
+                       text = ev_document_misc_format_datetime (info->creation_date);
                        set_property (properties, GTK_GRID (grid), CREATION_DATE_PROPERTY, text, &row);
                        g_free (text);
                }
        }
        if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
-               if (info->modified_date == -1) {
+               if (info->modified_date == NULL) {
                        set_property (properties, GTK_GRID (grid), MOD_DATE_PROPERTY, NULL, &row);
                } else {
-                       text = ev_document_misc_format_date (info->modified_date);
+                       text = ev_document_misc_format_datetime (info->modified_date);
                        set_property (properties, GTK_GRID (grid), MOD_DATE_PROPERTY, text, &row);
                        g_free (text);
                }


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