Debian Codegraph @

liblomiri-app-launch/utils-shared.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 "utils.h"
21 #include <gio/gio.h>
22
23 #include "ual-tracepoint.h"
24
25 /* Check to make sure we have the sections and keys we want */
26 gboolean
27 verify_keyfile (GKeyFile * inkeyfile, const gchar * desktop)
28 {
29 if (inkeyfile == NULL) return FALSE;
30
31 if (!g_key_file_has_group(inkeyfile, "Desktop Entry")) {
32 g_warning("Desktop file '%s' is missing the 'Desktop Entry' group", desktop);
33 return FALSE;
34 }
35
36 if (!g_key_file_has_key(inkeyfile, "Desktop Entry", "Exec", NULL)) {
37 g_warning("Desktop file '%s' is missing the 'Exec' key", desktop);
38 return FALSE;
39 }
40
41 return TRUE;
42 }
43
44 /* Try to find a desktop file in a particular data directory */
45 static GKeyFile *
46 try_dir (const char * dir, const gchar * desktop)
47 {
48 gchar * fullpath = g_build_filename(dir, "applications", desktop, NULL);
49 GKeyFile * keyfile = g_key_file_new();
50
51 /* NOTE: Leaving off the error here as we'll get a bunch of them,
52 so individuals aren't really useful */
53 gboolean loaded = g_key_file_load_from_file(keyfile, fullpath, G_KEY_FILE_NONE, NULL);
54
55 g_free(fullpath);
56
57 if (!loaded) {
58 g_key_file_free(keyfile);
59 return NULL;
60 }
61
62 if (!verify_keyfile(keyfile, desktop)) {
63 g_key_file_free(keyfile);
64 return NULL;
65 }
66
67 return keyfile;
68 }
69
70 /* Find the keyfile that we need for a particular AppID and return it.
71 Or NULL if we can't find it. */
72 GKeyFile *
73 keyfile_for_appid (const gchar * appid, gchar ** desktopfile)
74 {
75 gchar * desktop = g_strdup_printf("%s.desktop", appid);
76
77 const char * const * data_dirs = g_get_system_data_dirs();
78 GKeyFile * keyfile = NULL;
79 int i;
80
81 keyfile = try_dir(g_get_user_data_dir(), desktop);
82 if (keyfile != NULL && desktopfile != NULL && *desktopfile == NULL) {
83 *desktopfile = g_build_filename(g_get_user_data_dir(), "applications", desktop, NULL);
84 }
85
86 for (i = 0; data_dirs[i] != NULL && keyfile == NULL; i++) {
87 keyfile = try_dir(data_dirs[i], desktop);
88
89 if (keyfile != NULL && desktopfile != NULL && *desktopfile == NULL) {
90 *desktopfile = g_build_filename(data_dirs[i], "applications", desktop, NULL);
91 }
92 }
93
94 g_free(desktop);
95
96 return keyfile;
97 }
98
99 /* Global markers for the ual_tracepoint macro */
100 int _ual_tracepoints_env_checked = 0;
101 int _ual_tracepoints_enabled = 0;
102