enable gnome-autostart if the desktop file is in autostart dir but the entry turned off
[psensor.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 char *get_user_autostart_dir()
32 {
33         const char *xdg_cfg_dir;
34
35         xdg_cfg_dir = g_get_user_config_dir();
36
37         log_fct("g_user_config_dir(): %s", xdg_cfg_dir);
38
39         return path_append(xdg_cfg_dir, "autostart");
40 }
41
42 static char *get_user_desktop_file()
43 {
44         char *dir, *path;
45
46         dir = get_user_autostart_dir();
47         path = path_append(dir, "psensor.desktop");
48
49         free(dir);
50
51         return path;
52 }
53
54 static const char *get_desktop_file()
55 {
56         return DATADIR"/applications/psensor.desktop";
57 }
58
59 static int is_file_exists(const char *path)
60 {
61         struct stat st;
62
63         return stat(path, &st) == 0;
64 }
65
66 static GKeyFile *get_key_file(const char *path)
67 {
68         GKeyFile *kfile;
69         int ret;
70
71         kfile = g_key_file_new();
72         ret = g_key_file_load_from_file(kfile,
73                                         path,
74                                         G_KEY_FILE_KEEP_COMMENTS
75                                         | G_KEY_FILE_KEEP_TRANSLATIONS,
76                                         NULL);
77
78         if (ret) {
79                 return kfile;
80         } else {
81                 log_err("Failed to parse: %s", path);
82
83                 g_key_file_free(kfile);
84                 return NULL;
85         }
86 }
87
88 static int is_user_desktop_autostarted(GKeyFile *f)
89 {
90         return (!g_key_file_has_key(f,
91                                     G_KEY_FILE_DESKTOP_GROUP,
92                                     "X-GNOME-Autostart-enabled",
93                                     NULL))
94                 || g_key_file_get_boolean(f,
95                                           G_KEY_FILE_DESKTOP_GROUP,
96                                           "X-GNOME-Autostart-enabled",
97                                           NULL);
98 }
99
100 int pxdg_is_autostarted()
101 {
102         char *user_desktop;
103         unsigned int ret;
104         GKeyFile *kfile;
105
106         log_fct_enter();
107
108         user_desktop = get_user_desktop_file();
109
110         log_fct("user desktop file: %s", user_desktop);
111
112         ret = is_file_exists(user_desktop);
113
114         if (!ret) {
115                 log_fct("user desktop file does not exist.");
116         } else {
117                 log_fct("user desktop file exist.");
118                 if (ret) {
119                         kfile = get_key_file(user_desktop);
120                         if (kfile)
121                                 ret = is_user_desktop_autostarted(kfile);
122                         else
123                                 ret = -1;
124                 }
125                 g_key_file_free(kfile);
126         }
127
128         free(user_desktop);
129
130         log_fct_exit();
131
132         return ret;
133 }
134
135 void pxdg_set_autostart(unsigned int enable)
136 {
137         char *user_desktop, *data;
138         GKeyFile *f;
139
140         log_fct_enter();
141
142         user_desktop = get_user_desktop_file();
143
144         log_fct("user desktop file: %s", user_desktop);
145
146         log_fct("desktop file: %s", get_desktop_file());
147
148         if (enable) {
149                 if (!is_file_exists(user_desktop))
150                         file_copy(get_desktop_file(), user_desktop);
151
152                 f = get_key_file(user_desktop);
153                 if (f) {
154                         if (g_key_file_has_key(f,
155                                                G_KEY_FILE_DESKTOP_GROUP,
156                                                "X-GNOME-Autostart-enabled",
157                                                NULL))
158                                 g_key_file_set_boolean
159                                         (f,
160                                          G_KEY_FILE_DESKTOP_GROUP,
161                                          "X-GNOME-Autostart-enabled",
162                                          TRUE);
163                         data = g_key_file_to_data(f, NULL, NULL);
164                         g_file_set_contents(user_desktop,
165                                             data,
166                                             -1,
167                                             NULL);
168
169                         g_key_file_free(f);
170                 }
171         } else {
172                 /* because X-GNOME-Autostart-enabled does not turn off
173                  * autostart on all Desktop Envs. */
174                 remove(user_desktop);
175         }
176
177         log_fct_exit();
178
179         free(user_desktop);
180 }