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>
31 static char *get_user_autostart_dir()
33 const char *xdg_cfg_dir;
35 xdg_cfg_dir = g_get_user_config_dir();
37 log_fct("g_user_config_dir(): %s", xdg_cfg_dir);
39 return path_append(xdg_cfg_dir, "autostart");
42 static char *get_user_desktop_file()
46 dir = get_user_autostart_dir();
47 path = path_append(dir, "psensor.desktop");
54 static const char *get_desktop_file()
56 return DATADIR"/applications/psensor.desktop";
59 static int is_file_exists(const char *path)
63 return stat(path, &st) == 0;
66 static GKeyFile *get_key_file(const char *path)
71 kfile = g_key_file_new();
72 ret = g_key_file_load_from_file(kfile,
74 G_KEY_FILE_KEEP_COMMENTS
75 | G_KEY_FILE_KEEP_TRANSLATIONS,
81 log_err("Failed to parse: %s", path);
83 g_key_file_free(kfile);
88 static int is_user_desktop_autostarted(GKeyFile *f)
90 return (!g_key_file_has_key(f,
91 G_KEY_FILE_DESKTOP_GROUP,
92 "X-GNOME-Autostart-enabled",
94 || g_key_file_get_boolean(f,
95 G_KEY_FILE_DESKTOP_GROUP,
96 "X-GNOME-Autostart-enabled",
100 int pxdg_is_autostarted()
108 user_desktop = get_user_desktop_file();
110 log_fct("user desktop file: %s", user_desktop);
112 ret = is_file_exists(user_desktop);
115 log_fct("user desktop file does not exist.");
117 log_fct("user desktop file exist.");
119 kfile = get_key_file(user_desktop);
121 ret = is_user_desktop_autostarted(kfile);
125 g_key_file_free(kfile);
135 void pxdg_set_autostart(unsigned int enable)
137 char *user_desktop, *data;
142 user_desktop = get_user_desktop_file();
144 log_fct("user desktop file: %s", user_desktop);
146 log_fct("desktop file: %s", get_desktop_file());
149 if (!is_file_exists(user_desktop))
150 file_copy(get_desktop_file(), user_desktop);
152 f = get_key_file(user_desktop);
154 if (g_key_file_has_key(f,
155 G_KEY_FILE_DESKTOP_GROUP,
156 "X-GNOME-Autostart-enabled",
158 g_key_file_set_boolean
160 G_KEY_FILE_DESKTOP_GROUP,
161 "X-GNOME-Autostart-enabled",
163 data = g_key_file_to_data(f, NULL, NULL);
164 g_file_set_contents(user_desktop,
172 /* because X-GNOME-Autostart-enabled does not turn off
173 * autostart on all Desktop Envs. */
174 remove(user_desktop);