Rose
XDGFilePaths.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <array>
11 #include <filesystem>
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 namespace rose {
17 
23  class XDGFilePaths {
24  public:
25  enum XDG_Name : std::size_t {
26  XDG_DATA_HOME,
27  XDG_CONFIG_HOME,
28  XDG_DATA_DIRS,
29  XDG_CONFIG_DIRS,
30  XDG_CACHE_HOME,
31  XDG_RUNTIME_DIR
32  };
33 
34  struct XDG_Env_Spec {
35  XDG_Name name;
36  std::string_view varName;
37  std::string_view defaultPath;
38  bool homeRelative;
39  };
40 
41  using XDG_Env_Var_List = std::array<XDG_Env_Spec, 6>;
42 
43  using XDG_Path_Set = std::vector<std::filesystem::path>;
44 
45  using XDG_Paths = std::map<XDG_Name, XDG_Path_Set>;
46 
47  protected:
48  static constexpr XDG_Env_Var_List mEnvVars = {
49  XDG_Env_Spec{XDG_DATA_HOME, "XDG_DATA_HOME", ".local/share", true},
50  XDG_Env_Spec{XDG_CONFIG_HOME, "XDG_CONFIG_HOME", ".config", true},
51  XDG_Env_Spec{XDG_DATA_DIRS, "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", false},
52  XDG_Env_Spec{XDG_CONFIG_DIRS, "XDG_CONFIG_DIRS", "/etc/xdg", false},
53  XDG_Env_Spec{XDG_CACHE_HOME, "XDG_CACHE_HOME", ".cache", true},
54  XDG_Env_Spec{XDG_RUNTIME_DIR, "XDG_RUNTIME_DIR", "", false}
55  };
56 
57  XDG_Paths mPaths{};
58 
59  std::string mHome;
60 
61  public:
62 
63  XDGFilePaths();
64 
75  template<typename S>
76  std::tuple<bool,std::filesystem::path> findFilePath(XDG_Name name, const S &relativePath) {
77  for (auto const &path : mPaths[name]) {
78  auto tryPath{path};
79  tryPath.append(relativePath);
80  if (std::filesystem::exists(tryPath))
81  return std::make_tuple(true,tryPath);
82  }
83 
84  auto preferredPath{mPaths[name].front()};
85  preferredPath.append(relativePath);
86  return std::make_tuple(false, preferredPath);
87  }
88  };
89 }
Definition: XDGFilePaths.h:34
std::tuple< bool, std::filesystem::path > findFilePath(XDG_Name name, const S &relativePath)
Search for a relative path on one of the XDG standard locations.
Definition: XDGFilePaths.h:76
XDG_Paths mPaths
All the XDG specified paths.
Definition: XDGFilePaths.h:57
std::string mHome
User&#39;s home directory.
Definition: XDGFilePaths.h:59
A class to determine and provide searching of XDG standard files paths.
Definition: XDGFilePaths.h:23
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13