2 * Copyright (C) 2010-2012 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";
37 static const char *KEY_SENSOR_UPDATE_INTERVAL
38 = "/apps/psensor/sensor/update_interval";
40 static const char *KEY_GRAPH_UPDATE_INTERVAL
41 = "/apps/psensor/graph/update_interval";
43 static const char *KEY_GRAPH_MONITORING_DURATION
44 = "/apps/psensor/graph/monitoring_duration";
46 static const char *KEY_GRAPH_BACKGROUND_COLOR
47 = "/apps/psensor/graph/background_color";
49 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
51 static const char *KEY_GRAPH_BACKGROUND_ALPHA
52 = "/apps/psensor/graph/background_alpha";
54 static const char *KEY_GRAPH_FOREGROUND_COLOR
55 = "/apps/psensor/graph/foreground_color";
56 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
58 static const char *KEY_ALPHA_CHANNEL_ENABLED
59 = "/apps/psensor/graph/alpha_channel_enabled";
61 static const char *KEY_INTERFACE_SENSORLIST_POSITION
62 = "/apps/psensor/interface/sensorlist_position";
64 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
65 = "/apps/psensor/interface/window_decoration_disabled";
67 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
68 = "/apps/psensor/interface/window_keep_below_enabled";
70 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
71 = "/apps/psensor/interface/menu_bar_disabled";
73 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
74 = "/apps/psensor/interface/unity_launcher_count_disabled";
76 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
77 = "/apps/psensor/interface/hide_on_startup";
79 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
80 = "/apps/psensor/interface/window_restore_enabled";
82 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
83 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
84 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
85 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
87 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
88 = "/apps/psensor/interface/window_divider_pos";
90 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
91 = "/apps/psensor/interface/temperature_unit";
93 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
95 static GConfClient *client;
97 static char *get_string(const char *key, const char *default_value)
101 value = gconf_client_get_string(client, key, NULL);
104 value = strdup(default_value);
105 gconf_client_set_string(client, key, default_value, NULL);
111 static struct color *get_background_color()
116 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
117 DEFAULT_GRAPH_BACKGROUND_COLOR);
119 c = str_to_color(scolor);
123 return color_new(0xffff, 0xffff, 0xffff);
128 static struct color *get_foreground_color()
133 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
134 DEFAULT_GRAPH_FOREGROUND_COLOR);
136 c = str_to_color(scolor);
140 return color_new(0x0000, 0x0000, 0x0000);
145 static bool is_alpha_channel_enabled()
147 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
150 static enum sensorlist_position get_sensorlist_position()
152 return gconf_client_get_int(client,
153 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
156 static void set_sensorlist_position(enum sensorlist_position pos)
158 gconf_client_set_int(client,
159 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
162 static double get_graph_background_alpha()
166 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
168 gconf_client_set_float(client,
169 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
173 static void set_graph_background_alpha(double alpha)
175 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
178 static void set_background_color(const struct color *color)
182 scolor = color_to_str(color);
184 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
186 gconf_client_set_string(client,
187 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
192 static void set_foreground_color(const struct color *color)
196 str = color_to_str(color);
198 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
200 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
205 static char *get_sensor_att_key(const char *sid, const char *att)
209 esc_sid = gconf_escape_key(sid, -1);
210 /* [KEY_SENSORS]/[esc_sid]/[att] */
211 key = malloc(strlen(KEY_SENSORS)
212 + 1 + 2 * strlen(esc_sid)
213 + 1 + strlen(att) + 1);
215 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
223 config_get_sensor_color(const char *sid, const struct color *dft)
228 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
230 scolor = gconf_client_get_string(client, key, NULL);
235 color = str_to_color(scolor);
237 if (!scolor || !color) {
238 color = color_new(dft->red, dft->green, dft->blue);
239 scolor = color_to_str(color);
240 gconf_client_set_string(client, key, scolor, NULL);
249 void config_set_sensor_color(const char *sid, const struct color *color)
253 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
254 scolor = color_to_str(color);
256 gconf_client_set_string(client, key, scolor, NULL);
262 int config_get_sensor_alarm_high_threshold(const char *sid)
267 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
268 res = gconf_client_get_int(client, key, NULL);
275 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
279 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
280 gconf_client_set_int(client, key, threshold, NULL);
284 int config_get_sensor_alarm_low_threshold(const char *sid)
289 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
290 res = gconf_client_get_int(client, key, NULL);
297 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
301 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
302 gconf_client_set_int(client, key, threshold, NULL);
306 bool config_get_sensor_alarm_enabled(const char *sid)
311 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
312 b = gconf_client_get_bool(client, key, NULL);
318 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
322 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
323 gconf_client_set_bool(client, key, enabled, NULL);
327 bool config_is_sensor_enabled(const char *sid)
332 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
333 b = gconf_client_get_bool(client, key, NULL);
339 void config_set_sensor_enabled(const char *sid, bool enabled)
343 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
344 gconf_client_set_bool(client, key, enabled, NULL);
348 char *config_get_sensor_name(const char *sid)
352 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
353 name = gconf_client_get_string(client, key, NULL);
359 void config_set_sensor_name(const char *sid, const char *name)
363 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
364 gconf_client_set_string(client, key, name, NULL);
368 bool config_is_appindicator_enabled(const char *sid)
373 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
374 b = gconf_client_get_bool(client, key, NULL);
380 void config_set_appindicator_enabled(const char *sid, bool enabled)
384 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
385 gconf_client_set_bool(client, key, !enabled, NULL);
389 bool is_slog_enabled()
391 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
394 static void set_slog_enabled(bool enabled)
396 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
400 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
402 log_debug("config_slog_enabled_notify_add");
403 gconf_client_add_dir(client,
405 GCONF_CLIENT_PRELOAD_NONE,
407 gconf_client_notify_add(client,
415 static bool is_window_decoration_enabled()
417 return !gconf_client_get_bool(client,
418 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
422 static bool is_window_keep_below_enabled()
424 return gconf_client_get_bool(client,
425 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
429 static void set_window_decoration_enabled(bool enabled)
431 gconf_client_set_bool
433 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
436 static void set_window_keep_below_enabled(bool enabled)
438 gconf_client_set_bool(client,
439 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
444 * Initializes the GConf client.
449 client = gconf_client_get_default();
452 void config_cleanup()
455 g_object_unref(client);
460 struct config *config_load()
466 c = malloc(sizeof(struct config));
468 c->graph_bgcolor = get_background_color();
469 c->graph_fgcolor = get_foreground_color();
470 c->graph_bg_alpha = get_graph_background_alpha();
471 c->alpha_channel_enabled = is_alpha_channel_enabled();
472 c->sensorlist_position = get_sensorlist_position();
473 c->window_decoration_enabled = is_window_decoration_enabled();
474 c->window_keep_below_enabled = is_window_keep_below_enabled();
475 c->slog_enabled = is_slog_enabled();
477 c->sensor_update_interval
478 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
479 if (c->sensor_update_interval < 1)
480 c->sensor_update_interval = 1;
482 c->graph_update_interval
483 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
484 if (c->graph_update_interval < 1)
485 c->graph_update_interval = 1;
487 c->graph_monitoring_duration
488 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
490 if (c->graph_monitoring_duration < 1)
491 c->graph_monitoring_duration = 10;
493 c->sensor_values_max_length
495 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
496 if (c->sensor_values_max_length < 3)
497 c->sensor_values_max_length = 3;
500 = gconf_client_get_bool(client,
501 KEY_INTERFACE_MENU_BAR_DISABLED,
504 c->unity_launcher_count_disabled
505 = gconf_client_get_bool
507 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
511 = gconf_client_get_bool(client,
512 KEY_INTERFACE_HIDE_ON_STARTUP,
515 c->window_restore_enabled
516 = gconf_client_get_bool(client,
517 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
520 c->window_x = gconf_client_get_int(client,
521 KEY_INTERFACE_WINDOW_X,
523 c->window_y = gconf_client_get_int(client,
524 KEY_INTERFACE_WINDOW_Y,
526 c->window_w = gconf_client_get_int(client,
527 KEY_INTERFACE_WINDOW_W,
529 c->window_h = gconf_client_get_int(client,
530 KEY_INTERFACE_WINDOW_H,
532 c->window_divider_pos
533 = gconf_client_get_int(client,
534 KEY_INTERFACE_WINDOW_DIVIDER_POS,
537 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
542 c->temperature_unit = gconf_client_get_int
543 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
548 void config_save(const struct config *c)
550 set_background_color(c->graph_bgcolor);
551 set_foreground_color(c->graph_fgcolor);
552 set_graph_background_alpha(c->graph_bg_alpha);
553 set_sensorlist_position(c->sensorlist_position);
554 set_window_decoration_enabled(c->window_decoration_enabled);
555 set_window_keep_below_enabled(c->window_keep_below_enabled);
556 set_slog_enabled(c->slog_enabled);
558 gconf_client_set_int(client,
559 KEY_GRAPH_UPDATE_INTERVAL,
560 c->graph_update_interval, NULL);
562 gconf_client_set_int(client,
563 KEY_GRAPH_MONITORING_DURATION,
564 c->graph_monitoring_duration, NULL);
566 gconf_client_set_int(client,
567 KEY_SENSOR_UPDATE_INTERVAL,
568 c->sensor_update_interval, NULL);
570 gconf_client_set_bool(client,
571 KEY_INTERFACE_MENU_BAR_DISABLED,
572 c->menu_bar_disabled, NULL);
574 gconf_client_set_bool(client,
575 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
576 c->unity_launcher_count_disabled, NULL);
578 gconf_client_set_bool(client,
579 KEY_INTERFACE_HIDE_ON_STARTUP,
580 c->hide_on_startup, NULL);
582 gconf_client_set_bool(client,
583 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
584 c->window_restore_enabled,
587 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
588 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
589 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
590 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
592 gconf_client_set_int(client,
593 KEY_INTERFACE_WINDOW_DIVIDER_POS,
594 c->window_divider_pos,
597 gconf_client_set_int(client,
598 KEY_INTERFACE_TEMPERATURE_UNIT,