Fixed restoration of the panel divider position.
[psensor.git] / src / cfg.c
index 3f140d9..d903478 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -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
@@ -119,6 +119,10 @@ static const char *KEY_PROVIDER_LIBATASMART_ENABLED
 static const char *KEY_PROVIDER_NVCTRL_ENABLED = "provider-nvctrl-enabled";
 static const char *KEY_PROVIDER_UDISKS2_ENABLED = "provider-udisks2-enabled";
 
+static const char *KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE
+= "default-high-threshold-temperature";
+static const char *KEY_DEFAULT_SENSOR_ALARM_ENABLED
+= "default-sensor-alarm-enabled";
 
 static GSettings *settings;
 
@@ -233,12 +237,12 @@ static void set_alpha_channeld_enabled(bool b)
        set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
 }
 
-static enum sensorlist_position get_sensorlist_position(void)
+enum sensorlist_position config_get_sensorlist_position(void)
 {
        return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
 }
 
-static void set_sensorlist_position(enum sensorlist_position pos)
+void config_set_sensorlist_position(enum sensorlist_position pos)
 {
        set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
 }
@@ -324,22 +328,22 @@ static void set_slog_interval(int interval)
        set_int(KEY_SLOG_INTERVAL, interval);
 }
 
-static bool is_window_decoration_enabled(void)
+bool config_is_window_decoration_enabled(void)
 {
        return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
 }
 
-static bool is_window_keep_below_enabled(void)
+bool config_is_window_keep_below_enabled(void)
 {
        return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
 }
 
-static void set_window_decoration_enabled(bool enabled)
+void config_set_window_decoration_enabled(bool enabled)
 {
        set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
 }
 
-static void set_window_keep_below_enabled(bool enabled)
+void config_set_window_keep_below_enabled(bool enabled)
 {
        set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
 }
@@ -354,6 +358,15 @@ void config_set_smooth_curves_enabled(bool b)
        set_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED, b);
 }
 
+double config_get_default_high_threshold_temperature(void)
+{
+       return get_double(KEY_DEFAULT_HIGH_THRESHOLD_TEMPERATURE);
+}
+
+static bool config_get_default_sensor_alarm_enabled(void)
+{
+       return get_bool(KEY_DEFAULT_SENSOR_ALARM_ENABLED);
+}
 
 static void init(void)
 {
@@ -405,9 +418,6 @@ struct config *config_load(void)
        c->graph_fgcolor = get_foreground_color();
        c->graph_bg_alpha = get_graph_background_alpha();
        c->alpha_channel_enabled = is_alpha_channel_enabled();
-       c->sensorlist_position = get_sensorlist_position();
-       c->window_decoration_enabled = is_window_decoration_enabled();
-       c->window_keep_below_enabled = is_window_keep_below_enabled();
        c->slog_enabled = is_slog_enabled();
        c->slog_interval = config_get_slog_interval();
 
@@ -425,13 +435,6 @@ struct config *config_load(void)
        if (c->graph_monitoring_duration < 1)
                c->graph_monitoring_duration = 10;
 
-       c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
-
-       c->unity_launcher_count_disabled
-               = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
-
-       c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
-
        c->window_restore_enabled
                = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
 
@@ -442,13 +445,13 @@ struct config *config_load(void)
 
        c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
 
+       c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
+
        if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
                c->window_w = 800;
                c->window_h = 200;
        }
 
-       c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
-
        c->sensor_values_max_length = compute_values_max_length(c);
 
        return c;
@@ -460,9 +463,6 @@ void config_save(const struct config *c)
        set_background_color(c->graph_bgcolor);
        set_foreground_color(c->graph_fgcolor);
        set_graph_background_alpha(c->graph_bg_alpha);
-       set_sensorlist_position(c->sensorlist_position);
-       set_window_decoration_enabled(c->window_decoration_enabled);
-       set_window_keep_below_enabled(c->window_keep_below_enabled);
        set_slog_enabled(c->slog_enabled);
        set_slog_interval(c->slog_interval);
 
@@ -472,11 +472,6 @@ void config_save(const struct config *c)
 
        set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval);
 
-       set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled);
-
-       set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
-                c->unity_launcher_count_disabled);
-
        set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
 
        set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
@@ -488,8 +483,6 @@ void config_save(const struct config *c)
        set_int(KEY_INTERFACE_WINDOW_H, c->window_h);
 
        set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
-
-       set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit);
 }
 
 const char *get_psensor_user_dir(void)
@@ -553,10 +546,12 @@ static GKeyFile *get_sensor_key_file(void)
                                                | G_KEY_FILE_KEEP_TRANSLATIONS,
                                                &err);
 
-               if (!ret)
+               if (!ret) {
                        log_warn(_("Failed to load configuration file %s: %s"),
                                 path,
                                 err->message);
+                       g_error_free(err);
+               }
        }
 
        return key_file;
@@ -609,12 +604,65 @@ static char *sensor_get_str(const char *sid, const char *att)
        return g_key_file_get_string(kfile, sid, att, NULL);
 }
 
-static bool sensor_get_bool(const char *sid, const char *att)
+static bool sensor_get_double(const char *sid,
+                             const char *att,
+                             double *d,
+                             double d_default)
 {
        GKeyFile *kfile;
+       GError *err;
+       double v;
 
        kfile = get_sensor_key_file();
-       return g_key_file_get_boolean(kfile, sid, att, NULL);
+
+       err = NULL;
+       if (!g_key_file_has_key(kfile, sid, att, &err)) {
+               if (err) {
+                       log_err(err->message);
+                       g_error_free(err);
+                       return false;
+               }
+
+               *d = d_default;
+               return true;
+       }
+
+       err = NULL;
+       v = g_key_file_get_double(kfile, sid, att, &err);
+
+       if (err) {
+               log_err(err->message);
+
+               g_error_free(err);
+
+               return false;
+       }
+
+       *d = v;
+       return true;
+}
+
+static bool sensor_get_bool(const char *sid, const char *att, bool dft)
+{
+       GKeyFile *kfile;
+       GError *err;
+       bool ret;
+
+       kfile = get_sensor_key_file();
+
+       err = NULL;
+       ret = g_key_file_get_boolean(kfile, sid, att, &err);
+
+       if (err) {
+               if (err->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND)
+                       ret = dft;
+               else
+                       log_err(err->message);
+
+               g_error_free(err);
+       }
+
+       return ret;
 }
 
 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
@@ -653,43 +701,85 @@ void config_set_sensor_name(const char *sid, const char *name)
        sensor_set_str(sid, ATT_SENSOR_NAME, name);
 }
 
-void config_set_sensor_color(const char *sid, const struct color *color)
-{
-       char *scolor;
+void config_set_sensor_color(const char *sid, const GdkRGBA *color)
+{
+       gchar *str;
+
+       str = gdk_rgba_to_string(color);
+
+       sensor_set_str(sid, ATT_SENSOR_COLOR, str);
+
+       g_free(str);
+}
+
+static const char *next_default_color(void)
+{
+       /* copied from the default colors of the gtk color color
+        * chooser.
+        */
+       const char *default_colors[27] = {
+               "#ef2929",  /* Scarlet Red */
+               "#fcaf3e",  /* Orange */
+               "#fce94f",  /* Butter */
+               "#8ae234",  /* Chameleon */
+               "#729fcf",  /* Sky Blue */
+               "#ad7fa8",  /* Plum */
+               "#e9b96e",  /* Chocolate */
+               "#888a85",  /* Aluminum 1 */
+               "#eeeeec",  /* Aluminum 2 */
+               "#cc0000",
+               "#f57900",
+               "#edd400",
+               "#73d216",
+               "#3465a4",
+               "#75507b",
+               "#c17d11",
+               "#555753",
+               "#d3d7cf",
+               "#a40000",
+               "#ce5c00",
+               "#c4a000",
+               "#4e9a06",
+               "#204a87",
+               "#5c3566",
+               "#8f5902",
+               "#2e3436",
+               "#babdb6"
+       };
+       static int next_idx;
+       const char *c;
+
+       c = default_colors[next_idx % 27];
+
+       next_idx++;
 
-       scolor = color_to_str(color);
-
-       sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
-
-       free(scolor);
+       return c;
 }
 
-struct color *
-config_get_sensor_color(const char *sid, const struct color *dft)
+GdkRGBA *config_get_sensor_color(const char *sid)
 {
-       char *scolor;
-       struct color *color;
-
-       scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
+       GdkRGBA rgba;
+       char *str;
+       gboolean ret;
 
-       if (scolor)
-               color = str_to_color(scolor);
-       else
-               color = NULL;
+       str = sensor_get_str(sid, ATT_SENSOR_COLOR);
 
-       if (!color) {
-               color = color_new(dft->red, dft->green, dft->blue);
-               config_set_sensor_color(sid, color);
+       if (str) {
+               ret = gdk_rgba_parse(&rgba, str);
+               free(str);
        }
 
-       free(scolor);
+       if (!str || !ret) {
+               gdk_rgba_parse(&rgba, next_default_color());
+               config_set_sensor_color(sid, &rgba);
+       }
 
-       return color;
+       return gdk_rgba_copy(&rgba);
 }
 
 bool config_is_sensor_graph_enabled(const char *sid)
 {
-       return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
+       return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED, false);
 }
 
 void config_set_sensor_graph_enabled(const char *sid, bool enabled)
@@ -697,9 +787,11 @@ void config_set_sensor_graph_enabled(const char *sid, bool enabled)
        sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
 }
 
-int config_get_sensor_alarm_high_threshold(const char *sid)
+bool config_get_sensor_alarm_high_threshold(const char *sid,
+                                           double *v,
+                                           double d)
 {
-       return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
+       return sensor_get_double(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, v, d);
 }
 
 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
@@ -707,9 +799,9 @@ void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
        sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
 }
 
-int config_get_sensor_alarm_low_threshold(const char *sid)
+bool config_get_sensor_alarm_low_threshold(const char *sid, double *v, double d)
 {
-       return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
+       return sensor_get_double(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, v, d);
 }
 
 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
@@ -719,7 +811,9 @@ void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
 
 bool config_is_appindicator_enabled(const char *sid)
 {
-       return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
+       return !sensor_get_bool(sid,
+                               ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
+                               false);
 }
 
 void config_set_appindicator_enabled(const char *sid, bool enabled)
@@ -741,7 +835,7 @@ void config_set_sensor_position(const char *sid, int pos)
 
 bool config_get_sensor_alarm_enabled(const char *sid)
 {
-       return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
+       return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED, false);
 }
 
 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
@@ -751,7 +845,9 @@ void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
 
 bool config_is_sensor_enabled(const char *sid)
 {
-       return !sensor_get_bool(sid, ATT_SENSOR_HIDE);
+       return !sensor_get_bool(sid,
+                               ATT_SENSOR_HIDE,
+                               config_get_default_sensor_alarm_enabled());
 }
 
 void config_set_sensor_enabled(const char *sid, bool enabled)
@@ -761,7 +857,9 @@ void config_set_sensor_enabled(const char *sid, bool enabled)
 
 bool config_is_appindicator_label_enabled(const char *sid)
 {
-       return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
+       return sensor_get_bool(sid,
+                              ATT_SENSOR_APPINDICATOR_LABEL_ENABLED,
+                              false);
 }
 
 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
@@ -843,3 +941,33 @@ void config_set_udisks2_enable(bool b)
 {
        set_bool(KEY_PROVIDER_UDISKS2_ENABLED, b);
 }
+
+enum temperature_unit config_get_temperature_unit(void)
+{
+       return get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
+}
+
+void config_set_temperature_unit(enum temperature_unit u)
+{
+       set_int(KEY_INTERFACE_TEMPERATURE_UNIT, u);
+}
+
+bool config_is_menu_bar_enabled(void)
+{
+       return !get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
+}
+
+void config_set_menu_bar_enabled(bool enabled)
+{
+       set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, !enabled);
+}
+
+bool config_is_count_visible(void)
+{
+       return !get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
+}
+
+void config_set_count_visible(bool visible)
+{
+       set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED, !visible);
+}