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_POSITION = "position";
38 static const char *KEY_SENSOR_UPDATE_INTERVAL
39 = "/apps/psensor/sensor/update_interval";
41 static const char *KEY_GRAPH_UPDATE_INTERVAL
42 = "/apps/psensor/graph/update_interval";
44 static const char *KEY_GRAPH_MONITORING_DURATION
45 = "/apps/psensor/graph/monitoring_duration";
47 static const char *KEY_GRAPH_BACKGROUND_COLOR
48 = "/apps/psensor/graph/background_color";
50 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
52 static const char *KEY_GRAPH_BACKGROUND_ALPHA
53 = "/apps/psensor/graph/background_alpha";
55 static const char *KEY_GRAPH_FOREGROUND_COLOR
56 = "/apps/psensor/graph/foreground_color";
57 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
59 static const char *KEY_ALPHA_CHANNEL_ENABLED
60 = "/apps/psensor/graph/alpha_channel_enabled";
62 static const char *KEY_INTERFACE_SENSORLIST_POSITION
63 = "/apps/psensor/interface/sensorlist_position";
65 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
66 = "/apps/psensor/interface/window_decoration_disabled";
68 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
69 = "/apps/psensor/interface/window_keep_below_enabled";
71 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
72 = "/apps/psensor/interface/menu_bar_disabled";
74 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
75 = "/apps/psensor/interface/unity_launcher_count_disabled";
77 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
78 = "/apps/psensor/interface/hide_on_startup";
80 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
81 = "/apps/psensor/interface/window_restore_enabled";
83 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
84 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
85 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
86 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
88 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
89 = "/apps/psensor/interface/window_divider_pos";
91 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
92 = "/apps/psensor/interface/temperature_unit";
94 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
95 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
97 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
99 static GConfClient *client;
101 static char *get_string(const char *key, const char *default_value)
105 value = gconf_client_get_string(client, key, NULL);
108 value = strdup(default_value);
109 gconf_client_set_string(client, key, default_value, NULL);
115 char *config_get_notif_script()
119 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
120 if (str && !strlen(str)) {
128 void config_set_notif_script(const char *str)
130 if (str && strlen(str) > 0)
131 gconf_client_set_string(client,
132 KEY_NOTIFICATION_SCRIPT, str, NULL);
134 gconf_client_set_string(client,
135 KEY_NOTIFICATION_SCRIPT, "", NULL);
138 static struct color *get_background_color()
143 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
144 DEFAULT_GRAPH_BACKGROUND_COLOR);
146 c = str_to_color(scolor);
150 return color_new(0xffff, 0xffff, 0xffff);
155 static struct color *get_foreground_color()
160 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
161 DEFAULT_GRAPH_FOREGROUND_COLOR);
163 c = str_to_color(scolor);
167 return color_new(0x0000, 0x0000, 0x0000);
172 static bool is_alpha_channel_enabled()
174 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
177 static enum sensorlist_position get_sensorlist_position()
179 return gconf_client_get_int(client,
180 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
183 static void set_sensorlist_position(enum sensorlist_position pos)
185 gconf_client_set_int(client,
186 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
189 static double get_graph_background_alpha()
193 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
195 gconf_client_set_float(client,
196 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
200 static void set_graph_background_alpha(double alpha)
202 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
205 static void set_background_color(const struct color *color)
209 scolor = color_to_str(color);
211 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
213 gconf_client_set_string(client,
214 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
219 static void set_foreground_color(const struct color *color)
223 str = color_to_str(color);
225 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
227 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
232 static char *get_sensor_att_key(const char *sid, const char *att)
236 esc_sid = gconf_escape_key(sid, -1);
237 /* [KEY_SENSORS]/[esc_sid]/[att] */
238 key = malloc(strlen(KEY_SENSORS)
239 + 1 + 2 * strlen(esc_sid)
240 + 1 + strlen(att) + 1);
242 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
250 config_get_sensor_color(const char *sid, const struct color *dft)
255 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
257 scolor = gconf_client_get_string(client, key, NULL);
262 color = str_to_color(scolor);
264 if (!scolor || !color) {
265 color = color_new(dft->red, dft->green, dft->blue);
266 scolor = color_to_str(color);
267 gconf_client_set_string(client, key, scolor, NULL);
276 void config_set_sensor_color(const char *sid, const struct color *color)
280 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
281 scolor = color_to_str(color);
283 gconf_client_set_string(client, key, scolor, NULL);
289 int config_get_sensor_alarm_high_threshold(const char *sid)
294 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
295 res = gconf_client_get_int(client, key, NULL);
302 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
306 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
307 gconf_client_set_int(client, key, threshold, NULL);
311 int config_get_sensor_alarm_low_threshold(const char *sid)
316 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
317 res = gconf_client_get_int(client, key, NULL);
324 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
328 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
329 gconf_client_set_int(client, key, threshold, NULL);
333 bool config_get_sensor_alarm_enabled(const char *sid)
338 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
339 b = gconf_client_get_bool(client, key, NULL);
345 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
349 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
350 gconf_client_set_bool(client, key, enabled, NULL);
354 bool config_is_sensor_enabled(const char *sid)
359 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
360 b = gconf_client_get_bool(client, key, NULL);
366 void config_set_sensor_enabled(const char *sid, bool enabled)
370 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
371 gconf_client_set_bool(client, key, enabled, NULL);
375 char *config_get_sensor_name(const char *sid)
379 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
380 name = gconf_client_get_string(client, key, NULL);
386 void config_set_sensor_name(const char *sid, const char *name)
390 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
391 gconf_client_set_string(client, key, name, NULL);
395 int config_get_sensor_position(const char *sid)
400 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
401 pos = gconf_client_get_int(client, key, NULL);
407 void config_set_sensor_position(const char *sid, int pos)
411 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
412 gconf_client_set_int(client, key, pos, NULL);
416 bool config_is_appindicator_enabled(const char *sid)
421 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
422 b = gconf_client_get_bool(client, key, NULL);
428 void config_set_appindicator_enabled(const char *sid, bool enabled)
432 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
433 gconf_client_set_bool(client, key, !enabled, NULL);
437 bool is_slog_enabled()
439 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
442 static void set_slog_enabled(bool enabled)
444 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
447 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
449 log_debug("config_slog_enabled_notify_add");
450 gconf_client_add_dir(client,
452 GCONF_CLIENT_PRELOAD_NONE,
454 gconf_client_notify_add(client,
462 int config_get_slog_interval()
466 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
474 static void set_slog_interval(int interval)
479 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
482 static bool is_window_decoration_enabled()
484 return !gconf_client_get_bool(client,
485 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
489 static bool is_window_keep_below_enabled()
491 return gconf_client_get_bool(client,
492 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
496 static void set_window_decoration_enabled(bool enabled)
498 gconf_client_set_bool
500 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
503 static void set_window_keep_below_enabled(bool enabled)
505 gconf_client_set_bool(client,
506 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
511 * Initializes the GConf client.
516 client = gconf_client_get_default();
519 void config_cleanup()
522 g_object_unref(client);
527 struct config *config_load()
533 c = malloc(sizeof(struct config));
535 c->graph_bgcolor = get_background_color();
536 c->graph_fgcolor = get_foreground_color();
537 c->graph_bg_alpha = get_graph_background_alpha();
538 c->alpha_channel_enabled = is_alpha_channel_enabled();
539 c->sensorlist_position = get_sensorlist_position();
540 c->window_decoration_enabled = is_window_decoration_enabled();
541 c->window_keep_below_enabled = is_window_keep_below_enabled();
542 c->slog_enabled = is_slog_enabled();
543 c->slog_interval = config_get_slog_interval();
545 c->sensor_update_interval
546 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
547 if (c->sensor_update_interval < 1)
548 c->sensor_update_interval = 1;
550 c->graph_update_interval
551 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
552 if (c->graph_update_interval < 1)
553 c->graph_update_interval = 1;
555 c->graph_monitoring_duration
556 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
558 if (c->graph_monitoring_duration < 1)
559 c->graph_monitoring_duration = 10;
561 c->sensor_values_max_length
563 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
564 if (c->sensor_values_max_length < 3)
565 c->sensor_values_max_length = 3;
568 = gconf_client_get_bool(client,
569 KEY_INTERFACE_MENU_BAR_DISABLED,
572 c->unity_launcher_count_disabled
573 = gconf_client_get_bool
575 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
579 = gconf_client_get_bool(client,
580 KEY_INTERFACE_HIDE_ON_STARTUP,
583 c->window_restore_enabled
584 = gconf_client_get_bool(client,
585 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
588 c->window_x = gconf_client_get_int(client,
589 KEY_INTERFACE_WINDOW_X,
591 c->window_y = gconf_client_get_int(client,
592 KEY_INTERFACE_WINDOW_Y,
594 c->window_w = gconf_client_get_int(client,
595 KEY_INTERFACE_WINDOW_W,
597 c->window_h = gconf_client_get_int(client,
598 KEY_INTERFACE_WINDOW_H,
600 c->window_divider_pos
601 = gconf_client_get_int(client,
602 KEY_INTERFACE_WINDOW_DIVIDER_POS,
605 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
610 c->temperature_unit = gconf_client_get_int
611 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
616 void config_save(const struct config *c)
618 set_background_color(c->graph_bgcolor);
619 set_foreground_color(c->graph_fgcolor);
620 set_graph_background_alpha(c->graph_bg_alpha);
621 set_sensorlist_position(c->sensorlist_position);
622 set_window_decoration_enabled(c->window_decoration_enabled);
623 set_window_keep_below_enabled(c->window_keep_below_enabled);
624 set_slog_enabled(c->slog_enabled);
625 set_slog_interval(c->slog_interval);
627 gconf_client_set_int(client,
628 KEY_GRAPH_UPDATE_INTERVAL,
629 c->graph_update_interval, NULL);
631 gconf_client_set_int(client,
632 KEY_GRAPH_MONITORING_DURATION,
633 c->graph_monitoring_duration, NULL);
635 gconf_client_set_int(client,
636 KEY_SENSOR_UPDATE_INTERVAL,
637 c->sensor_update_interval, NULL);
639 gconf_client_set_bool(client,
640 KEY_INTERFACE_MENU_BAR_DISABLED,
641 c->menu_bar_disabled, NULL);
643 gconf_client_set_bool(client,
644 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
645 c->unity_launcher_count_disabled, NULL);
647 gconf_client_set_bool(client,
648 KEY_INTERFACE_HIDE_ON_STARTUP,
649 c->hide_on_startup, NULL);
651 gconf_client_set_bool(client,
652 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
653 c->window_restore_enabled,
656 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
657 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
658 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
659 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
661 gconf_client_set_int(client,
662 KEY_INTERFACE_WINDOW_DIVIDER_POS,
663 c->window_divider_pos,
666 gconf_client_set_int(client,
667 KEY_INTERFACE_TEMPERATURE_UNIT,