refactoring
[psensor.git] / src / cfg.c
index 1486d2d..548323a 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 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 published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2012 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
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #define KEY_INTERFACE_HIDE_ON_STARTUP \
 "/apps/psensor/interface/hide_on_startup"
 
-GConfClient *client;
+#define KEY_INTERFACE_WINDOW_RESTORE_ENABLED \
+"/apps/psensor/interface/window_restore_enabled"
+
+#define KEY_INTERFACE_WINDOW_X "/apps/psensor/interface/window_x"
+#define KEY_INTERFACE_WINDOW_Y "/apps/psensor/interface/window_y"
+#define KEY_INTERFACE_WINDOW_W "/apps/psensor/interface/window_w"
+#define KEY_INTERFACE_WINDOW_H "/apps/psensor/interface/window_h"
+
+#define KEY_INTERFACE_WINDOW_DIVIDER_POS \
+"/apps/psensor/interface/window_divider_pos"
+
+#define KEY_INTERFACE_TEMPERATURE_UNIT \
+"/apps/psensor/interface/temperature_unit"
+
+static GConfClient *client;
 
 static char *get_string(char *key, char *default_value)
 {
@@ -82,7 +95,7 @@ static struct color *config_get_background_color()
 {
 
        char *scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
-                                        DEFAULT_GRAPH_BACKGROUND_COLOR);
+                                 DEFAULT_GRAPH_BACKGROUND_COLOR);
 
        struct color *c = string_to_color(scolor);
 
@@ -135,7 +148,6 @@ static double config_get_graph_background_alpha()
        double a = gconf_client_get_float(client,
                                          KEY_GRAPH_BACKGROUND_ALPHA,
                                          NULL);
-
        if (a == 0)
                gconf_client_set_float(client,
                                       KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
@@ -173,15 +185,17 @@ static void config_set_foreground_color(struct color *color)
        free(scolor);
 }
 
-static char *config_get_sensor_key(char *sensor_name)
+static char *get_sensor_att_key(const char *name, const char *att)
 {
-       char *escaped_name = gconf_escape_key(sensor_name, -1);
-       /* /apps/psensor/sensors/[sensor_name]/color */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 6 + 1);
+       char *esc_name, *key;
 
-       sprintf(key, "/apps/psensor/sensors/%s/color", escaped_name);
+       esc_name = gconf_escape_key(name, -1);
+       /* /apps/psensor/sensors/[esc_name]/[att] */
+       key = malloc(22 + 2 * strlen(esc_name) + 1 + strlen(att) + 1);
 
-       free(escaped_name);
+       sprintf(key, "/apps/psensor/sensors/%s/%s", esc_name, att);
+
+       free(esc_name);
 
        return key;
 }
@@ -189,13 +203,14 @@ static char *config_get_sensor_key(char *sensor_name)
 struct color *config_get_sensor_color(char *sensor_name,
                                      struct color *default_color)
 {
-       char *key = config_get_sensor_key(sensor_name);
+       char *key, *scolor;
+       struct color *color;
+
+       key = get_sensor_att_key(sensor_name, "color");
 
-       char *scolor = gconf_client_get_string(client,
-                                              key,
-                                              NULL);
+       scolor = gconf_client_get_string(client, key, NULL);
 
-       struct color *color = NULL;
+       color = NULL;
 
        if (scolor)
                color = string_to_color(scolor);
@@ -217,130 +232,152 @@ struct color *config_get_sensor_color(char *sensor_name,
 
 void config_set_sensor_color(char *sensor_name, struct color *color)
 {
-       char *key = config_get_sensor_key(sensor_name);
+       char *key, *scolor;
 
-       char *scolor = color_to_string(color);
+       key = get_sensor_att_key(sensor_name, "color");
+       scolor = color_to_string(color);
 
        gconf_client_set_string(client, key, scolor, NULL);
 
        free(scolor);
+       free(key);
 }
 
 int config_get_sensor_alarm_limit(char *sensor_name, int def)
 {
        int res;
-       char *escaped_name = gconf_escape_key(sensor_name, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sensor_name, -1);
        /* /apps/psensor/sensors/[sensor_name]/alarmlimit */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 10 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 10 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/alarmlimit", escaped_name);
 
        res = gconf_client_get_int(client, key, NULL);
 
        free(escaped_name);
+       free(key);
 
        return res ? res : def;
 }
 
 void config_set_sensor_alarm_limit(char *sensor_name, int alarm_limit)
 {
-       char *escaped_name = gconf_escape_key(sensor_name, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sensor_name, -1);
        /* /apps/psensor/sensors/[sensor_name]/alarmlimit */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 10 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 10 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/alarmlimit", escaped_name);
 
        gconf_client_set_int(client, key, alarm_limit, NULL);
 
        free(escaped_name);
+       free(key);
 }
 
 int config_get_sensor_alarm_enabled(char *sid)
 {
        gboolean res;
-       char *escaped_name = gconf_escape_key(sid, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sid, -1);
        /* /apps/psensor/sensors/[sensor_name]/alarmenabled */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 12 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 12 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/alarmenabled", escaped_name);
 
        res = gconf_client_get_bool(client, key, NULL);
 
        free(escaped_name);
+       free(key);
 
        return res == TRUE;
 }
 
 void config_set_sensor_alarm_enabled(char *sid, int enabled)
 {
-       char *escaped_name = gconf_escape_key(sid, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sid, -1);
        /* /apps/psensor/sensors/[sensor_name]/alarmenabled */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 12 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 12 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/alarmenabled", escaped_name);
 
        gconf_client_set_bool(client, key, enabled, NULL);
 
        free(escaped_name);
+       free(key);
 }
 
 int config_is_sensor_enabled(char *sid)
 {
        gboolean res;
-       char *escaped_name = gconf_escape_key(sid, -1);
-       /* /apps/psensor/sensors/[sensor_name]/enabled */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 7 + 1);
+       char *escaped_name, *key;
 
+       escaped_name = gconf_escape_key(sid, -1);
+       /* /apps/psensor/sensors/[sensor_name]/enabled */
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 7 + 1);
        sprintf(key, "/apps/psensor/sensors/%s/enabled", escaped_name);
 
        res = gconf_client_get_bool(client, key, NULL);
-
+       free(key);
        free(escaped_name);
 
        return res == TRUE;
-
 }
 
 void config_set_sensor_enabled(char *sid, int enabled)
 {
-       char *escaped_name = gconf_escape_key(sid, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sid, -1);
        /* /apps/psensor/sensors/[sensor_name]/enabled */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 7 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 7 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/enabled", escaped_name);
 
        gconf_client_set_bool(client, key, enabled, NULL);
 
        free(escaped_name);
+       free(key);
 }
 
 char *config_get_sensor_name(char *sid)
 {
-       char *res;
-       char *escaped_name = gconf_escape_key(sid, -1);
+       char *res, *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sid, -1);
        /* /apps/psensor/sensors/[sensor_name]/name */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 4 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 4 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/name", escaped_name);
 
        res = gconf_client_get_string(client, key, NULL);
 
        free(escaped_name);
+       free(key);
 
        return res;
 }
 
 void config_set_sensor_name(char *sid, const char *name)
 {
-       char *escaped_name = gconf_escape_key(sid, -1);
+       char *escaped_name, *key;
+
+       escaped_name = gconf_escape_key(sid, -1);
        /* /apps/psensor/sensors/[sensor_name]/name */
-       char *key = malloc(22 + 2 * strlen(escaped_name) + 1 + 4 + 1);
+       key = malloc(22 + 2 * strlen(escaped_name) + 1 + 4 + 1);
 
        sprintf(key, "/apps/psensor/sensors/%s/name", escaped_name);
 
        gconf_client_set_string(client, key, name, NULL);
 
        free(escaped_name);
+       free(key);
 }
 
 static int config_is_window_decoration_enabled()
@@ -389,93 +426,172 @@ static void config_set_window_keep_below_enabled(int enabled)
                                      FALSE, NULL);
 }
 
-void config_init()
+/*
+ * Initializes the GConf client.
+ */
+static void init()
+{
+       if (!client)
+               client = gconf_client_get_default();
+}
+
+void config_cleanup()
 {
-       client = gconf_client_get_default();
+       if (client) {
+               g_object_unref(client);
+               client = NULL;
+       }
 }
 
 struct config *config_load()
 {
-       struct config *cfg = malloc(sizeof(struct config));
+       struct config *c;
+
+       init();
+
+       c = malloc(sizeof(struct config));
 
-       cfg->graph_bgcolor = config_get_background_color();
-       cfg->graph_fgcolor = config_get_foreground_color();
-       cfg->graph_bg_alpha = config_get_graph_background_alpha();
-       cfg->alpha_channel_enabled = config_is_alpha_channel_enabled();
-       cfg->sensorlist_position = config_get_sensorlist_position();
-       cfg->window_decoration_enabled = config_is_window_decoration_enabled();
-       cfg->window_keep_below_enabled = config_is_window_keep_below_enabled();
+       c->graph_bgcolor = config_get_background_color();
+       c->graph_fgcolor = config_get_foreground_color();
+       c->graph_bg_alpha = config_get_graph_background_alpha();
+       c->alpha_channel_enabled = config_is_alpha_channel_enabled();
+       c->sensorlist_position = config_get_sensorlist_position();
+       c->window_decoration_enabled = config_is_window_decoration_enabled();
+       c->window_keep_below_enabled = config_is_window_keep_below_enabled();
 
-       cfg->sensor_update_interval
+       c->sensor_update_interval
            = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
-       if (cfg->sensor_update_interval < 1)
-               cfg->sensor_update_interval = 1;
+       if (c->sensor_update_interval < 1)
+               c->sensor_update_interval = 1;
 
-       cfg->graph_update_interval
+       c->graph_update_interval
            = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
-       if (cfg->graph_update_interval < 1)
-               cfg->graph_update_interval = 1;
+       if (c->graph_update_interval < 1)
+               c->graph_update_interval = 1;
 
-       cfg->graph_monitoring_duration
+       c->graph_monitoring_duration
            = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
 
-       if (cfg->graph_monitoring_duration < 1)
-               cfg->graph_monitoring_duration = 10;
+       if (c->graph_monitoring_duration < 1)
+               c->graph_monitoring_duration = 10;
 
-       cfg->sensor_values_max_length
+       c->sensor_values_max_length
            =
-           (cfg->graph_monitoring_duration * 60) / cfg->sensor_update_interval;
-       if (cfg->sensor_values_max_length < 3)
-               cfg->sensor_values_max_length = 3;
+           (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
+       if (c->sensor_values_max_length < 3)
+               c->sensor_values_max_length = 3;
 
-       cfg->menu_bar_disabled = gconf_client_get_bool
-               (client,
-                KEY_INTERFACE_MENU_BAR_DISABLED,
-                NULL);
+       c->menu_bar_disabled
+               = gconf_client_get_bool(client,
+                                       KEY_INTERFACE_MENU_BAR_DISABLED,
+                                       NULL);
 
-       cfg->unity_launcher_count_disabled = gconf_client_get_bool
+       c->unity_launcher_count_disabled
+               = gconf_client_get_bool
                (client,
                 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
                 NULL);
 
-       cfg->hide_on_startup = gconf_client_get_bool
-               (client,
-                KEY_INTERFACE_HIDE_ON_STARTUP,
-                NULL);
+       c->hide_on_startup
+               = gconf_client_get_bool(client,
+                                       KEY_INTERFACE_HIDE_ON_STARTUP,
+                                       NULL);
+
+       c->window_restore_enabled
+               = gconf_client_get_bool(client,
+                                       KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
+                                       NULL);
+
+       c->window_x = gconf_client_get_int(client,
+                                          KEY_INTERFACE_WINDOW_X,
+                                          NULL);
+       c->window_y = gconf_client_get_int(client,
+                                          KEY_INTERFACE_WINDOW_Y,
+                                          NULL);
+       c->window_w = gconf_client_get_int(client,
+                                          KEY_INTERFACE_WINDOW_W,
+                                          NULL);
+       c->window_h = gconf_client_get_int(client,
+                                          KEY_INTERFACE_WINDOW_H,
+                                          NULL);
+       c->window_divider_pos
+               = gconf_client_get_int(client,
+                                      KEY_INTERFACE_WINDOW_DIVIDER_POS,
+                                      NULL);
+
+       if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
+               c->window_w = 800;
+               c->window_h = 200;
+       }
+
+       c->temperature_unit = gconf_client_get_int
+               (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
 
-       return cfg;
+       return c;
 }
 
-void config_save(struct config *cfg)
+void config_save(struct config *c)
 {
-       config_set_background_color(cfg->graph_bgcolor);
-       config_set_foreground_color(cfg->graph_fgcolor);
-       config_set_graph_background_alpha(cfg->graph_bg_alpha);
-       config_set_sensorlist_position(cfg->sensorlist_position);
-       config_set_window_decoration_enabled(cfg->window_decoration_enabled);
-       config_set_window_keep_below_enabled(cfg->window_keep_below_enabled);
+       config_set_background_color(c->graph_bgcolor);
+       config_set_foreground_color(c->graph_fgcolor);
+       config_set_graph_background_alpha(c->graph_bg_alpha);
+       config_set_sensorlist_position(c->sensorlist_position);
+       config_set_window_decoration_enabled(c->window_decoration_enabled);
+       config_set_window_keep_below_enabled(c->window_keep_below_enabled);
 
        gconf_client_set_int(client,
                             KEY_GRAPH_UPDATE_INTERVAL,
-                            cfg->graph_update_interval, NULL);
+                            c->graph_update_interval, NULL);
 
        gconf_client_set_int(client,
                             KEY_GRAPH_MONITORING_DURATION,
-                            cfg->graph_monitoring_duration, NULL);
+                            c->graph_monitoring_duration, NULL);
 
        gconf_client_set_int(client,
                             KEY_SENSOR_UPDATE_INTERVAL,
-                            cfg->sensor_update_interval, NULL);
+                            c->sensor_update_interval, NULL);
 
        gconf_client_set_bool(client,
                              KEY_INTERFACE_MENU_BAR_DISABLED,
-                             cfg->menu_bar_disabled, NULL);
+                             c->menu_bar_disabled, NULL);
 
        gconf_client_set_bool(client,
                              KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
-                             cfg->unity_launcher_count_disabled, NULL);
+                             c->unity_launcher_count_disabled, NULL);
 
        gconf_client_set_bool(client,
                              KEY_INTERFACE_HIDE_ON_STARTUP,
-                             cfg->hide_on_startup, NULL);
+                             c->hide_on_startup, NULL);
+
+       gconf_client_set_bool(client,
+                             KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
+                             c->window_restore_enabled,
+                             NULL);
+
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_WINDOW_X,
+                            c->window_x,
+                            NULL);
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_WINDOW_Y,
+                            c->window_y,
+                            NULL);
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_WINDOW_W,
+                            c->window_w,
+                            NULL);
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_WINDOW_H,
+                            c->window_h,
+                            NULL);
+
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_WINDOW_DIVIDER_POS,
+                            c->window_divider_pos,
+                            NULL);
+
+       gconf_client_set_int(client,
+                            KEY_INTERFACE_TEMPERATURE_UNIT,
+                            c->temperature_unit,
+                            NULL);
 }