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)
38 /* Properties of each sensor */
39 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm_enabled";
40 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm_high_threshold";
41 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm_low_threshold";
42 static const char *ATT_SENSOR_COLOR = "color";
43 static const char *ATT_SENSOR_GRAPH_ENABLED = "graph_enabled";
44 static const char *ATT_SENSOR_NAME = "name";
45 static const char *ATT_SENSOR_APPINDICATOR_MENU_DISABLED
46 = "appindicator_menu_disabled";
47 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
48 = "appindicator_label_enabled";
49 static const char *ATT_SENSOR_POSITION = "position";
51 /* Update interval of the measures of the sensors */
52 static const char *KEY_SENSOR_UPDATE_INTERVAL
53 = "/apps/psensor/sensor/update_interval";
56 static const char *KEY_GRAPH_UPDATE_INTERVAL = "graph-update-interval";
58 static const char *KEY_GRAPH_MONITORING_DURATION = "graph-monitoring-duration";
60 static const char *KEY_GRAPH_BACKGROUND_COLOR
61 = "/apps/psensor/graph/background_color";
63 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
65 static const char *KEY_GRAPH_BACKGROUND_ALPHA = "graph-background-alpha";
67 static const char *KEY_GRAPH_FOREGROUND_COLOR
68 = "/apps/psensor/graph/foreground_color";
70 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
72 static const char *KEY_ALPHA_CHANNEL_ENABLED = "graph-alpha-channel-enabled";
74 /* Inteface settings */
75 static const char *KEY_INTERFACE_SENSORLIST_POSITION
76 = "interface-sensorlist-position";
78 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
79 = "interface-window-decoration-disabled";
81 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
82 = "interface-window-keep-below-enabled";
84 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
85 = "interface-menu-bar-disabled";
87 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
88 = "interface-unity-launcher-count-disabled";
90 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
91 = "interface-hide-on-startup";
93 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
94 = "interface-window-restore-enabled";
96 static const char *KEY_INTERFACE_WINDOW_X = "interface-window-x";
97 static const char *KEY_INTERFACE_WINDOW_Y = "interface-window-y";
98 static const char *KEY_INTERFACE_WINDOW_W = "interface-window-w";
99 static const char *KEY_INTERFACE_WINDOW_H = "interface-window-h";
101 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
102 = "interface-window-divider-pos";
104 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
105 = "interface-temperature-unit";
107 /* Sensor logging settings */
108 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
109 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
111 /* Path to the script called when a notification is raised */
112 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
114 static GConfClient *client;
115 static GSettings *settings;
117 static char *user_dir;
119 static GKeyFile *key_file;
121 static char *sensor_config_path;
123 static char *get_string(const char *key, const char *default_value)
127 value = gconf_client_get_string(client, key, NULL);
130 value = strdup(default_value);
131 gconf_client_set_string(client, key, default_value, NULL);
137 static void set_bool(const char *k, bool b)
139 g_settings_set_boolean(settings, k, b);
142 static bool get_bool(const char *k)
144 return g_settings_get_boolean(settings, k);
147 static void set_int(const char *k, int i)
149 g_settings_set_int(settings, k, i);
152 static double get_double(const char *k)
154 return g_settings_get_double(settings, k);
157 static void set_double(const char *k, double d)
159 g_settings_set_double(settings, k, d);
162 static int get_int(const char *k)
164 return g_settings_get_int(settings, k);
167 char *config_get_notif_script()
171 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
172 if (str && !strlen(str)) {
180 void config_set_notif_script(const char *str)
182 if (str && strlen(str) > 0)
183 gconf_client_set_string(client,
184 KEY_NOTIFICATION_SCRIPT, str, NULL);
186 gconf_client_set_string(client,
187 KEY_NOTIFICATION_SCRIPT, "", NULL);
190 static struct color *get_background_color()
195 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
196 DEFAULT_GRAPH_BACKGROUND_COLOR);
198 c = str_to_color(scolor);
202 return color_new(0xffff, 0xffff, 0xffff);
207 static struct color *get_foreground_color()
212 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
213 DEFAULT_GRAPH_FOREGROUND_COLOR);
215 c = str_to_color(scolor);
219 return color_new(0x0000, 0x0000, 0x0000);
224 static bool is_alpha_channel_enabled()
226 return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
229 static void set_alpha_channeld_enabled(bool b)
231 set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
234 static enum sensorlist_position get_sensorlist_position()
236 return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
239 static void set_sensorlist_position(enum sensorlist_position pos)
241 set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
244 static double get_graph_background_alpha()
246 return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
249 static void set_graph_background_alpha(double alpha)
251 set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
254 static void set_background_color(const struct color *color)
258 scolor = color_to_str(color);
260 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
262 gconf_client_set_string(client,
263 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
268 static void set_foreground_color(const struct color *color)
272 str = color_to_str(color);
274 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
276 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
281 bool is_slog_enabled()
283 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
286 static void set_slog_enabled(bool enabled)
288 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
291 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
293 log_debug("config_slog_enabled_notify_add");
294 gconf_client_add_dir(client,
296 GCONF_CLIENT_PRELOAD_NONE,
298 gconf_client_notify_add(client,
306 int config_get_slog_interval()
310 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
318 static void set_slog_interval(int interval)
323 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
326 static bool is_window_decoration_enabled()
328 return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
331 static bool is_window_keep_below_enabled()
333 return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
336 static void set_window_decoration_enabled(bool enabled)
338 set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
341 static void set_window_keep_below_enabled(bool enabled)
343 set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
347 * Initializes the GConf client.
352 client = gconf_client_get_default();
355 settings = g_settings_new("psensor");
358 void config_cleanup()
363 g_object_unref(client);
369 g_object_unref(settings);
379 g_key_file_free(key_file);
383 if (sensor_config_path) {
384 free(sensor_config_path);
385 sensor_config_path = NULL;
389 struct config *config_load()
395 c = malloc(sizeof(struct config));
397 c->graph_bgcolor = get_background_color();
398 c->graph_fgcolor = get_foreground_color();
399 c->graph_bg_alpha = get_graph_background_alpha();
400 c->alpha_channel_enabled = is_alpha_channel_enabled();
401 c->sensorlist_position = get_sensorlist_position();
402 c->window_decoration_enabled = is_window_decoration_enabled();
403 c->window_keep_below_enabled = is_window_keep_below_enabled();
404 c->slog_enabled = is_slog_enabled();
405 c->slog_interval = config_get_slog_interval();
407 c->sensor_update_interval
408 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
409 if (c->sensor_update_interval < 1)
410 c->sensor_update_interval = 1;
412 c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
413 if (c->graph_update_interval < 1)
414 c->graph_update_interval = 1;
416 c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
418 if (c->graph_monitoring_duration < 1)
419 c->graph_monitoring_duration = 10;
421 c->sensor_values_max_length
422 = (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
424 if (c->sensor_values_max_length < 3)
425 c->sensor_values_max_length = 3;
427 c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
429 c->unity_launcher_count_disabled
430 = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
432 c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
434 c->window_restore_enabled
435 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
437 c->window_x = get_int(KEY_INTERFACE_WINDOW_X);
438 c->window_y = get_int(KEY_INTERFACE_WINDOW_Y);
439 c->window_w = get_int(KEY_INTERFACE_WINDOW_W);
440 c->window_h = get_int(KEY_INTERFACE_WINDOW_H);
442 c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
444 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
449 c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
454 void config_save(const struct config *c)
456 set_alpha_channeld_enabled(c->alpha_channel_enabled);
457 set_background_color(c->graph_bgcolor);
458 set_foreground_color(c->graph_fgcolor);
459 set_graph_background_alpha(c->graph_bg_alpha);
460 set_sensorlist_position(c->sensorlist_position);
461 set_window_decoration_enabled(c->window_decoration_enabled);
462 set_window_keep_below_enabled(c->window_keep_below_enabled);
463 set_slog_enabled(c->slog_enabled);
464 set_slog_interval(c->slog_interval);
466 set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
468 set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
470 gconf_client_set_int(client,
471 KEY_SENSOR_UPDATE_INTERVAL,
472 c->sensor_update_interval, NULL);
474 set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled);
476 set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
477 c->unity_launcher_count_disabled);
479 set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
481 set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
482 c->window_restore_enabled);
484 set_int(KEY_INTERFACE_WINDOW_X, c->window_x);
485 set_int(KEY_INTERFACE_WINDOW_Y, c->window_y);
486 set_int(KEY_INTERFACE_WINDOW_W, c->window_w);
487 set_int(KEY_INTERFACE_WINDOW_H, c->window_h);
489 set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
491 set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit);
494 const char *get_psensor_user_dir()
501 home = getenv("HOME");
506 user_dir = path_append(home, ".psensor");
508 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
509 log_err(_("Failed to create the directory %s: %s"),
523 static const char *get_sensor_config_path()
527 if (!sensor_config_path) {
528 dir = get_psensor_user_dir();
531 sensor_config_path = path_append(dir, "psensor.cfg");
534 return sensor_config_path;
537 static GKeyFile *get_sensor_key_file()
544 path = get_sensor_config_path();
546 key_file = g_key_file_new();
549 ret = g_key_file_load_from_file(key_file,
551 G_KEY_FILE_KEEP_COMMENTS
552 | G_KEY_FILE_KEEP_TRANSLATIONS,
556 if (err->code == G_KEY_FILE_ERROR_NOT_FOUND) {
557 log_fct(_("The configuration file "
560 log_err(_("Failed to parse configuration "
570 static void save_sensor_key_file()
578 kfile = get_sensor_key_file();
580 data = g_key_file_to_data(kfile, NULL, NULL);
582 path = get_sensor_config_path();
584 if (!g_file_set_contents(path, data, -1, NULL))
585 log_err(_("Failed to save configuration file %s."), path);
597 save_sensor_key_file();
602 sensor_set_str(const char *sid, const char *att, const char *str)
606 kfile = get_sensor_key_file();
607 g_key_file_set_string(kfile, sid, att, str);
610 static char *sensor_get_str(const char *sid, const char *att)
614 kfile = get_sensor_key_file();
615 return g_key_file_get_string(kfile, sid, att, NULL);
618 static bool sensor_get_bool(const char *sid, const char *att)
622 kfile = get_sensor_key_file();
623 return g_key_file_get_boolean(kfile, sid, att, NULL);
627 sensor_set_bool(const char *sid, const char *att, bool enabled)
631 kfile = get_sensor_key_file();
633 g_key_file_set_boolean(kfile, sid, att, enabled);
636 static int sensor_get_int(const char *sid, const char *att)
640 kfile = get_sensor_key_file();
641 return g_key_file_get_integer(kfile, sid, att, NULL);
644 static void sensor_set_int(const char *sid, const char *att, int i)
648 kfile = get_sensor_key_file();
650 g_key_file_set_integer(kfile, sid, att, i);
653 char *config_get_sensor_name(const char *sid)
655 return sensor_get_str(sid, ATT_SENSOR_NAME);
658 void config_set_sensor_name(const char *sid, const char *name)
660 sensor_set_str(sid, ATT_SENSOR_NAME, name);
663 void config_set_sensor_color(const char *sid, const struct color *color)
667 scolor = color_to_str(color);
669 sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
675 config_get_sensor_color(const char *sid, const struct color *dft)
680 scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
683 color = str_to_color(scolor);
688 color = color_new(dft->red, dft->green, dft->blue);
689 config_set_sensor_color(sid, color);
697 bool config_is_sensor_enabled(const char *sid)
699 return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
702 void config_set_sensor_enabled(const char *sid, bool enabled)
704 sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
707 int config_get_sensor_alarm_high_threshold(const char *sid)
709 return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
712 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
714 sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
717 int config_get_sensor_alarm_low_threshold(const char *sid)
719 return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
722 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
724 sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
727 bool config_is_appindicator_enabled(const char *sid)
729 return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
732 void config_set_appindicator_enabled(const char *sid, bool enabled)
734 return sensor_set_bool(sid,
735 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
739 int config_get_sensor_position(const char *sid)
741 return sensor_get_int(sid, ATT_SENSOR_POSITION);
744 void config_set_sensor_position(const char *sid, int pos)
746 return sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
749 bool config_get_sensor_alarm_enabled(const char *sid)
751 return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
754 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
756 sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
759 bool config_is_appindicator_label_enabled(const char *sid)
761 return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
764 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
766 sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);