Fix bug 3006812: ensure exif cache is initialized even when all files are unreadable.
[geeqie.git] / src / exif-common.c
index 81c7d06..f87d141 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2010 The Geeqie Team
  *
 */
 
@@ -9,6 +9,8 @@
 #  include "config.h"
 #endif
 
+#define _XOPEN_SOURCE
+
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
@@ -40,6 +42,7 @@
 #include "filecache.h"
 #include "format_raw.h"
 #include "ui_fileops.h"
+#include "cache.h"
 
 
 static gdouble exif_rational_to_double(ExifRational *r, gint sign)
@@ -118,7 +121,7 @@ static gdouble get_crop_factor(ExifData *exif)
 
 }
 
-static gint remove_suffix(gchar *str, const gchar *suffix, gint suffix_len)
+static gboolean remove_suffix(gchar *str, const gchar *suffix, gint suffix_len)
 {
        gint str_len = strlen(str);
        
@@ -187,16 +190,46 @@ static gchar *exif_build_formatted_DateTime(ExifData *exif)
 {
        gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
        gchar *subsec = NULL;
+       gchar buf[128];
+       gchar *tmp;
+       gint buflen;
+       struct tm tm;
+       GError *error = NULL;
 
-       if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
-       if (!text)
+       if (text)
+               {
+               subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
+               }
+       else
                {
                text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
                if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
                }
+
+       /* Convert the stuff into a tm struct */
+       memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
+       if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
+               {
+               buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
+               if (buflen > 0)
+                       {
+                       tmp = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
+                       if (error)
+                               {
+                               log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
+                               g_error_free(error);
+                               }
+                       else
+                               {
+                               g_free(text);
+                               text = g_strdup(tmp);
+                               }
+                       }
+               }
+
        if (subsec)
                {
-               gchar *tmp = text;
+               tmp = text;
                text = g_strconcat(tmp, ".", subsec, NULL);
                g_free(tmp);
                g_free(subsec);
@@ -520,10 +553,13 @@ ExifFormattedText ExifFormattedList[] = {
        EXIF_FORMATTED_TAG(ColorProfile,        N_("Color profile")),
        EXIF_FORMATTED_TAG(GPSPosition,         N_("GPS position")),
        EXIF_FORMATTED_TAG(GPSAltitude,         N_("GPS altitude")),
+       {"file.size",                           N_("File size"),        NULL},
+       {"file.date",                           N_("File date"),        NULL},
+       {"file.mode",                           N_("File mode"),        NULL},
        { NULL, NULL, NULL }
 };
 
-gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
+gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gboolean *key_valid)
 {
        if (strncmp(key, EXIF_FORMATTED(), EXIF_FORMATTED_LEN) == 0)
                {
@@ -533,7 +569,7 @@ gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_val
 
                key += EXIF_FORMATTED_LEN;
                for (i = 0; ExifFormattedList[i].key; i++)
-                       if (strcmp(key, ExifFormattedList[i].key + EXIF_FORMATTED_LEN) == 0)
+                       if (ExifFormattedList[i].build_func && strcmp(key, ExifFormattedList[i].key + EXIF_FORMATTED_LEN) == 0)
                                return ExifFormattedList[i].build_func(exif);
                }
 
@@ -545,13 +581,13 @@ gchar *exif_get_description_by_key(const gchar *key)
 {
        if (!key) return NULL;
 
-       if (strncmp(key, EXIF_FORMATTED(), EXIF_FORMATTED_LEN) == 0)
+       if (strncmp(key, EXIF_FORMATTED(), EXIF_FORMATTED_LEN) == 0 ||
+           strncmp(key, "file.", 5) == 0)
                {
                gint i;
 
-               key += EXIF_FORMATTED_LEN;
                for (i = 0; ExifFormattedList[i].key; i++)
-                       if (strcmp(key, ExifFormattedList[i].key + EXIF_FORMATTED_LEN) == 0)
+                       if (strcmp(key, ExifFormattedList[i].key) == 0)
                                return g_strdup(_(ExifFormattedList[i].description));
                }
 
@@ -578,7 +614,7 @@ gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
 {
        ExifItem *item;
        gchar *text;
-       gint key_valid;
+       gboolean key_valid;
 
        if (!key) return NULL;
 
@@ -600,53 +636,39 @@ void exif_release_cb(FileData *fd)
        fd->exif = NULL;
 }
 
-ExifData *exif_read_fd(FileData *fd)
+void exif_init_cache(void)
 {
-       gchar *sidecar_path = NULL;
+       assert(!exif_cache);
+       exif_cache = file_cache_new(exif_release_cb, 4);
+}
 
-       if (!fd) return NULL;
+ExifData *exif_read_fd(FileData *fd)
+{
+       gchar *sidecar_path;
        
-       if (!exif_cache) exif_cache = file_cache_new(exif_release_cb, 4);
+       if (!exif_cache) exif_init_cache();
+
+       if (!fd || !is_readable_file(fd->path)) return NULL;
        
        if (file_cache_get(exif_cache, fd)) return fd->exif;
+       
+       /* CACHE_TYPE_XMP_METADATA file should exist only if the metadata are
+        * not writable directly, thus it should contain the most up-to-date version */
+       sidecar_path = NULL;
 
-       if (filter_file_class(fd->extension, FORMAT_CLASS_RAWIMAGE))
-               {
-               GList *work;
-               
-               work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
-               while (work)
-                       {
-                       FileData *sfd = work->data;
-                       work = work->next;
-                       if (strcasecmp(sfd->extension, ".xmp") == 0)
-                               {
-                               sidecar_path = sfd->path;
-                               break;
-                               }
-                       }
-               }
+#ifdef HAVE_EXIV2
+       /* we are not able to handle XMP sidecars without exiv2 */
+       sidecar_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
+
+       if (!sidecar_path) sidecar_path = file_data_get_sidecar_path(fd, TRUE);
+#endif
 
        fd->exif = exif_read(fd->path, sidecar_path, fd->modified_xmp);
+
+       g_free(sidecar_path);
        return fd->exif;
 }
 
-gint exif_write_fd(FileData *fd)
-{
-       gint success;
-       ExifData *exif;
-       
-       /*  exif_read_fd can either use cached metadata which have fd->modified_xmp already applied 
-                                    or read metadata from file and apply fd->modified_xmp
-           metadata are read also if the file was modified meanwhile */
-       exif = exif_read_fd(fd); 
-       if (!exif) return FALSE;
-       success = exif_write(exif); /* write modified metadata */
-       exif_free_fd(fd, exif);
-       g_hash_table_destroy(fd->modified_xmp);
-       fd->modified_xmp = NULL;
-       return success;
-}
 
 void exif_free_fd(FileData *fd, ExifData *exif)
 {
@@ -677,7 +699,7 @@ void exif_free_fd(FileData *fd, ExifData *exif)
        NNN.: the data in this segment
  */
 
-gint exif_jpeg_segment_find(guchar *data, guint size,
+gboolean exif_jpeg_segment_find(guchar *data, guint size,
                            guchar app_marker, const gchar *magic, guint magic_len,
                            guint *seg_offset, guint *seg_length)
 {
@@ -716,7 +738,7 @@ gint exif_jpeg_segment_find(guchar *data, guint size,
        return FALSE;
 }
 
-gint exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
+gboolean exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
 {
        guint seg_offset = 0;
        guint seg_length = 0;
@@ -790,4 +812,70 @@ gint exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
 
        return FALSE;
 }
+
+/*
+ *-------------------------------------------------------------------
+ * file info
+ * it is here because it shares tag neming infrastructure with exif
+ * we should probably not invest too much effort into this because
+ * new exiv2 will support the same functionality
+ * http://dev.exiv2.org/issues/show/505
+ *-------------------------------------------------------------------
+ */
+
+static gchar *mode_number(mode_t m)
+{
+       gint mb, mu, mg, mo;
+       gchar pbuf[12];
+
+       mb = mu = mg = mo = 0;
+
+       if (m & S_ISUID) mb |= 4;
+       if (m & S_ISGID) mb |= 2;
+       if (m & S_ISVTX) mb |= 1;
+
+       if (m & S_IRUSR) mu |= 4;
+       if (m & S_IWUSR) mu |= 2;
+       if (m & S_IXUSR) mu |= 1;
+
+       if (m & S_IRGRP) mg |= 4;
+       if (m & S_IWGRP) mg |= 2;
+       if (m & S_IXGRP) mg |= 1;
+
+       if (m & S_IROTH) mo |= 4;
+       if (m & S_IWOTH) mo |= 2;
+       if (m & S_IXOTH) mo |= 1;
+
+       pbuf[0] = (m & S_IRUSR) ? 'r' : '-';
+       pbuf[1] = (m & S_IWUSR) ? 'w' : '-';
+       pbuf[2] = (m & S_IXUSR) ? 'x' : '-';
+       pbuf[3] = (m & S_IRGRP) ? 'r' : '-';
+       pbuf[4] = (m & S_IWGRP) ? 'w' : '-';
+       pbuf[5] = (m & S_IXGRP) ? 'x' : '-';
+       pbuf[6] = (m & S_IROTH) ? 'r' : '-';
+       pbuf[7] = (m & S_IWOTH) ? 'w' : '-';
+       pbuf[8] = (m & S_IXOTH) ? 'x' : '-';
+       pbuf[9] = '\0';
+
+       return g_strdup_printf("%s (%d%d%d%d)", pbuf, mb, mu, mg, mo);
+}
+
+gchar *metadata_file_info(FileData *fd, const gchar *key, MetadataFormat format)
+{
+       if (strcmp(key, "file.size") == 0)
+               {
+               return g_strdup_printf("%ld", (long)fd->size);
+               }
+       if (strcmp(key, "file.date") == 0)
+               {
+               return g_strdup(text_from_time(fd->date));
+               }
+       if (strcmp(key, "file.mode") == 0)
+               {
+               return mode_number(fd->mode);
+               }
+       return g_strdup("");
+}
+
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */