merged from plib
[psensor.git] / src / cfg.c
index 459193b..a5014ff 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2010-2014 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
@@ -21,9 +21,8 @@
 #include <string.h>
 #include <ctype.h>
 
-#include <gconf/gconf-client.h>
-
 #include "cfg.h"
+#include <plog.h>
 
 static const char *KEY_SENSORS = "/apps/psensor/sensors";
 
@@ -34,6 +33,10 @@ static const char *ATT_SENSOR_COLOR = "color";
 static const char *ATT_SENSOR_ENABLED = "enabled";
 static const char *ATT_SENSOR_NAME = "name";
 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
+static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
+= "appindicator/menu/enabled";
+
+static const char *ATT_SENSOR_POSITION = "position";
 
 static const char *KEY_SENSOR_UPDATE_INTERVAL
 = "/apps/psensor/sensor/update_interval";
@@ -91,6 +94,11 @@ static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
 = "/apps/psensor/interface/temperature_unit";
 
+static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
+static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
+
+static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
+
 static GConfClient *client;
 
 static char *get_string(const char *key, const char *default_value)
@@ -107,6 +115,29 @@ static char *get_string(const char *key, const char *default_value)
        return value;
 }
 
+char *config_get_notif_script()
+{
+       char *str;
+
+       str =  gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
+       if (str && !strlen(str)) {
+               free(str);
+               str = NULL;
+       }
+
+       return str;
+}
+
+void config_set_notif_script(const char *str)
+{
+       if (str && strlen(str) > 0)
+               gconf_client_set_string(client,
+                                       KEY_NOTIFICATION_SCRIPT, str, NULL);
+       else
+               gconf_client_set_string(client,
+                                       KEY_NOTIFICATION_SCRIPT, "", NULL);
+}
+
 static struct color *get_background_color()
 {
        char *scolor;
@@ -141,7 +172,7 @@ static struct color *get_foreground_color()
        return c;
 }
 
-static unsigned int is_alpha_channel_enabled()
+static bool is_alpha_channel_enabled()
 {
        return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
 }
@@ -302,7 +333,7 @@ config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
        free(key);
 }
 
-int config_get_sensor_alarm_enabled(const char *sid)
+bool config_get_sensor_alarm_enabled(const char *sid)
 {
        gboolean b;
        char *key;
@@ -314,7 +345,7 @@ int config_get_sensor_alarm_enabled(const char *sid)
        return b;
 }
 
-void config_set_sensor_alarm_enabled(const char *sid, int enabled)
+void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
 {
        char *key;
 
@@ -323,7 +354,7 @@ void config_set_sensor_alarm_enabled(const char *sid, int enabled)
        free(key);
 }
 
-int config_is_sensor_enabled(const char *sid)
+bool config_is_sensor_enabled(const char *sid)
 {
        gboolean b;
        char *key;
@@ -335,7 +366,7 @@ int config_is_sensor_enabled(const char *sid)
        return b;
 }
 
-void config_set_sensor_enabled(const char *sid, int enabled)
+void config_set_sensor_enabled(const char *sid, bool enabled)
 {
        char *key;
 
@@ -364,7 +395,28 @@ void config_set_sensor_name(const char *sid, const char *name)
        free(key);
 }
 
-unsigned int config_is_appindicator_enabled(const char *sid)
+int config_get_sensor_position(const char *sid)
+{
+       char *key;
+       int pos;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
+       pos = gconf_client_get_int(client, key, NULL);
+       free(key);
+
+       return pos;
+}
+
+void config_set_sensor_position(const char *sid, int pos)
+{
+       char *key;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
+       gconf_client_set_int(client, key, pos, NULL);
+       free(key);
+}
+
+bool config_is_appindicator_enabled(const char *sid)
 {
        char *key;
        gboolean b;
@@ -376,7 +428,7 @@ unsigned int config_is_appindicator_enabled(const char *sid)
        return !b;
 }
 
-void config_set_appindicator_enabled(const char *sid, unsigned int enabled)
+void config_set_appindicator_enabled(const char *sid, bool enabled)
 {
        char *key;
 
@@ -385,29 +437,94 @@ void config_set_appindicator_enabled(const char *sid, unsigned int enabled)
        free(key);
 }
 
+bool config_is_appindicator_label_enabled(const char *sid)
+{
+       char *key;
+       gboolean b;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
+       b = gconf_client_get_bool(client, key, NULL);
+       free(key);
+
+       return b;
+}
+
+void config_set_appindicator_label_enabled(const char *sid, bool enabled)
+{
+       char *key;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
+       gconf_client_set_bool(client, key, enabled, NULL);
+       free(key);
+}
+
+bool is_slog_enabled()
+{
+       return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
+}
+
+static void set_slog_enabled(bool enabled)
+{
+       gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
+}
+
+void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
+{
+       log_debug("config_slog_enabled_notify_add");
+       gconf_client_add_dir(client,
+                            KEY_SLOG_ENABLED,
+                            GCONF_CLIENT_PRELOAD_NONE,
+                            NULL);
+       gconf_client_notify_add(client,
+                               KEY_SLOG_ENABLED,
+                               cbk,
+                               data,
+                               NULL,
+                               NULL);
+}
+
+int config_get_slog_interval()
+{
+       int res;
+
+       res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
+
+       if (res <= 0)
+               return 300;
+       else
+               return res;
+}
+
+static void set_slog_interval(int interval)
+{
+       if (interval <= 0)
+               interval = 300;
+
+       gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
+}
 
-static int is_window_decoration_enabled()
+static bool is_window_decoration_enabled()
 {
        return !gconf_client_get_bool(client,
                                      KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
                                      NULL);
 }
 
-static int is_window_keep_below_enabled()
+static bool is_window_keep_below_enabled()
 {
        return gconf_client_get_bool(client,
                                     KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
                                     NULL);
 }
 
-static void set_window_decoration_enabled(int enabled)
+static void set_window_decoration_enabled(bool enabled)
 {
        gconf_client_set_bool
                (client,
                 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
 }
 
-static void set_window_keep_below_enabled(int enabled)
+static void set_window_keep_below_enabled(bool enabled)
 {
        gconf_client_set_bool(client,
                              KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
@@ -446,6 +563,8 @@ struct config *config_load()
        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();
 
        c->sensor_update_interval
            = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
@@ -526,6 +645,8 @@ void config_save(const struct config *c)
        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);
 
        gconf_client_set_int(client,
                             KEY_GRAPH_UPDATE_INTERVAL,