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, "alarmlimit");
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, "alarmlimit");
282 gconf_client_set_int(client, key, thresold, NULL);
286 int config_get_sensor_alarm_enabled(const char *sid)
291 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
292 b = gconf_client_get_bool(client, key, NULL);
298 void config_set_sensor_alarm_enabled(const char *sid, int enabled)
302 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
303 gconf_client_set_bool(client, key, enabled, NULL);
307 int config_is_sensor_enabled(const char *sid)
312 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
313 b = gconf_client_get_bool(client, key, NULL);
319 void config_set_sensor_enabled(const char *sid, int enabled)
323 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
324 gconf_client_set_bool(client, key, enabled, NULL);
328 char *config_get_sensor_name(const char *sid)
332 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
333 name = gconf_client_get_string(client, key, NULL);
339 void config_set_sensor_name(const char *sid, const char *name)
343 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
344 gconf_client_set_string(client, key, name, NULL);
348 static int is_window_decoration_enabled()
352 b = gconf_client_get_bool(client,
353 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
359 static int is_window_keep_below_enabled()
363 b = gconf_client_get_bool(client,
364 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
370 static void set_window_decoration_enabled(int enabled)
373 gconf_client_set_bool
375 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, FALSE, NULL);
377 gconf_client_set_bool
379 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, TRUE, NULL);
382 static void set_window_keep_below_enabled(int enabled)
385 gconf_client_set_bool(client,
386 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
389 gconf_client_set_bool(client,
390 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
395 * Initializes the GConf client.
400 client = gconf_client_get_default();
403 void config_cleanup()
406 g_object_unref(client);
411 struct config *config_load()
417 c = malloc(sizeof(struct config));
419 c->graph_bgcolor = get_background_color();
420 c->graph_fgcolor = get_foreground_color();
421 c->graph_bg_alpha = get_graph_background_alpha();
422 c->alpha_channel_enabled = is_alpha_channel_enabled();
423 c->sensorlist_position = get_sensorlist_position();
424 c->window_decoration_enabled = is_window_decoration_enabled();
425 c->window_keep_below_enabled = is_window_keep_below_enabled();
427 c->sensor_update_interval
428 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
429 if (c->sensor_update_interval < 1)
430 c->sensor_update_interval = 1;
432 c->graph_update_interval
433 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
434 if (c->graph_update_interval < 1)
435 c->graph_update_interval = 1;
437 c->graph_monitoring_duration
438 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
440 if (c->graph_monitoring_duration < 1)
441 c->graph_monitoring_duration = 10;
443 c->sensor_values_max_length
445 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
446 if (c->sensor_values_max_length < 3)
447 c->sensor_values_max_length = 3;
450 = gconf_client_get_bool(client,
451 KEY_INTERFACE_MENU_BAR_DISABLED,
454 c->unity_launcher_count_disabled
455 = gconf_client_get_bool
457 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
461 = gconf_client_get_bool(client,
462 KEY_INTERFACE_HIDE_ON_STARTUP,
465 c->window_restore_enabled
466 = gconf_client_get_bool(client,
467 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
470 c->window_x = gconf_client_get_int(client,
471 KEY_INTERFACE_WINDOW_X,
473 c->window_y = gconf_client_get_int(client,
474 KEY_INTERFACE_WINDOW_Y,
476 c->window_w = gconf_client_get_int(client,
477 KEY_INTERFACE_WINDOW_W,
479 c->window_h = gconf_client_get_int(client,
480 KEY_INTERFACE_WINDOW_H,
482 c->window_divider_pos
483 = gconf_client_get_int(client,
484 KEY_INTERFACE_WINDOW_DIVIDER_POS,
487 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
492 c->temperature_unit = gconf_client_get_int
493 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
498 void config_save(const struct config *c)
500 set_background_color(c->graph_bgcolor);
501 set_foreground_color(c->graph_fgcolor);
502 set_graph_background_alpha(c->graph_bg_alpha);
503 set_sensorlist_position(c->sensorlist_position);
504 set_window_decoration_enabled(c->window_decoration_enabled);
505 set_window_keep_below_enabled(c->window_keep_below_enabled);
507 gconf_client_set_int(client,
508 KEY_GRAPH_UPDATE_INTERVAL,
509 c->graph_update_interval, NULL);
511 gconf_client_set_int(client,
512 KEY_GRAPH_MONITORING_DURATION,
513 c->graph_monitoring_duration, NULL);
515 gconf_client_set_int(client,
516 KEY_SENSOR_UPDATE_INTERVAL,
517 c->sensor_update_interval, NULL);
519 gconf_client_set_bool(client,
520 KEY_INTERFACE_MENU_BAR_DISABLED,
521 c->menu_bar_disabled, NULL);
523 gconf_client_set_bool(client,
524 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
525 c->unity_launcher_count_disabled, NULL);
527 gconf_client_set_bool(client,
528 KEY_INTERFACE_HIDE_ON_STARTUP,
529 c->hide_on_startup, NULL);
531 gconf_client_set_bool(client,
532 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
533 c->window_restore_enabled,
536 gconf_client_set_int(client,
537 KEY_INTERFACE_WINDOW_X,
540 gconf_client_set_int(client,
541 KEY_INTERFACE_WINDOW_Y,
544 gconf_client_set_int(client,
545 KEY_INTERFACE_WINDOW_W,
548 gconf_client_set_int(client,
549 KEY_INTERFACE_WINDOW_H,
553 gconf_client_set_int(client,
554 KEY_INTERFACE_WINDOW_DIVIDER_POS,
555 c->window_divider_pos,
558 gconf_client_set_int(client,
559 KEY_INTERFACE_TEMPERATURE_UNIT,