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

Add XDG Base Directory specification support #122

Open
justbispo opened this issue Apr 4, 2024 · 2 comments · May be fixed by #160
Open

Add XDG Base Directory specification support #122

justbispo opened this issue Apr 4, 2024 · 2 comments · May be fixed by #160

Comments

@justbispo
Copy link

Implement the necessary changes to support the XDG Base Directory specification.

This issue was first brought up here with a bit more info, and a solution was presented in a pull request on wangqr's fork here.

I've tried to implement it myself on this fork, hoping I was lucky enough that it was just copy-paste with no issues, but it threw some errors and I don't know how to fix them since I don't have any knowledge of C++.

@witchymary
Copy link

I have a patch that works on arch1's fork, if you're interested:

From ee22306713bfa5ecdac072dbab6f648421b295d0 Mon Sep 17 00:00:00 2001
From: witchymary
Date: Sun, 8 Oct 2023 14:39:05 -0300
Subject: [PATCH] XDG Support

---
 libaegisub/unix/path.cpp | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libaegisub/unix/path.cpp b/libaegisub/unix/path.cpp
index 7a2c9a465..e40961a01 100644
--- a/libaegisub/unix/path.cpp
+++ b/libaegisub/unix/path.cpp
@@ -42,6 +42,14 @@ std::string home_dir() {
 	throw agi::EnvironmentError("Could not get home directory. Make sure HOME is set.");
 }
 
+std::string xdg_dir(const std::string &environment_variable,
+                    const std::string &fallback_directory)
+{
+	const char *env = getenv(environment_variable.c_str());
+	if (env && *env) return env;
+	return fallback_directory;
+}
+
 #ifdef APPIMAGE_BUILD
 std::string exe_dir() {
 	char *exe, *dir;
@@ -71,8 +79,17 @@ namespace agi {
 void Path::FillPlatformSpecificPaths() {
 #ifndef __APPLE__
 	agi::fs::path home = home_dir();
-	SetToken("?user", home/".aegisub");
-	SetToken("?local", home/".aegisub");
+	agi::fs::path prev_dir = home/".aegisub";
+	if (!boost::filesystem::exists(prev_dir))
+	{
+		agi::fs::path xdg_config_home = xdg_dir("XDG_CONFIG_HOME", (home/".config").string());
+		agi::fs::path xdg_cache_home = xdg_dir("XDG_CACHE_HOME", (home/".cache").string());
+		SetToken("?user", xdg_config_home/"Aegisub");
+		SetToken("?local", xdg_cache_home/"Aegisub");
+	} else {
+		SetToken("?user", prev_dir);
+	    SetToken("?local", prev_dir);
+	}
 
 #ifdef APPIMAGE_BUILD
 	agi::fs::path exe = exe_dir();
-- 
2.42.0

@witchymary witchymary linked a pull request Sep 15, 2024 that will close this issue
@justbispo
Copy link
Author

Oops, sorry I've missed your comment for so long, somehow only noticed it today. I'm glad you've submitted the pull request instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants