2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 static const char *KEY_SENSORS = "/apps/psensor/sensors";
29 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm/enabled";
30 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm/high_threshold";
31 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm/low_threshold";
32 static const char *ATT_SENSOR_COLOR = "color";
33 static const char *ATT_SENSOR_ENABLED = "enabled";
34 static const char *ATT_SENSOR_NAME = "name";
35 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
36 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
37 = "appindicator/menu/enabled";
39 static const char *ATT_SENSOR_POSITION = "position";
41 static const char *KEY_SENSOR_UPDATE_INTERVAL
42 = "/apps/psensor/sensor/update_interval";
44 static const char *KEY_GRAPH_UPDATE_INTERVAL
45 = "/apps/psensor/graph/update_interval";
47 static const char *KEY_GRAPH_MONITORING_DURATION
48 = "/apps/psensor/graph/monitoring_duration";
50 static const char *KEY_GRAPH_BACKGROUND_COLOR
51 = "/apps/psensor/graph/background_color";
53 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
55 static const char *KEY_GRAPH_BACKGROUND_ALPHA
56 = "/apps/psensor/graph/background_alpha";
58 static const char *KEY_GRAPH_FOREGROUND_COLOR
59 = "/apps/psensor/graph/foreground_color";
60 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
62 static const char *KEY_ALPHA_CHANNEL_ENABLED
63 = "/apps/psensor/graph/alpha_channel_enabled";
65 static const char *KEY_INTERFACE_SENSORLIST_POSITION
66 = "/apps/psensor/interface/sensorlist_position";
68 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
69 = "/apps/psensor/interface/window_decoration_disabled";
71 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
72 = "/apps/psensor/interface/window_keep_below_enabled";
74 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
75 = "/apps/psensor/interface/menu_bar_disabled";
77 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
78 = "/apps/psensor/interface/unity_launcher_count_disabled";
80 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
81 = "/apps/psensor/interface/hide_on_startup";
83 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
84 = "/apps/psensor/interface/window_restore_enabled";
86 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
87 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
88 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
89 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
91 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
92 = "/apps/psensor/interface/window_divider_pos";
94 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
95 = "/apps/psensor/interface/temperature_unit";
97 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
98 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
100 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
102 static GConfClient *client;
104 static char *get_string(const char *key, const char *default_value)
108 value = gconf_client_get_string(client, key, NULL);
111 value = strdup(default_value);
112 gconf_client_set_string(client, key, default_value, NULL);
118 char *config_get_notif_script()
122 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
123 if (str && !strlen(str)) {
131 void config_set_notif_script(const char *str)
133 if (str && strlen(str) > 0)
134 gconf_client_set_string(client,
135 KEY_NOTIFICATION_SCRIPT, str, NULL);
137 gconf_client_set_string(client,
138 KEY_NOTIFICATION_SCRIPT, "", NULL);
141 static struct color *get_background_color()
146 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
147 DEFAULT_GRAPH_BACKGROUND_COLOR);
149 c = str_to_color(scolor);
153 return color_new(0xffff, 0xffff, 0xffff);
158 static struct color *get_foreground_color()
163 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
164 DEFAULT_GRAPH_FOREGROUND_COLOR);
166 c = str_to_color(scolor);
170 return color_new(0x0000, 0x0000, 0x0000);
175 static bool is_alpha_channel_enabled()
177 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
180 static enum sensorlist_position get_sensorlist_position()
182 return gconf_client_get_int(client,
183 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
186 static void set_sensorlist_position(enum sensorlist_position pos)
188 gconf_client_set_int(client,
189 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
192 static double get_graph_background_alpha()
196 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
198 gconf_client_set_float(client,
199 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
203 static void set_graph_background_alpha(double alpha)
205 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
208 static void set_background_color(const struct color *color)
212 scolor = color_to_str(color);
214 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
216 gconf_client_set_string(client,
217 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
222 static void set_foreground_color(const struct color *color)
226 str = color_to_str(color);
228 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
230 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
235 static char *get_sensor_att_key(const char *sid, const char *att)
239 esc_sid = gconf_escape_key(sid, -1);
240 /* [KEY_SENSORS]/[esc_sid]/[att] */
241 key = malloc(strlen(KEY_SENSORS)
242 + 1 + 2 * strlen(esc_sid)
243 + 1 + strlen(att) + 1);
245 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
253 config_get_sensor_color(const char *sid, const struct color *dft)
258 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
260 scolor = gconf_client_get_string(client, key, NULL);
265 color = str_to_color(scolor);
267 if (!scolor || !color) {
268 color = color_new(dft->red, dft->green, dft->blue);
269 scolor = color_to_str(color);
270 gconf_client_set_string(client, key, scolor, NULL);
279 void config_set_sensor_color(const char *sid, const struct color *color)
283 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
284 scolor = color_to_str(color);
286 gconf_client_set_string(client, key, scolor, NULL);
292 int config_get_sensor_alarm_high_threshold(const char *sid)
297 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
298 res = gconf_client_get_int(client, key, NULL);
305 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
309 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
310 gconf_client_set_int(client, key, threshold, NULL);
314 int config_get_sensor_alarm_low_threshold(const char *sid)
319 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
320 res = gconf_client_get_int(client, key, NULL);
327 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
331 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
332 gconf_client_set_int(client, key, threshold, NULL);
336 bool config_get_sensor_alarm_enabled(const char *sid)
341 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
342 b = gconf_client_get_bool(client, key, NULL);
348 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
352 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
353 gconf_client_set_bool(client, key, enabled, NULL);
357 bool config_is_sensor_enabled(const char *sid)
362 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
363 b = gconf_client_get_bool(client, key, NULL);
369 void config_set_sensor_enabled(const char *sid, bool enabled)
373 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
374 gconf_client_set_bool(client, key, enabled, NULL);
378 char *config_get_sensor_name(const char *sid)
382 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
383 name = gconf_client_get_string(client, key, NULL);
389 void config_set_sensor_name(const char *sid, const char *name)
393 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
394 gconf_client_set_string(client, key, name, NULL);
398 int config_get_sensor_position(const char *sid)
403 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
404 pos = gconf_client_get_int(client, key, NULL);
410 void config_set_sensor_position(const char *sid, int pos)
414 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
415 gconf_client_set_int(client, key, pos, NULL);
419 bool config_is_appindicator_enabled(const char *sid)
424 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
425 b = gconf_client_get_bool(client, key, NULL);
431 void config_set_appindicator_enabled(const char *sid, bool enabled)
435 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
436 gconf_client_set_bool(client, key, !enabled, NULL);
440 bool config_is_appindicator_label_enabled(const char *sid)
445 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
446 b = gconf_client_get_bool(client, key, NULL);
452 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
456 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
457 gconf_client_set_bool(client, key, enabled, NULL);
461 bool is_slog_enabled()
463 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
466 static void set_slog_enabled(bool enabled)
468 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
471 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
473 log_debug("config_slog_enabled_notify_add");
474 gconf_client_add_dir(client,
476 GCONF_CLIENT_PRELOAD_NONE,
478 gconf_client_notify_add(client,
486 int config_get_slog_interval()
490 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
498 static void set_slog_interval(int interval)
503 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
506 static bool is_window_decoration_enabled()
508 return !gconf_client_get_bool(client,
509 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
513 static bool is_window_keep_below_enabled()
515 return gconf_client_get_bool(client,
516 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
520 static void set_window_decoration_enabled(bool enabled)
522 gconf_client_set_bool
524 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
527 static void set_window_keep_below_enabled(bool enabled)
529 gconf_client_set_bool(client,
530 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
535 * Initializes the GConf client.
540 client = gconf_client_get_default();
543 void config_cleanup()
546 g_object_unref(client);
551 struct config *config_load()
557 c = malloc(sizeof(struct config));
559 c->graph_bgcolor = get_background_color();
560 c->graph_fgcolor = get_foreground_color();
561 c->graph_bg_alpha = get_graph_background_alpha();
562 c->alpha_channel_enabled = is_alpha_channel_enabled();
563 c->sensorlist_position = get_sensorlist_position();
564 c->window_decoration_enabled = is_window_decoration_enabled();
565 c->window_keep_below_enabled = is_window_keep_below_enabled();
566 c->slog_enabled = is_slog_enabled();
567 c->slog_interval = config_get_slog_interval();
569 c->sensor_update_interval
570 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
571 if (c->sensor_update_interval < 1)
572 c->sensor_update_interval = 1;
574 c->graph_update_interval
575 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
576 if (c->graph_update_interval < 1)
577 c->graph_update_interval = 1;
579 c->graph_monitoring_duration
580 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
582 if (c->graph_monitoring_duration < 1)
583 c->graph_monitoring_duration = 10;
585 c->sensor_values_max_length
587 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
588 if (c->sensor_values_max_length < 3)
589 c->sensor_values_max_length = 3;
592 = gconf_client_get_bool(client,
593 KEY_INTERFACE_MENU_BAR_DISABLED,
596 c->unity_launcher_count_disabled
597 = gconf_client_get_bool
599 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
603 = gconf_client_get_bool(client,
604 KEY_INTERFACE_HIDE_ON_STARTUP,
607 c->window_restore_enabled
608 = gconf_client_get_bool(client,
609 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
612 c->window_x = gconf_client_get_int(client,
613 KEY_INTERFACE_WINDOW_X,
615 c->window_y = gconf_client_get_int(client,
616 KEY_INTERFACE_WINDOW_Y,
618 c->window_w = gconf_client_get_int(client,
619 KEY_INTERFACE_WINDOW_W,
621 c->window_h = gconf_client_get_int(client,
622 KEY_INTERFACE_WINDOW_H,
624 c->window_divider_pos
625 = gconf_client_get_int(client,
626 KEY_INTERFACE_WINDOW_DIVIDER_POS,
629 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
634 c->temperature_unit = gconf_client_get_int
635 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
640 void config_save(const struct config *c)
642 set_background_color(c->graph_bgcolor);
643 set_foreground_color(c->graph_fgcolor);
644 set_graph_background_alpha(c->graph_bg_alpha);
645 set_sensorlist_position(c->sensorlist_position);
646 set_window_decoration_enabled(c->window_decoration_enabled);
647 set_window_keep_below_enabled(c->window_keep_below_enabled);
648 set_slog_enabled(c->slog_enabled);
649 set_slog_interval(c->slog_interval);
651 gconf_client_set_int(client,
652 KEY_GRAPH_UPDATE_INTERVAL,
653 c->graph_update_interval, NULL);
655 gconf_client_set_int(client,
656 KEY_GRAPH_MONITORING_DURATION,
657 c->graph_monitoring_duration, NULL);
659 gconf_client_set_int(client,
660 KEY_SENSOR_UPDATE_INTERVAL,
661 c->sensor_update_interval, NULL);
663 gconf_client_set_bool(client,
664 KEY_INTERFACE_MENU_BAR_DISABLED,
665 c->menu_bar_disabled, NULL);
667 gconf_client_set_bool(client,
668 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
669 c->unity_launcher_count_disabled, NULL);
671 gconf_client_set_bool(client,
672 KEY_INTERFACE_HIDE_ON_STARTUP,
673 c->hide_on_startup, NULL);
675 gconf_client_set_bool(client,
676 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
677 c->window_restore_enabled,
680 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
681 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
682 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
683 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
685 gconf_client_set_int(client,
686 KEY_INTERFACE_WINDOW_DIVIDER_POS,
687 c->window_divider_pos,
690 gconf_client_set_int(client,
691 KEY_INTERFACE_TEMPERATURE_UNIT,