Imported Upstream version 1.1.2
[psensor-pkg-debian.git] / src / pxdg.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include <glib.h>
27
28 #include <pio.h>
29 #include <plog.h>
30
31 static const char *KEY_GNOME_AUTOSTART = "X-GNOME-Autostart-enabled";
32
33 static char *get_user_autostart_dir()
34 {
35         const char *xdg_cfg_dir;
36
37         xdg_cfg_dir = g_get_user_config_dir();
38
39         log_fct("g_user_config_dir(): %s", xdg_cfg_dir);
40
41         return path_append(xdg_cfg_dir, "autostart");
42 }
43
44 static char *get_user_desktop_file()
45 {
46         char *dir, *path;
47
48         dir = get_user_autostart_dir();
49         path = path_append(dir, PSENSOR_DESKTOP_FILE);
50
51         free(dir);
52
53         return path;
54 }
55
56 static const char *get_desktop_file()
57 {
58         return DATADIR"/applications/"PSENSOR_DESKTOP_FILE;
59 }
60
61 static int is_file_exists(const char *path)
62 {
63         struct stat st;
64
65         return stat(path, &st) == 0;
66 }
67
68 static GKeyFile *get_key_file(const char *path)
69 {
70         GKeyFile *kfile;
71         int ret;
72
73         kfile = g_key_file_new();
74         ret = g_key_file_load_from_file(kfile,
75                                         path,
76                                         G_KEY_FILE_KEEP_COMMENTS
77                                         | G_KEY_FILE_KEEP_TRANSLATIONS,
78                                         NULL);
79
80         if (ret)
81                 return kfile;
82
83         log_err("Failed to parse: %s", path);
84
85         g_key_file_free(kfile);
86         return NULL;
87 }
88
89 static int is_user_desktop_autostarted(GKeyFile *f)
90 {
91         return (!g_key_file_has_key(f,
92                                     G_KEY_FILE_DESKTOP_GROUP,
93                                     KEY_GNOME_AUTOSTART,
94                                     NULL))
95                 || g_key_file_get_boolean(f,
96                                           G_KEY_FILE_DESKTOP_GROUP,
97                                           KEY_GNOME_AUTOSTART,
98                                           NULL);
99 }
100
101 int pxdg_is_autostarted()
102 {
103         char *user_desktop;
104         unsigned int ret;
105         GKeyFile *kfile;
106
107         log_fct_enter();
108
109         user_desktop = get_user_desktop_file();
110
111         log_fct("user desktop file: %s", user_desktop);
112
113         ret = is_file_exists(user_desktop);
114
115         if (!ret) {
116                 log_fct("user desktop file does not exist.");
117         } else {
118                 log_fct("user desktop file exist.");
119                 if (ret) {
120                         kfile = get_key_file(user_desktop);
121                         if (kfile)
122                                 ret = is_user_desktop_autostarted(kfile);
123                         else
124                                 ret = -1;
125                         g_key_file_free(kfile);
126                 }
127         }
128
129         free(user_desktop);
130
131         log_fct_exit();
132
133         return ret;
134 }
135
136 static void enable_gnome_autostart(const char *path)
137 {
138         GKeyFile *f;
139         char *data;
140
141         f = get_key_file(path);
142         if (f) {
143                 if (g_key_file_has_key(f,
144                                        G_KEY_FILE_DESKTOP_GROUP,
145                                        KEY_GNOME_AUTOSTART,
146                                        NULL))
147                         g_key_file_set_boolean(f,
148                                                G_KEY_FILE_DESKTOP_GROUP,
149                                                KEY_GNOME_AUTOSTART,
150                                                TRUE);
151                 data = g_key_file_to_data(f, NULL, NULL);
152                 g_file_set_contents(path, data, -1, NULL);
153
154                 g_key_file_free(f);
155         } else {
156                 log_err("Fail to enable %s", KEY_GNOME_AUTOSTART);
157         }
158 }
159
160 void pxdg_set_autostart(unsigned int enable)
161 {
162         char *user_desktop, *dir;
163
164         log_fct_enter();
165
166         user_desktop = get_user_desktop_file();
167
168         log_fct("user desktop file: %s", user_desktop);
169
170         log_fct("desktop file: %s", get_desktop_file());
171
172         if (enable) {
173                 if (!is_file_exists(user_desktop)) {
174                         dir = get_user_autostart_dir();
175                         mkdirs(dir, 0700);
176                         free(dir);
177                         file_copy(get_desktop_file(), user_desktop);
178                 }
179                 enable_gnome_autostart(user_desktop);
180         } else {
181                 /* because X-GNOME-Autostart-enabled does not turn off
182                  * autostart on all Desktop Envs. */
183                 remove(user_desktop);
184         }
185
186         log_fct_exit();
187
188         free(user_desktop);
189 }