Debian Codegraph @

src/core/profilemanager.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "profilemanager.h"
5
6 #include "core/isysmodel.h"
7 #include "iprofile.h"
8 #include <algorithm>
9 #include <iterator>
10 #include <regex>
11 #include <spdlog/spdlog.h>
12 #include <utility>
13
14 ProfileManager::ProfileManager(
15 std::unique_ptr<IProfile> &&defautlProfile,
16 std::unique_ptr<IProfileStorage> &&profileStorage) noexcept
17 : defaultProfile_(std::move(defautlProfile))
18 , profileStorage_(std::move(profileStorage))
19 {
20 }
21
22 void ProfileManager::addObserver(std::shared_ptr<IProfileManager::Observer> observer)
23 {
24 std::lock_guard<std::mutex> lock(obMutex_);
25
26 auto const it = std::find(observers_.begin(), observers_.end(), observer);
27 if (it == observers_.end())
28 observers_.emplace_back(std::move(observer));
29 }
30
31 void ProfileManager::removeObserver(
32 std::shared_ptr<IProfileManager::Observer> const &observer)
33 {
34 std::lock_guard<std::mutex> lock(obMutex_);
35 std::erase(observers_, observer);
36 }
37
38 void ProfileManager::init(ISysModel const &model)
39 {
40 // initialize default profile
41 model.exportWith(*defaultProfile_->initializer());
42
43 profileStorage_->init(*defaultProfile_);
44
45 std::regex const invalidName(R"(^\s+$)");
46 std::regex const invalidExe(R"(\\\|\/|\||\x00|\*|`|;|:|'|")");
47 std::unordered_set<std::string> exes;
48
49 bool globalFound = false;
50
51 auto profiles = profileStorage_->profiles(*defaultProfile_);
52 for (auto &profile : profiles) {
53 auto &info = profile->info();
54
55 if (info.exe == IProfile::Info::GlobalID) {
56
57 auto overrideInfo = info;
58 overrideInfo.name = IProfile::Info::GlobalID;
59 overrideInfo.iconURL = IProfile::Info::GlobalIconURL;
60 profile->info(overrideInfo);
61
62 globalFound = true;
63 }
64
65 if (!globalFound && info.name == IProfile::Info::GlobalID) {
66 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
67 info.exe);
68 SPDLOG_WARN("Used profile name ({}) is reserved for the Global Profile",
69 info.name);
70 continue;
71 }
72
73 if (!globalFound && info.exe == IProfile::Info::GlobalID) {
74 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
75 info.exe);
76 SPDLOG_WARN("Used profile executable name ({}) is "
77 "reserved for the Global Profile",
78 info.exe);
79 continue;
80 }
81
82 if (std::regex_search(info.name, invalidName)) {
83 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
84 info.exe);
85 SPDLOG_WARN("Profile name ({}) has invalid characters", info.name);
86 continue;
87 }
88
89 auto profileIt = profiles_.find(info.name);
90 if (profileIt != profiles_.cend()) {
91 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
92 info.exe);
93 SPDLOG_WARN("There is another profile with the same name ({})", info.name);
94 continue;
95 }
96
97 if (info.exe != IProfile::Info::ManualID) {
98 if (std::regex_search(info.exe, invalidExe)) {
99 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
100 info.exe);
101 SPDLOG_WARN("Profile executable name ({}) has invalid characters",
102 info.exe);
103 continue;
104 }
105
106 auto exeIt = exes.find(info.exe);
107 if (exeIt != exes.cend()) {
108 SPDLOG_WARN("Ignoring profile with name: '{}', exe: '{}'", info.name,
109 info.exe);
110 SPDLOG_WARN("There is another profile for the same executable ({})",
111 info.exe);
112 continue;
113 }
114
115 exes.insert(info.exe);
116 }
117
118 profiles_.emplace(info.name, std::move(profile));
119 }
120
121 if (!globalFound) {
122 auto globalProfile = defaultProfile_->clone();
123 globalProfile->info({std::string(IProfile::Info::GlobalID),
124 std::string(IProfile::Info::GlobalID),
125 std::string(IProfile::Info::GlobalIconURL)});
126
127 profileStorage_->save(*globalProfile);
128 profiles_.emplace(globalProfile->info().name, std::move(globalProfile));
129 }
130 }
131
132 std::vector<std::string> ProfileManager::profiles() const
133 {
134 std::vector<std::string> profiles;
135 profiles.reserve(profiles_.size());
136 std::transform(profiles_.cbegin(), profiles_.cend(),
137 std::back_inserter(profiles),
138 [](auto const &kv) { return kv.first; });
139 return profiles;
140 }
141
142 std::optional<std::reference_wrapper<IProfile const>>
143 ProfileManager::profile(std::string const &profileName) const
144 {
145 auto const profileIt = profiles_.find(profileName);
146 if (profileIt != profiles_.cend())
147 return *profileIt->second;
148
149 return {};
150 }
151
152 std::vector<std::string> ProfileManager::unsavedProfiles() const
153 {
154 std::vector<std::string> profiles;
155 profiles.reserve(unsavedProfiles_.size());
156 std::transform(unsavedProfiles_.cbegin(), unsavedProfiles_.cend(),
157 std::back_inserter(profiles),
158 [](auto const &profile) { return profile; });
159 return profiles;
160 }
161
162 bool ProfileManager::unsaved(std::string const &profileName) const
163 {
164 return unsavedProfiles_.find(profileName) != unsavedProfiles_.cend();
165 }
166
167 void ProfileManager::add(IProfile::Info const &info)
168 {
169 auto const profileIt = profiles_.find(info.name);
170 if (profileIt == profiles_.cend()) {
171 auto newProfile = defaultProfile_->clone();
172 newProfile->info(info);
173 profileStorage_->save(*newProfile);
174 profiles_.emplace(info.name, std::move(newProfile));
175
176 notifyProfileAdded(info.name);
177 }
178 }
179
180 void ProfileManager::clone(IProfile::Info const &cloneInfo,
181 std::string const &baseProfileName)
182 {
183 auto const profileIt = profiles_.find(baseProfileName);
184 if (profileIt != profiles_.cend()) {
185
186 auto const clonedProfileIt = profiles_.find(cloneInfo.name);
187 if (clonedProfileIt == profiles_.cend()) {
188 auto clone = profileIt->second->clone();
189 clone->info(cloneInfo);
190
191 // manual profiles are always active
192 if (cloneInfo.exe == IProfile::Info::ManualID)
193 clone->activate(true);
194
195 profileStorage_->save(*clone);
196 profiles_.emplace(cloneInfo.name, std::move(clone));
197
198 notifyProfileAdded(cloneInfo.name);
199 }
200 }
201 }
202
203 void ProfileManager::remove(std::string const &profileName)
204 {
205 auto const profileIt = profiles_.find(profileName);
206 if (profileIt != profiles_.cend()) {
207 auto info = profileIt->second->info();
208 profileStorage_->remove(info);
209 profiles_.erase(profileIt);
210
211 notifyProfileRemoved(profileName);
212 }
213 }
214
215 void ProfileManager::activate(std::string const &profileName, bool active)
216 {
217 auto const profileIt = profiles_.find(profileName);
218 if (profileIt != profiles_.cend()) {
219 auto &profile = profileIt->second;
220
221 // update stored profile active state
222 auto storedProfile = profile->clone();
223 profileStorage_->load(*storedProfile);
224 storedProfile->activate(active);
225 profileStorage_->save(*storedProfile);
226
227 // update profile active state
228 profile->activate(active);
229
230 notifyProfileActiveChanged(profileName, active);
231 }
232 }
233
234 void ProfileManager::reset(std::string const &profileName)
235 {
236 auto const profileIt = profiles_.find(profileName);
237 if (profileIt != profiles_.cend()) {
238 auto resetedProfile = defaultProfile_->clone();
239 resetedProfile->info(profileIt->second->info());
240 resetedProfile->activate(profileIt->second->active());
241 profiles_.insert_or_assign(profileName, std::move(resetedProfile));
242 unsavedProfiles_.insert(profileName);
243
244 notifyProfileChanged(profileName);
245 }
246 }
247
248 void ProfileManager::restore(std::string const &profileName)
249 {
250 auto const profileIt = profiles_.find(profileName);
251 if (profileIt != profiles_.cend()) {
252 profileStorage_->load(*profileIt->second);
253 unsavedProfiles_.erase(profileName);
254
255 notifyProfileChanged(profileName);
256 }
257 }
258
259 void ProfileManager::update(std::string const &profileName,
260 IProfile::Info const &newInfo)
261 {
262 auto const profileIt = profiles_.find(profileName);
263 if (profileIt != profiles_.cend()) {
264 auto &profile = *profileIt->second;
265 auto oldInfo = profile.info();
266
267 auto info = newInfo;
268 profileStorage_->update(profile, info);
269
270 // update profile info
271 profile.info(info);
272 if (info.name != profileName) {
273 // re-inserting element with the new name as key
274 auto profileNodeHandler = profiles_.extract(profileIt);
275 profileNodeHandler.key() = info.name;
276 profiles_.insert(std::move(profileNodeHandler));
277
278 // updating unsaved profile name
279 auto unsavedIt = unsavedProfiles_.find(profileName);
280 if (unsavedIt != unsavedProfiles_.cend()) {
281 unsavedProfiles_.erase(unsavedIt);
282 unsavedProfiles_.insert(info.name);
283 }
284 }
285 notifyProfileInfoChanged(oldInfo, info);
286
287 // manual profiles are always active
288 if (info.exe == IProfile::Info::ManualID && !profile.active()) {
289 profile.activate(true);
290 notifyProfileActiveChanged(info.name, true);
291 }
292 }
293 }
294
295 void ProfileManager::update(std::string const &profileName,
296 Importable::Importer &importer)
297 {
298 auto const profileIt = profiles_.find(profileName);
299 if (profileIt != profiles_.cend()) {
300 profileIt->second->importWith(importer);
301 unsavedProfiles_.insert(profileName);
302
303 notifyProfileChanged(profileName);
304 }
305 }
306
307 bool ProfileManager::loadFrom(std::string const &profileName,
308 std::filesystem::path const &path)
309 {
310 auto const profileIt = profiles_.find(profileName);
311 if (profileIt != profiles_.cend()) {
312 auto &profile = *profileIt->second;
313
314 auto info = profile.info();
315 auto active = profile.active();
316
317 if (!profileStorage_->loadFrom(profile, path))
318 return false;
319
320 profile.activate(active);
321 profile.info(info);
322
323 unsavedProfiles_.insert(profileName);
324 notifyProfileChanged(profileName);
325
326 return true;
327 }
328
329 return false;
330 }
331
332 void ProfileManager::save(std::string const &profileName)
333 {
334 auto const profileIt = profiles_.find(profileName);
335 if (profileIt != profiles_.cend()) {
336 profileStorage_->save(*profileIt->second);
337 unsavedProfiles_.erase(profileName);
338
339 notifyProfileSaved(profileName);
340 }
341 }
342
343 bool ProfileManager::exportTo(std::string const &profileName,
344 std::filesystem::path const &path)
345 {
346 auto const profileIt = profiles_.find(profileName);
347 if (profileIt != profiles_.cend())
348 return profileStorage_->exportTo(*profileIt->second, path);
349
350 return false;
351 }
352
353 void ProfileManager::notifyProfileAdded(std::string const &profileName)
354 {
355 std::lock_guard<std::mutex> lock(obMutex_);
356 for (auto &o : observers_)
357 o->profileAdded(profileName);
358 }
359
360 void ProfileManager::notifyProfileRemoved(std::string const &profileName)
361 {
362 std::lock_guard<std::mutex> lock(obMutex_);
363 for (auto &o : observers_)
364 o->profileRemoved(profileName);
365 }
366
367 void ProfileManager::notifyProfileChanged(std::string const &profileName)
368 {
369 std::lock_guard<std::mutex> lock(obMutex_);
370 for (auto &o : observers_)
371 o->profileChanged(profileName);
372 }
373
374 void ProfileManager::notifyProfileActiveChanged(std::string const &profileName,
375 bool active)
376 {
377 std::lock_guard<std::mutex> lock(obMutex_);
378 for (auto &o : observers_)
379 o->profileActiveChanged(profileName, active);
380 }
381
382 void ProfileManager::notifyProfileSaved(std::string const &profileName)
383 {
384 std::lock_guard<std::mutex> lock(obMutex_);
385 for (auto &o : observers_)
386 o->profileSaved(profileName);
387 }
388
389 void ProfileManager::notifyProfileInfoChanged(IProfile::Info const &oldInfo,
390 IProfile::Info const &newInfo)
391 {
392 std::lock_guard<std::mutex> lock(obMutex_);
393 for (auto &o : observers_)
394 o->profileInfoChanged(oldInfo, newInfo);
395 }