2 * By Tony Houghton, <h@realh.co.uk>.
10 #include "xdgautostart.h"
13 #define _DE "Desktop Entry"
16 static const char *locale_name
= NULL
;
17 static const char *short_locale_name
= NULL
;
19 static void desktop_entry_free_keyfile_mirrors(DesktopEntry
*de
)
29 g_strfreev(de
->only_show_in
);
30 de
->only_show_in
= NULL
;
31 g_strfreev(de
->not_show_in
);
32 de
->not_show_in
= NULL
;
35 static void desktop_entry_read_name_from_keyfile(DesktopEntry
*xde
)
40 xde
->name
= g_key_file_get_locale_string(xde
->kf
, _DE
, "Name",
43 if (!xde
->name
&& short_locale_name
)
45 xde
->name
= g_key_file_get_locale_string(xde
->kf
, _DE
, "Name",
46 short_locale_name
, NULL
);
49 xde
->name
= g_key_file_get_string(xde
->kf
, _DE
, "Name", NULL
);
51 xde
->name
= g_strdup(_("No name"));
54 void desktop_entry_read_keyfile(DesktopEntry
*de
)
56 GKeyFile
*kf
= de
->kf
;
58 desktop_entry_free_keyfile_mirrors(de
);
59 desktop_entry_read_name_from_keyfile(de
);
60 de
->exec
= g_key_file_get_string(kf
, _DE
, "Exec", NULL
);
61 de
->path
= g_key_file_get_string(kf
, _DE
, "Path", NULL
);
62 de
->terminal
= g_key_file_get_boolean(kf
, _DE
, "Terminal", NULL
);
63 de
->try_exec
= g_key_file_get_string(kf
, _DE
, "TryExec", NULL
);
64 de
->only_show_in
= g_key_file_get_string_list(kf
, _DE
, "OnlyShowIn",
66 de
->not_show_in
= g_key_file_get_string_list(kf
, _DE
, "NotShowIn",
70 gboolean
desktop_entry_load(DesktopEntry
*de
, const char *pathname
,
71 const char *basename
, gboolean keep_keyfile
, gboolean savable
)
73 GKeyFile
*kf
= g_key_file_new();
77 if (!g_key_file_load_from_file(kf
, pathname
,
79 G_KEY_FILE_KEEP_COMMENTS
| G_KEY_FILE_KEEP_TRANSLATIONS
:
83 g_warning(_("Can't load key-file '%s': %s"), pathname
,
84 err
? err
->message
: _("unknown reason"));
88 if (!g_key_file_has_group(kf
, _DE
))
90 g_warning(_("'%s' is not a .desktop file"), pathname
);
94 de
->pathname
= g_strdup(pathname
);
95 de
->basename
= g_strdup(basename
);
96 de
->savable
= savable
;
97 de
->deletable
= savable
;
99 desktop_entry_read_keyfile(de
);
109 void desktop_entry_free(DesktopEntry
*de
)
111 desktop_entry_free_keyfile_mirrors(de
);
112 g_free(de
->pathname
);
114 g_free(de
->basename
);
118 g_key_file_free(de
->kf
);
123 void desktop_entry_delete(DesktopEntry
*de
)
125 desktop_entry_free(de
);
129 DesktopEntry
*desktop_entry_new(const char *pathname
, const char *basename
,
132 DesktopEntry
*de
= g_new0(DesktopEntry
, 1);
134 if (!desktop_entry_load(de
, pathname
, basename
, FALSE
, savable
))
136 desktop_entry_delete(de
);
142 gboolean
desktop_entry_str_in_strv(char **v
, const char *s
)
144 for ( ; v
&& *v
; ++v
)
152 gboolean
desktop_entry_get_show_in_environ(const DesktopEntry
*de
,
155 if (de
->only_show_in
)
157 return desktop_entry_str_in_strv(de
->only_show_in
, environ
);
161 if (desktop_entry_str_in_strv(de
->not_show_in
, environ
))
167 /* Also checks Exec and TryExec */
168 gboolean
desktop_entry_can_start_in_rox(const DesktopEntry
*de
)
170 if (!desktop_entry_get_show_in_rox(de
))
176 if (!g_file_test(de
->try_exec
, G_FILE_TEST_EXISTS
))
179 if (de
->exec
[0] == '/')
181 return g_file_test(de
->exec
, G_FILE_TEST_IS_EXECUTABLE
);
185 char *ef
= g_find_program_in_path(de
->exec
);
186 gboolean result
= ef
!= NULL
;
194 static GList
*scan_dir(const char *dir
, GList
*desl
,
195 DesktopEntryConstructor ctor
, gboolean savable
)
197 char *dir2
= g_build_filename(dir
, "autostart", NULL
);
199 const char *basename
;
201 if (!g_file_test(dir2
, G_FILE_TEST_IS_DIR
))
203 dir0
= g_dir_open(dir2
, 0, NULL
);
206 while ((basename
= g_dir_read_name(dir0
)) != NULL
)
209 gboolean dupl
= FALSE
;
211 /* Don't add if there's already one with the same basename */
212 for (node
= desl
; node
; node
= g_list_next(node
))
214 DesktopEntry
*de
= node
->data
;
216 if (!strcmp(basename
, de
->basename
))
224 char *pathname
= g_build_filename(dir2
, basename
, NULL
);
225 DesktopEntry
*de
= ctor(pathname
, basename
, savable
);
228 desl
= g_list_append(desl
, de
);
239 GList
*get_desktop_entries(DesktopEntryConstructor ctor
)
242 const char *udir
= g_get_user_config_dir();
243 const char * const * sdirs
;
245 desl
= scan_dir(udir
, desl
, ctor
, TRUE
);
246 for (sdirs
= g_get_system_config_dirs(); sdirs
&& *sdirs
; ++sdirs
)
248 desl
= scan_dir(*sdirs
, desl
, ctor
, FALSE
);
253 gboolean
str_v_is_empty(char **sv
)
265 void desktop_entry_set_locale_names(const char *full_name
,
266 const char *short_name
)
268 locale_name
= full_name
;
269 short_locale_name
= short_name
;
272 void desktop_entry_init_locales(char const **long_name
, char const **short_name
)
274 char *lang
= getenv("LANG");
278 char *sep
= strchr(lang
, '.');
279 int l
= sep
? sep
- lang
: strlen(lang
);
281 locale_name
= g_strdup_printf("%.*s", l
, lang
);
282 sep
= strchr(locale_name
, '_');
285 short_locale_name
= g_strdup_printf("%.*s",
286 (int) (sep
- locale_name
), locale_name
);
294 *long_name
= locale_name
;
296 *short_name
= short_locale_name
;