Debian Codegraph @

liblomiri-app-launch/application-info-desktop.cpp

raw
1 /*
2 * Copyright © 2016 Canonical Ltd.
3 * Copyright © 2022 Guido Berhoerster <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 3, as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranties of
11 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors:
18 * Ted Gould <[email protected]>
19 */
20
21 #include "application-info-desktop.h"
22 #include "application-icon-finder.h"
23 #include "registry-impl.h"
24 #include "string-util.h"
25 #include <algorithm>
26 #include <cstdlib>
27 #include <lomiri/util/GlibMemory.h>
28
29 using namespace lomiri::util;
30
31 namespace lomiri
32 {
33 namespace app_launch
34 {
35 namespace app_info
36 {
37 namespace
38 {
39
40 static std::set<std::string> strvToSet(gchar** strv)
41 {
42 std::set<std::string> retval;
43
44 if (strv != nullptr)
45 {
46 for (int i = 0; strv[i] != nullptr; i++)
47 {
48 if (strv[i][0] != '\0')
49 {
50 retval.emplace(strv[i]);
51 }
52 }
53 }
54
55 return retval;
56 }
57
58 constexpr const char* DESKTOP_GROUP = "Desktop Entry";
59
60 struct TypeTag;
61 typedef TypeTagger<TypeTag, std::string> Type;
62
63 struct HiddenTag;
64 typedef TypeTagger<HiddenTag, bool> Hidden;
65
66 struct NoDisplayTag;
67 typedef TypeTagger<NoDisplayTag, bool> NoDisplay;
68 } // anonymous namespace
69
70 template <typename T>
71 auto stringFromKeyfileRequiredWithFallback(const std::shared_ptr<GKeyFile>& keyfile,
72 const std::string& key,
73 const std::string& fallback,
74 const std::string& exceptionText) -> T
75 {
76 GError* error = nullptr;
77 auto keyval =
78 unique_gchar(g_key_file_get_locale_string(keyfile.get(), DESKTOP_GROUP, key.c_str(), nullptr, &error));
79
80 if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) && !fallback.empty())
81 {
82 g_clear_error(&error);
83
84 keyval.reset(g_key_file_get_locale_string(keyfile.get(), DESKTOP_GROUP, fallback.c_str(), nullptr, &error));
85 }
86
87 if (error != nullptr)
88 {
89 auto perror = unique_glib(error);
90 error = nullptr;
91 if (!exceptionText.empty())
92 {
93 throw std::runtime_error(exceptionText + perror.get()->message);
94 }
95
96 return T::from_raw({});
97 }
98
99 T retval = T::from_raw(keyval.get());
100 return retval;
101 }
102
103 template <typename T>
104 auto stringFromKeyfileRequired(const std::shared_ptr<GKeyFile>& keyfile,
105 const std::string& key,
106 const std::string& exceptionText) -> T
107 {
108 return stringFromKeyfileRequiredWithFallback<T>(keyfile, key, {}, exceptionText);
109 }
110
111 template <typename T>
112 auto stringFromKeyfileWithFallback(const std::shared_ptr<GKeyFile>& keyfile, const std::string& key, const std::string& fallback) -> T
113 {
114 return stringFromKeyfileRequiredWithFallback<T>(keyfile, key, fallback, {});
115 }
116
117 template <typename T>
118 auto stringFromKeyfile(const std::shared_ptr<GKeyFile>& keyfile, const std::string& key) -> T
119 {
120 return stringFromKeyfileRequiredWithFallback<T>(keyfile, key, {}, {});
121 }
122
123 template <typename T>
124 auto fileFromKeyfileRequiredWithFallback(const std::shared_ptr<GKeyFile>& keyfile,
125 const std::string& basePath,
126 const std::string& rootDir,
127 const std::string& key,
128 const std::string& fallback,
129 const std::string& exceptionText) -> T
130 {
131 GError* error = nullptr;
132 auto keyval =
133 unique_gchar(g_key_file_get_locale_string(keyfile.get(), DESKTOP_GROUP, key.c_str(), nullptr, &error));
134
135 if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) && !fallback.empty())
136 {
137 g_clear_error(&error);
138
139 keyval.reset(g_key_file_get_locale_string(keyfile.get(), DESKTOP_GROUP, fallback.c_str(), nullptr, &error));
140 }
141
142 if (error != nullptr)
143 {
144 auto perror = unique_glib(error);
145 if (!exceptionText.empty())
146 {
147 throw std::runtime_error(exceptionText + perror.get()->message);
148 }
149
150 return T::from_raw({});
151 }
152
153 /* If we're already an absolute path, don't prepend the base path */
154 if (keyval.get()[0] == '/')
155 {
156 T retval = T::from_raw(keyval.get());
157
158 if (!rootDir.empty())
159 {
160 auto fullpath = unique_gchar(g_build_filename(rootDir.c_str(), keyval.get(), nullptr));
161 retval = T::from_raw(fullpath.get());
162 }
163
164 return retval;
165 }
166
167 auto cpath = unique_gchar(g_build_filename(basePath.c_str(), keyval.get(), nullptr));
168
169 T retval = T::from_raw(cpath.get());
170
171 return retval;
172 }
173
174 template <typename T>
175 auto fileFromKeyfileRequired(const std::shared_ptr<GKeyFile>& keyfile,
176 const std::string& basePath,
177 const std::string& rootDir,
178 const std::string& key,
179 const std::string& exceptionText) -> T
180 {
181 return fileFromKeyfileRequiredWithFallback<T>(keyfile, basePath, rootDir, key, {}, exceptionText);
182 }
183
184 template <typename T>
185 auto fileFromKeyfileWithFallback(const std::shared_ptr<GKeyFile>& keyfile,
186 const std::string& basePath,
187 const std::string& rootDir,
188 const std::string& key,
189 const std::string& fallback) -> T
190 {
191 return fileFromKeyfileRequiredWithFallback<T>(keyfile, basePath, rootDir, key, fallback, {});
192 }
193
194 template <typename T>
195 auto fileFromKeyfile(const std::shared_ptr<GKeyFile>& keyfile,
196 const std::string& basePath,
197 const std::string& rootDir,
198 const std::string& key) -> T
199 {
200 return fileFromKeyfileRequiredWithFallback<T>(keyfile, basePath, rootDir, key, {}, {});
201 }
202
203 template <typename T>
204 auto boolFromKeyfileRequiredWithFallback(const std::shared_ptr<GKeyFile>& keyfile,
205 const std::string& key,
206 const std::string& fallback,
207 const std::string& exceptionText) -> T
208 {
209 GError* error = nullptr;
210 auto keyval = g_key_file_get_boolean(keyfile.get(), DESKTOP_GROUP, key.c_str(), &error);
211
212 if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) && !fallback.empty())
213 {
214 g_clear_error(&error);
215
216 keyval = g_key_file_get_boolean(keyfile.get(), DESKTOP_GROUP, fallback.c_str(), &error);
217 }
218
219 if (error != nullptr)
220 {
221 auto perror = unique_glib(error);
222 error = nullptr;
223 throw std::runtime_error(exceptionText + perror.get()->message);
224 }
225
226 T retval = T::from_raw(keyval == TRUE);
227 return retval;
228 }
229
230 template <typename T>
231 auto boolFromKeyfileRequired(const std::shared_ptr<GKeyFile>& keyfile,
232 const std::string& key,
233 const std::string& exceptionText) -> T
234 {
235 return boolFromKeyfileRequiredWithFallback<T>(keyfile, key, {}, exceptionText);
236 }
237
238 template <typename T>
239 auto boolFromKeyfile(const std::shared_ptr<GKeyFile>& keyfile, const std::string& key, bool defaultReturn) -> T
240 {
241 try
242 {
243 return boolFromKeyfileRequiredWithFallback<T>(keyfile, key, {}, "Boolean value not available, but not required");
244 }
245 catch (std::runtime_error& e)
246 {
247 return T::from_raw(defaultReturn);
248 }
249 }
250
251 template <typename T>
252 auto boolFromKeyfileWithFallback(const std::shared_ptr<GKeyFile>& keyfile, const std::string& key, const std::string& fallback, bool defaultReturn) -> T
253 {
254 try
255 {
256 return boolFromKeyfileRequiredWithFallback<T>(keyfile, key, fallback, "Boolean value not available, but not required");
257 }
258 catch (std::runtime_error& e)
259 {
260 return T::from_raw(defaultReturn);
261 }
262 }
263
264 template <typename T>
265 auto stringlistFromKeyfileRequiredWithFallback(const std::shared_ptr<GKeyFile>& keyfile,
266 const gchar* key,
267 const gchar* fallback,
268 const std::string& exceptionText) -> T
269 {
270 GError* error = nullptr;
271 auto keyval =
272 unique_gcharv(g_key_file_get_locale_string_list(keyfile.get(), DESKTOP_GROUP, key, nullptr, nullptr, &error));
273
274 if (error != nullptr)
275 {
276 auto perror = unique_glib(error);
277 error = nullptr;
278 if (!exceptionText.empty())
279 {
280 throw std::runtime_error(exceptionText + perror.get()->message);
281 }
282
283 return T::from_raw({});
284 }
285
286 std::vector<std::string> results;
287 for (auto i = 0; keyval.get()[i] != nullptr; ++i)
288 {
289 if (strlen(keyval.get()[i]) != 0)
290 {
291 results.emplace_back(keyval.get()[i]);
292 }
293 }
294
295 return T::from_raw(results);
296 }
297
298 template <typename T>
299 auto stringlistFromKeyfile(const std::shared_ptr<GKeyFile>& keyfile, const gchar* key) -> T
300 {
301 return stringlistFromKeyfileRequiredWithFallback<T>(keyfile, key, {}, {});
302 }
303
304 bool stringlistFromKeyfileContains(const std::shared_ptr<GKeyFile>& keyfile,
305 const gchar* key,
306 const std::string& match,
307 bool defaultValue)
308 {
309 GError* error = nullptr;
310 auto results = unique_gcharv(g_key_file_get_string_list(keyfile.get(), DESKTOP_GROUP, key, nullptr, &error));
311 if (error != nullptr)
312 {
313 g_error_free(error);
314 return defaultValue;
315 }
316
317 bool result = false;
318 for (auto i = 0; results.get()[i] != nullptr; ++i)
319 {
320 if (results.get()[i] == match)
321 {
322 result = true;
323 break;
324 }
325 }
326
327 return result;
328 }
329
330 std::set<std::string> stringlistFromKeyfileSet(const std::shared_ptr<GKeyFile>& keyfile, const gchar* key)
331 {
332 GError* error = nullptr;
333 auto results = unique_gcharv(g_key_file_get_string_list(keyfile.get(), DESKTOP_GROUP, key, nullptr, &error));
334 if (error != nullptr)
335 {
336 g_error_free(error);
337 return {};
338 }
339
340 auto retval = strvToSet(results.get());
341 return retval;
342 }
343
344 Desktop::Desktop(const AppID& appid,
345 const std::shared_ptr<GKeyFile>& keyfile,
346 const std::string& basePath,
347 const std::string& rootDir,
348 std::bitset<3> flags,
349 const std::shared_ptr<Registry::Impl>& registry)
350 : _keyfile([keyfile, flags]() {
351 if (!keyfile)
352 {
353 throw std::runtime_error("Can not build a desktop application info object with a null keyfile");
354 }
355 if (stringFromKeyfile<Type>(keyfile, "Type").value() != "Application")
356 {
357 throw std::runtime_error("Keyfile does not represent application type");
358 }
359 if (boolFromKeyfile<NoDisplay>(keyfile, "NoDisplay", false).value() &&
360 (flags & DesktopFlags::ALLOW_NO_DISPLAY).none())
361 {
362 throw std::runtime_error("Application is not meant to be displayed");
363 }
364 if (boolFromKeyfile<Hidden>(keyfile, "Hidden", false).value())
365 {
366 throw std::runtime_error("Application keyfile is hidden");
367 }
368 auto xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
369 if (xdg_current_desktop != nullptr)
370 {
371 /* Split the CURRENT_DESKTOP by colons if there are multiple */
372 auto current_desktops = unique_gcharv(g_strsplit(xdg_current_desktop, ":", -1));
373 auto cdesktops = strvToSet(current_desktops.get());
374
375 auto onlyshowin = stringlistFromKeyfileSet(keyfile, "OnlyShowIn");
376 auto noshowin = stringlistFromKeyfileSet(keyfile, "NotShowIn");
377
378 auto hasAnyOf = [](std::set<std::string>& a, std::set<std::string>& b) {
379 return std::find_first_of(a.cbegin(), a.cend(), b.cbegin(), b.cend()) != a.cend();
380 };
381
382 if ((!hasAnyOf(onlyshowin, cdesktops) && !onlyshowin.empty()) || hasAnyOf(noshowin, cdesktops))
383 {
384 throw std::runtime_error("Application is not shown in '" + std::string{xdg_current_desktop} + "'");
385 }
386 }
387
388 return keyfile;
389 }())
390 , _basePath(basePath)
391 , _rootDir(rootDir)
392 , _name(stringFromKeyfileRequired<Application::Info::Name>(keyfile, "Name", "Unable to get name from keyfile"))
393 , _description(stringFromKeyfile<Application::Info::Description>(keyfile, "Comment"))
394 , _iconPath([keyfile, basePath, rootDir, registry]() {
395 if (registry != nullptr)
396 {
397 auto iconName = stringFromKeyfile<Application::Info::IconPath>(keyfile, "Icon");
398
399 if (!iconName.value().empty() && iconName.value()[0] != '/')
400 {
401 /* If it is not a direct filename look it up */
402 return registry->getIconFinder(basePath)->find(iconName);
403 }
404 }
405 return fileFromKeyfile<Application::Info::IconPath>(keyfile, basePath, rootDir, "Icon");
406 }())
407 , _defaultDepartment(
408 stringFromKeyfileWithFallback<Application::Info::DefaultDepartment>(keyfile, "X-Lomiri-Default-Department-ID", "X-Ubuntu-Default-Department-ID"))
409 , _screenshotPath([keyfile, basePath, rootDir]() {
410 return fileFromKeyfile<Application::Info::IconPath>(keyfile, basePath, rootDir, "X-Screenshot");
411 }())
412 , _keywords(stringlistFromKeyfile<Application::Info::Keywords>(keyfile, "Keywords"))
413 , _popularity([registry, appid] {
414 if (registry)
415 return registry->getZgWatcher()->lookupAppPopularity(appid);
416 else
417 return Application::Info::Popularity::from_raw(0);
418 }())
419 , _splashInfo(
420 {stringFromKeyfileWithFallback<Application::Info::Splash::Title>(keyfile, "X-Lomiri-Splash-Title", "X-Ubuntu-Splash-Title"),
421 fileFromKeyfileWithFallback<Application::Info::Splash::Image>(keyfile, basePath, rootDir, "X-Lomiri-Splash-Image", "X-Ubuntu-Splash-Image"),
422 stringFromKeyfileWithFallback<Application::Info::Splash::Color>(keyfile, "X-Lomiri-Splash-Color", "X-Ubuntu-Splash-Color"),
423 stringFromKeyfileWithFallback<Application::Info::Splash::Color>(keyfile, "X-Lomiri-Splash-Color-Header", "X-Ubuntu-Splash-Color-Header"),
424 stringFromKeyfileWithFallback<Application::Info::Splash::Color>(keyfile, "X-Lomiri-Splash-Color-Footer", "X-Ubuntu-Splash-Color-Footer"),
425 boolFromKeyfileWithFallback<Application::Info::Splash::ShowHeader>(keyfile, "X-Lomiri-Splash-Show-Header", "X-Ubuntu-Splash-Show-Header", false),
426 // This can either be X-Ubuntu-Splash-Show or No Display, but will always prefer X-Ubuntu-Splash-Show
427 boolFromKeyfileWithFallback<Application::Info::Splash::Show>(keyfile, "X-Lomiri-Splash-Show", "X-Ubuntu-Splash-Show",
428 (flags & DesktopFlags::NO_SPLASH).any() ? false :
429 boolFromKeyfileWithFallback<Application::Info::LomiriLifecycle>(keyfile, "X-Lomiri-Touch", "X-Ubuntu-Touch",
430 boolFromKeyfile<NoDisplay>(keyfile, "NoDisplay", false).value()).value())})
431 , _supportedOrientations([keyfile]() {
432 Orientations all = {true, true, true, true};
433
434 GError* error = nullptr;
435 auto orientationStrv = unique_gcharv(g_key_file_get_string_list(
436 keyfile.get(), DESKTOP_GROUP, "X-Lomiri-Supported-Orientations", nullptr, &error));
437
438 if (error != nullptr)
439 {
440 g_error_free(error);
441 error = nullptr;
442
443 orientationStrv.reset(g_key_file_get_string_list(
444 keyfile.get(), DESKTOP_GROUP, "X-Ubuntu-Supported-Orientations", nullptr, &error));
445 }
446
447 if (error != nullptr)
448 {
449 g_error_free(error);
450 return all;
451 }
452
453 Orientations retval = {false, false, false, false};
454
455 try
456 {
457 for (auto i = 0; orientationStrv.get()[i] != nullptr; i++)
458 {
459 auto item = orientationStrv.get()[i];
460 g_strstrip(item); /* remove whitespace */
461
462 if (g_ascii_strcasecmp("portrait", item) == 0)
463 {
464 retval.portrait = true;
465 }
466 else if (g_ascii_strcasecmp("landscape", item) == 0)
467 {
468 retval.landscape = true;
469 }
470 else if (g_ascii_strcasecmp("invertedPortrait", item) == 0)
471 {
472 retval.invertedPortrait = true;
473 }
474 else if (g_ascii_strcasecmp("invertedLandscape", item) == 0)
475 {
476 retval.invertedLandscape = true;
477 }
478 else if (g_ascii_strcasecmp("primary", item) == 0 && i == 0)
479 {
480 /* Pass, we'll let primary be the first entry, it should be the only. */
481 }
482 else
483 {
484 throw std::runtime_error("Invalid orientation string '" + std::string(item) + "'");
485 }
486 }
487 }
488 catch (...)
489 {
490 retval = all;
491 }
492
493 return retval;
494 }())
495 , _rotatesWindow(
496 boolFromKeyfileWithFallback<Application::Info::RotatesWindow>(keyfile, "X-Lomiri-Rotates-Window-Contents", "X-Ubuntu-Rotates-Window-Contents", false))
497 , _lomiriLifecycle(boolFromKeyfileWithFallback<Application::Info::LomiriLifecycle>(keyfile, "X-Lomiri-Touch", "X-Ubuntu-Touch", false))
498 , _xMirEnable(app_info::Desktop::XMirEnable::from_raw(false))
499 , _exec(stringFromKeyfile<Exec>(keyfile, "Exec"))
500 , _singleInstance(boolFromKeyfileWithFallback<SingleInstance>(keyfile, "X-Lomiri-Single-Instance", "X-Ubuntu-Single-Instance", false))
501 {
502 }
503
504 } // namespace app_info
505 } // namespace app_launch
506 } // namespace lomiri