enable gnome-autostart if the desktop file is in autostart dir but the entry turned off
authorJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 25 Apr 2014 12:56:38 +0000 (14:56 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 25 Apr 2014 12:56:38 +0000 (14:56 +0200)
(gnome startup application does not remove the file but turn off the entry)

po/Makefile.in
src/pxdg.c
src/pxdg.h

index 0ea07b3..37928c5 100644 (file)
@@ -21,7 +21,7 @@ srcdir = .
 top_srcdir = ..
 
 
-prefix = /usr/local
+prefix = /tmp
 exec_prefix = ${prefix}
 datarootdir = ${prefix}/share
 datadir = ${datarootdir}
index 3687c8f..12735d0 100644 (file)
@@ -63,10 +63,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,
+                                   "X-GNOME-Autostart-enabled",
+                                   NULL))
+               || g_key_file_get_boolean(f,
+                                         G_KEY_FILE_DESKTOP_GROUP,
+                                         "X-GNOME-Autostart-enabled",
+                                         NULL);
+}
+
+int pxdg_is_autostarted()
 {
        char *user_desktop;
        unsigned int ret;
+       GKeyFile *kfile;
 
        log_fct_enter();
 
@@ -76,8 +111,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);
 
@@ -88,7 +134,8 @@ unsigned int pxdg_is_autostarted()
 
 void pxdg_set_autostart(unsigned int enable)
 {
-       char *user_desktop;
+       char *user_desktop, *data;
+       GKeyFile *f;
 
        log_fct_enter();
 
@@ -101,7 +148,29 @@ void pxdg_set_autostart(unsigned int enable)
        if (enable) {
                if (!is_file_exists(user_desktop))
                        file_copy(get_desktop_file(), user_desktop);
+
+               f = get_key_file(user_desktop);
+               if (f) {
+                       if (g_key_file_has_key(f,
+                                              G_KEY_FILE_DESKTOP_GROUP,
+                                              "X-GNOME-Autostart-enabled",
+                                              NULL))
+                               g_key_file_set_boolean
+                                       (f,
+                                        G_KEY_FILE_DESKTOP_GROUP,
+                                        "X-GNOME-Autostart-enabled",
+                                        TRUE);
+                       data = g_key_file_to_data(f, NULL, NULL);
+                       g_file_set_contents(user_desktop,
+                                           data,
+                                           -1,
+                                           NULL);
+
+                       g_key_file_free(f);
+               }
        } else {
+               /* because X-GNOME-Autostart-enabled does not turn off
+                * autostart on all Desktop Envs. */
                remove(user_desktop);
        }
 
index 82abc44..78aa571 100644 (file)
 #ifndef _PXDG_H_
 #define _PXDG_H_
 
-unsigned int pxdg_is_autostarted();
+/*
+ * returns:
+ *  - 1 if psensor is currently launch during session startup.
+ *  - 0 if psensor is not currently launch during session startup.
+ *  - -1 if it cannot be determined.
+ */
+int pxdg_is_autostarted();
 void pxdg_set_autostart(unsigned int);
 
 #endif