added fcts to handle autostart of psensor
[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 unsigned int pxdg_is_autostarted()
67 {
68         char *user_desktop;
69         unsigned int ret;
70
71         log_fct_enter();
72
73         user_desktop = get_user_desktop_file();
74
75         log_fct("user desktop file: %s", user_desktop);
76
77         ret = is_file_exists(user_desktop);
78
79         if (!ret)
80                 log_fct("user desktop file does not exist.");
81
82         free(user_desktop);
83
84         log_fct_exit();
85
86         return ret;
87 }
88
89 void pxdg_set_autostart(unsigned int enable)
90 {
91         char *user_desktop;
92
93         log_fct_enter();
94
95         user_desktop = get_user_desktop_file();
96
97         log_fct("user desktop file: %s", user_desktop);
98
99         log_fct("desktop file: %s", get_desktop_file());
100
101         if (enable) {
102                 if (!is_file_exists(user_desktop))
103                         file_copy(get_desktop_file(), user_desktop);
104         } else {
105                 remove(user_desktop);
106         }
107
108         log_fct_exit();
109
110         free(user_desktop);
111 }