Debian Codegraph @

libs/settings/SettingsManager.h

raw
1 #pragma once
2
3 #include <string>
4 #include "version.h"
5 #include "imodule.h"
6
7 #include "MajorMinorVersion.h"
8 #include "os/dir.h"
9 #include "os/path.h"
10
11 namespace settings
12 {
13
14 /**
15 * Settings file manager, dealing with saving and loading settings files
16 * to the user's settings path. DarkRadiant 2.15+ or higher stores its settings
17 * in folders specific to their major and minor versions:
18 *
19 * $HOME/.config/darkradiant/2.15/
20 * $HOME/.config/darkradiant/3.0/
21 *
22 * When loading settings files, this manager class will search the config folders
23 * of the current version and all previous versions, making it possible to use
24 * or upgrade settings saved by older versions. The most recent versions will
25 * take precedence over older files.
26 * It will ignore any files stored in higher or future version folders.
27 * For backwards compatibility with DarkRadiant versions that have not been
28 * using the version-specific settings folders, files in the base settings path
29 * will be considered too, with the lowest priority.
30 *
31 * When saving settings files, there's only a single target folder, which is
32 * the one corresponding to the current application version.
33 */
34 class SettingsManager
35 {
36 private:
37 const IApplicationContext& _context;
38 MajorMinorVersion _currentVersion;
39
40 // The path where this version writes its settings files to
41 std::string _currentVersionSettingsFolder;
42
43 // All existing version folders found in the settings folder
44 std::set<MajorMinorVersion> _existingVersionFolders;
45
46 public:
47 // Construct a settings manager for this version of DarkRadiant
48 // Will create the settings path for this version if it's not existing yet.
49 SettingsManager(const IApplicationContext& context) :
50 SettingsManager(context, RADIANT_VERSION)
51 {}
52
53 SettingsManager(const SettingsManager& other) = delete;
54 SettingsManager& operator=(const SettingsManager& other) = delete;
55
56 // Construct a settings manager instance with a specific version
57 // string in the format "Major.Minor.Micro[BuildSuffix]", or "2.14.0pre1".
58 // Mainly used for unit test purposes, regular code should use the default constructor.
59 SettingsManager(const IApplicationContext& context, const std::string& currentVersion) :
60 _context(context),
61 _currentVersion(currentVersion)
62 {
63 // Set up the path to the current version
64 _currentVersionSettingsFolder = os::standardPathWithSlash(context.getSettingsPath() + _currentVersion.toString());
65
66 // Make sure the output folder exists
67 os::makeDirectory(_currentVersionSettingsFolder);
68
69 // Create an inventory of the existing version folders
70 checkExistingVersionFolders();
71 }
72
73 // Returns the base path (non-version specific), which is the same as IApplicationContext::getSettingsPath()
74 std::string getBaseSettingsPath() const
75 {
76 return _context.getSettingsPath();
77 }
78
79 // Returns the output path (including trailing slash) where all settings files
80 // for the current version can be saved to.
81 // Matches the pattern IApplicationContext::getSettingsPath()/"major.minor"/
82 const std::string& getCurrentVersionSettingsFolder() const
83 {
84 return _currentVersionSettingsFolder;
85 }
86
87 /**
88 * Returns the full path to the first matching existing settings file (given by its path
89 * relative to the settings folder).
90 *
91 * This will search any settings folders for the current or any previous
92 * application version, returning the first matching file.
93 * The version folders are searched in descending order, with the current version having
94 * the highest priority. This way the file saved by the most recent known version
95 * will be picked, whereas versions saved by "future" application versions will be ignored.
96 *
97 * For compatibility reasons, files saved in the root settings folder will be
98 * considered too (with the lowest priority), as DarkRadiant versions prior to 2.15
99 * have not been creating version settings folders.
100 *
101 * Will return an empty string if no file was matching.
102 */
103 std::string getExistingSettingsFile(const std::string& relativePath) const
104 {
105 fs::path settingsPath = _context.getSettingsPath();
106
107 // Go through the existing versions, starting from the highest (usually the current version)
108 for (auto v = _existingVersionFolders.rbegin(); v != _existingVersionFolders.rend(); ++v)
109 {
110 auto fullPath = settingsPath / v->toString() / relativePath;
111
112 if (fs::is_regular_file(fullPath))
113 {
114 return os::standardPath(fullPath.string()); // found a match
115 }
116 }
117
118 // Finally, check the base folder if nothing else has been matching
119 auto fullPath = settingsPath / relativePath;
120
121 return fs::is_regular_file(fullPath) ? os::standardPath(fullPath.string()) : std::string();
122 }
123
124 private:
125 void checkExistingVersionFolders()
126 {
127 _existingVersionFolders.clear();
128
129 // Enumerate all existing folders and sort them by version
130 os::forEachItemInDirectory(_context.getSettingsPath(), [&](const fs::path& item)
131 {
132 // Skip non-directories
133 if (!fs::is_directory(item)) return;
134
135 try
136 {
137 // Attempt to parse the version from the folder name and insert into the list of
138 // existing versions. Due to operator< they will end up sorted in ascending order
139 MajorMinorVersion version(item.filename().string());
140
141 // Ignore all versions higher than the current one
142 if (_currentVersion < version) return;
143
144 // Sort this into the set
145 _existingVersionFolders.insert(version);
146 }
147 catch (const std::invalid_argument&)
148 {} // ignore invalid folder names
149 });
150 }
151 };
152
153 }