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 GConfClient *client;
96 static char *get_string(const char *key, const char *default_value)
100 value = gconf_client_get_string(client, key, NULL);
103 value = strdup(default_value);
104 gconf_client_set_string(client, key, default_value, NULL);
110 static struct color *get_background_color()
115 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
116 DEFAULT_GRAPH_BACKGROUND_COLOR);
118 c = str_to_color(scolor);
122 return color_new(0xffff, 0xffff, 0xffff);
127 static struct color *get_foreground_color()
132 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
133 DEFAULT_GRAPH_FOREGROUND_COLOR);
135 c = str_to_color(scolor);
139 return color_new(0x0000, 0x0000, 0x0000);
144 static unsigned int is_alpha_channel_enabled()
148 b = gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
153 static enum sensorlist_position get_sensorlist_position()
155 return gconf_client_get_int(client,
156 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
159 static void set_sensorlist_position(enum sensorlist_position pos)
161 gconf_client_set_int(client,
162 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
165 static double get_graph_background_alpha()
169 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
171 gconf_client_set_float(client,
172 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
176 static void set_graph_background_alpha(double alpha)
178 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
181 static void set_background_color(const struct color *color)
185 scolor = color_to_str(color);
187 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
189 gconf_client_set_string(client,
190 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
195 static void set_foreground_color(const struct color *color)
199 str = color_to_str(color);
201 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
203 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
208 static char *get_sensor_att_key(const char *sid, const char *att)
212 esc_sid = gconf_escape_key(sid, -1);
213 /* [KEY_SENSORS]/[esc_sid]/[att] */
214 key = malloc(strlen(KEY_SENSORS)
215 + 1 + 2 * strlen(esc_sid)
216 + 1 + strlen(att) + 1);
218 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
226 config_get_sensor_color(const char *sid, const struct color *dft)
231 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
233 scolor = gconf_client_get_string(client, key, NULL);
238 color = str_to_color(scolor);
240 if (!scolor || !color) {
241 color = color_new(dft->red, dft->green, dft->blue);
242 scolor = color_to_str(color);
243 gconf_client_set_string(client, key, scolor, NULL);
252 void config_set_sensor_color(const char *sid, const struct color *color)
256 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
257 scolor = color_to_str(color);
259 gconf_client_set_string(client, key, scolor, NULL);
265 int config_get_sensor_alarm_high_threshold(const char *sid)
270 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
271 res = gconf_client_get_int(client, key, NULL);
278 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
282 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
283 gconf_client_set_int(client, key, threshold, NULL);
287 int config_get_sensor_alarm_low_threshold(const char *sid)
292 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
293 res = gconf_client_get_int(client, key, NULL);
300 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
304 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
305 gconf_client_set_int(client, key, threshold, NULL);
309 int config_get_sensor_alarm_enabled(const char *sid)
314 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
315 b = gconf_client_get_bool(client, key, NULL);
321 void config_set_sensor_alarm_enabled(const char *sid, int enabled)
325 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
326 gconf_client_set_bool(client, key, enabled, NULL);
330 int config_is_sensor_enabled(const char *sid)
335 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
336 b = gconf_client_get_bool(client, key, NULL);
342 void config_set_sensor_enabled(const char *sid, int enabled)
346 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
347 gconf_client_set_bool(client, key, enabled, NULL);
351 char *config_get_sensor_name(const char *sid)
355 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
356 name = gconf_client_get_string(client, key, NULL);
362 void config_set_sensor_name(const char *sid, const char *name)
366 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
367 gconf_client_set_string(client, key, name, NULL);
371 unsigned int config_is_appindicator_enabled(const char *sid)
376 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
377 b = gconf_client_get_bool(client, key, NULL);
383 void config_set_appindicator_enabled(const char *sid, unsigned int enabled)
393 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
394 gconf_client_set_bool(client, key, b, NULL);
399 static int is_window_decoration_enabled()
403 b = gconf_client_get_bool(client,
404 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
410 static int is_window_keep_below_enabled()
414 b = gconf_client_get_bool(client,
415 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
421 static void set_window_decoration_enabled(int enabled)
424 gconf_client_set_bool
426 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, FALSE, NULL);
428 gconf_client_set_bool
430 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, TRUE, NULL);
433 static void set_window_keep_below_enabled(int enabled)
436 gconf_client_set_bool(client,
437 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
440 gconf_client_set_bool(client,
441 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
446 * Initializes the GConf client.
451 client = gconf_client_get_default();
454 void config_cleanup()
457 g_object_unref(client);
462 struct config *config_load()
468 c = malloc(sizeof(struct config));
470 c->graph_bgcolor = get_background_color();
471 c->graph_fgcolor = get_foreground_color();
472 c->graph_bg_alpha = get_graph_background_alpha();
473 c->alpha_channel_enabled = is_alpha_channel_enabled();
474 c->sensorlist_position = get_sensorlist_position();
475 c->window_decoration_enabled = is_window_decoration_enabled();
476 c->window_keep_below_enabled = is_window_keep_below_enabled();
478 c->sensor_update_interval
479 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
480 if (c->sensor_update_interval < 1)
481 c->sensor_update_interval = 1;
483 c->graph_update_interval
484 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
485 if (c->graph_update_interval < 1)
486 c->graph_update_interval = 1;
488 c->graph_monitoring_duration
489 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
491 if (c->graph_monitoring_duration < 1)
492 c->graph_monitoring_duration = 10;
494 c->sensor_values_max_length
496 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
497 if (c->sensor_values_max_length < 3)
498 c->sensor_values_max_length = 3;
501 = gconf_client_get_bool(client,
502 KEY_INTERFACE_MENU_BAR_DISABLED,
505 c->unity_launcher_count_disabled
506 = gconf_client_get_bool
508 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
512 = gconf_client_get_bool(client,
513 KEY_INTERFACE_HIDE_ON_STARTUP,
516 c->window_restore_enabled
517 = gconf_client_get_bool(client,
518 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
521 c->window_x = gconf_client_get_int(client,
522 KEY_INTERFACE_WINDOW_X,
524 c->window_y = gconf_client_get_int(client,
525 KEY_INTERFACE_WINDOW_Y,
527 c->window_w = gconf_client_get_int(client,
528 KEY_INTERFACE_WINDOW_W,
530 c->window_h = gconf_client_get_int(client,
531 KEY_INTERFACE_WINDOW_H,
533 c->window_divider_pos
534 = gconf_client_get_int(client,
535 KEY_INTERFACE_WINDOW_DIVIDER_POS,
538 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
543 c->temperature_unit = gconf_client_get_int
544 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
549 void config_save(const struct config *c)
551 set_background_color(c->graph_bgcolor);
552 set_foreground_color(c->graph_fgcolor);
553 set_graph_background_alpha(c->graph_bg_alpha);
554 set_sensorlist_position(c->sensorlist_position);
555 set_window_decoration_enabled(c->window_decoration_enabled);
556 set_window_keep_below_enabled(c->window_keep_below_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,