Fixed restoration of the panel divider position.
[psensor.git] / src / pxdg.c
index 12735d0..a43d075 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2014 jeanfi@gmail.com
+ * Copyright (C) 2010-2016 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 
 #include <pio.h>
 #include <plog.h>
+#include <pxdg.h>
 
-static char *get_user_autostart_dir()
+static const char *KEY_GNOME_AUTOSTART = "X-GNOME-Autostart-enabled";
+
+static char *get_user_autostart_dir(void)
 {
        const char *xdg_cfg_dir;
 
@@ -39,21 +42,21 @@ static char *get_user_autostart_dir()
        return path_append(xdg_cfg_dir, "autostart");
 }
 
-static char *get_user_desktop_file()
+static char *get_user_desktop_file(void)
 {
        char *dir, *path;
 
        dir = get_user_autostart_dir();
-       path = path_append(dir, "psensor.desktop");
+       path = path_append(dir, PSENSOR_DESKTOP_FILE);
 
        free(dir);
 
        return path;
 }
 
-static const char *get_desktop_file()
+static const char *get_desktop_file(void)
 {
-       return DATADIR"/applications/psensor.desktop";
+       return DATADIR"/applications/"PSENSOR_DESKTOP_FILE;
 }
 
 static int is_file_exists(const char *path)
@@ -75,29 +78,28 @@ static GKeyFile *get_key_file(const char *path)
                                        | G_KEY_FILE_KEEP_TRANSLATIONS,
                                        NULL);
 
-       if (ret) {
+       if (ret)
                return kfile;
-       } else {
-               log_err("Failed to parse: %s", path);
 
-               g_key_file_free(kfile);
-               return NULL;
-       }
+       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",
+                                   KEY_GNOME_AUTOSTART,
                                    NULL))
                || g_key_file_get_boolean(f,
                                          G_KEY_FILE_DESKTOP_GROUP,
-                                         "X-GNOME-Autostart-enabled",
+                                         KEY_GNOME_AUTOSTART,
                                          NULL);
 }
 
-int pxdg_is_autostarted()
+int pxdg_is_autostarted(void)
 {
        char *user_desktop;
        unsigned int ret;
@@ -121,8 +123,8 @@ int pxdg_is_autostarted()
                                ret = is_user_desktop_autostarted(kfile);
                        else
                                ret = -1;
+                       g_key_file_free(kfile);
                }
-               g_key_file_free(kfile);
        }
 
        free(user_desktop);
@@ -132,10 +134,33 @@ int pxdg_is_autostarted()
        return ret;
 }
 
-void pxdg_set_autostart(unsigned int enable)
+static void enable_gnome_autostart(const char *path)
 {
-       char *user_desktop, *data;
        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, *dir;
 
        log_fct_enter();
 
@@ -146,31 +171,17 @@ void pxdg_set_autostart(unsigned int enable)
        log_fct("desktop file: %s", get_desktop_file());
 
        if (enable) {
-               if (!is_file_exists(user_desktop))
+               if (!is_file_exists(user_desktop)) {
+                       dir = get_user_autostart_dir();
+                       mkdirs(dir, 0700);
+                       free(dir);
                        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);
                }
+               enable_gnome_autostart(user_desktop);
        } else {
                /* because X-GNOME-Autostart-enabled does not turn off
-                * autostart on all Desktop Envs. */
+                * autostart on all Desktop Envs.
+                */
                remove(user_desktop);
        }