From 49d74ee288bc8a1bc28242b59c47b18b03c6d973 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 5 Dec 2022 12:46:53 -0500 Subject: [PATCH] LibCore: Add a standard path for application data --- Userland/Libraries/LibCore/StandardPaths.cpp | 18 ++++++++++++++++++ Userland/Libraries/LibCore/StandardPaths.h | 1 + 2 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibCore/StandardPaths.cpp b/Userland/Libraries/LibCore/StandardPaths.cpp index 49e20408c3..73d51d7302 100644 --- a/Userland/Libraries/LibCore/StandardPaths.cpp +++ b/Userland/Libraries/LibCore/StandardPaths.cpp @@ -65,6 +65,24 @@ DeprecatedString StandardPaths::config_directory() return LexicalPath::canonicalized_path(builder.to_deprecated_string()); } +DeprecatedString StandardPaths::data_directory() +{ + if (auto* data_directory = getenv("XDG_DATA_HOME")) + return LexicalPath::canonicalized_path(data_directory); + + StringBuilder builder; + builder.append(home_directory()); +#if defined(AK_OS_SERENITY) + builder.append("/.data"sv); +#elif defined(AK_OS_MACOS) + builder.append("/Library/Application Support"sv); +#else + builder.append("/.local/share"sv); +#endif + + return LexicalPath::canonicalized_path(builder.to_deprecated_string()); +} + DeprecatedString StandardPaths::tempfile_directory() { return "/tmp"; diff --git a/Userland/Libraries/LibCore/StandardPaths.h b/Userland/Libraries/LibCore/StandardPaths.h index 234eab42dc..78014ea279 100644 --- a/Userland/Libraries/LibCore/StandardPaths.h +++ b/Userland/Libraries/LibCore/StandardPaths.h @@ -18,6 +18,7 @@ public: static DeprecatedString downloads_directory(); static DeprecatedString tempfile_directory(); static DeprecatedString config_directory(); + static DeprecatedString data_directory(); }; }