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_THRESOLD = "alarm/high_thresold";
32 static const char *ATT_SENSOR_ALARM_LOW_THRESOLD = "alarm/low_thresold";
33 static const char *ATT_SENSOR_COLOR = "color";
34 static const char *ATT_SENSOR_ENABLED = "enabled";
35 static const char *ATT_SENSOR_NAME = "name";
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 GConfClient *client;
95 static char *get_string(const char *key, const char *default_value)
99 value = gconf_client_get_string(client, key, NULL);
102 value = strdup(default_value);
103 gconf_client_set_string(client, key, default_value, NULL);
109 static struct color *get_background_color()
114 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
115 DEFAULT_GRAPH_BACKGROUND_COLOR);
117 c = str_to_color(scolor);
121 return color_new(0xffff, 0xffff, 0xffff);
126 static struct color *get_foreground_color()
131 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
132 DEFAULT_GRAPH_FOREGROUND_COLOR);
134 c = str_to_color(scolor);
138 return color_new(0x0000, 0x0000, 0x0000);
143 static unsigned int is_alpha_channel_enabled()
147 b = gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
152 static enum sensorlist_position get_sensorlist_position()
154 return gconf_client_get_int(client,
155 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
158 static void set_sensorlist_position(enum sensorlist_position pos)
160 gconf_client_set_int(client,
161 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
164 static double get_graph_background_alpha()
168 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
170 gconf_client_set_float(client,
171 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
175 static void set_graph_background_alpha(double alpha)
177 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
180 static void set_background_color(const struct color *color)
184 scolor = color_to_str(color);
186 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
188 gconf_client_set_string(client,
189 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
194 static void set_foreground_color(const struct color *color)
198 str = color_to_str(color);
200 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
202 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
207 static char *get_sensor_att_key(const char *sid, const char *att)
211 esc_sid = gconf_escape_key(sid, -1);
212 /* [KEY_SENSORS]/[esc_sid]/[att] */
213 key = malloc(strlen(KEY_SENSORS)
214 + 1 + 2 * strlen(esc_sid)
215 + 1 + strlen(att) + 1);
217 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
225 config_get_sensor_color(const char *sid, const struct color *dft)
230 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
232 scolor = gconf_client_get_string(client, key, NULL);
237 color = str_to_color(scolor);
239 if (!scolor || !color) {
240 color = color_new(dft->red, dft->green, dft->blue);
241 scolor = color_to_str(color);
242 gconf_client_set_string(client, key, scolor, NULL);
251 void config_set_sensor_color(const char *sid, const struct color *color)
255 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
256 scolor = color_to_str(color);
258 gconf_client_set_string(client, key, scolor, NULL);
264 int config_get_sensor_alarm_high_thresold(const char *sid)
269 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESOLD);
270 res = gconf_client_get_int(client, key, NULL);
277 config_set_sensor_alarm_high_thresold(const char *sid, int thresold)
281 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESOLD);
282 gconf_client_set_int(client, key, thresold, NULL);
286 int config_get_sensor_alarm_low_thresold(const char *sid)
291 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESOLD);
292 res = gconf_client_get_int(client, key, NULL);
299 config_set_sensor_alarm_low_thresold(const char *sid, int thresold)
303 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESOLD);
304 gconf_client_set_int(client, key, thresold, NULL);
308 int config_get_sensor_alarm_enabled(const char *sid)
313 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
314 b = gconf_client_get_bool(client, key, NULL);
320 void config_set_sensor_alarm_enabled(const char *sid, int enabled)
324 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
325 gconf_client_set_bool(client, key, enabled, NULL);
329 int config_is_sensor_enabled(const char *sid)
334 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
335 b = gconf_client_get_bool(client, key, NULL);
341 void config_set_sensor_enabled(const char *sid, int enabled)
345 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
346 gconf_client_set_bool(client, key, enabled, NULL);
350 char *config_get_sensor_name(const char *sid)
354 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
355 name = gconf_client_get_string(client, key, NULL);
361 void config_set_sensor_name(const char *sid, const char *name)
365 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
366 gconf_client_set_string(client, key, name, NULL);
370 static int is_window_decoration_enabled()
374 b = gconf_client_get_bool(client,
375 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
381 static int is_window_keep_below_enabled()
385 b = gconf_client_get_bool(client,
386 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
392 static void set_window_decoration_enabled(int enabled)
395 gconf_client_set_bool
397 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, FALSE, NULL);
399 gconf_client_set_bool
401 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, TRUE, NULL);
404 static void set_window_keep_below_enabled(int enabled)
407 gconf_client_set_bool(client,
408 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
411 gconf_client_set_bool(client,
412 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
417 * Initializes the GConf client.
422 client = gconf_client_get_default();
425 void config_cleanup()
428 g_object_unref(client);
433 struct config *config_load()
439 c = malloc(sizeof(struct config));
441 c->graph_bgcolor = get_background_color();
442 c->graph_fgcolor = get_foreground_color();
443 c->graph_bg_alpha = get_graph_background_alpha();
444 c->alpha_channel_enabled = is_alpha_channel_enabled();
445 c->sensorlist_position = get_sensorlist_position();
446 c->window_decoration_enabled = is_window_decoration_enabled();
447 c->window_keep_below_enabled = is_window_keep_below_enabled();
449 c->sensor_update_interval
450 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
451 if (c->sensor_update_interval < 1)
452 c->sensor_update_interval = 1;
454 c->graph_update_interval
455 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
456 if (c->graph_update_interval < 1)
457 c->graph_update_interval = 1;
459 c->graph_monitoring_duration
460 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
462 if (c->graph_monitoring_duration < 1)
463 c->graph_monitoring_duration = 10;
465 c->sensor_values_max_length
467 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
468 if (c->sensor_values_max_length < 3)
469 c->sensor_values_max_length = 3;
472 = gconf_client_get_bool(client,
473 KEY_INTERFACE_MENU_BAR_DISABLED,
476 c->unity_launcher_count_disabled
477 = gconf_client_get_bool
479 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
483 = gconf_client_get_bool(client,
484 KEY_INTERFACE_HIDE_ON_STARTUP,
487 c->window_restore_enabled
488 = gconf_client_get_bool(client,
489 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
492 c->window_x = gconf_client_get_int(client,
493 KEY_INTERFACE_WINDOW_X,
495 c->window_y = gconf_client_get_int(client,
496 KEY_INTERFACE_WINDOW_Y,
498 c->window_w = gconf_client_get_int(client,
499 KEY_INTERFACE_WINDOW_W,
501 c->window_h = gconf_client_get_int(client,
502 KEY_INTERFACE_WINDOW_H,
504 c->window_divider_pos
505 = gconf_client_get_int(client,
506 KEY_INTERFACE_WINDOW_DIVIDER_POS,
509 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
514 c->temperature_unit = gconf_client_get_int
515 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
520 void config_save(const struct config *c)
522 set_background_color(c->graph_bgcolor);
523 set_foreground_color(c->graph_fgcolor);
524 set_graph_background_alpha(c->graph_bg_alpha);
525 set_sensorlist_position(c->sensorlist_position);
526 set_window_decoration_enabled(c->window_decoration_enabled);
527 set_window_keep_below_enabled(c->window_keep_below_enabled);
529 gconf_client_set_int(client,
530 KEY_GRAPH_UPDATE_INTERVAL,
531 c->graph_update_interval, NULL);
533 gconf_client_set_int(client,
534 KEY_GRAPH_MONITORING_DURATION,
535 c->graph_monitoring_duration, NULL);
537 gconf_client_set_int(client,
538 KEY_SENSOR_UPDATE_INTERVAL,
539 c->sensor_update_interval, NULL);
541 gconf_client_set_bool(client,
542 KEY_INTERFACE_MENU_BAR_DISABLED,
543 c->menu_bar_disabled, NULL);
545 gconf_client_set_bool(client,
546 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
547 c->unity_launcher_count_disabled, NULL);
549 gconf_client_set_bool(client,
550 KEY_INTERFACE_HIDE_ON_STARTUP,
551 c->hide_on_startup, NULL);
553 gconf_client_set_bool(client,
554 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
555 c->window_restore_enabled,
558 gconf_client_set_int(client,
559 KEY_INTERFACE_WINDOW_X,
562 gconf_client_set_int(client,
563 KEY_INTERFACE_WINDOW_Y,
566 gconf_client_set_int(client,
567 KEY_INTERFACE_WINDOW_W,
570 gconf_client_set_int(client,
571 KEY_INTERFACE_WINDOW_H,
575 gconf_client_set_int(client,
576 KEY_INTERFACE_WINDOW_DIVIDER_POS,
577 c->window_divider_pos,
580 gconf_client_set_int(client,
581 KEY_INTERFACE_TEMPERATURE_UNIT,