Debian Codegraph @

src/core/profileiconcache.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "profileiconcache.h"
5
6 #include "ifilecache.h"
7 #include <spdlog/spdlog.h>
8 #include <utility>
9
10 ProfileIconCache::ProfileIconCache(std::unique_ptr<IFileCache> &&cache) noexcept
11 : cache_(std::move(cache))
12 {
13 }
14
15 void ProfileIconCache::init()
16 {
17 cache_->init();
18 }
19
20 bool ProfileIconCache::tryOrCache(IProfile::Info &info,
21 std::vector<char> const &fallbackIcon)
22 {
23 auto fileName = info.exe != IProfile::Info::ManualID ? info.exe
24 : info.exe + info.name;
25 // try the cache
26 auto cacheURL = cache_->get(fileName);
27 if (cacheURL.has_value()) {
28 if (info.iconURL != *cacheURL)
29 info.iconURL = *cacheURL;
30 return true;
31 }
32
33 // cache fallback icon
34 return cache(info, fallbackIcon);
35 }
36
37 bool ProfileIconCache::cache(IProfile::Info &info,
38 std::vector<char> const &iconData)
39 {
40 auto cacheURL = cacheIconFromData(iconData, info);
41 if (cacheURL.has_value()) {
42 info.iconURL = *cacheURL;
43 return true;
44 }
45
46 return false;
47 }
48
49 std::pair<bool, bool> ProfileIconCache::syncCache(IProfile::Info &info)
50 {
51 auto fileName = info.exe != IProfile::Info::ManualID ? info.exe
52 : info.exe + info.name;
53 auto cacheURL = cache_->add(info.iconURL, fileName);
54 if (cacheURL.has_value()) {
55 auto updateURL = info.iconURL != *cacheURL;
56 if (updateURL)
57 info.iconURL = *cacheURL;
58
59 return {true, updateURL};
60 }
61
62 SPDLOG_DEBUG("Failed to cache icon for {}", fileName.data());
63 return {false, false};
64 }
65
66 void ProfileIconCache::clean(IProfile::Info &info)
67 {
68 auto fileName = info.exe != IProfile::Info::ManualID ? info.exe
69 : info.exe + info.name;
70 cache_->remove(fileName);
71 }
72
73 std::optional<std::filesystem::path>
74 ProfileIconCache::cacheIconFromData(std::vector<char> const &iconData,
75 IProfile::Info const &info) const
76 {
77 auto fileName = info.exe != IProfile::Info::ManualID ? info.exe
78 : info.exe + info.name;
79 auto cacheURL = cache_->add(iconData, fileName);
80 if (cacheURL.has_value())
81 return cacheURL;
82
83 SPDLOG_DEBUG("Failed to cache icon for {}", fileName.data());
84 return {};
85 }