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
24 #include <gconf/gconf-client.h>
28 static const char *KEY_SENSORS = "/apps/psensor/sensors";
30 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm/enabled";
31 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm/high_threshold";
32 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm/low_threshold";
33 static const char *ATT_SENSOR_COLOR = "color";
34 static const char *ATT_SENSOR_ENABLED = "enabled";
35 static const char *ATT_SENSOR_NAME = "name";
36 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
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";
96 static GConfClient *client;
98 static char *get_string(const char *key, const char *default_value)
102 value = gconf_client_get_string(client, key, NULL);
105 value = strdup(default_value);
106 gconf_client_set_string(client, key, default_value, NULL);
112 static struct color *get_background_color()
117 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
118 DEFAULT_GRAPH_BACKGROUND_COLOR);
120 c = str_to_color(scolor);
124 return color_new(0xffff, 0xffff, 0xffff);
129 static struct color *get_foreground_color()
134 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
135 DEFAULT_GRAPH_FOREGROUND_COLOR);
137 c = str_to_color(scolor);
141 return color_new(0x0000, 0x0000, 0x0000);
146 static bool is_alpha_channel_enabled()
148 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
151 static enum sensorlist_position get_sensorlist_position()
153 return gconf_client_get_int(client,
154 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
157 static void set_sensorlist_position(enum sensorlist_position pos)
159 gconf_client_set_int(client,
160 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
163 static double get_graph_background_alpha()
167 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
169 gconf_client_set_float(client,
170 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
174 static void set_graph_background_alpha(double alpha)
176 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
179 static void set_background_color(const struct color *color)
183 scolor = color_to_str(color);
185 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
187 gconf_client_set_string(client,
188 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
193 static void set_foreground_color(const struct color *color)
197 str = color_to_str(color);
199 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
201 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
206 static char *get_sensor_att_key(const char *sid, const char *att)
210 esc_sid = gconf_escape_key(sid, -1);
211 /* [KEY_SENSORS]/[esc_sid]/[att] */
212 key = malloc(strlen(KEY_SENSORS)
213 + 1 + 2 * strlen(esc_sid)
214 + 1 + strlen(att) + 1);
216 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
224 config_get_sensor_color(const char *sid, const struct color *dft)
229 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
231 scolor = gconf_client_get_string(client, key, NULL);
236 color = str_to_color(scolor);
238 if (!scolor || !color) {
239 color = color_new(dft->red, dft->green, dft->blue);
240 scolor = color_to_str(color);
241 gconf_client_set_string(client, key, scolor, NULL);
250 void config_set_sensor_color(const char *sid, const struct color *color)
254 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
255 scolor = color_to_str(color);
257 gconf_client_set_string(client, key, scolor, NULL);
263 int config_get_sensor_alarm_high_threshold(const char *sid)
268 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
269 res = gconf_client_get_int(client, key, NULL);
276 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
280 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
281 gconf_client_set_int(client, key, threshold, NULL);
285 int config_get_sensor_alarm_low_threshold(const char *sid)
290 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
291 res = gconf_client_get_int(client, key, NULL);
298 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
302 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
303 gconf_client_set_int(client, key, threshold, NULL);
307 bool config_get_sensor_alarm_enabled(const char *sid)
312 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
313 b = gconf_client_get_bool(client, key, NULL);
319 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
323 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
324 gconf_client_set_bool(client, key, enabled, NULL);
328 bool config_is_sensor_enabled(const char *sid)
333 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
334 b = gconf_client_get_bool(client, key, NULL);
340 void config_set_sensor_enabled(const char *sid, bool enabled)
344 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
345 gconf_client_set_bool(client, key, enabled, NULL);
349 char *config_get_sensor_name(const char *sid)
353 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
354 name = gconf_client_get_string(client, key, NULL);
360 void config_set_sensor_name(const char *sid, const char *name)
364 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
365 gconf_client_set_string(client, key, name, NULL);
369 bool config_is_appindicator_enabled(const char *sid)
374 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
375 b = gconf_client_get_bool(client, key, NULL);
381 void config_set_appindicator_enabled(const char *sid, bool enabled)
385 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
386 gconf_client_set_bool(client, key, !enabled, NULL);
390 static bool is_slog_enabled()
392 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
395 static void set_slog_enabled(bool enabled)
397 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
400 static bool is_window_decoration_enabled()
402 return !gconf_client_get_bool(client,
403 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
407 static bool is_window_keep_below_enabled()
409 return gconf_client_get_bool(client,
410 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
414 static void set_window_decoration_enabled(bool enabled)
416 gconf_client_set_bool
418 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
421 static void set_window_keep_below_enabled(bool enabled)
423 gconf_client_set_bool(client,
424 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
429 * Initializes the GConf client.
434 client = gconf_client_get_default();
437 void config_cleanup()
440 g_object_unref(client);
445 struct config *config_load()
451 c = malloc(sizeof(struct config));
453 c->graph_bgcolor = get_background_color();
454 c->graph_fgcolor = get_foreground_color();
455 c->graph_bg_alpha = get_graph_background_alpha();
456 c->alpha_channel_enabled = is_alpha_channel_enabled();
457 c->sensorlist_position = get_sensorlist_position();
458 c->window_decoration_enabled = is_window_decoration_enabled();
459 c->window_keep_below_enabled = is_window_keep_below_enabled();
460 c->slog_enabled = is_slog_enabled();
462 c->sensor_update_interval
463 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
464 if (c->sensor_update_interval < 1)
465 c->sensor_update_interval = 1;
467 c->graph_update_interval
468 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
469 if (c->graph_update_interval < 1)
470 c->graph_update_interval = 1;
472 c->graph_monitoring_duration
473 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
475 if (c->graph_monitoring_duration < 1)
476 c->graph_monitoring_duration = 10;
478 c->sensor_values_max_length
480 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
481 if (c->sensor_values_max_length < 3)
482 c->sensor_values_max_length = 3;
485 = gconf_client_get_bool(client,
486 KEY_INTERFACE_MENU_BAR_DISABLED,
489 c->unity_launcher_count_disabled
490 = gconf_client_get_bool
492 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
496 = gconf_client_get_bool(client,
497 KEY_INTERFACE_HIDE_ON_STARTUP,
500 c->window_restore_enabled
501 = gconf_client_get_bool(client,
502 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
505 c->window_x = gconf_client_get_int(client,
506 KEY_INTERFACE_WINDOW_X,
508 c->window_y = gconf_client_get_int(client,
509 KEY_INTERFACE_WINDOW_Y,
511 c->window_w = gconf_client_get_int(client,
512 KEY_INTERFACE_WINDOW_W,
514 c->window_h = gconf_client_get_int(client,
515 KEY_INTERFACE_WINDOW_H,
517 c->window_divider_pos
518 = gconf_client_get_int(client,
519 KEY_INTERFACE_WINDOW_DIVIDER_POS,
522 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
527 c->temperature_unit = gconf_client_get_int
528 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
533 void config_save(const struct config *c)
535 set_background_color(c->graph_bgcolor);
536 set_foreground_color(c->graph_fgcolor);
537 set_graph_background_alpha(c->graph_bg_alpha);
538 set_sensorlist_position(c->sensorlist_position);
539 set_window_decoration_enabled(c->window_decoration_enabled);
540 set_window_keep_below_enabled(c->window_keep_below_enabled);
541 set_slog_enabled(c->slog_enabled);
543 gconf_client_set_int(client,
544 KEY_GRAPH_UPDATE_INTERVAL,
545 c->graph_update_interval, NULL);
547 gconf_client_set_int(client,
548 KEY_GRAPH_MONITORING_DURATION,
549 c->graph_monitoring_duration, NULL);
551 gconf_client_set_int(client,
552 KEY_SENSOR_UPDATE_INTERVAL,
553 c->sensor_update_interval, NULL);
555 gconf_client_set_bool(client,
556 KEY_INTERFACE_MENU_BAR_DISABLED,
557 c->menu_bar_disabled, NULL);
559 gconf_client_set_bool(client,
560 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
561 c->unity_launcher_count_disabled, NULL);
563 gconf_client_set_bool(client,
564 KEY_INTERFACE_HIDE_ON_STARTUP,
565 c->hide_on_startup, NULL);
567 gconf_client_set_bool(client,
568 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
569 c->window_restore_enabled,
572 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
573 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
574 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
575 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
577 gconf_client_set_int(client,
578 KEY_INTERFACE_WINDOW_DIVIDER_POS,
579 c->window_divider_pos,
582 gconf_client_set_int(client,
583 KEY_INTERFACE_TEMPERATURE_UNIT,