浏览代码

LibCore: Add Music, Pictures and Videos user directory helpers

Bastiaan van der Plaat 1 年之前
父节点
当前提交
c8dc77a552
共有 2 个文件被更改,包括 49 次插入0 次删除
  1. 46 0
      Userland/Libraries/LibCore/StandardPaths.cpp
  2. 3 0
      Userland/Libraries/LibCore/StandardPaths.h

+ 46 - 0
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"))

+ 3 - 0
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();