Commit c146da76 authored by Jason Crain's avatar Jason Crain Committed by Albert Astals Cid

glib: Deprecate PopplerAttachment GTime fields

GTime was deprecated in glib 2.62 and will overflow in 2038.

The PopplerAttachment struct publicly uses GTime for the 'mtime' and
'ctime' fields. Deprecate these two fields and add accessor functions,
poppler_attachment_get_ctime and poppler_attachment_get_mtime, which
retun GDateTime* instead.

Fixes #765, Related to #715
parent d7ead51f
......@@ -42,19 +42,22 @@ static void pgd_attachments_fill_model(GtkListStore *model, PopplerDocument *doc
PopplerAttachment *attachment = POPPLER_ATTACHMENT(l->data);
GtkTreeIter iter;
gchar *size;
gchar *ctime, *mtime;
GDateTime *ctime, *mtime;
gchar *ctime_str, *mtime_str;
size = g_strdup_printf("%" G_GSIZE_FORMAT, attachment->size);
ctime = pgd_format_date(attachment->ctime);
mtime = pgd_format_date(attachment->mtime);
ctime = poppler_attachment_get_ctime(attachment);
ctime_str = ctime ? g_date_time_format(ctime, "%c") : NULL;
mtime = poppler_attachment_get_mtime(attachment);
mtime_str = mtime ? g_date_time_format(mtime, "%c") : NULL;
gtk_list_store_append(model, &iter);
gtk_list_store_set(model, &iter, ATTACHMENTS_NAME_COLUMN, attachment->name ? attachment->name : "Unknown", ATTACHMENTS_DESCRIPTION_COLUMN, attachment->description ? attachment->description : "Unknown", ATTACHMENTS_SIZE_COLUMN,
size ? size : "Unknown", ATTACHMENTS_CTIME_COLUMN, ctime ? ctime : "Unknown", ATTACHMENTS_MTIME_COLUMN, mtime ? mtime : "Unknown", ATTACHMENTS_ATTACHMENT_COLUMN, attachment, -1);
size ? size : "Unknown", ATTACHMENTS_CTIME_COLUMN, ctime_str ? ctime_str : "Unknown", ATTACHMENTS_MTIME_COLUMN, mtime_str ? mtime_str : "Unknown", ATTACHMENTS_ATTACHMENT_COLUMN, attachment, -1);
g_free(size);
g_free(ctime);
g_free(mtime);
g_free(ctime_str);
g_free(mtime_str);
g_object_unref(attachment);
}
......
......@@ -38,6 +38,8 @@
struct PopplerAttachmentPrivate
{
Object obj_stream {};
GDateTime *mtime;
GDateTime *ctime;
};
static void poppler_attachment_finalize(GObject *obj);
......@@ -79,6 +81,9 @@ static void poppler_attachment_finalize(GObject *obj)
g_string_free(attachment->checksum, TRUE);
attachment->checksum = nullptr;
g_clear_pointer(&priv->mtime, g_date_time_unref);
g_clear_pointer(&priv->ctime, g_date_time_unref);
priv->~PopplerAttachmentPrivate();
G_OBJECT_CLASS(poppler_attachment_parent_class)->finalize(obj);
......@@ -107,14 +112,20 @@ PopplerAttachment *_poppler_attachment_new(FileSpec *emb_file)
attachment->size = embFile->size();
if (embFile->createDate()) {
time_t aux;
_poppler_convert_pdf_date_to_gtime(embFile->createDate(), &aux);
attachment->ctime = (GTime)aux; // FIXME This will overflow on dates from after 2038
priv->ctime = _poppler_convert_pdf_date_to_date_time(embFile->createDate());
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* This will overflow on dates from after 2038. This field is
* deprecated, only kept for backward compatibility. */
attachment->ctime = (GTime)g_date_time_to_unix(priv->ctime);
G_GNUC_END_IGNORE_DEPRECATIONS
}
if (embFile->modDate()) {
time_t aux;
_poppler_convert_pdf_date_to_gtime(embFile->modDate(), &aux);
attachment->mtime = (GTime)aux; // FIXME This will overflow on dates from after 2038
priv->mtime = _poppler_convert_pdf_date_to_date_time(embFile->modDate());
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* This will overflow on dates from after 2038. This field is
* deprecated, only kept for backward compatibility. */
attachment->mtime = (GTime)g_date_time_to_unix(priv->mtime);
G_GNUC_END_IGNORE_DEPRECATIONS
}
if (embFile->checksum() && embFile->checksum()->getLength() > 0)
......@@ -128,6 +139,35 @@ PopplerAttachment *_poppler_attachment_new(FileSpec *emb_file)
return attachment;
}
/**
* poppler_attachment_get_ctime:
* @attachment: a #PopplerAttachment
*
* Returns: (transfer none) (nullable): The attachment's creation date and time
* as a #GDateTime, or %NULL if the creation date and time is not available.
*
* Since: 20.09.0
*/
GDateTime *poppler_attachment_get_ctime(PopplerAttachment *attachment)
{
return GET_PRIVATE(attachment)->ctime;
}
/**
* poppler_attachment_get_mtime:
* @attachment: a #PopplerAttachment
*
* Returns: (transfer none) (nullable): The attachment's modification date and
* time as a #GDateTime, or %NULL if the modification date and time is not
* available.
*
* Since: 20.09.0
*/
GDateTime *poppler_attachment_get_mtime(PopplerAttachment *attachment)
{
return GET_PRIVATE(attachment)->mtime;
}
static gboolean save_helper(const gchar *buf, gsize count, gpointer data, GError **error)
{
FILE *f = (FILE *)data;
......
......@@ -50,11 +50,23 @@ G_BEGIN_DECLS
typedef gboolean (*PopplerAttachmentSaveFunc)(const gchar *buf, gsize count, gpointer data, GError **error);
/* GTime is deprecated, but is part of our ABI here (see #715, #765). */
/**
* PopplerAttachment:
* @name: The filename
* @description: Descriptive text
* @size: The size of the file
* @mtime: The date and time when the file was last modified. Deprecated in
* poppler 20.09.0. Use poppler_attachment_get_mtime() instead.
* @ctime: The date and time when the file was created. Deprecated in poppler
* 20.09.0. Use poppler_attachment_get_ctime() instead.
* @checksum: A 16-byte checksum of the file.
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
struct _PopplerAttachment
{
GObject parent;
/*< public >*/
gchar *name;
gchar *description;
gsize size;
......@@ -72,6 +84,10 @@ typedef struct _PopplerAttachmentClass
POPPLER_PUBLIC
GType poppler_attachment_get_type(void) G_GNUC_CONST;
POPPLER_PUBLIC
GDateTime *poppler_attachment_get_ctime(PopplerAttachment *attachment);
POPPLER_PUBLIC
GDateTime *poppler_attachment_get_mtime(PopplerAttachment *attachment);
POPPLER_PUBLIC
gboolean poppler_attachment_save(PopplerAttachment *attachment, const char *filename, GError **error);
POPPLER_PUBLIC
gboolean poppler_attachment_save_to_callback(PopplerAttachment *attachment, PopplerAttachmentSaveFunc save_func, gpointer user_data, GError **error);
......
......@@ -3309,3 +3309,39 @@ gboolean _poppler_convert_pdf_date_to_gtime(const GooString *date, time_t *gdate
return retval;
}
/**
* _poppler_convert_pdf_date_to_date_time:
* @date: a PDF date
*
* Converts the PDF date in @date to a #GDateTime.
*
* Returns: The converted date, or %NULL on error.
**/
GDateTime *_poppler_convert_pdf_date_to_date_time(const GooString *date)
{
GDateTime *date_time = nullptr;
GTimeZone *time_zone = nullptr;
int year, mon, day, hour, min, sec, tzHours, tzMins;
char tz;
if (parseDateString(date->c_str(), &year, &mon, &day, &hour, &min, &sec, &tz, &tzHours, &tzMins)) {
if (tz == '+' || tz == '-') {
gchar *identifier;
identifier = g_strdup_printf("%c%02u:%02u", tz, tzHours, tzMins);
time_zone = g_time_zone_new(identifier);
g_free(identifier);
} else if (tz == '\0' || tz == 'Z') {
time_zone = g_time_zone_new_utc();
} else {
g_warning("unexpected tz val '%c'", tz);
time_zone = g_time_zone_new_utc();
}
date_time = g_date_time_new(time_zone, year, mon, day, hour, min, sec);
g_time_zone_unref(time_zone);
}
return date_time;
}
......@@ -140,6 +140,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);
/*
* A convenience macro for boxed type implementations, which defines a
......
......@@ -330,6 +330,8 @@ poppler_action_movie_operation_get_type
<TITLE>PopplerAttachment</TITLE>
PopplerAttachment
PopplerAttachmentSaveFunc
poppler_attachment_get_ctime
poppler_attachment_get_mtime
poppler_attachment_save
poppler_attachment_save_to_callback
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment