2 * Copyright (C) 2010-2013 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
27 static const char *KEY_SENSORS = "/apps/psensor/sensors";
29 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm/enabled";
30 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm/high_threshold";
31 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm/low_threshold";
32 static const char *ATT_SENSOR_COLOR = "color";
33 static const char *ATT_SENSOR_ENABLED = "enabled";
34 static const char *ATT_SENSOR_NAME = "name";
35 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
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 const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
94 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
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 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);
401 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
403 log_debug("config_slog_enabled_notify_add");
404 gconf_client_add_dir(client,
406 GCONF_CLIENT_PRELOAD_NONE,
408 gconf_client_notify_add(client,
416 int config_get_slog_interval()
420 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
428 static void set_slog_interval(int interval)
433 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
436 static bool is_window_decoration_enabled()
438 return !gconf_client_get_bool(client,
439 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
443 static bool is_window_keep_below_enabled()
445 return gconf_client_get_bool(client,
446 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
450 static void set_window_decoration_enabled(bool enabled)
452 gconf_client_set_bool
454 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
457 static void set_window_keep_below_enabled(bool enabled)
459 gconf_client_set_bool(client,
460 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
465 * Initializes the GConf client.
470 client = gconf_client_get_default();
473 void config_cleanup()
476 g_object_unref(client);
481 struct config *config_load()
487 c = malloc(sizeof(struct config));
489 c->graph_bgcolor = get_background_color();
490 c->graph_fgcolor = get_foreground_color();
491 c->graph_bg_alpha = get_graph_background_alpha();
492 c->alpha_channel_enabled = is_alpha_channel_enabled();
493 c->sensorlist_position = get_sensorlist_position();
494 c->window_decoration_enabled = is_window_decoration_enabled();
495 c->window_keep_below_enabled = is_window_keep_below_enabled();
496 c->slog_enabled = is_slog_enabled();
497 c->slog_interval = config_get_slog_interval();
499 c->sensor_update_interval
500 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
501 if (c->sensor_update_interval < 1)
502 c->sensor_update_interval = 1;
504 c->graph_update_interval
505 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
506 if (c->graph_update_interval < 1)
507 c->graph_update_interval = 1;
509 c->graph_monitoring_duration
510 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
512 if (c->graph_monitoring_duration < 1)
513 c->graph_monitoring_duration = 10;
515 c->sensor_values_max_length
517 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
518 if (c->sensor_values_max_length < 3)
519 c->sensor_values_max_length = 3;
522 = gconf_client_get_bool(client,
523 KEY_INTERFACE_MENU_BAR_DISABLED,
526 c->unity_launcher_count_disabled
527 = gconf_client_get_bool
529 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
533 = gconf_client_get_bool(client,
534 KEY_INTERFACE_HIDE_ON_STARTUP,
537 c->window_restore_enabled
538 = gconf_client_get_bool(client,
539 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
542 c->window_x = gconf_client_get_int(client,
543 KEY_INTERFACE_WINDOW_X,
545 c->window_y = gconf_client_get_int(client,
546 KEY_INTERFACE_WINDOW_Y,
548 c->window_w = gconf_client_get_int(client,
549 KEY_INTERFACE_WINDOW_W,
551 c->window_h = gconf_client_get_int(client,
552 KEY_INTERFACE_WINDOW_H,
554 c->window_divider_pos
555 = gconf_client_get_int(client,
556 KEY_INTERFACE_WINDOW_DIVIDER_POS,
559 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
564 c->temperature_unit = gconf_client_get_int
565 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
570 void config_save(const struct config *c)
572 set_background_color(c->graph_bgcolor);
573 set_foreground_color(c->graph_fgcolor);
574 set_graph_background_alpha(c->graph_bg_alpha);
575 set_sensorlist_position(c->sensorlist_position);
576 set_window_decoration_enabled(c->window_decoration_enabled);
577 set_window_keep_below_enabled(c->window_keep_below_enabled);
578 set_slog_enabled(c->slog_enabled);
579 set_slog_interval(c->slog_interval);
581 gconf_client_set_int(client,
582 KEY_GRAPH_UPDATE_INTERVAL,
583 c->graph_update_interval, NULL);
585 gconf_client_set_int(client,
586 KEY_GRAPH_MONITORING_DURATION,
587 c->graph_monitoring_duration, NULL);
589 gconf_client_set_int(client,
590 KEY_SENSOR_UPDATE_INTERVAL,
591 c->sensor_update_interval, NULL);
593 gconf_client_set_bool(client,
594 KEY_INTERFACE_MENU_BAR_DISABLED,
595 c->menu_bar_disabled, NULL);
597 gconf_client_set_bool(client,
598 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
599 c->unity_launcher_count_disabled, NULL);
601 gconf_client_set_bool(client,
602 KEY_INTERFACE_HIDE_ON_STARTUP,
603 c->hide_on_startup, NULL);
605 gconf_client_set_bool(client,
606 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
607 c->window_restore_enabled,
610 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
611 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
612 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
613 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
615 gconf_client_set_int(client,
616 KEY_INTERFACE_WINDOW_DIVIDER_POS,
617 c->window_divider_pos,
620 gconf_client_set_int(client,
621 KEY_INTERFACE_TEMPERATURE_UNIT,