Debian Codegraph @

src/core/profilestorage.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "profilestorage.h"
5
6 #include "common/fileutils.h"
7 #include "iprofilefileparser.h"
8 #include "iprofileiconcache.h"
9 #include "iprofileparser.h"
10 #include <exception>
11 #include <format>
12 #include <spdlog/spdlog.h>
13 #include <stdexcept>
14 #include <utility>
15
16 namespace fs = std::filesystem;
17
18 ProfileStorage::ProfileStorage(
19 std::filesystem::path &&path,
20 std::unique_ptr<IProfileParser> &&profileParser,
21 std::unique_ptr<IProfileFileParser> &&profileFileParser,
22 std::unique_ptr<IProfileIconCache> &&iconCache) noexcept
23 : path_(std::move(path))
24 , profileParser_(std::move(profileParser))
25 , profileFileParser_(std::move(profileFileParser))
26 , iconCache_(std::move(iconCache))
27 , profileDataFileName_(IProfileFileParser::ProfileDataFileName)
28 {
29 fileExtension_ += "." + profileFileParser_->fileExtension();
30 profileDataFileName_ += "." + profileParser_->format();
31 }
32
33 void ProfileStorage::init(IProfile const &defaultProfile)
34 {
35 initProfilesDirectory();
36
37 // initialize components
38 defaultProfile.exportWith(*profileParser_->initializer());
39 iconCache_->init();
40 }
41
42 std::vector<std::unique_ptr<IProfile>>
43 ProfileStorage::profiles(IProfile const &baseProfile)
44 {
45 std::vector<std::unique_ptr<IProfile>> profiles;
46
47 if (profilesDirectoryExist()) {
48 for (auto const &pathIt : fs::directory_iterator(path_)) {
49
50 auto const &filePath = pathIt.path();
51 if (Utils::File::isFilePathValid(filePath) &&
52 filePath.extension() == fileExtension_) {
53
54 auto profile = baseProfile.clone();
55 if (loadProfileFromStorage(filePath, *profile))
56 profiles.emplace_back(std::move(profile));
57 }
58 }
59 }
60
61 return profiles;
62 }
63
64 bool ProfileStorage::load(IProfile &profile)
65 {
66 if (profilesDirectoryExist()) {
67 auto info = profile.info();
68 auto fileName = info.exe != IProfile::Info::ManualID
69 ? info.exe + fileExtension_
70 : info.exe + info.name + fileExtension_;
71 return loadProfileFromStorage(path_ / fileName, profile);
72 }
73
74 return false;
75 }
76
77 bool ProfileStorage::save(IProfile &profile)
78 {
79 if (profilesDirectoryExist()) {
80
81 auto info = profile.info();
82 auto fileName = info.exe != IProfile::Info::ManualID
83 ? info.exe + fileExtension_
84 : info.exe + info.name + fileExtension_;
85 auto exported = exportTo(profile, path_ / fileName);
86 if (exported) {
87
88 if (info.hasCustomIcon()) {
89
90 // sync icon cache when custom icons are used
91 auto [success, updated] = iconCache_->syncCache(info);
92 if (success && updated)
93 profile.info(info);
94 }
95
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 bool ProfileStorage::loadFrom(IProfile &profile,
104 std::filesystem::path const &path) const
105 {
106 if (Utils::File::isFilePathValid(path) && path.extension() == fileExtension_)
107 return loadProfileFrom(path, profile);
108 else
109 SPDLOG_DEBUG("Cannot load {}. Invalid file.", path.c_str());
110
111 return false;
112 }
113
114 bool ProfileStorage::exportTo(IProfile const &profile,
115 std::filesystem::path const &path) const
116 {
117 std::vector<char> profileData;
118 if (profileParser_->save(profileData, profile)) {
119
120 std::vector<std::pair<std::string, std::vector<char>>> data;
121 data.emplace_back(std::string(profileDataFileName_), std::move(profileData));
122
123 auto info = profile.info();
124 if (info.hasCustomIcon()) {
125
126 // save the custom icon in the profile
127 auto iconData = Utils::File::readFile(info.iconURL);
128 if (!iconData.empty())
129 data.emplace_back(std::string(IProfileFileParser::IconDataFileName),
130 std::move(iconData));
131 }
132
133 auto targetFilePath = path;
134 if (targetFilePath.extension() != fileExtension_)
135 targetFilePath += fileExtension_;
136
137 return profileFileParser_->save(targetFilePath, data);
138 }
139
140 return false;
141 }
142
143 bool ProfileStorage::update(IProfile const &profile, IProfile::Info &newInfo)
144 {
145 auto updatedProfile = profile.clone();
146 if (!load(*updatedProfile))
147 return false;
148
149 // manual profiles are always active
150 if (newInfo.exe == IProfile::Info::ManualID && !updatedProfile->active())
151 updatedProfile->activate(true);
152
153 auto oldInfo = profile.info();
154 updatedProfile->info(newInfo);
155 if (!save(*updatedProfile))
156 return false;
157
158 auto cacheURL = updatedProfile->info().iconURL;
159 if (cacheURL != newInfo.iconURL)
160 newInfo.iconURL = cacheURL;
161
162 if (oldInfo.exe != newInfo.exe ||
163 (oldInfo.exe == IProfile::Info::ManualID &&
164 newInfo.exe == IProfile::Info::ManualID && oldInfo.name != newInfo.name))
165 remove(oldInfo);
166
167 return true;
168 }
169
170 void ProfileStorage::remove(IProfile::Info &info)
171 {
172 if (profilesDirectoryExist()) {
173 iconCache_->clean(info);
174
175 auto fileName = info.exe != IProfile::Info::ManualID
176 ? info.exe + fileExtension_
177 : info.exe + info.name + fileExtension_;
178 try {
179 fs::remove(path_ / fileName);
180 }
181 catch (std::exception const &e) {
182 SPDLOG_DEBUG(e.what());
183 }
184 }
185 }
186
187 void ProfileStorage::initProfilesDirectory() const
188 {
189 if (!fs::exists(path_)) {
190 fs::create_directories(path_);
191 fs::permissions(path_, fs::perms::owner_all | fs::perms::group_read |
192 fs::perms::group_exec | fs::perms::others_read |
193 fs::perms::others_exec);
194 }
195
196 if (!fs::is_directory(path_))
197 throw std::runtime_error(
198 std::format("{} is not a directory.", path_.c_str()));
199 }
200
201 bool ProfileStorage::profilesDirectoryExist() const
202 {
203 if (Utils::File::isDirectoryPathValid(path_))
204 return true;
205
206 SPDLOG_DEBUG("Something went wrong with the profile storage directory: ",
207 path_.c_str());
208
209 return false;
210 }
211
212 bool ProfileStorage::loadProfileFromStorage(std::filesystem::path const &path,
213 IProfile &profile) const
214 {
215 auto profileData = profileFileParser_->load(path, profileDataFileName_);
216 if (profileData.has_value()) {
217 if (profileParser_->load(*profileData, profile)) {
218
219 auto info = profile.info();
220 if (info.exe == IProfile::Info::GlobalID)
221 info.iconURL = IProfile::Info::GlobalIconURL;
222 else {
223 // try to read the icon stored in the profile
224 auto profileIcon = profileFileParser_->load(
225 path, std::string(IProfileFileParser::IconDataFileName));
226
227 // profiles without icons use the default icon
228 if (!profileIcon.has_value())
229 info.iconURL = IProfile::Info::DefaultIconURL;
230
231 // profiles with icons use a cached icon
232 else if (iconCache_->tryOrCache(info, *profileIcon))
233 profile.info(info);
234 }
235
236 return true;
237 }
238 }
239
240 return false;
241 }
242
243 bool ProfileStorage::loadProfileFrom(std::filesystem::path const &path,
244 IProfile &profile) const
245 {
246 auto profileData = profileFileParser_->load(path, profileDataFileName_);
247 if (profileData.has_value())
248 return profileParser_->load(*profileData, profile);
249
250 return false;
251 }