Commit 02ddc685 authored by Paolo Bacchilega's avatar Paolo Bacchilega
Browse files

use strftime to get the formatted date

parent eac37117
......@@ -88,7 +88,7 @@ gth_edit_comment_page_real_set_file (GthEditMetadataPage *base,
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::DateTime");
if (metadata != NULL) {
gtk_combo_box_set_active (GTK_COMBO_BOX (self->priv->date_combobox), FOLLOWING_DATE);
gth_time_selector_set_exif_date (GTH_TIME_SELECTOR (self->priv->date_selector), gth_metadata_get_formatted (metadata));
gth_time_selector_set_exif_date (GTH_TIME_SELECTOR (self->priv->date_selector), gth_metadata_get_raw (metadata));
/*gtk_widget_set_sensitive (self->priv->date_selector, TRUE);*/
}
else {
......@@ -162,11 +162,11 @@ gth_edit_comment_page_real_update_info (GthEditMetadataPage *base,
gth_time_selector_get_value (GTH_TIME_SELECTOR (self->priv->date_selector), date_time);
exif_date = gth_datetime_to_exif_date (date_time);
metadata = g_object_new (GTH_TYPE_METADATA,
"id", "Embedded::Image::Date",
"id", "Embedded::Image::DateTime",
"raw", exif_date,
"formatted", exif_date,
NULL);
g_file_info_set_attribute_object (self->priv->file_data->info, "Embedded::Image::Date", G_OBJECT (metadata));
g_file_info_set_attribute_object (self->priv->file_data->info, "Embedded::Image::DateTime", G_OBJECT (metadata));
g_object_unref (metadata);
gth_datetime_free (date_time);
......
......@@ -43,14 +43,15 @@ static gpointer parent_class = NULL;
static void
set_attribute_from_string (GFileInfo *info,
const char *key,
const char *value)
const char *value,
const char *formatted)
{
GthMetadata *metadata;
metadata = g_object_new (GTH_TYPE_METADATA,
"id", key,
"raw", value,
"formatted", value,
"formatted", (formatted != NULL ? formatted : value),
NULL);
g_file_info_set_attribute_object (info, key, G_OBJECT (metadata));
}
......@@ -76,13 +77,13 @@ gth_metadata_provider_comment_read (GthMetadataProvider *self,
value = gth_comment_get_note (comment);
if (value != NULL) {
g_file_info_set_attribute_string (file_data->info, "comment::note", value);
set_attribute_from_string (file_data->info, "Embedded::Image::Comment", value);
set_attribute_from_string (file_data->info, "Embedded::Image::Comment", value, NULL);
}
value = gth_comment_get_place (comment);
if (value != NULL) {
g_file_info_set_attribute_string (file_data->info, "comment::place", value);
set_attribute_from_string (file_data->info, "Embedded::Image::Location", value);
set_attribute_from_string (file_data->info, "Embedded::Image::Location", value, NULL);
}
categories = gth_comment_get_categories (comment);
......@@ -97,8 +98,17 @@ gth_metadata_provider_comment_read (GthMetadataProvider *self,
comment_time = gth_comment_get_time_as_exif_format (comment);
if (comment_time != NULL) {
g_file_info_set_attribute_string (file_data->info, "comment::time", comment_time);
set_attribute_from_string (file_data->info, "Embedded::Image::DateTime", comment_time);
GTimeVal time_;
char *formatted;
if (_g_time_val_from_exif_date (comment_time, &time_))
formatted = _g_time_val_strftime (&time_, "%x %X");
else
formatted = g_strdup (comment_time);
set_attribute_from_string (file_data->info, "comment::time", comment_time, formatted);
set_attribute_from_string (file_data->info, "Embedded::Image::DateTime", comment_time, formatted);
g_free (formatted);
g_free (comment_time);
}
......@@ -143,11 +153,13 @@ gth_metadata_provider_comment_write (GthMetadataProvider *self,
/* time */
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::Date");
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::DateTime");
if (metadata == NULL)
text = g_file_info_get_attribute_string (file_data->info, "comment::time");
else
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL)
text = gth_metadata_get_raw (metadata);
else
text = NULL;
gth_comment_set_time_from_exif_format (comment, text);
/* keywords */
......
......@@ -92,7 +92,11 @@ comments__read_metadata_ready_cb (GthFileData *file_data,
comment = gth_comment_new ();
gth_comment_set_note (comment, g_file_info_get_attribute_string (file_data->info, "comment::note"));
gth_comment_set_place (comment, g_file_info_get_attribute_string (file_data->info, "comment::place"));
gth_comment_set_time_from_exif_format (comment, g_file_info_get_attribute_string (file_data->info, "comment::time"));
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL)
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
keywords = gth_comment_get_categories (comment);
for (i = 0; i < keywords->len; i++)
gth_comment_add_category (comment, g_ptr_array_index (keywords, i));
......@@ -117,12 +121,15 @@ comments__read_metadata_ready_cb (GthFileData *file_data,
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::Date");
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::DateTime");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::time");
if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
write_comment = TRUE;
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL) {
text = gth_metadata_get_raw (metadata);
if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
write_comment = TRUE;
}
}
}
......
......@@ -167,6 +167,20 @@ exiv2_key_to_attribute (const char *key)
}
static gboolean
attribute_is_date (const char *key)
{
int i;
for (i = 0; _DATE_TAG_NAMES[i] != NULL; i++) {
if (strcmp (_DATE_TAG_NAMES[i], key) == 0)
return TRUE;
}
return FALSE;
}
inline static void
set_file_info (GFileInfo *info,
const char *key,
......@@ -186,7 +200,16 @@ set_file_info (GFileInfo *info,
attribute = exiv2_key_to_attribute (key);
description_utf8 = g_locale_to_utf8 (description, -1, NULL, NULL, NULL);
formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
if (attribute_is_date (attribute)) {
GTimeVal time_;
if (_g_time_val_from_exif_date (raw_value, &time_))
formatted_value_utf8 = _g_time_val_strftime (&time_, "%x %X");
else
formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
}
else
formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
/*
g_print ("%s (%s): %s (%s)\n", key, description, formatted_value, raw_value);
......@@ -614,11 +637,13 @@ exiv2_write_metadata_private (Exiv2::Image::AutoPtr image,
// Update the DateTime tag
GTimeVal current_time;
g_get_current_time (&current_time);
char *date_time = _g_time_val_to_exif_date (&current_time);
ed["Exif.Image.DateTime"] = date_time;
g_free (date_time);
if (g_file_info_get_attribute_object (info, "Exif::Image::DateTime") == NULL) {
GTimeVal current_time;
g_get_current_time (&current_time);
char *date_time = _g_time_val_to_exif_date (&current_time);
ed["Exif.Image.DateTime"] = date_time;
g_free (date_time);
}
// IPTC Data
......
......@@ -122,6 +122,10 @@ gth_metadata_provider_exiv2_write (GthMetadataProvider *self,
g_object_unref (meta);
}
metadata = g_file_info_get_attribute_object (file_data->info, "Embedded::Image::DateTime");
if (metadata != NULL)
g_file_info_set_attribute_object (file_data->info, "Exif::Image::DateTime", metadata);
if (exiv2_write_metadata_to_buffer (&buffer,
&size,
file_data->info,
......
......@@ -332,6 +332,64 @@ performance (const char *file,
/* GTimeVal utils */
/* taken from the glib function g_date_strftime */
char *
struct_tm_strftime (struct tm *tm,
const char *format)
{
gsize locale_format_len = 0;
char *locale_format;
GError *error = NULL;
gsize tmpbufsize;
char *tmpbuf;
gsize tmplen;
char *retval;
locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
if (error != NULL) {
g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
g_error_free (error);
return NULL;
}
tmpbufsize = MAX (128, locale_format_len * 2);
while (TRUE) {
tmpbuf = g_malloc (tmpbufsize);
/* Set the first byte to something other than '\0', to be able to
* recognize whether strftime actually failed or just returned "".
*/
tmpbuf[0] = '\1';
tmplen = strftime (tmpbuf, tmpbufsize, locale_format, tm);
if ((tmplen == 0) && (tmpbuf[0] != '\0')) {
g_free (tmpbuf);
tmpbufsize *= 2;
if (tmpbufsize > 65536) {
g_warning (G_STRLOC "Maximum buffer size for gth_datetime_strftime exceeded: giving up\n");
g_free (locale_format);
return NULL;
}
}
else
break;
}
g_free (locale_format);
retval = g_locale_to_utf8 (tmpbuf, tmplen, NULL, NULL, &error);
if (error != NULL) {
g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
g_error_free (error);
return NULL;
}
g_free (tmpbuf);
return retval;
}
int
_g_time_val_cmp (GTimeVal *a,
GTimeVal *b)
......@@ -455,7 +513,6 @@ _g_time_val_to_exif_date (GTimeVal *time_)
secs = time_->tv_sec;
tm = localtime (&secs);
retval = g_strdup_printf ("%4d:%02d:%02d %02d:%02d:%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
......@@ -468,6 +525,19 @@ _g_time_val_to_exif_date (GTimeVal *time_)
}
char *
_g_time_val_strftime (GTimeVal *time_,
const char *format)
{
time_t secs;
struct tm *tm;
secs = time_->tv_sec;
tm = localtime (&secs);
return struct_tm_strftime (tm, format);
}
/* Bookmark file utils */
......
......@@ -126,12 +126,16 @@ void performance (const char *file,
/* GTimeVal utils */
char * struct_tm_strftime (struct tm *tm,
const char *format);
int _g_time_val_cmp (GTimeVal *a,
GTimeVal *b);
void _g_time_val_reset (GTimeVal *time_);
gboolean _g_time_val_from_exif_date (const char *exif_date,
GTimeVal *time_);
char * _g_time_val_to_exif_date (GTimeVal *time_);
char * _g_time_val_strftime (GTimeVal *time_,
const char *format);
/* Bookmark file utils */
......
......@@ -56,7 +56,7 @@ gth_metadata_provider_file_read (GthMetadataProvider *self,
g_free (value);
timeval_p = gth_file_data_get_modification_time (file_data);
value = _g_time_val_to_exif_date (timeval_p);
value = _g_time_val_strftime (timeval_p, "%x %X");
g_file_info_set_attribute_string (file_data->info, "gth::file::display-mtime", value);
g_free (value);
......
......@@ -22,6 +22,7 @@
#include <config.h>
#include <stdlib.h>
#include "glib-utils.h"
#include "gth-time.h"
......@@ -248,62 +249,12 @@ gth_datetime_to_struct_tm (GthDateTime *dt,
}
/* started from the glib function g_date_strftime */
char *
gth_datetime_strftime (GthDateTime *dt,
const char *format)
{
gsize locale_format_len = 0;
char *locale_format;
GError *error = NULL;
struct tm tm;
gsize tmpbufsize;
char *tmpbuf;
gsize tmplen;
char *retval;
locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
if (error != NULL) {
g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
g_error_free (error);
return NULL;
}
struct tm tm;
gth_datetime_to_struct_tm (dt, &tm);
tmpbufsize = MAX (128, locale_format_len * 2);
while (TRUE) {
tmpbuf = g_malloc (tmpbufsize);
/* Set the first byte to something other than '\0', to be able to
* recognize whether strftime actually failed or just returned "".
*/
tmpbuf[0] = '\1';
tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
if ((tmplen == 0) && (tmpbuf[0] != '\0')) {
g_free (tmpbuf);
tmpbufsize *= 2;
if (tmpbufsize > 65536) {
g_warning (G_STRLOC "Maximum buffer size for gth_datetime_strftime exceeded: giving up\n");
g_free (locale_format);
return NULL;
}
}
else
break;
}
g_free (locale_format);
retval = g_locale_to_utf8 (tmpbuf, tmplen, NULL, NULL, &error);
if (error != NULL) {
g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
g_error_free (error);
return NULL;
}
g_free (tmpbuf);
return retval;
return struct_tm_strftime (&tm, format);
}
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