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 *ATT_SENSOR_ALARM_ENABLED = "alarm_enabled";
37 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm_high_threshold";
38 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm_low_threshold";
39 static const char *ATT_SENSOR_COLOR = "color";
40 static const char *ATT_SENSOR_GRAPH_ENABLED = "graph_enabled";
41 static const char *ATT_SENSOR_NAME = "name";
42 static const char *ATT_SENSOR_APPINDICATOR_MENU_DISABLED
43 = "appindicator_menu_disabled";
44 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
45 = "appindicator_label_enabled";
46 static const char *ATT_SENSOR_POSITION = "position";
48 static const char *KEY_SENSOR_UPDATE_INTERVAL
49 = "/apps/psensor/sensor/update_interval";
51 static const char *KEY_GRAPH_UPDATE_INTERVAL
52 = "/apps/psensor/graph/update_interval";
54 static const char *KEY_GRAPH_MONITORING_DURATION
55 = "/apps/psensor/graph/monitoring_duration";
57 static const char *KEY_GRAPH_BACKGROUND_COLOR
58 = "/apps/psensor/graph/background_color";
60 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
62 static const char *KEY_GRAPH_BACKGROUND_ALPHA
63 = "/apps/psensor/graph/background_alpha";
65 static const char *KEY_GRAPH_FOREGROUND_COLOR
66 = "/apps/psensor/graph/foreground_color";
67 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
69 static const char *KEY_ALPHA_CHANNEL_ENABLED
70 = "/apps/psensor/graph/alpha_channel_enabled";
72 static const char *KEY_INTERFACE_SENSORLIST_POSITION
73 = "/apps/psensor/interface/sensorlist_position";
75 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
76 = "/apps/psensor/interface/window_decoration_disabled";
78 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
79 = "/apps/psensor/interface/window_keep_below_enabled";
81 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
82 = "/apps/psensor/interface/menu_bar_disabled";
84 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
85 = "/apps/psensor/interface/unity_launcher_count_disabled";
87 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
88 = "/apps/psensor/interface/hide_on_startup";
90 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
91 = "/apps/psensor/interface/window_restore_enabled";
93 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
94 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
95 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
96 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
98 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
99 = "/apps/psensor/interface/window_divider_pos";
101 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
102 = "/apps/psensor/interface/temperature_unit";
104 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
105 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
107 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
109 static GConfClient *client;
111 static char *user_dir;
113 static GKeyFile *key_file;
115 static char *sensor_config_path;
117 static char *get_string(const char *key, const char *default_value)
121 value = gconf_client_get_string(client, key, NULL);
124 value = strdup(default_value);
125 gconf_client_set_string(client, key, default_value, NULL);
131 char *config_get_notif_script()
135 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
136 if (str && !strlen(str)) {
144 void config_set_notif_script(const char *str)
146 if (str && strlen(str) > 0)
147 gconf_client_set_string(client,
148 KEY_NOTIFICATION_SCRIPT, str, NULL);
150 gconf_client_set_string(client,
151 KEY_NOTIFICATION_SCRIPT, "", NULL);
154 static struct color *get_background_color()
159 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
160 DEFAULT_GRAPH_BACKGROUND_COLOR);
162 c = str_to_color(scolor);
166 return color_new(0xffff, 0xffff, 0xffff);
171 static struct color *get_foreground_color()
176 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
177 DEFAULT_GRAPH_FOREGROUND_COLOR);
179 c = str_to_color(scolor);
183 return color_new(0x0000, 0x0000, 0x0000);
188 static bool is_alpha_channel_enabled()
190 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
193 static enum sensorlist_position get_sensorlist_position()
195 return gconf_client_get_int(client,
196 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
199 static void set_sensorlist_position(enum sensorlist_position pos)
201 gconf_client_set_int(client,
202 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
205 static double get_graph_background_alpha()
209 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
211 gconf_client_set_float(client,
212 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
216 static void set_graph_background_alpha(double alpha)
218 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
221 static void set_background_color(const struct color *color)
225 scolor = color_to_str(color);
227 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
229 gconf_client_set_string(client,
230 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
235 static void set_foreground_color(const struct color *color)
239 str = color_to_str(color);
241 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
243 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
248 bool is_slog_enabled()
250 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
253 static void set_slog_enabled(bool enabled)
255 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
258 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
260 log_debug("config_slog_enabled_notify_add");
261 gconf_client_add_dir(client,
263 GCONF_CLIENT_PRELOAD_NONE,
265 gconf_client_notify_add(client,
273 int config_get_slog_interval()
277 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
285 static void set_slog_interval(int interval)
290 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
293 static bool is_window_decoration_enabled()
295 return !gconf_client_get_bool(client,
296 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
300 static bool is_window_keep_below_enabled()
302 return gconf_client_get_bool(client,
303 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
307 static void set_window_decoration_enabled(bool enabled)
309 gconf_client_set_bool
311 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
314 static void set_window_keep_below_enabled(bool enabled)
316 gconf_client_set_bool(client,
317 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
322 * Initializes the GConf client.
327 client = gconf_client_get_default();
330 void config_cleanup()
333 g_object_unref(client);
345 g_key_file_free(key_file);
349 if (sensor_config_path) {
350 free(sensor_config_path);
351 sensor_config_path = NULL;
355 struct config *config_load()
361 c = malloc(sizeof(struct config));
363 c->graph_bgcolor = get_background_color();
364 c->graph_fgcolor = get_foreground_color();
365 c->graph_bg_alpha = get_graph_background_alpha();
366 c->alpha_channel_enabled = is_alpha_channel_enabled();
367 c->sensorlist_position = get_sensorlist_position();
368 c->window_decoration_enabled = is_window_decoration_enabled();
369 c->window_keep_below_enabled = is_window_keep_below_enabled();
370 c->slog_enabled = is_slog_enabled();
371 c->slog_interval = config_get_slog_interval();
373 c->sensor_update_interval
374 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
375 if (c->sensor_update_interval < 1)
376 c->sensor_update_interval = 1;
378 c->graph_update_interval
379 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
380 if (c->graph_update_interval < 1)
381 c->graph_update_interval = 1;
383 c->graph_monitoring_duration
384 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
386 if (c->graph_monitoring_duration < 1)
387 c->graph_monitoring_duration = 10;
389 c->sensor_values_max_length
391 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
392 if (c->sensor_values_max_length < 3)
393 c->sensor_values_max_length = 3;
396 = gconf_client_get_bool(client,
397 KEY_INTERFACE_MENU_BAR_DISABLED,
400 c->unity_launcher_count_disabled
401 = gconf_client_get_bool
403 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
407 = gconf_client_get_bool(client,
408 KEY_INTERFACE_HIDE_ON_STARTUP,
411 c->window_restore_enabled
412 = gconf_client_get_bool(client,
413 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
416 c->window_x = gconf_client_get_int(client,
417 KEY_INTERFACE_WINDOW_X,
419 c->window_y = gconf_client_get_int(client,
420 KEY_INTERFACE_WINDOW_Y,
422 c->window_w = gconf_client_get_int(client,
423 KEY_INTERFACE_WINDOW_W,
425 c->window_h = gconf_client_get_int(client,
426 KEY_INTERFACE_WINDOW_H,
428 c->window_divider_pos
429 = gconf_client_get_int(client,
430 KEY_INTERFACE_WINDOW_DIVIDER_POS,
433 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
438 c->temperature_unit = gconf_client_get_int
439 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
444 void config_save(const struct config *c)
446 set_background_color(c->graph_bgcolor);
447 set_foreground_color(c->graph_fgcolor);
448 set_graph_background_alpha(c->graph_bg_alpha);
449 set_sensorlist_position(c->sensorlist_position);
450 set_window_decoration_enabled(c->window_decoration_enabled);
451 set_window_keep_below_enabled(c->window_keep_below_enabled);
452 set_slog_enabled(c->slog_enabled);
453 set_slog_interval(c->slog_interval);
455 gconf_client_set_int(client,
456 KEY_GRAPH_UPDATE_INTERVAL,
457 c->graph_update_interval, NULL);
459 gconf_client_set_int(client,
460 KEY_GRAPH_MONITORING_DURATION,
461 c->graph_monitoring_duration, NULL);
463 gconf_client_set_int(client,
464 KEY_SENSOR_UPDATE_INTERVAL,
465 c->sensor_update_interval, NULL);
467 gconf_client_set_bool(client,
468 KEY_INTERFACE_MENU_BAR_DISABLED,
469 c->menu_bar_disabled, NULL);
471 gconf_client_set_bool(client,
472 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
473 c->unity_launcher_count_disabled, NULL);
475 gconf_client_set_bool(client,
476 KEY_INTERFACE_HIDE_ON_STARTUP,
477 c->hide_on_startup, NULL);
479 gconf_client_set_bool(client,
480 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
481 c->window_restore_enabled,
484 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
485 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
486 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
487 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
489 gconf_client_set_int(client,
490 KEY_INTERFACE_WINDOW_DIVIDER_POS,
491 c->window_divider_pos,
494 gconf_client_set_int(client,
495 KEY_INTERFACE_TEMPERATURE_UNIT,
500 const char *get_psensor_user_dir()
507 home = getenv("HOME");
512 user_dir = path_append(home, ".psensor");
514 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
515 log_err(_("Failed to create the directory %s: %s"),
529 static const char *get_sensor_config_path()
533 if (!sensor_config_path) {
534 dir = get_psensor_user_dir();
537 sensor_config_path = path_append(dir, "psensor.cfg");
540 return sensor_config_path;
543 static GKeyFile *get_sensor_key_file()
550 path = get_sensor_config_path();
552 key_file = g_key_file_new();
555 ret = g_key_file_load_from_file(key_file,
557 G_KEY_FILE_KEEP_COMMENTS
558 | G_KEY_FILE_KEEP_TRANSLATIONS,
562 if (err->code == G_KEY_FILE_ERROR_NOT_FOUND) {
563 log_fct(_("The configuration file "
566 log_err(_("Failed to parse configuration "
576 static void save_sensor_key_file()
584 kfile = get_sensor_key_file();
586 data = g_key_file_to_data(kfile, NULL, NULL);
588 path = get_sensor_config_path();
590 if (!g_file_set_contents(path, data, -1, NULL))
591 log_err(_("Failed to save configuration file %s."), path);
601 save_sensor_key_file();
606 config_sensor_set_string(const char *sid, const char *att, const char *str)
610 kfile = get_sensor_key_file();
611 g_key_file_set_string(kfile, sid, att, str);
614 static char *config_sensor_get_string(const char *sid, const char *att)
618 kfile = get_sensor_key_file();
619 return g_key_file_get_string(kfile, sid, att, NULL);
622 static bool config_sensor_get_bool(const char *sid, const char *att)
627 kfile = get_sensor_key_file();
628 return g_key_file_get_boolean(kfile, sid, att, NULL);
632 config_sensor_set_bool(const char *sid, const char *att, bool enabled)
636 kfile = get_sensor_key_file();
638 g_key_file_set_boolean(kfile, sid, att, enabled);
641 static int config_sensor_get_int(const char *sid, const char *att)
646 kfile = get_sensor_key_file();
647 return g_key_file_get_integer(kfile, sid, att, NULL);
651 config_sensor_set_int(const char *sid, const char *att, int i)
655 kfile = get_sensor_key_file();
657 g_key_file_set_integer(kfile, sid, att, i);
660 char *config_get_sensor_name(const char *sid)
662 return config_sensor_get_string(sid, ATT_SENSOR_NAME);
665 void config_set_sensor_name(const char *sid, const char *name)
667 config_sensor_set_string(sid, ATT_SENSOR_NAME, name);
670 void config_set_sensor_color(const char *sid, const struct color *color)
674 scolor = color_to_str(color);
676 config_sensor_set_string(sid, ATT_SENSOR_COLOR, scolor);
682 config_get_sensor_color(const char *sid, const struct color *dft)
687 scolor = config_sensor_get_string(sid, ATT_SENSOR_COLOR);
690 color = str_to_color(scolor);
695 color = color_new(dft->red, dft->green, dft->blue);
696 config_set_sensor_color(sid, color);
704 bool config_is_sensor_enabled(const char *sid)
706 return config_sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
709 void config_set_sensor_enabled(const char *sid, bool enabled)
711 config_sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
714 int config_get_sensor_alarm_high_threshold(const char *sid)
716 return config_sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
719 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
721 config_sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
724 int config_get_sensor_alarm_low_threshold(const char *sid)
726 return config_sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
729 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
731 config_sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
734 bool config_is_appindicator_enabled(const char *sid)
736 return !config_sensor_get_bool(sid,
737 ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
740 void config_set_appindicator_enabled(const char *sid, bool enabled)
742 return config_sensor_set_bool(sid,
743 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
747 int config_get_sensor_position(const char *sid)
749 return config_sensor_get_int(sid, ATT_SENSOR_POSITION);
752 void config_set_sensor_position(const char *sid, int pos)
754 return config_sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
757 bool config_get_sensor_alarm_enabled(const char *sid)
759 return config_sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
762 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
764 config_sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
767 bool config_is_appindicator_label_enabled(const char *sid)
769 return config_sensor_get_bool(sid,
770 ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
773 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
775 config_sensor_set_bool(sid,
776 ATT_SENSOR_APPINDICATOR_LABEL_ENABLED,