Debian Codegraph @

liblomiri-app-launch/utils.c

raw
1 /*
2 * Copyright 2013 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 <json-glib/json-glib.h>
21 #include <click.h>
22 #include "utils.h"
23
24 /* Take an app ID and validate it and then break it up
25 and spit it out. These are newly allocated strings */
26 gboolean
27 app_id_to_triplet (const gchar * app_id, gchar ** package, gchar ** application, gchar ** version)
28 {
29 /* 'Parse' the App ID */
30 gchar ** app_id_segments = g_strsplit(app_id, "_", 4);
31 if (g_strv_length(app_id_segments) != 3) {
32 g_debug("Unable to parse Application ID: %s", app_id);
33 g_strfreev(app_id_segments);
34 return FALSE;
35 }
36
37 if (package != NULL) {
38 *package = app_id_segments[0];
39 } else {
40 g_free(app_id_segments[0]);
41 }
42
43 if (application != NULL) {
44 *application = app_id_segments[1];
45 } else {
46 g_free(app_id_segments[1]);
47 }
48
49 if (version != NULL) {
50 *version = app_id_segments[2];
51 } else {
52 g_free(app_id_segments[2]);
53 }
54
55 g_free(app_id_segments);
56 return TRUE;
57 }
58
59 /* Take a manifest, parse it, find the application and
60 and then return the path to the desktop file */
61 gchar *
62 manifest_to_desktop (const gchar * app_dir, const gchar * app_id)
63 {
64 gchar * package = NULL;
65 gchar * application = NULL;
66 gchar * version = NULL;
67 ClickDB * db = NULL;
68 ClickUser * user = NULL;
69 JsonObject * manifest = NULL;
70 gchar * desktoppath = NULL;
71 GError * error = NULL;
72
73 if (!app_id_to_triplet(app_id, &package, &application, &version)) {
74 g_warning("Unable to parse triplet: %s", app_id);
75 return NULL;
76 }
77
78 db = click_db_new();
79 /* If TEST_CLICK_DB is unset, this reads the system database. */
80 click_db_read(db, g_getenv("TEST_CLICK_DB"), &error);
81 if (error != NULL) {
82 g_warning("Unable to read Click database: %s", error->message);
83 goto manifest_out;
84 }
85 /* If TEST_CLICK_USER is unset, this uses the current user name. */
86 user = click_user_new_for_user(db, g_getenv("TEST_CLICK_USER"), &error);
87 if (error != NULL) {
88 g_warning("Unable to read Click database: %s", error->message);
89 goto manifest_out;
90 }
91 manifest = click_user_get_manifest(user, package, &error);
92 if (error != NULL) {
93 g_warning("Unable to get manifest for '%s': %s", package, error->message);
94 goto manifest_out;
95 }
96
97 if (!json_object_has_member(manifest, "version")) {
98 g_warning("Manifest '%s' doesn't have a version", package);
99 goto manifest_out;
100 }
101
102 if (g_strcmp0(json_object_get_string_member(manifest, "version"), version) != 0) {
103 g_warning("Manifest '%s' version '%s' doesn't match AppID version '%s'", package, json_object_get_string_member(manifest, "version"), version);
104 goto manifest_out;
105 }
106
107 if (!json_object_has_member(manifest, "hooks")) {
108 g_warning("Manifest '%s' doesn't have an hooks section", package);
109 goto manifest_out;
110 }
111
112 JsonObject * appsobj = json_object_get_object_member(manifest, "hooks");
113 if (appsobj == NULL) {
114 g_warning("Manifest '%s' has an hooks section that is not a JSON object", package);
115 goto manifest_out;
116 }
117
118 if (!json_object_has_member(appsobj, application)) {
119 g_warning("Manifest '%s' doesn't have the application '%s' defined", package, application);
120 goto manifest_out;
121 }
122
123 JsonObject * appobj = json_object_get_object_member(appsobj, application);
124 if (appobj == NULL) {
125 g_warning("Manifest '%s' has a definition for application '%s' that is not an object", package, application);
126 goto manifest_out;
127 }
128
129 gchar * filename = NULL;
130 if (json_object_has_member(appobj, "desktop")) {
131 filename = g_strdup(json_object_get_string_member(appobj, "desktop"));
132 } else {
133 filename = g_strdup_printf("%s.desktop", application);
134 }
135
136 desktoppath = g_build_filename(app_dir, filename, NULL);
137 g_free(filename);
138
139 if (!g_file_test(desktoppath, G_FILE_TEST_EXISTS)) {
140 g_warning("Application desktop file '%s' doesn't exist", desktoppath);
141 g_free(desktoppath);
142 desktoppath = NULL;
143 }
144
145 manifest_out:
146 if (error != NULL)
147 g_error_free(error);
148 if (manifest != NULL)
149 json_object_unref(manifest);
150 g_clear_object(&user);
151 g_clear_object(&db);
152 g_free(package);
153 g_free(application);
154 g_free(version);
155
156 return desktoppath;
157 }
158
159 /* Take a desktop file, make sure that it makes sense and
160 then return the exec line */
161 gchar *
162 desktop_to_exec (GKeyFile * desktop_file, const gchar * from)
163 {
164 GError * error = NULL;
165
166 if (!g_key_file_has_group(desktop_file, "Desktop Entry")) {
167 g_warning("Desktop file '%s' does not have a 'Desktop Entry' group", from);
168 return NULL;
169 }
170
171 gchar * type = g_key_file_get_string(desktop_file, "Desktop Entry", "Type", &error);
172 if (error != NULL) {
173 g_warning("Desktop file '%s' unable to get type: %s", from, error->message);
174 g_error_free(error);
175 g_free(type);
176 return NULL;
177 }
178
179 if (g_strcmp0(type, "Application") != 0) {
180 g_warning("Desktop file '%s' has a type of '%s' instead of 'Application'", from, type);
181 g_free(type);
182 return NULL;
183 }
184 g_free(type);
185
186 if (g_key_file_has_key(desktop_file, "Desktop Entry", "NoDisplay", NULL)) {
187 gboolean nodisplay = g_key_file_get_boolean(desktop_file, "Desktop Entry", "NoDisplay", NULL);
188 if (nodisplay) {
189 g_warning("Desktop file '%s' is set to not display, not copying", from);
190 return NULL;
191 }
192 }
193
194 if (g_key_file_has_key(desktop_file, "Desktop Entry", "Hidden", NULL)) {
195 gboolean hidden = g_key_file_get_boolean(desktop_file, "Desktop Entry", "Hidden", NULL);
196 if (hidden) {
197 g_warning("Desktop file '%s' is set to be hidden, not copying", from);
198 return NULL;
199 }
200 }
201
202 if (g_key_file_has_key(desktop_file, "Desktop Entry", "Terminal", NULL)) {
203 gboolean terminal = g_key_file_get_boolean(desktop_file, "Desktop Entry", "Terminal", NULL);
204 if (terminal) {
205 g_warning("Desktop file '%s' is set to run in a terminal, not copying", from);
206 return NULL;
207 }
208 }
209
210 if (!g_key_file_has_key(desktop_file, "Desktop Entry", "Exec", NULL)) {
211 g_warning("Desktop file '%s' has no 'Exec' key", from);
212 return NULL;
213 }
214
215 gchar * exec = g_key_file_get_string(desktop_file, "Desktop Entry", "Exec", NULL);
216 return exec;
217 }
218
219 /* Convert a URI into a file */
220 static gchar *
221 uri2file (const gchar * uri)
222 {
223 GError * error = NULL;
224 gchar * retval = g_filename_from_uri(uri, NULL, &error);
225
226 if (error != NULL) {
227 g_warning("Unable to resolve '%s' to a filename: %s", uri, error->message);
228 g_error_free(error);
229 }
230
231 if (retval == NULL) {
232 retval = g_strdup("");
233 }
234
235 g_debug("Converting URI '%s' to file '%s'", uri, retval);
236 return retval;
237 }
238
239 /* Put the list of files into the argument array */
240 static inline void
241 file_list_handling (GArray * outarray, gchar ** list, gchar * (*dup_func) (const gchar * in))
242 {
243 /* No URLs, cool, this is a noop */
244 if (list == NULL || list[0] == NULL) {
245 return;
246 }
247
248 int i;
249 for (i = 0; list[i] != NULL; i++) {
250 gchar * entry = dup_func(list[i]);
251
252 /* No NULLs */
253 if (entry != NULL && entry[0] != '\0') {
254 g_array_append_val(outarray, entry);
255 } else {
256 g_free(entry);
257 }
258 }
259 }
260
261 /* Parse a desktop exec line and return the next string */
262 static void
263 desktop_exec_segment_parse (GArray * finalarray, const gchar * execsegment, gchar ** uri_list)
264 {
265 /* No NULL strings */
266 if (execsegment == NULL || execsegment[0] == '\0')
267 return;
268
269 /* Handle %F and %U as an argument on their own as per the spec */
270 if (g_strcmp0(execsegment, "%U") == 0) {
271 file_list_handling(finalarray, uri_list, g_strdup);
272 return;
273 }
274 if (g_strcmp0(execsegment, "%F") == 0) {
275 file_list_handling(finalarray, uri_list, uri2file);
276 return;
277 }
278
279 /* Start looking at individual codes */
280 gchar ** execsplit = g_strsplit(execsegment, "%", 0);
281
282 /* If we didn't have any codes, just exit here */
283 if (execsplit[1] == NULL) {
284 g_strfreev(execsplit);
285 gchar * dup = g_strdup(execsegment);
286 g_array_append_val(finalarray, dup);
287 return;
288 }
289
290 int i;
291
292 gboolean previous_percent = FALSE;
293 GArray * outarray = g_array_new(TRUE, FALSE, sizeof(const gchar *));
294 g_array_append_val(outarray, execsplit[0]);
295 gchar * single_file = NULL;
296
297 /* The variables allowed in an exec line from the Freedesktop.org Desktop
298 File specification: http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables */
299 for (i = 1; execsplit[i] != NULL; i++) {
300 const gchar * skipchar = &(execsplit[i][1]);
301
302 /* Handle the case of %%F printing "%F" */
303 if (previous_percent) {
304 g_array_append_val(outarray, execsplit[i]);
305 previous_percent = FALSE;
306 continue;
307 }
308
309 switch (execsplit[i][0]) {
310 case '\0': {
311 const gchar * percent = "%";
312 g_array_append_val(outarray, percent); /* %% is the literal */
313 previous_percent = TRUE;
314 break;
315 }
316 case 'd':
317 case 'D':
318 case 'n':
319 case 'N':
320 case 'v':
321 case 'm':
322 /* Deprecated */
323 g_array_append_val(outarray, skipchar);
324 break;
325 case 'f':
326 if (uri_list != NULL && uri_list[0] != NULL) {
327 if (single_file == NULL)
328 single_file = uri2file(uri_list[0]);
329 g_array_append_val(outarray, single_file);
330 }
331
332 g_array_append_val(outarray, skipchar);
333 break;
334 case 'F':
335 g_warning("Exec line segment has a '%%F' that isn't its own argument '%s', ignoring.", execsegment);
336 g_array_append_val(outarray, skipchar);
337 break;
338 case 'i':
339 case 'c':
340 case 'k':
341 /* Perhaps? Not sure anyone uses these */
342 g_array_append_val(outarray, skipchar);
343 break;
344 case 'U':
345 g_warning("Exec line segment has a '%%U' that isn't its own argument '%s', ignoring.", execsegment);
346 g_array_append_val(outarray, skipchar);
347 break;
348 case 'u':
349 if (uri_list != NULL && uri_list[0] != NULL) {
350 g_array_append_val(outarray, uri_list[0]);
351 }
352
353 g_array_append_val(outarray, skipchar);
354 break;
355 default:
356 g_warning("Desktop Exec line code '%%%c' unknown, skipping.", execsplit[i][0]);
357 g_array_append_val(outarray, skipchar);
358 break;
359 }
360 }
361
362 gchar * output = g_strjoinv(NULL, (gchar **)outarray->data);
363 g_array_free(outarray, TRUE);
364
365 if (output != NULL && output[0] != '\0') {
366 g_array_append_val(finalarray, output);
367 } else {
368 g_free(output);
369 }
370
371 g_free(single_file);
372 g_strfreev(execsplit);
373 }
374
375 /* Take a full exec line, split it out, parse the segments and return
376 it to the caller */
377 GArray *
378 desktop_exec_parse (const gchar * execline, const gchar * urilist)
379 {
380 GError * error = NULL;
381 gchar ** splitexec = NULL;
382 gchar ** splituris = NULL;
383 gint execitems = 0;
384
385 /* This returns from desktop file style quoting to straight strings with
386 the appropriate characters split by the spaces that were meant for
387 splitting. Trickier than it sounds. But now we should be able to assume
388 that each string in the array is expected to be its own parameter. */
389 g_shell_parse_argv(execline, &execitems, &splitexec, &error);
390
391 if (error != NULL) {
392 g_warning("Unable to parse exec line '%s': %s", execline, error->message);
393 g_error_free(error);
394 return NULL;
395 }
396
397 if (urilist != NULL && urilist[0] != '\0') {
398 g_shell_parse_argv(urilist, NULL, &splituris, &error);
399
400 if (error != NULL) {
401 g_warning("Unable to parse URIs '%s': %s", urilist, error->message);
402 g_error_free(error);
403 /* Continuing without URIs */
404 splituris = NULL;
405 }
406 }
407
408
409 GArray * newargv = g_array_new(TRUE, FALSE, sizeof(gchar *));
410 int i;
411 for (i = 0; i < execitems; i++) {
412 desktop_exec_segment_parse(newargv, splitexec[i], splituris);
413 }
414 g_strfreev(splitexec);
415
416 if (splituris != NULL) {
417 g_strfreev(splituris);
418 }
419
420 /* Each string here should be its own param */
421
422 return newargv;
423 }
424
425 static void
426 lomiri_signal_cb (GDBusConnection * con, const gchar * sender, const gchar * path, const gchar * interface, const gchar * signal, GVariant * params, gpointer user_data)
427 {
428 GMainLoop * mainloop = (GMainLoop *)user_data;
429 g_main_loop_quit(mainloop);
430 }
431
432 struct _handshake_t {
433 GDBusConnection * con;
434 GMainLoop * mainloop;
435 guint signal_subscribe;
436 GSource * timeout;
437 };
438
439 static gboolean
440 lomiri_too_slow_cb (gpointer user_data)
441 {
442 handshake_t * handshake = (handshake_t *)user_data;
443 g_main_loop_quit(handshake->mainloop);
444 handshake->timeout = NULL;
445 return G_SOURCE_REMOVE;
446 }
447
448 handshake_t *
449 starting_handshake_start (const gchar * app_id, const gchar * instance_id, int timeout_s)
450 {
451 GError * error = NULL;
452 handshake_t * handshake = g_new0(handshake_t, 1);
453
454 GMainContext * context = g_main_context_get_thread_default();
455
456 handshake->mainloop = g_main_loop_new(context, FALSE);
457 handshake->con = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
458
459 if (error != NULL) {
460 g_critical("Unable to connect to session bus: %s", error->message);
461 g_error_free(error);
462 g_free(handshake);
463 return NULL;
464 }
465
466 /* Set up listening for the unfrozen signal from Lomiri */
467 handshake->signal_subscribe = g_dbus_connection_signal_subscribe(handshake->con,
468 NULL, /* sender */
469 "com.lomiri.LomiriAppLaunch", /* interface */
470 "LomiriStartingSignal", /* signal */
471 "/", /* path */
472 app_id, /* arg0 */
473 G_DBUS_SIGNAL_FLAGS_NONE,
474 lomiri_signal_cb, handshake->mainloop,
475 NULL); /* user data destroy */
476
477 /* Send unfreeze to to Lomiri */
478 g_dbus_connection_emit_signal(handshake->con,
479 NULL, /* destination */
480 "/", /* path */
481 "com.lomiri.LomiriAppLaunch", /* interface */
482 "LomiriStartingBroadcast", /* signal */
483 g_variant_new("(ss)", app_id, instance_id),
484 &error);
485
486 /* Really, Lomiri? */
487 handshake->timeout = g_timeout_source_new_seconds(timeout_s);
488 g_source_set_callback(handshake->timeout, lomiri_too_slow_cb, handshake, NULL);
489 g_source_attach(handshake->timeout, context);
490
491 return handshake;
492 }
493
494 void
495 starting_handshake_wait (handshake_t * handshake)
496 {
497 if (handshake == NULL)
498 return;
499
500 g_main_loop_run(handshake->mainloop);
501
502 if (handshake->timeout != NULL) {
503 g_source_destroy(handshake->timeout);
504 g_source_unref(handshake->timeout);
505 handshake->timeout = NULL;
506 }
507 g_main_loop_unref(handshake->mainloop);
508 g_dbus_connection_signal_unsubscribe(handshake->con, handshake->signal_subscribe);
509 g_object_unref(handshake->con);
510
511 g_free(handshake);
512 }
513