Debian Codegraph @

liblomiri-app-launch/app-store-click.cpp

raw
1 /*
2 * Copyright © 2017 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ted Gould <[email protected]>
18 */
19
20 #include "app-store-click.h"
21 #include "application-impl-click.h"
22 #include "registry-impl.h"
23
24 #include <algorithm>
25
26 namespace lomiri
27 {
28 namespace app_launch
29 {
30 namespace app_store
31 {
32
33 std::list<AppID::AppName> manifestApps(const std::shared_ptr<JsonObject>& manifest);
34 AppID::Version manifestVersion(const std::shared_ptr<JsonObject>& manifest);
35
36 Click::Click(const std::shared_ptr<Registry::Impl>& registry)
37 : Base(registry)
38 {
39 }
40
41 Click::~Click()
42 {
43 }
44
45 /** Tries to get the Click manifest for a package. If it can successfully
46 get the manifest returns true.
47
48 \param package Name of the package
49 */
50 bool Click::verifyPackage(const AppID::Package& package)
51 {
52 return getReg()->getClickManifest(package) != nullptr;
53 }
54
55 /** Verifies the applicaiton name by getting the list of applications
56 in the package manifest and seeing if the appname is in the list.
57
58 \param package Name of the package
59 \param appname Name of the application
60 */
61 bool Click::verifyAppname(const AppID::Package& package,
62 const AppID::AppName& appname)
63 {
64 auto manifest = getReg()->getClickManifest(package);
65 auto apps = manifestApps(manifest);
66
67 return std::find_if(apps.begin(), apps.end(), [&appname](const AppID::AppName& listApp) -> bool {
68 return appname.value() == listApp.value();
69 }) != apps.end();
70 }
71
72 /** Finds an application name based on a wildcard search. Gets the list
73 from the manifest, and then returns a value from that list.
74
75 \param package Name of the package
76 \param card Wildcard to search as
77 */
78 AppID::AppName Click::findAppname(const AppID::Package& package,
79 AppID::ApplicationWildcard card)
80 {
81 auto manifest = getReg()->getClickManifest(package);
82 auto apps = manifestApps(manifest);
83
84 if (apps.empty())
85 {
86 throw std::runtime_error("No apps in package '" + package.value() + "' to find");
87 }
88
89 switch (card)
90 {
91 case AppID::ApplicationWildcard::FIRST_LISTED:
92 return *apps.begin();
93 case AppID::ApplicationWildcard::LAST_LISTED:
94 return *apps.rbegin();
95 case AppID::ApplicationWildcard::ONLY_LISTED:
96 if (apps.size() != 1)
97 {
98 throw std::runtime_error("More than a single app in package '" + package.value() +
99 "' when requested to find only app");
100 }
101 return *apps.begin();
102 }
103
104 throw std::logic_error("Got a value of the app wildcard enum that can't exist");
105 }
106
107 /** Find the version of a package that that is requested
108
109 \param package Name of the package
110 \param appname Name of the application (not used)
111 */
112 AppID::Version Click::findVersion(const AppID::Package& package,
113 const AppID::AppName& appname)
114 {
115 auto manifest = getReg()->getClickManifest(package);
116 return manifestVersion(manifest);
117 }
118
119 /** Check to see if this AppID has a desktop file that is in our link
120 farm built by Click. Click puts a symoblic link there for every
121 valid AppID.
122
123 \param appid Application ID to check
124 */
125 bool Click::hasAppId(const AppID& appid)
126 {
127 std::string appiddesktop = std::string(appid) + ".desktop";
128 gchar* click_link = nullptr;
129 const gchar* link_farm_dir = g_getenv("LOMIRI_APP_LAUNCH_LINK_FARM");
130 if (G_LIKELY(link_farm_dir == nullptr))
131 {
132 click_link =
133 g_build_filename(g_get_user_cache_dir(), "lomiri-app-launch", "desktop", appiddesktop.c_str(), (gchar *)NULL);
134 }
135 else
136 {
137 click_link = g_build_filename(link_farm_dir, appiddesktop.c_str(), (gchar *)NULL);
138 }
139
140 bool click = g_file_test(click_link, G_FILE_TEST_EXISTS);
141 g_free(click_link);
142
143 return click;
144 }
145
146 std::list<std::shared_ptr<Application>> Click::list()
147 {
148 std::list<std::shared_ptr<Application>> applist;
149
150 try
151 {
152 for (auto pkg : getReg()->getClickPackages())
153 {
154 try
155 {
156 auto manifest = getReg()->getClickManifest(pkg);
157
158 for (auto appname : manifestApps(manifest))
159 {
160 try
161 {
162 AppID appid{pkg, appname, manifestVersion(manifest)};
163 auto app = std::make_shared<app_impls::Click>(appid, manifest, getReg());
164 applist.emplace_back(app);
165 }
166 catch (std::runtime_error& e)
167 {
168 g_debug("Unable to create Click for application '%s' in package '%s': %s",
169 appname.value().c_str(), pkg.value().c_str(), e.what());
170 }
171 }
172 }
173 catch (std::runtime_error& e)
174 {
175 g_debug("Unable to get information to build Click app on package '%s': %s", pkg.value().c_str(),
176 e.what());
177 }
178 }
179 }
180 catch (std::runtime_error& e)
181 {
182 g_debug("Unable to get packages from Click database: %s", e.what());
183 }
184
185 return applist;
186 }
187
188 std::shared_ptr<app_impls::Base> Click::create(const AppID& appid)
189 {
190 return std::make_shared<app_impls::Click>(appid, getReg());
191 }
192
193 std::list<AppID::AppName> manifestApps(const std::shared_ptr<JsonObject>& manifest)
194 {
195 JsonObject* hooks = nullptr;
196 if (!json_object_has_member(manifest.get(), "hooks") ||
197 (hooks = json_object_get_object_member(manifest.get(), "hooks")) == nullptr)
198 {
199 throw std::runtime_error("Manifest does not have a 'hooks' field: " + Registry::Impl::printJson(manifest));
200 }
201
202 auto gapps = json_object_get_members(hooks);
203 if (gapps == nullptr)
204 {
205 throw std::runtime_error("GLib JSON confusion, please talk to your library vendor");
206 }
207
208 std::list<AppID::AppName> apps;
209
210 for (GList* item = gapps; item != nullptr; item = g_list_next(item))
211 {
212 auto appname = (const gchar*)item->data;
213
214 auto hooklist = json_object_get_object_member(hooks, appname);
215
216 if (json_object_has_member(hooklist, "desktop") == TRUE)
217 {
218 apps.emplace_back(AppID::AppName::from_raw(appname));
219 }
220 }
221
222 g_list_free(gapps);
223 return apps;
224 }
225
226 AppID::Version manifestVersion(const std::shared_ptr<JsonObject>& manifest)
227 {
228 const gchar* cstr = nullptr;
229 if (!json_object_has_member(manifest.get(), "version") ||
230 (cstr = json_object_get_string_member(manifest.get(), "version")) == nullptr)
231 {
232 throw std::runtime_error("Unable to find version number in manifest: " + Registry::Impl::printJson(manifest));
233 }
234
235 auto cppstr = AppID::Version::from_raw(cstr);
236 return cppstr;
237 }
238
239 } // namespace app_store
240 } // namespace app_launch
241 } // namespace lomiri