Bump Exiv2 minimum version requirement to 0.18
[geeqie.git] / src / cache.cc
index cf9ae59..9796461 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
 #include "cache.h"
 
+#include <config.h>
+
+#include "debug.h"
+#include "intl.h"
+#include "main-defines.h"
 #include "md5-util.h"
+#include "options.h"
 #include "secure-save.h"
 #include "thumb-standard.h"
 #include "ui-fileops.h"
@@ -57,7 +62,7 @@
  *-------------------------------------------------------------------
  */
 
-CacheData *cache_sim_data_new(void)
+CacheData *cache_sim_data_new()
 {
        CacheData *cd;
 
@@ -115,7 +120,8 @@ static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
 
 static gboolean cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
 {
-       guint x, y;
+       guint x;
+       guint y;
        guint8 buf[3 * 32];
 
        if (!cd || !cd->similarity || !cd->sim || !cd->sim->filled) return FALSE;
@@ -200,7 +206,7 @@ static gboolean cache_sim_read_skipline(FILE *f, gint s)
        return FALSE;
 }
 
-static gboolean cache_sim_read_comment(FILE *f, gchar *buf, gint s, CacheData *cd)
+static gboolean cache_sim_read_comment(FILE *f, const gchar *buf, gint s, CacheData *cd)
 {
        if (!f || !buf || !cd) return FALSE;
 
@@ -220,7 +226,8 @@ static gboolean cache_sim_read_dimensions(FILE *f, gchar *buf, gint s, CacheData
                gchar b;
                gchar buf[1024];
                gsize p = 0;
-               gint w, h;
+               gint w;
+               gint h;
 
                b = 'X';
                while (b != '[')
@@ -282,7 +289,7 @@ static gboolean cache_sim_read_date(FILE *f, gchar *buf, gint s, CacheData *cd)
                        }
 
                buf[p] = '\0';
-               cd->date = strtol(buf, NULL, 10);
+               cd->date = strtol(buf, nullptr, 10);
 
                cd->have_date = TRUE;
 
@@ -342,7 +349,8 @@ static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData
                gchar b;
                guint8 pixel_buf[3];
                ImageSimilarityData *sd;
-               gint x, y;
+               gint x;
+               gint y;
 
                b = 'X';
                while (b != '=')
@@ -354,7 +362,7 @@ static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData
                        {
                        /* use current sim that may already contain data we will not touch here */
                        sd = cd->sim;
-                       cd->sim = NULL;
+                       cd->sim = nullptr;
                        cd->similarity = FALSE;
                        }
                else
@@ -393,23 +401,25 @@ static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData
        return FALSE;
 }
 
-#define CACHE_LOAD_LINE_NOISE 8
+enum {
+       CACHE_LOAD_LINE_NOISE = 8
+};
 
 CacheData *cache_sim_data_load(const gchar *path)
 {
        FILE *f;
-       CacheData *cd = NULL;
+       CacheData *cd = nullptr;
        gchar buf[32];
        gint success = CACHE_LOAD_LINE_NOISE;
        gchar *pathl;
 
-       if (!path) return NULL;
+       if (!path) return nullptr;
 
        pathl = path_from_utf8(path);
        f = fopen(pathl, "r");
        g_free(pathl);
 
-       if (!f) return NULL;
+       if (!f) return nullptr;
 
        cd = cache_sim_data_new();
        cd->path = g_strdup(path);
@@ -462,7 +472,7 @@ CacheData *cache_sim_data_load(const gchar *path)
            !cd->similarity)
                {
                cache_sim_data_free(cd);
-               cd = NULL;
+               cd = nullptr;
                }
 
        return cd;
@@ -483,15 +493,18 @@ void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
        cd->dimensions = TRUE;
 }
 
-//void cache_sim_data_set_date(CacheData *cd, time_t date)
-//{
-       //if (!cd) return;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+void cache_sim_data_set_date_unused(CacheData *cd, time_t date)
+{
+       if (!cd) return;
 
-       //cd->date = date;
-       //cd->have_date = TRUE;
-//}
+       cd->date = date;
+       cd->have_date = TRUE;
+}
+#pragma GCC diagnostic pop
 
-void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16])
+void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16])
 {
        gint i;
 
@@ -530,65 +543,87 @@ gboolean cache_sim_data_filled(ImageSimilarityData *sd)
  *-------------------------------------------------------------------
  */
 
-
-static void cache_path_parts(CacheType type,
-                            const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
+struct CachePathParts
 {
-       switch (type)
-               {
-               case CACHE_TYPE_THUMB:
-                       *cache_rc = get_thumbnails_cache_dir();
-                       *cache_local = GQ_CACHE_LOCAL_THUMB;
-                       *cache_ext = GQ_CACHE_EXT_THUMB;
-                       break;
-               case CACHE_TYPE_SIM:
-                       *cache_rc = get_thumbnails_cache_dir();
-                       *cache_local = GQ_CACHE_LOCAL_THUMB;
-                       *cache_ext = GQ_CACHE_EXT_SIM;
-                       break;
-               case CACHE_TYPE_METADATA:
-                       *cache_rc = get_metadata_cache_dir();
-                       *cache_local = GQ_CACHE_LOCAL_METADATA;
-                       *cache_ext = GQ_CACHE_EXT_METADATA;
-                       break;
-               case CACHE_TYPE_XMP_METADATA:
-                       *cache_rc = get_metadata_cache_dir();
-                       *cache_local = GQ_CACHE_LOCAL_METADATA;
-                       *cache_ext = GQ_CACHE_EXT_XMP_METADATA;
-                       break;
-               }
-}
+       CachePathParts(CacheType type)
+       {
+               switch (type)
+                       {
+                       case CACHE_TYPE_THUMB:
+                               rc = get_thumbnails_cache_dir();
+                               local = GQ_CACHE_LOCAL_THUMB;
+                               ext = GQ_CACHE_EXT_THUMB;
+                               break;
+                       case CACHE_TYPE_SIM:
+                               rc = get_thumbnails_cache_dir();
+                               local = GQ_CACHE_LOCAL_THUMB;
+                               ext = GQ_CACHE_EXT_SIM;
+                               break;
+                       case CACHE_TYPE_METADATA:
+                               rc = get_metadata_cache_dir();
+                               local = GQ_CACHE_LOCAL_METADATA;
+                               ext = GQ_CACHE_EXT_METADATA;
+                               break;
+                       case CACHE_TYPE_XMP_METADATA:
+                               rc = get_metadata_cache_dir();
+                               local = GQ_CACHE_LOCAL_METADATA;
+                               ext = GQ_CACHE_EXT_XMP_METADATA;
+                               break;
+                       }
+       }
+
+       gchar *build_path_local(const gchar *source) const
+       {
+               gchar *base = remove_level_from_path(source);
+               gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
+               gchar *path = g_build_filename(base, local, name, nullptr);
+               g_free(name);
+               g_free(base);
+
+               return path;
+       }
+
+       gchar *build_path_rc(const gchar *source) const
+       {
+               gchar *name = g_strconcat(source, ext, nullptr);
+               gchar *path = g_build_filename(rc, name, nullptr);
+               g_free(name);
+
+               return path;
+       }
+
+       const gchar *rc = nullptr;
+       const gchar *local = nullptr;
+       const gchar *ext = nullptr;
+};
 
 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
 {
-       gchar *path = NULL;
+       gchar *path = nullptr;
        gchar *base;
-       gchar *name = NULL;
-       const gchar *cache_rc;
-       const gchar *cache_local;
-       const gchar *cache_ext;
+       gchar *name = nullptr;
 
-       if (!source) return NULL;
+       if (!source) return nullptr;
 
-       cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
+       const CachePathParts cache{type};
 
        base = remove_level_from_path(source);
        if (include_name)
                {
-               name = g_strconcat(filename_from_path(source), cache_ext, NULL);
+               name = g_strconcat(filename_from_path(source), cache.ext, NULL);
                }
 
        if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
             ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
            access_file(base, W_OK))
                {
-               path = g_build_filename(base, cache_local, name, NULL);
+               path = g_build_filename(base, cache.local, name, NULL);
                if (mode) *mode = 0775;
                }
 
        if (!path)
                {
-               path = g_build_filename(cache_rc, base, name, NULL);
+               path = g_build_filename(cache.rc, base, name, NULL);
                if (mode) *mode = 0755;
                }
 
@@ -598,39 +633,14 @@ gchar *cache_get_location(CacheType type, const gchar *source, gint include_name
        return path;
 }
 
-static gchar *cache_build_path_local(const gchar *source, const gchar *cache_local, const gchar *cache_ext)
-{
-       gchar *path;
-       gchar *base = remove_level_from_path(source);
-       gchar *name = g_strconcat(filename_from_path(source), cache_ext, NULL);
-       path = g_build_filename(base, cache_local, name, NULL);
-       g_free(name);
-       g_free(base);
-
-       return path;
-}
-
-static gchar *cache_build_path_rc(const gchar *source, const gchar *cache_rc, const gchar *cache_ext)
-{
-       gchar *path;
-       gchar *name = g_strconcat(source, cache_ext, NULL);
-       path = g_build_filename(cache_rc, name, NULL);
-       g_free(name);
-
-       return path;
-}
-
 gchar *cache_find_location(CacheType type, const gchar *source)
 {
        gchar *path;
-       const gchar *cache_rc;
-       const gchar *cache_local;
-       const gchar *cache_ext;
        gboolean prefer_local;
 
-       if (!source) return NULL;
+       if (!source) return nullptr;
 
-       cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
+       const CachePathParts cache{type};
 
        if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
                {
@@ -643,11 +653,11 @@ gchar *cache_find_location(CacheType type, const gchar *source)
 
        if (prefer_local)
                {
-               path = cache_build_path_local(source, cache_local, cache_ext);
+               path = cache.build_path_local(source);
                }
        else
                {
-               path = cache_build_path_rc(source, cache_rc, cache_ext);
+               path = cache.build_path_rc(source);
                }
 
        if (!isfile(path))
@@ -657,17 +667,17 @@ gchar *cache_find_location(CacheType type, const gchar *source)
                /* try the opposite method if not found */
                if (!prefer_local)
                        {
-                       path = cache_build_path_local(source, cache_local, cache_ext);
+                       path = cache.build_path_local(source);
                        }
                else
                        {
-                       path = cache_build_path_rc(source, cache_rc, cache_ext);
+                       path = cache.build_path_rc(source);
                        }
 
                if (!isfile(path))
                        {
                        g_free(path);
-                       path = NULL;
+                       path = nullptr;
                        }
                }
 
@@ -714,9 +724,9 @@ gboolean cache_time_valid(const gchar *cache, const gchar *path)
        return ret;
 }
 
-const gchar *get_thumbnails_cache_dir(void)
+const gchar *get_thumbnails_cache_dir()
 {
-       static gchar *thumbnails_cache_dir = NULL;
+       static gchar *thumbnails_cache_dir = nullptr;
 
        if (thumbnails_cache_dir) return thumbnails_cache_dir;
 
@@ -733,9 +743,9 @@ const gchar *get_thumbnails_cache_dir(void)
        return thumbnails_cache_dir;
 }
 
-const gchar *get_thumbnails_standard_cache_dir(void)
+const gchar *get_thumbnails_standard_cache_dir()
 {
-       static gchar *thumbnails_standard_cache_dir = NULL;
+       static gchar *thumbnails_standard_cache_dir = nullptr;
 
        if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
 
@@ -745,9 +755,9 @@ const gchar *get_thumbnails_standard_cache_dir(void)
        return thumbnails_standard_cache_dir;
 }
 
-const gchar *get_metadata_cache_dir(void)
+const gchar *get_metadata_cache_dir()
 {
-       static gchar *metadata_cache_dir = NULL;
+       static gchar *metadata_cache_dir = nullptr;
 
        if (metadata_cache_dir) return metadata_cache_dir;