diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bf00968a..c58ecdd14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # next (unreleased) * Fix Python crash on start (#583, Max Krummenacher). * Prevent save failures on network and cloud drives (#593, Robert Little). +* Implement XDG Base Directory spec (#581, @dgcampea) # 2.22 (2021-04-25) * Add a "Give Feedback" button (#551, Rahul Jha). diff --git a/rednotebook/gui/menu.py b/rednotebook/gui/menu.py index af31bf2ba..347233e35 100644 --- a/rednotebook/gui/menu.py +++ b/rednotebook/gui/menu.py @@ -16,6 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # ----------------------------------------------------------------------- import os +import platform import webbrowser from gi.repository import GdkPixbuf, Gtk @@ -408,7 +409,18 @@ def on_help_menu_item_activate(self, widget): headers=[_("RedNotebook Documentation"), info.version, ""], options={"toc": 1}, ) - utils.show_html_in_browser(html, os.path.join(temp_dir, "help.html")) + + # workaround: for flatpak, temp_dir is located at /tmp which is not visible outside sandbox + # but XDG_CACHE_HOME is. + if platform.system() == "Linux": + cache_dir = os.getenv( + "XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".cache") + ) + help_file = os.path.join(cache_dir, "help.html") + else: + help_file = os.path.join(temp_dir, "help.html") + + utils.show_html_in_browser(html, help_file) def on_online_help(self, widget): webbrowser.open(info.answers_url) diff --git a/rednotebook/util/filesystem.py b/rednotebook/util/filesystem.py index 3c4699ee2..12cfb7cb8 100644 --- a/rednotebook/util/filesystem.py +++ b/rednotebook/util/filesystem.py @@ -23,6 +23,7 @@ import platform import subprocess import sys +import tempfile ENCODING = sys.getfilesystemencoding() or locale.getlocale()[1] or "UTF-8" @@ -78,10 +79,11 @@ def __init__(self, config): self.data_dir = self.default_data_dir # Assert that all dirs and files are in place so that logging can take start - make_directories( - [self.journal_user_dir, self.data_dir, self.template_dir, self.temp_dir] - ) - make_files([(self.config_file, ""), (self.log_file, "")]) + make_directories([self.journal_user_dir, self.data_dir, self.template_dir]) + self.temp_dir = tempfile.mkdtemp(prefix="rednotebook-") + + make_file(self.config_file) + make_file_with_dir(self.log_file, "") self.last_pic_dir = self.user_home_dir self.last_file_dir = self.user_home_dir @@ -102,7 +104,20 @@ def get_user_dir(self, config): if self.portable: user_dir = os.path.join(self.app_dir, "user") else: + # preserve backward compat: if ~/.rednotebook exists keep using it user_dir = os.path.join(self.user_home_dir, ".rednotebook") + if os.path.exists(user_dir): + pass + elif "XDG_CONFIG_HOME" in os.environ: + user_dir = os.path.join( + os.environ["XDG_CONFIG_HOME"], "rednotebook" + ) + elif "APPDATA" in os.environ and platform.system() == "Windows": + user_dir = os.path.join(os.environ["APPDATA"], "rednotebook") + else: + user_dir = os.path.join( + self.user_home_dir, ".config", "rednotebook" + ) return user_dir @@ -112,13 +127,25 @@ def is_valid_journal_path(self, path): def __getattribute__(self, attr): user_paths = { "template_dir": "templates", - "temp_dir": "tmp", "default_data_dir": "data", "config_file": "configuration.cfg", "log_file": "rednotebook.log", } if attr in user_paths: + if attr == "log_file": + if platform.system() == "Windows": + # do not apply XDG spec for Windows + logpath = self.journal_user_dir + elif "XDG_STATE_HOME" in os.environ: + logpath = os.path.join(os.environ["XDG_STATE_HOME"], "rednotebook") + else: + logpath = os.path.join( + self.user_home_dir, ".local", "state", "rednotebook" + ) + + return os.path.join(logpath, user_paths.get("log_file")) + return os.path.join(self.journal_user_dir, user_paths.get(attr)) return dict.__getattribute__(self, attr)