Debian Codegraph @

liblomiri-app-launch/app-store-legacy.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-legacy.h"
21 #include "application-impl-legacy.h"
22 #include "registry-impl.h"
23 #include "string-util.h"
24
25 #include <regex>
26
27 namespace lomiri
28 {
29 namespace app_launch
30 {
31 namespace app_store
32 {
33
34 Legacy::Legacy(const std::shared_ptr<Registry::Impl>& registry)
35 : Base(registry)
36 {
37 }
38
39 Legacy::~Legacy()
40 {
41 }
42
43 /** Checks the AppID by ensuring the version and package are empty
44 then looks for the application.
45
46 \param appid AppID to check
47 \param registry persistent connections to use
48 */
49 bool Legacy::hasAppId(const AppID& appid)
50 {
51 try
52 {
53 if (!appid.version.value().empty())
54 {
55 return false;
56 }
57
58 return verifyAppname(appid.package, appid.appname);
59 }
60 catch (std::runtime_error& e)
61 {
62 return false;
63 }
64 }
65
66 /** Ensure the package is empty
67
68 \param package Container name
69 \param registry persistent connections to use
70 */
71 bool Legacy::verifyPackage(const AppID::Package& package)
72 {
73 return package.value().empty();
74 }
75
76 /** Looks for an application by looking through the system and user
77 application directories to find the desktop file.
78
79 \param package Container name
80 \param appname Application name to look for
81 \param registry persistent connections to use
82 */
83 bool Legacy::verifyAppname(const AppID::Package& package, const AppID::AppName& appname)
84 {
85 if (!verifyPackage(package))
86 {
87 throw std::runtime_error{"Invalid Legacy package: " + std::string(package)};
88 }
89
90 auto desktop = std::string(appname) + ".desktop";
91 auto evaldir = [&desktop](const gchar* dir) {
92 auto fulldir = unique_gchar(g_build_filename(dir, "applications", desktop.c_str(), nullptr));
93 gboolean found = g_file_test(fulldir.get(), G_FILE_TEST_EXISTS);
94 return found == TRUE;
95 };
96
97 if (evaldir(g_get_user_data_dir()))
98 {
99 return true;
100 }
101
102 auto&& data_dirs = g_get_system_data_dirs();
103 for (int i = 0; data_dirs[i] != nullptr; i++)
104 {
105 if (evaldir(data_dirs[i]))
106 {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114 /** We don't really have a way to implement this for Legacy, any
115 search wouldn't really make sense. We just throw an error.
116
117 \param package Container name
118 \param card Application search paths
119 \param registry persistent connections to use
120 */
121 AppID::AppName Legacy::findAppname(const AppID::Package& package, AppID::ApplicationWildcard card)
122 {
123 throw std::runtime_error("Legacy apps can't be discovered by package");
124 }
125
126 /** Function to return an empty string
127
128 \param package Container name (unused)
129 \param appname Application name (unused)
130 \param registry persistent connections to use (unused)
131 */
132 AppID::Version Legacy::findVersion(const AppID::Package& package, const AppID::AppName& appname)
133 {
134 return AppID::Version::from_raw({});
135 }
136
137 static const std::regex desktop_remover("^(.*)\\.desktop$");
138
139 std::list<std::shared_ptr<Application>> Legacy::list()
140 {
141 auto reg = getReg();
142 std::list<std::shared_ptr<Application>> list;
143 std::unique_ptr<GList, decltype(&g_list_free)> head(g_app_info_get_all(),
144 [](GList* l) { g_list_free_full(l, g_object_unref); });
145 for (GList* item = head.get(); item != nullptr; item = g_list_next(item))
146 {
147 GDesktopAppInfo* appinfo = G_DESKTOP_APP_INFO(item->data);
148
149 if (appinfo == nullptr)
150 {
151 continue;
152 }
153
154 if (g_app_info_should_show(G_APP_INFO(appinfo)) == FALSE)
155 {
156 continue;
157 }
158
159 auto desktopappid = std::string(g_app_info_get_id(G_APP_INFO(appinfo)));
160 std::string appname;
161 std::smatch match;
162 if (std::regex_match(desktopappid, match, desktop_remover))
163 {
164 appname = match[1].str();
165 }
166 else
167 {
168 continue;
169 }
170
171 /* Remove entries generated by the desktop hook in .local */
172 if (g_desktop_app_info_has_key(appinfo, "X-Lomiri-Application-ID"))
173 {
174 continue;
175 }
176
177 try
178 {
179 auto app = std::make_shared<app_impls::Legacy>(AppID::AppName::from_raw(appname), reg);
180 list.push_back(app);
181 }
182 catch (std::runtime_error& e)
183 {
184 g_debug("Unable to create application for legacy appname '%s': %s", appname.c_str(), e.what());
185 }
186 }
187
188 return list;
189 }
190
191 std::shared_ptr<app_impls::Base> Legacy::create(const AppID& appid)
192 {
193 return std::make_shared<app_impls::Legacy>(appid.appname, getReg());
194 }
195
196 /** Turns a directory changed event from a file monitor into an
197 * internal signal. Makes sure we can deal with it first, and
198 * then propegates up the stack. */
199 void Legacy::directoryChanged(GFile* file, GFileMonitorEvent type)
200 {
201 g_debug("Getting event for '%s'", unique_gchar(g_file_get_path(file)).get());
202
203 auto filetype = g_file_query_file_type(file, G_FILE_QUERY_INFO_NONE, nullptr);
204 if (filetype != G_FILE_TYPE_REGULAR && filetype != G_FILE_TYPE_UNKNOWN)
205 {
206 g_debug("\tNot a regular file");
207 return;
208 }
209
210 auto cdesktopname = unique_gchar(g_file_get_basename(file));
211 if (!cdesktopname)
212 {
213 g_debug("\tNo basename");
214 return;
215 }
216 std::string desktopname{cdesktopname.get()};
217
218 std::string appname;
219 std::smatch match;
220 if (std::regex_match(desktopname, match, desktop_remover))
221 {
222 appname = match[1].str();
223 }
224 else
225 {
226 return;
227 }
228
229 auto reg = getReg();
230
231 switch (type)
232 {
233 case G_FILE_MONITOR_EVENT_CREATED:
234 {
235 auto app = std::make_shared<app_impls::Legacy>(AppID::AppName::from_raw(appname), reg);
236
237 appAdded_(app);
238 break;
239 }
240 case G_FILE_MONITOR_EVENT_CHANGED:
241 {
242 auto app = std::make_shared<app_impls::Legacy>(AppID::AppName::from_raw(appname), reg);
243
244 infoChanged_(app);
245 break;
246 }
247 case G_FILE_MONITOR_EVENT_DELETED:
248 {
249 AppID appid{AppID::Package::from_raw({}), AppID::AppName::from_raw(appname), AppID::Version::from_raw({})};
250 if (verifyAppname(appid.package, appid.appname))
251 {
252 /* Check to see if we've got a shadow situation and we
253 * can still build this app */
254 auto app = std::make_shared<app_impls::Legacy>(AppID::AppName::from_raw(appname), reg);
255 infoChanged_(app);
256 }
257 else
258 {
259 appRemoved_(appid);
260 }
261 break;
262 }
263 default:
264 break;
265 };
266 }
267
268 /** Function that setups file monitors on all of the system application
269 * directories and the user application directory. Any time an application
270 * is added or removed or changed we send the appropriate signal up the
271 * stack. */
272 void Legacy::setupMonitors()
273 {
274 std::call_once(monitorsSetup_, [this]() {
275 auto reg = getReg();
276 monitors_ =
277 reg->thread.executeOnThread<std::set<std::unique_ptr<GFileMonitor, lomiri::util::GObjectDeleter>>>([this]() {
278 std::set<std::unique_ptr<GFileMonitor, lomiri::util::GObjectDeleter>> monitors;
279
280 auto monitorDir = [this](const gchar* dirname) {
281 auto appdir = unique_gchar(g_build_filename(dirname, "applications", nullptr));
282 auto gfile = lomiri::util::unique_gobject(g_file_new_for_path(appdir.get()));
283
284 if (!g_file_query_exists(gfile.get(), nullptr))
285 {
286 throw std::runtime_error{std::string{"Directory '"} + appdir.get() + "' doesn't exist"};
287 }
288
289 if (g_file_query_file_type(gfile.get(), G_FILE_QUERY_INFO_NONE, nullptr) != G_FILE_TYPE_DIRECTORY)
290 {
291 throw std::runtime_error{std::string{"'"} + appdir.get() + "' is not a directory"};
292 }
293
294 GError* error = nullptr;
295 auto monitor = lomiri::util::unique_gobject(
296 g_file_monitor_directory(gfile.get(), G_FILE_MONITOR_NONE, nullptr, &error));
297
298 if (error != nullptr)
299 {
300 std::string message = std::string{"Unable to create file monitor: "} + error->message;
301 g_error_free(error);
302 throw std::runtime_error{message};
303 }
304
305 g_signal_connect(
306 monitor.get(), "changed",
307 G_CALLBACK(+[](GFileMonitor*, GFile* file, GFile*, GFileMonitorEvent type, gpointer user_data) {
308 auto pthis = static_cast<Legacy*>(user_data);
309 pthis->directoryChanged(file, type);
310 }),
311 this);
312
313 return monitor;
314 };
315
316 auto dirs = g_get_system_data_dirs();
317 for (int i = 0; dirs != nullptr && dirs[i] != nullptr; i++)
318 {
319 try
320 {
321 monitors.insert(monitorDir(dirs[i]));
322 }
323 catch (std::runtime_error& e)
324 {
325 g_debug("Unable to create directory monitor for system dir '%s': %s", dirs[i], e.what());
326 }
327 }
328
329 try
330 {
331 monitors.insert(monitorDir(g_get_user_data_dir()));
332 }
333 catch (std::runtime_error& e)
334 {
335 g_debug("Unable to create directory monitor for user data dir: %s", e.what());
336 }
337
338 return monitors;
339 });
340 });
341 }
342
343 /** Return the signal object, but make sure we have the
344 * monitors setup first */
345 core::Signal<const std::shared_ptr<Application>&>& Legacy::infoChanged()
346 {
347 setupMonitors();
348 return infoChanged_;
349 }
350
351 /** Return the signal object, but make sure we have the
352 * monitors setup first */
353 core::Signal<const std::shared_ptr<Application>&>& Legacy::appAdded()
354 {
355 setupMonitors();
356 return appAdded_;
357 }
358
359 /** Return the signal object, but make sure we have the
360 * monitors setup first */
361 core::Signal<const AppID&>& Legacy::appRemoved()
362 {
363 setupMonitors();
364 return appRemoved_;
365 }
366
367 } // namespace app_store
368 } // namespace app_launch
369 } // namespace lomiri