2 * Copyright (C) 2010-2014 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
26 #include <sys/types.h>
30 #define _(str) gettext(str)
36 static const char *KEY_SENSORS = "/apps/psensor/sensors";
38 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm/enabled";
39 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm/high_threshold";
40 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm/low_threshold";
41 static const char *ATT_SENSOR_COLOR = "color";
42 static const char *ATT_SENSOR_ENABLED = "enabled";
43 static const char *ATT_SENSOR_NAME = "name";
44 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
45 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
46 = "appindicator/menu/enabled";
48 static const char *ATT_SENSOR_POSITION = "position";
50 static const char *KEY_SENSOR_UPDATE_INTERVAL
51 = "/apps/psensor/sensor/update_interval";
53 static const char *KEY_GRAPH_UPDATE_INTERVAL
54 = "/apps/psensor/graph/update_interval";
56 static const char *KEY_GRAPH_MONITORING_DURATION
57 = "/apps/psensor/graph/monitoring_duration";
59 static const char *KEY_GRAPH_BACKGROUND_COLOR
60 = "/apps/psensor/graph/background_color";
62 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
64 static const char *KEY_GRAPH_BACKGROUND_ALPHA
65 = "/apps/psensor/graph/background_alpha";
67 static const char *KEY_GRAPH_FOREGROUND_COLOR
68 = "/apps/psensor/graph/foreground_color";
69 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
71 static const char *KEY_ALPHA_CHANNEL_ENABLED
72 = "/apps/psensor/graph/alpha_channel_enabled";
74 static const char *KEY_INTERFACE_SENSORLIST_POSITION
75 = "/apps/psensor/interface/sensorlist_position";
77 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
78 = "/apps/psensor/interface/window_decoration_disabled";
80 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
81 = "/apps/psensor/interface/window_keep_below_enabled";
83 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
84 = "/apps/psensor/interface/menu_bar_disabled";
86 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
87 = "/apps/psensor/interface/unity_launcher_count_disabled";
89 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
90 = "/apps/psensor/interface/hide_on_startup";
92 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
93 = "/apps/psensor/interface/window_restore_enabled";
95 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
96 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
97 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
98 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
100 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
101 = "/apps/psensor/interface/window_divider_pos";
103 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
104 = "/apps/psensor/interface/temperature_unit";
106 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
107 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
109 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
111 static GConfClient *client;
113 static char *user_dir;
115 static char *get_string(const char *key, const char *default_value)
119 value = gconf_client_get_string(client, key, NULL);
122 value = strdup(default_value);
123 gconf_client_set_string(client, key, default_value, NULL);
129 char *config_get_notif_script()
133 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
134 if (str && !strlen(str)) {
142 void config_set_notif_script(const char *str)
144 if (str && strlen(str) > 0)
145 gconf_client_set_string(client,
146 KEY_NOTIFICATION_SCRIPT, str, NULL);
148 gconf_client_set_string(client,
149 KEY_NOTIFICATION_SCRIPT, "", NULL);
152 static struct color *get_background_color()
157 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
158 DEFAULT_GRAPH_BACKGROUND_COLOR);
160 c = str_to_color(scolor);
164 return color_new(0xffff, 0xffff, 0xffff);
169 static struct color *get_foreground_color()
174 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
175 DEFAULT_GRAPH_FOREGROUND_COLOR);
177 c = str_to_color(scolor);
181 return color_new(0x0000, 0x0000, 0x0000);
186 static bool is_alpha_channel_enabled()
188 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
191 static enum sensorlist_position get_sensorlist_position()
193 return gconf_client_get_int(client,
194 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
197 static void set_sensorlist_position(enum sensorlist_position pos)
199 gconf_client_set_int(client,
200 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
203 static double get_graph_background_alpha()
207 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
209 gconf_client_set_float(client,
210 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
214 static void set_graph_background_alpha(double alpha)
216 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
219 static void set_background_color(const struct color *color)
223 scolor = color_to_str(color);
225 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
227 gconf_client_set_string(client,
228 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
233 static void set_foreground_color(const struct color *color)
237 str = color_to_str(color);
239 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
241 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
246 static char *get_sensor_att_key(const char *sid, const char *att)
250 esc_sid = gconf_escape_key(sid, -1);
251 /* [KEY_SENSORS]/[esc_sid]/[att] */
252 key = malloc(strlen(KEY_SENSORS)
253 + 1 + 2 * strlen(esc_sid)
254 + 1 + strlen(att) + 1);
256 sprintf(key, "%s/%s/%s", KEY_SENSORS, esc_sid, att);
264 config_get_sensor_color(const char *sid, const struct color *dft)
269 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
271 scolor = gconf_client_get_string(client, key, NULL);
276 color = str_to_color(scolor);
278 if (!scolor || !color) {
279 color = color_new(dft->red, dft->green, dft->blue);
280 scolor = color_to_str(color);
281 gconf_client_set_string(client, key, scolor, NULL);
290 void config_set_sensor_color(const char *sid, const struct color *color)
294 key = get_sensor_att_key(sid, ATT_SENSOR_COLOR);
295 scolor = color_to_str(color);
297 gconf_client_set_string(client, key, scolor, NULL);
303 int config_get_sensor_alarm_high_threshold(const char *sid)
308 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
309 res = gconf_client_get_int(client, key, NULL);
316 config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
320 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
321 gconf_client_set_int(client, key, threshold, NULL);
325 int config_get_sensor_alarm_low_threshold(const char *sid)
330 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
331 res = gconf_client_get_int(client, key, NULL);
338 config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
342 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
343 gconf_client_set_int(client, key, threshold, NULL);
347 bool config_get_sensor_alarm_enabled(const char *sid)
352 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
353 b = gconf_client_get_bool(client, key, NULL);
359 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
363 key = get_sensor_att_key(sid, ATT_SENSOR_ALARM_ENABLED);
364 gconf_client_set_bool(client, key, enabled, NULL);
368 bool config_is_sensor_enabled(const char *sid)
373 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
374 b = gconf_client_get_bool(client, key, NULL);
380 void config_set_sensor_enabled(const char *sid, bool enabled)
384 key = get_sensor_att_key(sid, ATT_SENSOR_ENABLED);
385 gconf_client_set_bool(client, key, enabled, NULL);
389 char *config_get_sensor_name(const char *sid)
393 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
394 name = gconf_client_get_string(client, key, NULL);
400 void config_set_sensor_name(const char *sid, const char *name)
404 key = get_sensor_att_key(sid, ATT_SENSOR_NAME);
405 gconf_client_set_string(client, key, name, NULL);
409 int config_get_sensor_position(const char *sid)
414 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
415 pos = gconf_client_get_int(client, key, NULL);
421 void config_set_sensor_position(const char *sid, int pos)
425 key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
426 gconf_client_set_int(client, key, pos, NULL);
430 bool config_is_appindicator_enabled(const char *sid)
435 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
436 b = gconf_client_get_bool(client, key, NULL);
442 void config_set_appindicator_enabled(const char *sid, bool enabled)
446 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_DISABLED);
447 gconf_client_set_bool(client, key, !enabled, NULL);
451 bool config_is_appindicator_label_enabled(const char *sid)
456 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
457 b = gconf_client_get_bool(client, key, NULL);
463 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
467 key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
468 gconf_client_set_bool(client, key, enabled, NULL);
472 bool is_slog_enabled()
474 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
477 static void set_slog_enabled(bool enabled)
479 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
482 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
484 log_debug("config_slog_enabled_notify_add");
485 gconf_client_add_dir(client,
487 GCONF_CLIENT_PRELOAD_NONE,
489 gconf_client_notify_add(client,
497 int config_get_slog_interval()
501 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
509 static void set_slog_interval(int interval)
514 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
517 static bool is_window_decoration_enabled()
519 return !gconf_client_get_bool(client,
520 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
524 static bool is_window_keep_below_enabled()
526 return gconf_client_get_bool(client,
527 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
531 static void set_window_decoration_enabled(bool enabled)
533 gconf_client_set_bool
535 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
538 static void set_window_keep_below_enabled(bool enabled)
540 gconf_client_set_bool(client,
541 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
546 * Initializes the GConf client.
551 client = gconf_client_get_default();
554 void config_cleanup()
557 g_object_unref(client);
567 struct config *config_load()
573 c = malloc(sizeof(struct config));
575 c->graph_bgcolor = get_background_color();
576 c->graph_fgcolor = get_foreground_color();
577 c->graph_bg_alpha = get_graph_background_alpha();
578 c->alpha_channel_enabled = is_alpha_channel_enabled();
579 c->sensorlist_position = get_sensorlist_position();
580 c->window_decoration_enabled = is_window_decoration_enabled();
581 c->window_keep_below_enabled = is_window_keep_below_enabled();
582 c->slog_enabled = is_slog_enabled();
583 c->slog_interval = config_get_slog_interval();
585 c->sensor_update_interval
586 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
587 if (c->sensor_update_interval < 1)
588 c->sensor_update_interval = 1;
590 c->graph_update_interval
591 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
592 if (c->graph_update_interval < 1)
593 c->graph_update_interval = 1;
595 c->graph_monitoring_duration
596 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
598 if (c->graph_monitoring_duration < 1)
599 c->graph_monitoring_duration = 10;
601 c->sensor_values_max_length
603 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
604 if (c->sensor_values_max_length < 3)
605 c->sensor_values_max_length = 3;
608 = gconf_client_get_bool(client,
609 KEY_INTERFACE_MENU_BAR_DISABLED,
612 c->unity_launcher_count_disabled
613 = gconf_client_get_bool
615 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
619 = gconf_client_get_bool(client,
620 KEY_INTERFACE_HIDE_ON_STARTUP,
623 c->window_restore_enabled
624 = gconf_client_get_bool(client,
625 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
628 c->window_x = gconf_client_get_int(client,
629 KEY_INTERFACE_WINDOW_X,
631 c->window_y = gconf_client_get_int(client,
632 KEY_INTERFACE_WINDOW_Y,
634 c->window_w = gconf_client_get_int(client,
635 KEY_INTERFACE_WINDOW_W,
637 c->window_h = gconf_client_get_int(client,
638 KEY_INTERFACE_WINDOW_H,
640 c->window_divider_pos
641 = gconf_client_get_int(client,
642 KEY_INTERFACE_WINDOW_DIVIDER_POS,
645 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
650 c->temperature_unit = gconf_client_get_int
651 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
656 void config_save(const struct config *c)
658 set_background_color(c->graph_bgcolor);
659 set_foreground_color(c->graph_fgcolor);
660 set_graph_background_alpha(c->graph_bg_alpha);
661 set_sensorlist_position(c->sensorlist_position);
662 set_window_decoration_enabled(c->window_decoration_enabled);
663 set_window_keep_below_enabled(c->window_keep_below_enabled);
664 set_slog_enabled(c->slog_enabled);
665 set_slog_interval(c->slog_interval);
667 gconf_client_set_int(client,
668 KEY_GRAPH_UPDATE_INTERVAL,
669 c->graph_update_interval, NULL);
671 gconf_client_set_int(client,
672 KEY_GRAPH_MONITORING_DURATION,
673 c->graph_monitoring_duration, NULL);
675 gconf_client_set_int(client,
676 KEY_SENSOR_UPDATE_INTERVAL,
677 c->sensor_update_interval, NULL);
679 gconf_client_set_bool(client,
680 KEY_INTERFACE_MENU_BAR_DISABLED,
681 c->menu_bar_disabled, NULL);
683 gconf_client_set_bool(client,
684 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
685 c->unity_launcher_count_disabled, NULL);
687 gconf_client_set_bool(client,
688 KEY_INTERFACE_HIDE_ON_STARTUP,
689 c->hide_on_startup, NULL);
691 gconf_client_set_bool(client,
692 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
693 c->window_restore_enabled,
696 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
697 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
698 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
699 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
701 gconf_client_set_int(client,
702 KEY_INTERFACE_WINDOW_DIVIDER_POS,
703 c->window_divider_pos,
706 gconf_client_set_int(client,
707 KEY_INTERFACE_TEMPERATURE_UNIT,
712 const char *get_psensor_user_dir()
719 home = getenv("HOME");
724 user_dir = path_append(home, ".psensor");
726 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
727 log_err(_("Failed to create the directory %s: %s"),