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_limit(const char *sid, int dft)
269 key = get_sensor_att_key(sid, "alarmlimit");
270 res = gconf_client_get_int(client, key, NULL);
273 return res ? res : dft;
276 void config_set_sensor_alarm_limit(const char *sid, int alarm_limit)
280 key = get_sensor_att_key(sid, "alarmlimit");
281 gconf_client_set_int(client, key, alarm_limit, NULL);
285 int config_get_sensor_alarm_enabled(const char *sid)
290 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
291 b = gconf_client_get_bool(client, key, NULL);
297 void config_set_sensor_alarm_enabled(const char *sid, int enabled)
301 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
302 gconf_client_set_bool(client, key, enabled, NULL);
306 int config_is_sensor_enabled(const char *sid)
311 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
312 b = gconf_client_get_bool(client, key, NULL);
318 void config_set_sensor_enabled(const char *sid, int enabled)
322 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
323 gconf_client_set_bool(client, key, enabled, NULL);
327 char *config_get_sensor_name(const char *sid)
331 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
332 name = gconf_client_get_string(client, key, NULL);
338 void config_set_sensor_name(const char *sid, const char *name)
342 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
343 gconf_client_set_string(client, key, name, NULL);
347 static int is_window_decoration_enabled()
351 b = gconf_client_get_bool(client,
352 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
358 static int is_window_keep_below_enabled()
362 b = gconf_client_get_bool(client,
363 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
369 static void set_window_decoration_enabled(int enabled)
372 gconf_client_set_bool
374 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, FALSE, NULL);
376 gconf_client_set_bool
378 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, TRUE, NULL);
381 static void set_window_keep_below_enabled(int enabled)
384 gconf_client_set_bool(client,
385 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
388 gconf_client_set_bool(client,
389 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
394 * Initializes the GConf client.
399 client = gconf_client_get_default();
402 void config_cleanup()
405 g_object_unref(client);
410 struct config *config_load()
416 c = malloc(sizeof(struct config));
418 c->graph_bgcolor = get_background_color();
419 c->graph_fgcolor = get_foreground_color();
420 c->graph_bg_alpha = get_graph_background_alpha();
421 c->alpha_channel_enabled = is_alpha_channel_enabled();
422 c->sensorlist_position = get_sensorlist_position();
423 c->window_decoration_enabled = is_window_decoration_enabled();
424 c->window_keep_below_enabled = is_window_keep_below_enabled();
426 c->sensor_update_interval
427 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
428 if (c->sensor_update_interval < 1)
429 c->sensor_update_interval = 1;
431 c->graph_update_interval
432 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
433 if (c->graph_update_interval < 1)
434 c->graph_update_interval = 1;
436 c->graph_monitoring_duration
437 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
439 if (c->graph_monitoring_duration < 1)
440 c->graph_monitoring_duration = 10;
442 c->sensor_values_max_length
444 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
445 if (c->sensor_values_max_length < 3)
446 c->sensor_values_max_length = 3;
449 = gconf_client_get_bool(client,
450 KEY_INTERFACE_MENU_BAR_DISABLED,
453 c->unity_launcher_count_disabled
454 = gconf_client_get_bool
456 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
460 = gconf_client_get_bool(client,
461 KEY_INTERFACE_HIDE_ON_STARTUP,
464 c->window_restore_enabled
465 = gconf_client_get_bool(client,
466 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
469 c->window_x = gconf_client_get_int(client,
470 KEY_INTERFACE_WINDOW_X,
472 c->window_y = gconf_client_get_int(client,
473 KEY_INTERFACE_WINDOW_Y,
475 c->window_w = gconf_client_get_int(client,
476 KEY_INTERFACE_WINDOW_W,
478 c->window_h = gconf_client_get_int(client,
479 KEY_INTERFACE_WINDOW_H,
481 c->window_divider_pos
482 = gconf_client_get_int(client,
483 KEY_INTERFACE_WINDOW_DIVIDER_POS,
486 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
491 c->temperature_unit = gconf_client_get_int
492 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
497 void config_save(const struct config *c)
499 set_background_color(c->graph_bgcolor);
500 set_foreground_color(c->graph_fgcolor);
501 set_graph_background_alpha(c->graph_bg_alpha);
502 set_sensorlist_position(c->sensorlist_position);
503 set_window_decoration_enabled(c->window_decoration_enabled);
504 set_window_keep_below_enabled(c->window_keep_below_enabled);
506 gconf_client_set_int(client,
507 KEY_GRAPH_UPDATE_INTERVAL,
508 c->graph_update_interval, NULL);
510 gconf_client_set_int(client,
511 KEY_GRAPH_MONITORING_DURATION,
512 c->graph_monitoring_duration, NULL);
514 gconf_client_set_int(client,
515 KEY_SENSOR_UPDATE_INTERVAL,
516 c->sensor_update_interval, NULL);
518 gconf_client_set_bool(client,
519 KEY_INTERFACE_MENU_BAR_DISABLED,
520 c->menu_bar_disabled, NULL);
522 gconf_client_set_bool(client,
523 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
524 c->unity_launcher_count_disabled, NULL);
526 gconf_client_set_bool(client,
527 KEY_INTERFACE_HIDE_ON_STARTUP,
528 c->hide_on_startup, NULL);
530 gconf_client_set_bool(client,
531 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
532 c->window_restore_enabled,
535 gconf_client_set_int(client,
536 KEY_INTERFACE_WINDOW_X,
539 gconf_client_set_int(client,
540 KEY_INTERFACE_WINDOW_Y,
543 gconf_client_set_int(client,
544 KEY_INTERFACE_WINDOW_W,
547 gconf_client_set_int(client,
548 KEY_INTERFACE_WINDOW_H,
552 gconf_client_set_int(client,
553 KEY_INTERFACE_WINDOW_DIVIDER_POS,
554 c->window_divider_pos,
557 gconf_client_set_int(client,
558 KEY_INTERFACE_TEMPERATURE_UNIT,