From c8dc77a552c732c114fe65d461290e60f68f7212 Mon Sep 17 00:00:00 2001 From: Bastiaan van der Plaat Date: Mon, 29 Jan 2024 19:18:53 +0100 Subject: [PATCH] LibCore: Add Music, Pictures and Videos user directory helpers --- Userland/Libraries/LibCore/StandardPaths.cpp | 46 ++++++++++++++++++++ Userland/Libraries/LibCore/StandardPaths.h | 3 ++ 2 files changed, 49 insertions(+) diff --git a/Userland/Libraries/LibCore/StandardPaths.cpp b/Userland/Libraries/LibCore/StandardPaths.cpp index 467572f8b5..77ddbeb953 100644 --- a/Userland/Libraries/LibCore/StandardPaths.cpp +++ b/Userland/Libraries/LibCore/StandardPaths.cpp @@ -36,6 +36,9 @@ ByteString StandardPaths::home_directory() ByteString StandardPaths::desktop_directory() { + if (auto* desktop_directory = getenv("XDG_DESKTOP_DIR")) + return LexicalPath::canonicalized_path(desktop_directory); + StringBuilder builder; builder.append(home_directory()); builder.append("/Desktop"sv); @@ -44,6 +47,9 @@ ByteString StandardPaths::desktop_directory() ByteString StandardPaths::documents_directory() { + if (auto* documents_directory = getenv("XDG_DOCUMENTS_DIR")) + return LexicalPath::canonicalized_path(documents_directory); + StringBuilder builder; builder.append(home_directory()); builder.append("/Documents"sv); @@ -52,12 +58,52 @@ ByteString StandardPaths::documents_directory() ByteString StandardPaths::downloads_directory() { + if (auto* downloads_directory = getenv("XDG_DOWNLOAD_DIR")) + return LexicalPath::canonicalized_path(downloads_directory); + StringBuilder builder; builder.append(home_directory()); builder.append("/Downloads"sv); return LexicalPath::canonicalized_path(builder.to_byte_string()); } +ByteString StandardPaths::music_directory() +{ + if (auto* music_directory = getenv("XDG_MUSIC_DIR")) + return LexicalPath::canonicalized_path(music_directory); + + StringBuilder builder; + builder.append(home_directory()); + builder.append("/Music"sv); + return LexicalPath::canonicalized_path(builder.to_byte_string()); +} + +ByteString StandardPaths::pictures_directory() +{ + if (auto* pictures_directory = getenv("XDG_PICTURES_DIR")) + return LexicalPath::canonicalized_path(pictures_directory); + + StringBuilder builder; + builder.append(home_directory()); + builder.append("/Pictures"sv); + return LexicalPath::canonicalized_path(builder.to_byte_string()); +} + +ByteString StandardPaths::videos_directory() +{ + if (auto* videos_directory = getenv("XDG_VIDEOS_DIR")) + return LexicalPath::canonicalized_path(videos_directory); + + StringBuilder builder; + builder.append(home_directory()); +#if defined(AK_OS_MACOS) + builder.append("/Movies"sv); +#else + builder.append("/Videos"sv); +#endif + return LexicalPath::canonicalized_path(builder.to_byte_string()); +} + ByteString StandardPaths::config_directory() { if (auto* config_directory = getenv("XDG_CONFIG_HOME")) diff --git a/Userland/Libraries/LibCore/StandardPaths.h b/Userland/Libraries/LibCore/StandardPaths.h index 5791f8fadf..e82115d19b 100644 --- a/Userland/Libraries/LibCore/StandardPaths.h +++ b/Userland/Libraries/LibCore/StandardPaths.h @@ -18,6 +18,9 @@ public: static ByteString desktop_directory(); static ByteString documents_directory(); static ByteString downloads_directory(); + static ByteString music_directory(); + static ByteString pictures_directory(); + static ByteString videos_directory(); static ByteString tempfile_directory(); static ByteString config_directory(); static ByteString data_directory();