a43d075288551ef5040c4d502f8413857760dfa8
[psensor-pkg-debian.git] / src / pxdg.c
1 /*
2  * Copyright (C) 2010-2016 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 #include <pxdg.h>
31
32 static const char *KEY_GNOME_AUTOSTART = "X-GNOME-Autostart-enabled";
33
34 static char *get_user_autostart_dir(void)
35 {
36         const char *xdg_cfg_dir;
37
38         xdg_cfg_dir = g_get_user_config_dir();
39
40         log_fct("g_user_config_dir(): %s", xdg_cfg_dir);
41
42         return path_append(xdg_cfg_dir, "autostart");
43 }
44
45 static char *get_user_desktop_file(void)
46 {
47         char *dir, *path;
48
49         dir = get_user_autostart_dir();
50         path = path_append(dir, PSENSOR_DESKTOP_FILE);
51
52         free(dir);
53
54         return path;
55 }
56
57 static const char *get_desktop_file(void)
58 {
59         return DATADIR"/applications/"PSENSOR_DESKTOP_FILE;
60 }
61
62 static int is_file_exists(const char *path)
63 {
64         struct stat st;
65
66         return stat(path, &st) == 0;
67 }
68
69 static GKeyFile *get_key_file(const char *path)
70 {
71         GKeyFile *kfile;
72         int ret;
73
74         kfile = g_key_file_new();
75         ret = g_key_file_load_from_file(kfile,
76                                         path,
77                                         G_KEY_FILE_KEEP_COMMENTS
78                                         | G_KEY_FILE_KEEP_TRANSLATIONS,
79                                         NULL);
80
81         if (ret)
82                 return kfile;
83
84         log_err("Failed to parse: %s", path);
85
86         g_key_file_free(kfile);
87         return NULL;
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(void)
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                  */
185                 remove(user_desktop);
186         }
187
188         log_fct_exit();
189
190         free(user_desktop);
191 }