2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <sys/types.h>
32 static const char *KEY_GNOME_AUTOSTART = "X-GNOME-Autostart-enabled";
34 static char *get_user_autostart_dir(void)
36 const char *xdg_cfg_dir;
38 xdg_cfg_dir = g_get_user_config_dir();
40 log_fct("g_user_config_dir(): %s", xdg_cfg_dir);
42 return path_append(xdg_cfg_dir, "autostart");
45 static char *get_user_desktop_file(void)
49 dir = get_user_autostart_dir();
50 path = path_append(dir, PSENSOR_DESKTOP_FILE);
57 static const char *get_desktop_file(void)
59 return DATADIR"/applications/"PSENSOR_DESKTOP_FILE;
62 static int is_file_exists(const char *path)
66 return stat(path, &st) == 0;
69 static GKeyFile *get_key_file(const char *path)
74 kfile = g_key_file_new();
75 ret = g_key_file_load_from_file(kfile,
77 G_KEY_FILE_KEEP_COMMENTS
78 | G_KEY_FILE_KEEP_TRANSLATIONS,
84 log_err("Failed to parse: %s", path);
86 g_key_file_free(kfile);
90 static int is_user_desktop_autostarted(GKeyFile *f)
92 return (!g_key_file_has_key(f,
93 G_KEY_FILE_DESKTOP_GROUP,
96 || g_key_file_get_boolean(f,
97 G_KEY_FILE_DESKTOP_GROUP,
102 int pxdg_is_autostarted(void)
110 user_desktop = get_user_desktop_file();
112 log_fct("user desktop file: %s", user_desktop);
114 ret = is_file_exists(user_desktop);
117 log_fct("user desktop file does not exist.");
119 log_fct("user desktop file exist.");
121 kfile = get_key_file(user_desktop);
123 ret = is_user_desktop_autostarted(kfile);
126 g_key_file_free(kfile);
137 static void enable_gnome_autostart(const char *path)
142 f = get_key_file(path);
144 if (g_key_file_has_key(f,
145 G_KEY_FILE_DESKTOP_GROUP,
148 g_key_file_set_boolean(f,
149 G_KEY_FILE_DESKTOP_GROUP,
152 data = g_key_file_to_data(f, NULL, NULL);
153 g_file_set_contents(path, data, -1, NULL);
157 log_err("Fail to enable %s", KEY_GNOME_AUTOSTART);
161 void pxdg_set_autostart(unsigned int enable)
163 char *user_desktop, *dir;
167 user_desktop = get_user_desktop_file();
169 log_fct("user desktop file: %s", user_desktop);
171 log_fct("desktop file: %s", get_desktop_file());
174 if (!is_file_exists(user_desktop)) {
175 dir = get_user_autostart_dir();
178 file_copy(get_desktop_file(), user_desktop);
180 enable_gnome_autostart(user_desktop);
182 /* because X-GNOME-Autostart-enabled does not turn off
183 * autostart on all Desktop Envs. */
184 remove(user_desktop);