Merge tag 'upstream/1.0.3'
[psensor-pkg-ubuntu.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");
50
51         free(dir);
52
53         return path;
54 }
55
56 static const char *get_desktop_file()
57 {
58         return DATADIR"/applications/psensor.desktop";
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         } else {
83                 log_err("Failed to parse: %s", path);
84
85                 g_key_file_free(kfile);
86                 return NULL;
87         }
88 }
89
90 static int is_user_desktop_autostarted(GKeyFile *f)
91 {
92         return (!g_key_file_has_key(f,
93                                     G_KEY_FILE_DESKTOP_GROUP,
94                                     KEY_GNOME_AUTOSTART,
95                                     NULL))
96                 || g_key_file_get_boolean(f,
97                                           G_KEY_FILE_DESKTOP_GROUP,
98                                           KEY_GNOME_AUTOSTART,
99                                           NULL);
100 }
101
102 int pxdg_is_autostarted()
103 {
104         char *user_desktop;
105         unsigned int ret;
106         GKeyFile *kfile;
107
108         log_fct_enter();
109
110         user_desktop = get_user_desktop_file();
111
112         log_fct("user desktop file: %s", user_desktop);
113
114         ret = is_file_exists(user_desktop);
115
116         if (!ret) {
117                 log_fct("user desktop file does not exist.");
118         } else {
119                 log_fct("user desktop file exist.");
120                 if (ret) {
121                         kfile = get_key_file(user_desktop);
122                         if (kfile)
123                                 ret = is_user_desktop_autostarted(kfile);
124                         else
125                                 ret = -1;
126                         g_key_file_free(kfile);
127                 }
128         }
129
130         free(user_desktop);
131
132         log_fct_exit();
133
134         return ret;
135 }
136
137 static void enable_gnome_autostart(const char *path)
138 {
139         GKeyFile *f;
140         char *data;
141
142         f = get_key_file(path);
143         if (f) {
144                 if (g_key_file_has_key(f,
145                                        G_KEY_FILE_DESKTOP_GROUP,
146                                        KEY_GNOME_AUTOSTART,
147                                        NULL))
148                         g_key_file_set_boolean(f,
149                                                G_KEY_FILE_DESKTOP_GROUP,
150                                                KEY_GNOME_AUTOSTART,
151                                                TRUE);
152                 data = g_key_file_to_data(f, NULL, NULL);
153                 g_file_set_contents(path, data, -1, NULL);
154
155                 g_key_file_free(f);
156         } else {
157                 log_err("Fail to enable %s", KEY_GNOME_AUTOSTART);
158         }
159 }
160
161 void pxdg_set_autostart(unsigned int enable)
162 {
163         char *user_desktop, *dir;
164
165         log_fct_enter();
166
167         user_desktop = get_user_desktop_file();
168
169         log_fct("user desktop file: %s", user_desktop);
170
171         log_fct("desktop file: %s", get_desktop_file());
172
173         if (enable) {
174                 if (!is_file_exists(user_desktop)) {
175                         dir = get_user_autostart_dir();
176                         mkdirs(dir, 0700);
177                         free(dir);
178                         file_copy(get_desktop_file(), user_desktop);
179                 }
180                 enable_gnome_autostart(user_desktop);
181         } else {
182                 /* because X-GNOME-Autostart-enabled does not turn off
183                  * autostart on all Desktop Envs. */
184                 remove(user_desktop);
185         }
186
187         log_fct_exit();
188
189         free(user_desktop);
190 }