Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XDG_CACHE_HOME. #175

Merged
merged 5 commits into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions contrib/gtktheme/jgmenu-gtktheme.py
Expand Up @@ -56,9 +56,11 @@ def process_line(line):
setconfig("color_title_border", rgb2hex(line))

def cache(themename):
""" save the theme-name to ~/.cache/jgmenu/.last-gtktheme """
""" save the theme-name to XDG_CACHE_HOME/jgmenu/.last-gtktheme or
~/.cache/jgmenu/.last-gtktheme if it doesn't exist """
print("themename={}".format(themename))
directory = os.environ["HOME"] + "/.cache/jgmenu"
home = os.environ["HOME"]
directory = os.getenv("XDG_CACHE_HOME", home + "/.cache") + "/jgmenu"
if not os.path.exists(directory):
os.mkdir(directory)
f = open(directory + "/.last-gtktheme", "w")
Expand Down
4 changes: 3 additions & 1 deletion docs/notes_on_icons
Expand Up @@ -7,7 +7,9 @@ The main loop kicks off two threads to load icons,
This allows the root-menu to be displayed quickly whilst the rest is loaded in
the background.

When jgmenu if first run, icon 'cache' is created at '~/.cache/jgmenu/icons/'.
When jgmenu if first run, icon 'cache' is created in the path specified in the
XDG_CACHE_HOME environment variable. If this variable is unset, it default back
on '~/.cache/jgmenu/icons/'.
The cache consists of symlinks pointing to icons which match the name, size and
theme. For example: folder -> /usr/share/icons/Adwaita/22x22/places/folder.png

Expand Down
24 changes: 20 additions & 4 deletions src/cache.c
Expand Up @@ -10,7 +10,7 @@
#include "util.h"
#include "banned.h"

#define CACHE_LOCATION "~/.cache/jgmenu/icons"
#define DEFAULT_CACHE_LOCATION "~/.cache"
static struct sbuf *cache_location;

static struct sbuf icon_theme;
Expand Down Expand Up @@ -110,6 +110,23 @@ static int cache_check_index_theme(const char *theme, int size)
return ret;
}

static void cache_get_dir(void)
{
const char *xdg_cache_home = getenv("XDG_CACHE_HOME");

if (xdg_cache_home && *xdg_cache_home)
sbuf_addstr(cache_location, xdg_cache_home);
else
sbuf_addstr(cache_location, DEFAULT_CACHE_LOCATION);
sbuf_addstr(cache_location, "/jgmenu/icons");
sbuf_expand_tilde(cache_location);
}

char *cache_icon_get_dir(void)
{
return cache_location->buf;
}

static void cache_delete(void)
{
char cmd[512];
Expand All @@ -136,11 +153,10 @@ static void cache_init(void)
die("cache.c: icon_{theme,size} needs to be set");
cache_location = xmalloc(sizeof(struct sbuf));
sbuf_init(cache_location);
sbuf_cpy(cache_location, CACHE_LOCATION);
sbuf_expand_tilde(cache_location);
cache_get_dir();
if (cache_check_index_theme(icon_theme.buf, icon_size) < 0) {
cache_delete();
mkdir_p(CACHE_LOCATION);
mkdir_p(cache_location->buf);
cache_create_index_theme(icon_theme.buf, icon_size);
}
first_run = 0;
Expand Down
1 change: 1 addition & 0 deletions src/cache.h
Expand Up @@ -5,6 +5,7 @@

void cache_set_icon_theme(const char *theme);
void cache_set_icon_size(int size);
char *cache_icon_get_dir(void);
int cache_touch(const char *name);
int cache_strdup_path(const char *name, struct sbuf *path);
int cache_create_symlink(char *path, char *name);
Expand Down
3 changes: 1 addition & 2 deletions src/icon.c
Expand Up @@ -23,7 +23,6 @@
#include "xpm-loader.h"
#include "cache.h"
#include "config.h"
#include "icon.h"
#include "banned.h"

#define DEBUG_THEMES 0
Expand Down Expand Up @@ -225,7 +224,7 @@ void icon_load(void)
}
}
if (nr_symlinks)
fprintf(stderr, "info: created %d symlinks in ~/.cache/jgmenu/icons/\n", nr_symlinks);
fprintf(stderr, "info: created %d symlinks in %s\n", nr_symlinks, cache_icon_get_dir());
list_for_each_entry_safe(path, tmp_path, &icon_paths, list) {
free(path->name.buf);
free(path->path.buf);
Expand Down
10 changes: 8 additions & 2 deletions src/jgmenu-init.sh
Expand Up @@ -285,8 +285,14 @@ unicode files are not XDG compliant and may give unpredicted results"
}

icon_theme_last_used_by_jgmenu () {
icon_theme=$(grep -i 'Inherits' ~/.cache/jgmenu/icons/index.theme)
icon_size=$(grep -i 'Size' ~/.cache/jgmenu/icons/index.theme)
if [ -d "$XDG_CACHE_HOME" ]
then
icon_theme=$(grep -i 'Inherits' $XDG_CACHE_HOME/jgmenu/icons/index.theme)
icon_size=$(grep -i 'Size' $XDG_CACHE_HOME/jgmenu/icons/index.theme)
else
icon_theme=$(grep -i 'Inherits' ~/.cache/jgmenu/icons/index.theme)
icon_size=$(grep -i 'Size' ~/.cache/jgmenu/icons/index.theme)
fi
printf 'last time, icon-theme %s-%s was used\n' "${icon_theme#Inherits=}" \
"${icon_size#Size=}"
}
Expand Down
7 changes: 6 additions & 1 deletion tests/t1201-lx.t
Expand Up @@ -62,7 +62,12 @@ test_menu () {
printf "%b\n" "XDG_DATA_DIRS=$XDG_DATA_DIRS"
export XDG_MENU_PREFIX="${1}-"

rm -rf ~/.cache/menus &&
if [ -d "$XDG_CACHE_HOME" ]
then
rm -rf $XDG_CACHE_HOME/menus
else
rm -rf ~/.cache/menus
fi
cp "../t1201/${1}.expect" expect &&
LANG=C LC_ALL=C ../../contrib/lx/jgmenu-lx >actual &&
cp "../t1201/${1}.expect" expect &&
Expand Down