2 * By Tony Houghton, <h@realh.co.uk>.
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
)
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
)
35 xde
->name
= g_key_file_get_locale_string(xde
->kf
, _DE
, "Name",
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
);
44 xde
->name
= g_key_file_get_string(xde
->kf
, _DE
, "Name", NULL
);
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",
61 de
->not_show_in
= g_key_file_get_string_list(kf
, _DE
, "NotShowIn",
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();
72 if (!g_key_file_load_from_file(kf
, pathname
,
74 G_KEY_FILE_KEEP_COMMENTS
| G_KEY_FILE_KEEP_TRANSLATIONS
:
78 g_warning(_("Can't load key-file '%s': %s"), pathname
,
79 err
? err
->message
: _("unknown reason"));
83 if (!g_key_file_has_group(kf
, _DE
))
85 g_warning(_("'%s' is not a .desktop file"), pathname
);
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
);
104 void desktop_entry_free(DesktopEntry
*de
)
106 desktop_entry_free_keyfile_mirrors(de
);
107 g_free(de
->pathname
);
109 g_free(de
->basename
);
113 g_key_file_free(de
->kf
);
118 void desktop_entry_delete(DesktopEntry
*de
)
120 desktop_entry_free(de
);
124 DesktopEntry
*desktop_entry_new(const char *pathname
, const char *basename
,
127 DesktopEntry
*de
= g_new0(DesktopEntry
, 1);
129 if (!desktop_entry_load(de
, pathname
, basename
, FALSE
, savable
))
131 desktop_entry_delete(de
);
137 gboolean
desktop_entry_str_in_strv(char **v
, const char *s
)
139 for ( ; v
&& *v
; ++v
)
147 gboolean
desktop_entry_get_show_in_environ(const DesktopEntry
*de
,
150 if (de
->only_show_in
)
152 if (desktop_entry_str_in_strv(de
->only_show_in
, environ
))
157 if (desktop_entry_str_in_strv(de
->not_show_in
, environ
))
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
))
172 if (!g_file_test(de
->try_exec
, G_FILE_TEST_EXISTS
))
175 if (de
->exec
[0] == '/')
177 return g_file_test(de
->exec
, G_FILE_TEST_IS_EXECUTABLE
);
181 char *ef
= g_find_program_in_path(de
->exec
);
182 gboolean result
= ef
!= NULL
;
190 static GList
*scan_dir(const char *dir
, GList
*desl
,
191 DesktopEntryConstructor ctor
, gboolean savable
)
193 char *dir2
= g_build_filename(dir
, "autostart", NULL
);
195 const char *basename
;
197 if (!g_file_test(dir2
, G_FILE_TEST_IS_DIR
))
199 dir0
= g_dir_open(dir2
, 0, NULL
);
202 while ((basename
= g_dir_read_name(dir0
)) != NULL
)
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
))
220 char *pathname
= g_build_filename(dir2
, basename
, NULL
);
221 DesktopEntry
*de
= ctor(pathname
, basename
, savable
);
224 desl
= g_list_append(desl
, de
);
235 GList
*get_desktop_entries(DesktopEntryConstructor ctor
)
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
);
249 gboolean
str_v_is_empty(char **sv
)
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
;