Reading name from desktop entry file is now done by desktop_entry_read_keyfile.
[AutoStart.git] / src / xdgautostart.c
blob630863abc4259453621fbf263d928743a6fa2c84
1 /*
2 * By Tony Houghton, <h@realh.co.uk>.
3 */
5 #include "config.h"
7 #include <string.h>
9 #include "xdgautostart.h"
11 static const char *locale_name = NULL;
12 static const char *short_locale_name = NULL;
14 static void desktop_entry_free_keyfile_mirrors(DesktopEntry *de)
16 g_free(de->name);
17 de->name = NULL;
18 g_free(de->exec);
19 de->exec = NULL;
20 g_free(de->path);
21 de->path = NULL;
22 g_free(de->try_exec);
23 de->try_exec = NULL;
24 g_strfreev(de->only_show_in);
25 de->only_show_in = NULL;
26 g_strfreev(de->not_show_in);
27 de->not_show_in = NULL;
30 static void desktop_entry_read_name_from_keyfile(DesktopEntry *xde)
32 g_free(xde->name);
33 if (locale_name)
35 xde->name = g_key_file_get_locale_string(xde->kf, _DE, "Name",
36 locale_name, NULL);
38 if (!xde->name && short_locale_name)
40 xde->name = g_key_file_get_locale_string(xde->kf, _DE, "Name",
41 short_locale_name, NULL);
43 if (!xde->name)
44 xde->name = g_key_file_get_string(xde->kf, _DE, "Name", NULL);
45 if (!xde->name)
46 xde->name = g_strdup(_("No name"));
49 void desktop_entry_read_keyfile(DesktopEntry *de)
51 GKeyFile *kf = de->kf;
53 desktop_entry_free_keyfile_mirrors(de);
54 desktop_entry_read_name_from_keyfile(de);
55 de->exec = g_key_file_get_string(kf, _DE, "Exec", NULL);
56 de->path = g_key_file_get_string(kf, _DE, "Path", NULL);
57 de->terminal = g_key_file_get_boolean(kf, _DE, "Terminal", NULL);
58 de->try_exec = g_key_file_get_string(kf, _DE, "TryExec", NULL);
59 de->only_show_in = g_key_file_get_string_list(kf, _DE, "OnlyShowIn",
60 NULL, NULL);
61 de->not_show_in = g_key_file_get_string_list(kf, _DE, "NotShowIn",
62 NULL, NULL);
65 gboolean desktop_entry_load(DesktopEntry *de, const char *pathname,
66 const char *basename, gboolean keep_keyfile, gboolean savable)
68 GKeyFile *kf = g_key_file_new();
69 GError *err = NULL;
71 de->kf = kf;
72 if (!g_key_file_load_from_file(kf, pathname,
73 keep_keyfile ?
74 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS :
75 G_KEY_FILE_NONE,
76 &err))
78 g_warning(_("Can't load key-file '%s': %s"), pathname,
79 err ? err->message : _("unknown reason"));
80 g_error_free(err);
81 return FALSE;
83 if (!g_key_file_has_group(kf, _DE))
85 g_warning(_("'%s' is not a .desktop file"), pathname);
86 return FALSE;
89 de->pathname = g_strdup(pathname);
90 de->basename = g_strdup(basename);
91 de->savable = savable;
92 de->deletable = savable;
94 desktop_entry_read_keyfile(de);
96 if (!keep_keyfile)
98 g_key_file_free(kf);
99 de->kf = NULL;
101 return TRUE;
104 void desktop_entry_free(DesktopEntry *de)
106 desktop_entry_free_keyfile_mirrors(de);
107 g_free(de->pathname);
108 de->pathname = NULL;
109 g_free(de->basename);
110 de->basename = NULL;
111 if (de->kf)
113 g_key_file_free(de->kf);
114 de->kf = NULL;
118 void desktop_entry_delete(DesktopEntry *de)
120 desktop_entry_free(de);
121 g_free(de);
124 DesktopEntry *desktop_entry_new(const char *pathname, const char *basename,
125 gboolean savable)
127 DesktopEntry *de = g_new0(DesktopEntry, 1);
129 if (!desktop_entry_load(de, pathname, basename, FALSE, savable))
131 desktop_entry_delete(de);
132 de = NULL;
134 return de;
137 gboolean desktop_entry_str_in_strv(char **v, const char *s)
139 for ( ; v && *v; ++v)
141 if (!strcmp(*v, s))
142 return TRUE;
144 return FALSE;
147 gboolean desktop_entry_get_show_in_environ(const DesktopEntry *de,
148 const char *environ)
150 if (de->only_show_in)
152 if (desktop_entry_str_in_strv(de->only_show_in, environ))
153 return TRUE;
155 if (de->not_show_in)
157 if (desktop_entry_str_in_strv(de->not_show_in, environ))
158 return FALSE;
160 return TRUE;
163 /* Also checks Exec and TryExec */
164 gboolean desktop_entry_can_start_in_rox(const DesktopEntry *de)
166 if (!desktop_entry_get_show_in_rox(de))
167 return FALSE;
168 if (!de->exec)
169 return FALSE;
170 if (de->try_exec)
172 if (!g_file_test(de->try_exec, G_FILE_TEST_EXISTS))
173 return FALSE;
175 if (de->exec[0] == '/')
177 return g_file_test(de->exec, G_FILE_TEST_IS_EXECUTABLE);
179 else
181 char *ef = g_find_program_in_path(de->exec);
182 gboolean result = ef != NULL;
184 g_free(ef);
185 return result;
187 return FALSE;
190 static GList *scan_dir(const char *dir, GList *desl,
191 DesktopEntryConstructor ctor, gboolean savable)
193 char *dir2 = g_build_filename(dir, "autostart", NULL);
194 GDir *dir0;
195 const char *basename;
197 if (!g_file_test(dir2, G_FILE_TEST_IS_DIR))
198 goto no_dir;
199 dir0 = g_dir_open(dir2, 0, NULL);
200 if (!dir0)
201 goto no_dir;
202 while ((basename = g_dir_read_name(dir0)) != NULL)
204 GList *node;
205 gboolean dupl = FALSE;
207 /* Don't add if there's already one with the same basename */
208 for (node = desl; node; node = g_list_next(node))
210 DesktopEntry *de = node->data;
212 if (!strcmp(basename, de->basename))
214 dupl = TRUE;
215 break;
218 if (!dupl)
220 char *pathname = g_build_filename(dir2, basename, NULL);
221 DesktopEntry *de = ctor(pathname, basename, savable);
223 if (de)
224 desl = g_list_append(desl, de);
225 g_free(pathname);
229 g_dir_close(dir0);
230 no_dir:
231 g_free(dir2);
232 return desl;
235 GList *get_desktop_entries(DesktopEntryConstructor ctor)
237 GList *desl = NULL;
238 const char *udir = g_get_user_config_dir();
239 const char * const * sdirs;
241 desl = scan_dir(udir, desl, ctor, TRUE);
242 for (sdirs = g_get_system_config_dirs(); sdirs && *sdirs; ++sdirs)
244 desl = scan_dir(*sdirs, desl, ctor, FALSE);
246 return desl;
249 gboolean str_v_is_empty(char **sv)
251 if (!sv)
252 return TRUE;
253 for ( ; *sv; ++sv)
255 if (*sv)
256 return FALSE;
258 return TRUE;
261 void desktop_entry_set_locale_name(const char *full_name,
262 const char *short_name)
264 locale_name = full_name;
265 short_locale_name = short_name;