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 /* Properties of each sensor */
37 static const char *ATT_SENSOR_ALARM_ENABLED = "alarm_enabled";
38 static const char *ATT_SENSOR_ALARM_HIGH_THRESHOLD = "alarm_high_threshold";
39 static const char *ATT_SENSOR_ALARM_LOW_THRESHOLD = "alarm_low_threshold";
40 static const char *ATT_SENSOR_COLOR = "color";
41 static const char *ATT_SENSOR_GRAPH_ENABLED = "graph_enabled";
42 static const char *ATT_SENSOR_NAME = "name";
43 static const char *ATT_SENSOR_APPINDICATOR_MENU_DISABLED
44 = "appindicator_menu_disabled";
45 static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
46 = "appindicator_label_enabled";
47 static const char *ATT_SENSOR_POSITION = "position";
48 static const char *ATT_SENSOR_HIDE = "hide";
50 /* Update interval of the measures of the sensors */
51 static const char *KEY_SENSOR_UPDATE_INTERVAL
52 = "sensor-update-interval";
55 static const char *KEY_GRAPH_UPDATE_INTERVAL = "graph-update-interval";
56 static const char *KEY_GRAPH_MONITORING_DURATION = "graph-monitoring-duration";
57 static const char *KEY_GRAPH_BACKGROUND_COLOR = "graph-background-color";
58 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
59 static const char *KEY_GRAPH_BACKGROUND_ALPHA = "graph-background-alpha";
60 static const char *KEY_GRAPH_FOREGROUND_COLOR
61 = "graph-foreground-color";
62 static const char *KEY_GRAPH_SMOOTH_CURVES_ENABLED
63 = "graph-smooth-curves-enabled";
65 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
67 static const char *KEY_ALPHA_CHANNEL_ENABLED = "graph-alpha-channel-enabled";
69 /* Inteface settings */
70 static const char *KEY_INTERFACE_SENSORLIST_POSITION
71 = "interface-sensorlist-position";
73 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
74 = "interface-window-decoration-disabled";
76 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
77 = "interface-window-keep-below-enabled";
79 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
80 = "interface-menu-bar-disabled";
82 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
83 = "interface-unity-launcher-count-disabled";
85 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
86 = "interface-hide-on-startup";
88 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
89 = "interface-window-restore-enabled";
91 static const char *KEY_INTERFACE_WINDOW_X = "interface-window-x";
92 static const char *KEY_INTERFACE_WINDOW_Y = "interface-window-y";
93 static const char *KEY_INTERFACE_WINDOW_W = "interface-window-w";
94 static const char *KEY_INTERFACE_WINDOW_H = "interface-window-h";
96 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
97 = "interface-window-divider-pos";
99 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
100 = "interface-temperature-unit";
102 /* Sensor logging settings */
103 static const char *KEY_SLOG_ENABLED = "slog-enabled";
104 static const char *KEY_SLOG_INTERVAL = "slog-interval";
106 /* Path to the script called when a notification is raised */
107 static const char *KEY_NOTIFICATION_SCRIPT = "notif-script";
109 static GSettings *settings;
111 static char *user_dir;
113 static GKeyFile *key_file;
115 static char *sensor_config_path;
117 static void (*slog_enabled_cbk)(void *);
119 static char *get_string(const char *key)
121 return g_settings_get_string(settings, key);
124 static void set_string(const char *key, const char *str)
126 g_settings_set_string(settings, key, str);
129 static void set_bool(const char *k, bool b)
131 g_settings_set_boolean(settings, k, b);
134 static bool get_bool(const char *k)
136 return g_settings_get_boolean(settings, k);
139 static void set_int(const char *k, int i)
141 g_settings_set_int(settings, k, i);
144 static double get_double(const char *k)
146 return g_settings_get_double(settings, k);
149 static void set_double(const char *k, double d)
151 g_settings_set_double(settings, k, d);
154 static int get_int(const char *k)
156 return g_settings_get_int(settings, k);
159 char *config_get_notif_script()
163 str = get_string(KEY_NOTIFICATION_SCRIPT);
164 if (str && !strlen(str)) {
172 void config_set_notif_script(const char *str)
174 if (str && strlen(str) > 0)
175 set_string(KEY_NOTIFICATION_SCRIPT, str);
177 set_string(KEY_NOTIFICATION_SCRIPT, "");
180 static struct color *get_background_color()
185 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR);
187 c = str_to_color(scolor);
191 return color_new(1, 1, 1);
196 static struct color *get_foreground_color()
201 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR);
203 c = str_to_color(scolor);
207 return color_new(0, 0, 0);
212 static bool is_alpha_channel_enabled()
214 return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
217 static void set_alpha_channeld_enabled(bool b)
219 set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
222 static enum sensorlist_position get_sensorlist_position()
224 return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
227 static void set_sensorlist_position(enum sensorlist_position pos)
229 set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
232 static double get_graph_background_alpha()
234 return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
237 static void set_graph_background_alpha(double alpha)
239 set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
242 static void set_background_color(const struct color *color)
246 scolor = color_to_str(color);
248 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
250 set_string(KEY_GRAPH_BACKGROUND_COLOR, scolor);
255 static void set_foreground_color(const struct color *color)
259 str = color_to_str(color);
261 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
263 set_string(KEY_GRAPH_FOREGROUND_COLOR, str);
268 bool is_slog_enabled()
270 return get_bool(KEY_SLOG_ENABLED);
273 static void set_slog_enabled(bool enabled)
275 set_bool(KEY_SLOG_ENABLED, enabled);
278 static void slog_enabled_changed_cbk(GSettings *settings,
282 if (slog_enabled_cbk)
283 slog_enabled_cbk(data);
286 void config_set_slog_enabled_changed_cbk(void (*cbk)(void *), void *data)
290 slog_enabled_cbk = cbk;
292 g_signal_connect_after(settings,
293 "changed::slog-enabled",
294 G_CALLBACK(slog_enabled_changed_cbk),
300 int config_get_slog_interval()
302 return get_int(KEY_SLOG_INTERVAL);
305 static void set_slog_interval(int interval)
310 set_int(KEY_SLOG_INTERVAL, interval);
313 static bool is_window_decoration_enabled()
315 return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
318 static bool is_window_keep_below_enabled()
320 return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
323 static void set_window_decoration_enabled(bool enabled)
325 set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
328 static void set_window_keep_below_enabled(bool enabled)
330 set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
333 bool config_is_smooth_curves_enabled()
335 return get_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED);
338 void config_set_smooth_curves_enabled(bool b)
340 set_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED, b);
349 settings = g_settings_new("psensor");
354 void config_cleanup()
360 g_object_unref(settings);
370 g_key_file_free(key_file);
374 if (sensor_config_path) {
375 free(sensor_config_path);
376 sensor_config_path = NULL;
379 slog_enabled_cbk = NULL;
382 struct config *config_load()
388 c = malloc(sizeof(struct config));
390 c->graph_bgcolor = get_background_color();
391 c->graph_fgcolor = get_foreground_color();
392 c->graph_bg_alpha = get_graph_background_alpha();
393 c->alpha_channel_enabled = is_alpha_channel_enabled();
394 c->sensorlist_position = get_sensorlist_position();
395 c->window_decoration_enabled = is_window_decoration_enabled();
396 c->window_keep_below_enabled = is_window_keep_below_enabled();
397 c->slog_enabled = is_slog_enabled();
398 c->slog_interval = config_get_slog_interval();
400 c->sensor_update_interval
401 = get_int(KEY_SENSOR_UPDATE_INTERVAL);
402 if (c->sensor_update_interval < 1)
403 c->sensor_update_interval = 1;
405 c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
406 if (c->graph_update_interval < 1)
407 c->graph_update_interval = 1;
409 c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
411 if (c->graph_monitoring_duration < 1)
412 c->graph_monitoring_duration = 10;
414 c->sensor_values_max_length
415 = (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
417 if (c->sensor_values_max_length < 3)
418 c->sensor_values_max_length = 3;
420 c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
422 c->unity_launcher_count_disabled
423 = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
425 c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
427 c->window_restore_enabled
428 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
430 c->window_x = get_int(KEY_INTERFACE_WINDOW_X);
431 c->window_y = get_int(KEY_INTERFACE_WINDOW_Y);
432 c->window_w = get_int(KEY_INTERFACE_WINDOW_W);
433 c->window_h = get_int(KEY_INTERFACE_WINDOW_H);
435 c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
437 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
442 c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
447 void config_save(const struct config *c)
449 set_alpha_channeld_enabled(c->alpha_channel_enabled);
450 set_background_color(c->graph_bgcolor);
451 set_foreground_color(c->graph_fgcolor);
452 set_graph_background_alpha(c->graph_bg_alpha);
453 set_sensorlist_position(c->sensorlist_position);
454 set_window_decoration_enabled(c->window_decoration_enabled);
455 set_window_keep_below_enabled(c->window_keep_below_enabled);
456 set_slog_enabled(c->slog_enabled);
457 set_slog_interval(c->slog_interval);
459 set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
461 set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
463 set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval);
465 set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled);
467 set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
468 c->unity_launcher_count_disabled);
470 set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
472 set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
473 c->window_restore_enabled);
475 set_int(KEY_INTERFACE_WINDOW_X, c->window_x);
476 set_int(KEY_INTERFACE_WINDOW_Y, c->window_y);
477 set_int(KEY_INTERFACE_WINDOW_W, c->window_w);
478 set_int(KEY_INTERFACE_WINDOW_H, c->window_h);
480 set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
482 set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit);
485 const char *get_psensor_user_dir()
492 home = getenv("HOME");
497 user_dir = path_append(home, ".psensor");
499 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
500 log_err(_("Failed to create the directory %s: %s"),
514 static const char *get_sensor_config_path()
518 if (!sensor_config_path) {
519 dir = get_psensor_user_dir();
522 sensor_config_path = path_append(dir, "psensor.cfg");
525 return sensor_config_path;
528 static GKeyFile *get_sensor_key_file()
535 path = get_sensor_config_path();
537 key_file = g_key_file_new();
540 ret = g_key_file_load_from_file(key_file,
542 G_KEY_FILE_KEEP_COMMENTS
543 | G_KEY_FILE_KEEP_TRANSLATIONS,
547 log_warn(_("Failed to load configuration file %s: %s"),
555 static void save_sensor_key_file()
563 kfile = get_sensor_key_file();
565 data = g_key_file_to_data(kfile, NULL, NULL);
567 path = get_sensor_config_path();
569 if (!g_file_set_contents(path, data, -1, NULL))
570 log_err(_("Failed to save configuration file %s."), path);
582 save_sensor_key_file();
586 static void sensor_set_str(const char *sid, const char *att, const char *str)
590 kfile = get_sensor_key_file();
591 g_key_file_set_string(kfile, sid, att, str);
594 static char *sensor_get_str(const char *sid, const char *att)
598 kfile = get_sensor_key_file();
599 return g_key_file_get_string(kfile, sid, att, NULL);
602 static bool sensor_get_bool(const char *sid, const char *att)
606 kfile = get_sensor_key_file();
607 return g_key_file_get_boolean(kfile, sid, att, NULL);
610 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
614 kfile = get_sensor_key_file();
616 g_key_file_set_boolean(kfile, sid, att, enabled);
619 static int sensor_get_int(const char *sid, const char *att)
623 kfile = get_sensor_key_file();
624 return g_key_file_get_integer(kfile, sid, att, NULL);
627 static void sensor_set_int(const char *sid, const char *att, int i)
631 kfile = get_sensor_key_file();
633 g_key_file_set_integer(kfile, sid, att, i);
636 char *config_get_sensor_name(const char *sid)
638 return sensor_get_str(sid, ATT_SENSOR_NAME);
641 void config_set_sensor_name(const char *sid, const char *name)
643 sensor_set_str(sid, ATT_SENSOR_NAME, name);
646 void config_set_sensor_color(const char *sid, const struct color *color)
650 scolor = color_to_str(color);
652 sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
658 config_get_sensor_color(const char *sid, const struct color *dft)
663 scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
666 color = str_to_color(scolor);
671 color = color_new(dft->red, dft->green, dft->blue);
672 config_set_sensor_color(sid, color);
680 bool config_is_sensor_graph_enabled(const char *sid)
682 return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
685 void config_set_sensor_graph_enabled(const char *sid, bool enabled)
687 sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
690 int config_get_sensor_alarm_high_threshold(const char *sid)
692 return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
695 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
697 sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
700 int config_get_sensor_alarm_low_threshold(const char *sid)
702 return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
705 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
707 sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
710 bool config_is_appindicator_enabled(const char *sid)
712 return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
715 void config_set_appindicator_enabled(const char *sid, bool enabled)
717 return sensor_set_bool(sid,
718 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
722 int config_get_sensor_position(const char *sid)
724 return sensor_get_int(sid, ATT_SENSOR_POSITION);
727 void config_set_sensor_position(const char *sid, int pos)
729 return sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
732 bool config_get_sensor_alarm_enabled(const char *sid)
734 return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
737 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
739 sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
742 bool config_is_sensor_enabled(const char *sid)
744 return !sensor_get_bool(sid, ATT_SENSOR_HIDE);
747 void config_set_sensor_enabled(const char *sid, bool enabled)
749 sensor_set_bool(sid, ATT_SENSOR_HIDE, !enabled);
752 bool config_is_appindicator_label_enabled(const char *sid)
754 return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
757 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
759 sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);
762 GSettings *config_get_GSettings()