From c41e7244a1283362161b3f72034d68b264f19d35 Mon Sep 17 00:00:00 2001
From: Olof-Joachim Frahm <olof@macrolet.net>
Date: Wed, 5 Apr 2017 23:02:38 +0200
Subject: Use XDG_CACHE_HOME for thumbnails.

---
 src/thumbnail.c | 67 ++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 45 insertions(+), 22 deletions(-)

diff --git a/src/thumbnail.c b/src/thumbnail.c
index 43168c4..d400507 100644
--- a/src/thumbnail.c
+++ b/src/thumbnail.c
@@ -582,21 +582,39 @@ int feh_thumbnail_get_thumbnail(Imlib_Image * image, feh_file * file,
 	return status;
 }
 
+static char *feh_thumbnail_get_prefix()
+{
+	char *dir = NULL, *home, *xdg_cache_home;
+
+        // TODO: perhaps make sure that either of those paths aren't /-terminated
+
+	xdg_cache_home = getenv("XDG_CACHE_HOME");
+	if (xdg_cache_home && xdg_cache_home[0] == '/') {
+		dir = estrjoin("/", xdg_cache_home, "thumbnails", td.cache_dir, NULL);
+	} else {
+		home = getenv("HOME");
+		if (home && home[0] == '/') {
+			dir = estrjoin("/", home, ".cache/thumbnails", td.cache_dir, NULL);
+		}
+	}
+
+	return dir;
+}
+
 char *feh_thumbnail_get_name(char *uri)
 {
-	char *home = NULL, *thumb_file = NULL, *md5_name = NULL;
+	char *prefix, *thumb_file = NULL, *md5_name;
 
 	/* FIXME: make sure original file isn't under ~/.thumbnails */
 
-	md5_name = feh_thumbnail_get_name_md5(uri);
-
-	home = getenv("HOME");
-	if (home) {
-		thumb_file = estrjoin("/", home, ".thumbnails", td.cache_dir, md5_name, NULL);
+        prefix = feh_thumbnail_get_prefix();
+        if (prefix) {
+		md5_name = feh_thumbnail_get_name_md5(uri);
+		thumb_file = estrjoin("/", prefix, md5_name, NULL);
+		free(md5_name);
+                free(prefix);
 	}
 
-	free(md5_name);
-
 	return thumb_file;
 }
 
@@ -847,31 +865,36 @@ int feh_thumbnail_setup_thumbnail_dir(void)
 {
 	int status = 0;
 	struct stat sb;
-	char *dir, *dir_thumbnails, *home;
+	char *dir, *p;
 
-	home = getenv("HOME");
-	if (home != NULL) {
-		dir = estrjoin("/", home, ".thumbnails", td.cache_dir, NULL);
+        dir = feh_thumbnail_get_prefix();
 
+	if (dir) {
 		if (!stat(dir, &sb)) {
 			if (S_ISDIR(sb.st_mode))
 				status = 1;
 			else
 				weprintf("%s should be a directory", dir);
 		} else {
-			dir_thumbnails = estrjoin("/", home, ".thumbnails", NULL);
+			for (p = dir + 1; *p; p++) {
+				if (*p != '/') {
+					continue;
+				}
 
-			if (stat(dir_thumbnails, &sb) != 0) {
-				if (mkdir(dir_thumbnails, 0700) == -1)
-					weprintf("unable to create directory %s", dir_thumbnails);
+				*p = 0;
+				if (stat(dir, &sb) != 0) {
+					if (mkdir(dir, 0700) == -1) {
+						weprintf("unable to create directory %s", dir);
+					}
+				}
+				*p = '/';
 			}
 
-			free(dir_thumbnails);
-
-			if (mkdir(dir, 0700) == -1)
-				weprintf("unable to create directory %s", dir);
-			else
-				status = 1;
+			if (stat(dir, &sb) != 0) {
+				if (mkdir(dir, 0700) == -1) {
+					weprintf("unable to create directory %s", dir);
+				}
+			}
 		}
 		free(dir);
 	}
-- 
cgit v1.2.3