code cleanup
[psensor.git] / src / pxdg.c
index 3687c8f..7c3cffa 100644 (file)
@@ -28,6 +28,8 @@
 #include <pio.h>
 #include <plog.h>
 
+static const char *KEY_GNOME_AUTOSTART = "X-GNOME-Autostart-enabled";
+
 static char *get_user_autostart_dir()
 {
        const char *xdg_cfg_dir;
@@ -63,10 +65,45 @@ static int is_file_exists(const char *path)
        return stat(path, &st) == 0;
 }
 
-unsigned int pxdg_is_autostarted()
+static GKeyFile *get_key_file(const char *path)
+{
+       GKeyFile *kfile;
+       int ret;
+
+       kfile = g_key_file_new();
+       ret = g_key_file_load_from_file(kfile,
+                                       path,
+                                       G_KEY_FILE_KEEP_COMMENTS
+                                       | G_KEY_FILE_KEEP_TRANSLATIONS,
+                                       NULL);
+
+       if (ret) {
+               return kfile;
+       } else {
+               log_err("Failed to parse: %s", path);
+
+               g_key_file_free(kfile);
+               return NULL;
+       }
+}
+
+static int is_user_desktop_autostarted(GKeyFile *f)
+{
+       return (!g_key_file_has_key(f,
+                                   G_KEY_FILE_DESKTOP_GROUP,
+                                   KEY_GNOME_AUTOSTART,
+                                   NULL))
+               || g_key_file_get_boolean(f,
+                                         G_KEY_FILE_DESKTOP_GROUP,
+                                         KEY_GNOME_AUTOSTART,
+                                         NULL);
+}
+
+int pxdg_is_autostarted()
 {
        char *user_desktop;
        unsigned int ret;
+       GKeyFile *kfile;
 
        log_fct_enter();
 
@@ -76,8 +113,19 @@ unsigned int pxdg_is_autostarted()
 
        ret = is_file_exists(user_desktop);
 
-       if (!ret)
+       if (!ret) {
                log_fct("user desktop file does not exist.");
+       } else {
+               log_fct("user desktop file exist.");
+               if (ret) {
+                       kfile = get_key_file(user_desktop);
+                       if (kfile)
+                               ret = is_user_desktop_autostarted(kfile);
+                       else
+                               ret = -1;
+               }
+               g_key_file_free(kfile);
+       }
 
        free(user_desktop);
 
@@ -86,6 +134,30 @@ unsigned int pxdg_is_autostarted()
        return ret;
 }
 
+static void enable_gnome_autostart(const char *path)
+{
+       GKeyFile *f;
+       char *data;
+
+       f = get_key_file(path);
+       if (f) {
+               if (g_key_file_has_key(f,
+                                      G_KEY_FILE_DESKTOP_GROUP,
+                                      KEY_GNOME_AUTOSTART,
+                                      NULL))
+                       g_key_file_set_boolean(f,
+                                              G_KEY_FILE_DESKTOP_GROUP,
+                                              KEY_GNOME_AUTOSTART,
+                                              TRUE);
+               data = g_key_file_to_data(f, NULL, NULL);
+               g_file_set_contents(path, data, -1, NULL);
+
+               g_key_file_free(f);
+       } else {
+               log_err("Fail to enable %s", KEY_GNOME_AUTOSTART);
+       }
+}
+
 void pxdg_set_autostart(unsigned int enable)
 {
        char *user_desktop;
@@ -101,7 +173,10 @@ void pxdg_set_autostart(unsigned int enable)
        if (enable) {
                if (!is_file_exists(user_desktop))
                        file_copy(get_desktop_file(), user_desktop);
+               enable_gnome_autostart(user_desktop);
        } else {
+               /* because X-GNOME-Autostart-enabled does not turn off
+                * autostart on all Desktop Envs. */
                remove(user_desktop);
        }