Debian Codegraph @

src/core/profilemanagerui.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "profilemanagerui.h"
5
6 #include "iprofile.h"
7 #include "iprofilemanager.h"
8 #include "isession.h"
9 #include "isysmodelui.h"
10 #include "qmlcomponentregistry.h"
11 #include <QQmlApplicationEngine>
12 #include <QtGlobal>
13 #include <QtQml>
14 #include <algorithm>
15 #include <utility>
16
17 char const *const ProfileManagerUI::trStrings[] = {
18 QT_TRANSLATE_NOOP("ProfileManagerUI", "_global_"),
19 };
20
21 class ProfileManagerUI::ProfileManagerObserver : public IProfileManager::Observer
22 {
23 public:
24 ProfileManagerObserver(ProfileManagerUI &outer) noexcept
25 : outer_(outer)
26 {
27 }
28
29 void profileAdded(std::string const &profileName) override;
30 void profileRemoved(std::string const &profileName) override;
31 void profileChanged(std::string const &profileName) override;
32 void profileActiveChanged(std::string const &profileName, bool active) override;
33 void profileSaved(std::string const &profileName) override;
34 void profileInfoChanged(IProfile::Info const &oldInfo,
35 IProfile::Info const &newInfo) override;
36
37 private:
38 ProfileManagerUI &outer_;
39 };
40
41 void ProfileManagerUI::ProfileManagerObserver::profileAdded(
42 std::string const &profileName)
43 {
44 outer_.addProfileComponet(profileName);
45 }
46
47 void ProfileManagerUI::ProfileManagerObserver::profileRemoved(
48 std::string const &profileName)
49 {
50 emit outer_.profileRemoved(QString::fromStdString(profileName));
51 }
52
53 void ProfileManagerUI::ProfileManagerObserver::profileChanged(
54 std::string const &profileName)
55 {
56 emit outer_.profileChanged(QString::fromStdString(profileName));
57 }
58
59 void ProfileManagerUI::ProfileManagerObserver::profileActiveChanged(
60 std::string const &profileName, bool active)
61 {
62 emit outer_.profileActiveChanged(QString::fromStdString(profileName), active);
63 }
64
65 void ProfileManagerUI::ProfileManagerObserver::profileSaved(
66 std::string const &profileName)
67 {
68 emit outer_.profileSaved(QString::fromStdString(profileName));
69 }
70
71 void ProfileManagerUI::ProfileManagerObserver::profileInfoChanged(
72 IProfile::Info const &oldInfo, IProfile::Info const &newInfo)
73 {
74 outer_.addProfileUsedNames(newInfo.name);
75 auto profile = outer_.profileManager_->profile(newInfo.name);
76 emit outer_.profileInfoChanged(
77 QString::fromStdString(oldInfo.name), QString::fromStdString(newInfo.name),
78 newInfo.exe != IProfile::Info::ManualID
79 ? QString::fromStdString(newInfo.exe)
80 : QLatin1String(""),
81 outer_.toQMLIconPath(newInfo.iconURL), newInfo.hasCustomIcon(),
82 profile->get().active());
83 }
84
85 class ProfileManagerUI::ManualProfileObserver
86 : public ISession::ManualProfileObserver
87 {
88 public:
89 ManualProfileObserver(ProfileManagerUI &outer) noexcept
90 : outer_(outer)
91 {
92 }
93
94 void toggled(std::string const &profileName, bool active) override;
95
96 private:
97 ProfileManagerUI &outer_;
98 };
99
100 void ProfileManagerUI::ManualProfileObserver::toggled(
101 const std::string &profileName, bool active)
102 {
103 emit outer_.manualProfileToggled(QString::fromStdString(profileName), active);
104 }
105
106 ProfileManagerUI::ProfileManagerUI(QObject *parent) noexcept
107 : QObject(parent)
108 , profileManagerObserver_(
109 std::make_shared<ProfileManagerUI::ProfileManagerObserver>(*this))
110 , manualProfileObserver_(
111 std::make_shared<ProfileManagerUI::ManualProfileObserver>(*this))
112 {
113 usedExecutableNames_.insert(
114 QString::fromLatin1(IProfile::Info::ManualID.data()));
115 }
116
117 void ProfileManagerUI::init(ISession *session, ISysModelUI *sysModelUI)
118 {
119 sysModelUI_ = sysModelUI;
120 session_ = session;
121 session_->addManualProfileObserver(manualProfileObserver_);
122 profileManager_ = &session_->profileManager();
123 profileManager_->addObserver(profileManagerObserver_);
124
125 // create profile components
126 auto profileNames = profileManager_->profiles();
127
128 // short profile names
129 std::sort(profileNames.begin(), profileNames.end());
130
131 // the global profile must be the first element
132 auto globalIt = std::find_if(
133 profileNames.begin(), profileNames.end(),
134 [](auto const &profile) { return profile == IProfile::Info::GlobalID; });
135 if (globalIt != profileNames.cend())
136 std::rotate(profileNames.begin(), globalIt, globalIt + 1);
137
138 QVariantList list;
139 for (auto const &profileName : profileNames) {
140 addProfileUsedNames(profileName);
141
142 auto profile = profileManager_->profile(profileName);
143 auto &info = profile->get().info();
144
145 list.append(QString::fromStdString(info.name));
146 list.append(info.exe != IProfile::Info::ManualID
147 ? QString::fromStdString(info.exe)
148 : QLatin1String(""));
149 list.append(toQMLIconPath(info.iconURL));
150 list.append(profile->get().active());
151 list.append(info.hasCustomIcon());
152 }
153
154 emit initProfiles(list);
155 }
156
157 QString ProfileManagerUI::defaultIcon() const
158 {
159 return toQMLIconPath(IProfile::Info::DefaultIconURL.data());
160 }
161
162 bool ProfileManagerUI::isProfileNameInUse(QString const &profileName)
163 {
164 return usedProfileNames_.contains(profileName);
165 }
166
167 bool ProfileManagerUI::isExecutableNameInUse(QString const &executableName)
168 {
169 return usedExecutableNames_.contains(executableName);
170 }
171
172 bool ProfileManagerUI::isProfileUnsaved(QString const &profileName)
173 {
174 return profileManager_->unsaved(profileName.toStdString());
175 }
176
177 bool ProfileManagerUI::isProfileActive(QString const &profileName)
178 {
179 auto profile = profileManager_->profile(profileName.toStdString());
180 if (profile.has_value())
181 return profile->get().active();
182
183 return false;
184 }
185
186 void ProfileManagerUI::add(QString const &name, QString const &exe,
187 QString const &icon, QString const &base)
188 {
189 IProfile::Info info(name.toStdString(),
190 exe.length() > 0 ? exe.toStdString()
191 : std::string(IProfile::Info::ManualID));
192 info.iconURL = cleanIconFilePath(icon);
193
194 if (base == "defaultProfile")
195 profileManager_->add(std::move(info));
196 else
197 profileManager_->clone(std::move(info), base.toStdString());
198 }
199
200 void ProfileManagerUI::remove(QString const &name)
201 {
202 auto profileName = name.toStdString();
203 removeProfileUsedNames(profileName);
204 profileManager_->remove(profileName);
205 }
206
207 void ProfileManagerUI::updateInfo(QString const &oldName, QString const &newName,
208 QString const &exe, QString const &icon)
209 {
210 auto profileName = oldName.toStdString();
211 removeProfileUsedNames(profileName);
212 IProfile::Info info(newName.toStdString(),
213 exe.length() > 0 ? exe.toStdString()
214 : std::string(IProfile::Info::ManualID),
215 cleanIconFilePath(icon));
216 profileManager_->update(profileName, info);
217 }
218
219 void ProfileManagerUI::activate(QString const &name, bool active)
220 {
221 profileManager_->activate(name.toStdString(), active);
222 }
223
224 void ProfileManagerUI::toggleManualProfile(QString const &name)
225 {
226 session_->toggleManualProfile(name.toStdString());
227 }
228
229 void ProfileManagerUI::loadSettings(QString const &name)
230 {
231 auto profile = profileManager_->profile(name.toStdString());
232 if (profile.has_value())
233 profile->get().exportWith(*sysModelUI_);
234 }
235
236 bool ProfileManagerUI::loadSettings(QString const &name, QUrl const &path)
237 {
238 if (profileManager_->loadFrom(
239 name.toStdString(), path.toString(QUrl::RemoveScheme).toStdString())) {
240 loadSettings(name);
241
242 return true;
243 }
244
245 return false;
246 }
247
248 void ProfileManagerUI::applySettings(QString const &name)
249 {
250 profileManager_->update(name.toStdString(), *sysModelUI_);
251 }
252
253 void ProfileManagerUI::resetSettings(QString const &name)
254 {
255 profileManager_->reset(name.toStdString());
256 loadSettings(name);
257 }
258
259 void ProfileManagerUI::restoreSettings(QString const &name)
260 {
261 profileManager_->restore(name.toStdString());
262 loadSettings(name);
263 }
264
265 void ProfileManagerUI::saveSettings(QString const &name)
266 {
267 profileManager_->save(name.toStdString());
268 }
269
270 bool ProfileManagerUI::exportProfile(QString const &name, QUrl const &path)
271 {
272 return profileManager_->exportTo(
273 name.toStdString(), path.toString(QUrl::RemoveScheme).toStdString());
274 }
275
276 std::string ProfileManagerUI::cleanIconFilePath(QString iconPath) const
277 {
278 if (iconPath.startsWith("file://"))
279 iconPath.remove("file://");
280 else if (iconPath.startsWith("qrc:"))
281 iconPath.remove(0, 3);
282
283 return iconPath.toStdString();
284 }
285
286 QString ProfileManagerUI::toQMLIconPath(std::string const &iconPath) const
287 {
288 auto qtIconPath = QString::fromStdString(iconPath);
289 if (qtIconPath.startsWith(":/"))
290 qtIconPath.prepend("qrc");
291 else
292 qtIconPath.prepend("file://");
293
294 return qtIconPath;
295 }
296
297 void ProfileManagerUI::addProfileComponet(std::string const &profileName)
298 {
299 addProfileUsedNames(profileName);
300
301 auto profile = profileManager_->profile(profileName);
302 auto &info = profile->get().info();
303 emit profileAdded(QString::fromStdString(info.name),
304 info.exe != IProfile::Info::ManualID
305 ? QString::fromStdString(info.exe)
306 : QLatin1String(""),
307 toQMLIconPath(info.iconURL), info.hasCustomIcon(),
308 profile->get().active());
309 }
310
311 void ProfileManagerUI::addProfileUsedNames(std::string const &profileName)
312 {
313 auto profile = profileManager_->profile(profileName);
314 auto &profileInfo = profile->get().info();
315
316 auto name = QString::fromStdString(profileName);
317 usedProfileNames_.insert(name);
318
319 if (profileInfo.exe != IProfile::Info::ManualID) {
320 auto exe = QString::fromStdString(profileInfo.exe);
321 usedExecutableNames_.insert(exe);
322 }
323 }
324
325 void ProfileManagerUI::removeProfileUsedNames(std::string const &profileName)
326 {
327 auto profile = profileManager_->profile(profileName);
328 auto &profileInfo = profile->get().info();
329
330 auto name = QString::fromStdString(profileName);
331 usedProfileNames_.remove(name);
332
333 if (profileInfo.exe != IProfile::Info::ManualID) {
334 auto exe = QString::fromStdString(profileInfo.exe);
335 usedExecutableNames_.remove(exe);
336 }
337 }
338
339 bool const ProfileManagerUI::registered_ =
340 QMLComponentRegistry::addQMLTypeRegisterer([]() {
341 qmlRegisterType<ProfileManagerUI>("CoreCtrl.UIComponents", 1, 0,
342 ProfileManagerUI::QMLComponentID.data());
343 });