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 = "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 = "graph-background-color";
62 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
64 static const char *KEY_GRAPH_BACKGROUND_ALPHA = "graph-background-alpha";
66 static const char *KEY_GRAPH_FOREGROUND_COLOR
67 = "graph-foreground-color";
69 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
71 static const char *KEY_ALPHA_CHANNEL_ENABLED = "graph-alpha-channel-enabled";
73 /* Inteface settings */
74 static const char *KEY_INTERFACE_SENSORLIST_POSITION
75 = "interface-sensorlist-position";
77 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
78 = "interface-window-decoration-disabled";
80 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
81 = "interface-window-keep-below-enabled";
83 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
84 = "interface-menu-bar-disabled";
86 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
87 = "interface-unity-launcher-count-disabled";
89 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
90 = "interface-hide-on-startup";
92 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
93 = "interface-window-restore-enabled";
95 static const char *KEY_INTERFACE_WINDOW_X = "interface-window-x";
96 static const char *KEY_INTERFACE_WINDOW_Y = "interface-window-y";
97 static const char *KEY_INTERFACE_WINDOW_W = "interface-window-w";
98 static const char *KEY_INTERFACE_WINDOW_H = "interface-window-h";
100 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
101 = "interface-window-divider-pos";
103 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
104 = "interface-temperature-unit";
106 /* Sensor logging settings */
107 static const char *KEY_SLOG_ENABLED = "slog-enabled";
108 static const char *KEY_SLOG_INTERVAL = "slog-interval";
110 /* Path to the script called when a notification is raised */
111 static const char *KEY_NOTIFICATION_SCRIPT = "notif-script";
113 static GSettings *settings;
115 static char *user_dir;
117 static GKeyFile *key_file;
119 static char *sensor_config_path;
121 static void (*slog_enabled_cbk)(void *);
123 static char *get_string(const char *key)
125 return g_settings_get_string(settings, key);
128 static void set_string(const char *key, const char *str)
130 g_settings_set_string(settings, key, str);
133 static void set_bool(const char *k, bool b)
135 g_settings_set_boolean(settings, k, b);
138 static bool get_bool(const char *k)
140 return g_settings_get_boolean(settings, k);
143 static void set_int(const char *k, int i)
145 g_settings_set_int(settings, k, i);
148 static double get_double(const char *k)
150 return g_settings_get_double(settings, k);
153 static void set_double(const char *k, double d)
155 g_settings_set_double(settings, k, d);
158 static int get_int(const char *k)
160 return g_settings_get_int(settings, k);
163 char *config_get_notif_script()
167 str = get_string(KEY_NOTIFICATION_SCRIPT);
168 if (str && !strlen(str)) {
176 void config_set_notif_script(const char *str)
178 if (str && strlen(str) > 0)
179 set_string(KEY_NOTIFICATION_SCRIPT, str);
181 set_string(KEY_NOTIFICATION_SCRIPT, "");
184 static struct color *get_background_color()
189 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR);
191 c = str_to_color(scolor);
195 return color_new(0xffff, 0xffff, 0xffff);
200 static struct color *get_foreground_color()
205 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR);
207 c = str_to_color(scolor);
211 return color_new(0x0000, 0x0000, 0x0000);
216 static bool is_alpha_channel_enabled()
218 return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
221 static void set_alpha_channeld_enabled(bool b)
223 set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
226 static enum sensorlist_position get_sensorlist_position()
228 return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
231 static void set_sensorlist_position(enum sensorlist_position pos)
233 set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
236 static double get_graph_background_alpha()
238 return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
241 static void set_graph_background_alpha(double alpha)
243 set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
246 static void set_background_color(const struct color *color)
250 scolor = color_to_str(color);
252 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
254 set_string(KEY_GRAPH_BACKGROUND_COLOR, scolor);
259 static void set_foreground_color(const struct color *color)
263 str = color_to_str(color);
265 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
267 set_string(KEY_GRAPH_FOREGROUND_COLOR, str);
272 bool is_slog_enabled()
274 return get_bool(KEY_SLOG_ENABLED);
277 static void set_slog_enabled(bool enabled)
279 set_bool(KEY_SLOG_ENABLED, enabled);
282 static void slog_enabled_changed_cbk(GSettings *settings,
286 if (slog_enabled_cbk)
287 slog_enabled_cbk(data);
290 void config_set_slog_enabled_changed_cbk(void (*cbk)(void *), void *data)
294 slog_enabled_cbk = cbk;
296 g_signal_connect_after(settings,
297 "changed::slog-enabled",
298 G_CALLBACK(slog_enabled_changed_cbk),
304 int config_get_slog_interval()
306 return get_int(KEY_SLOG_INTERVAL);
309 static void set_slog_interval(int interval)
314 set_int(KEY_SLOG_INTERVAL, interval);
317 static bool is_window_decoration_enabled()
319 return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
322 static bool is_window_keep_below_enabled()
324 return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
327 static void set_window_decoration_enabled(bool enabled)
329 set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
332 static void set_window_keep_below_enabled(bool enabled)
334 set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
340 settings = g_settings_new("psensor");
343 void config_cleanup()
349 g_object_unref(settings);
359 g_key_file_free(key_file);
363 if (sensor_config_path) {
364 free(sensor_config_path);
365 sensor_config_path = NULL;
368 slog_enabled_cbk = NULL;
371 struct config *config_load()
377 c = malloc(sizeof(struct config));
379 c->graph_bgcolor = get_background_color();
380 c->graph_fgcolor = get_foreground_color();
381 c->graph_bg_alpha = get_graph_background_alpha();
382 c->alpha_channel_enabled = is_alpha_channel_enabled();
383 c->sensorlist_position = get_sensorlist_position();
384 c->window_decoration_enabled = is_window_decoration_enabled();
385 c->window_keep_below_enabled = is_window_keep_below_enabled();
386 c->slog_enabled = is_slog_enabled();
387 c->slog_interval = config_get_slog_interval();
389 c->sensor_update_interval
390 = get_int(KEY_SENSOR_UPDATE_INTERVAL);
391 if (c->sensor_update_interval < 1)
392 c->sensor_update_interval = 1;
394 c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
395 if (c->graph_update_interval < 1)
396 c->graph_update_interval = 1;
398 c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
400 if (c->graph_monitoring_duration < 1)
401 c->graph_monitoring_duration = 10;
403 c->sensor_values_max_length
404 = (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
406 if (c->sensor_values_max_length < 3)
407 c->sensor_values_max_length = 3;
409 c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
411 c->unity_launcher_count_disabled
412 = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
414 c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
416 c->window_restore_enabled
417 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
419 c->window_x = get_int(KEY_INTERFACE_WINDOW_X);
420 c->window_y = get_int(KEY_INTERFACE_WINDOW_Y);
421 c->window_w = get_int(KEY_INTERFACE_WINDOW_W);
422 c->window_h = get_int(KEY_INTERFACE_WINDOW_H);
424 c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
426 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
431 c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
436 void config_save(const struct config *c)
438 set_alpha_channeld_enabled(c->alpha_channel_enabled);
439 set_background_color(c->graph_bgcolor);
440 set_foreground_color(c->graph_fgcolor);
441 set_graph_background_alpha(c->graph_bg_alpha);
442 set_sensorlist_position(c->sensorlist_position);
443 set_window_decoration_enabled(c->window_decoration_enabled);
444 set_window_keep_below_enabled(c->window_keep_below_enabled);
445 set_slog_enabled(c->slog_enabled);
446 set_slog_interval(c->slog_interval);
448 set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
450 set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
452 set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval);
454 set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled);
456 set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
457 c->unity_launcher_count_disabled);
459 set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
461 set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
462 c->window_restore_enabled);
464 set_int(KEY_INTERFACE_WINDOW_X, c->window_x);
465 set_int(KEY_INTERFACE_WINDOW_Y, c->window_y);
466 set_int(KEY_INTERFACE_WINDOW_W, c->window_w);
467 set_int(KEY_INTERFACE_WINDOW_H, c->window_h);
469 set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
471 set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit);
474 const char *get_psensor_user_dir()
481 home = getenv("HOME");
486 user_dir = path_append(home, ".psensor");
488 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
489 log_err(_("Failed to create the directory %s: %s"),
503 static const char *get_sensor_config_path()
507 if (!sensor_config_path) {
508 dir = get_psensor_user_dir();
511 sensor_config_path = path_append(dir, "psensor.cfg");
514 return sensor_config_path;
517 static GKeyFile *get_sensor_key_file()
524 path = get_sensor_config_path();
526 key_file = g_key_file_new();
529 ret = g_key_file_load_from_file(key_file,
531 G_KEY_FILE_KEEP_COMMENTS
532 | G_KEY_FILE_KEEP_TRANSLATIONS,
536 if (err->code == G_KEY_FILE_ERROR_NOT_FOUND)
537 log_fct(_("The configuration file "
540 log_err(_("Failed to parse configuration "
549 static void save_sensor_key_file()
557 kfile = get_sensor_key_file();
559 data = g_key_file_to_data(kfile, NULL, NULL);
561 path = get_sensor_config_path();
563 if (!g_file_set_contents(path, data, -1, NULL))
564 log_err(_("Failed to save configuration file %s."), path);
576 save_sensor_key_file();
580 static void sensor_set_str(const char *sid, const char *att, const char *str)
584 kfile = get_sensor_key_file();
585 g_key_file_set_string(kfile, sid, att, str);
588 static char *sensor_get_str(const char *sid, const char *att)
592 kfile = get_sensor_key_file();
593 return g_key_file_get_string(kfile, sid, att, NULL);
596 static bool sensor_get_bool(const char *sid, const char *att)
600 kfile = get_sensor_key_file();
601 return g_key_file_get_boolean(kfile, sid, att, NULL);
604 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
608 kfile = get_sensor_key_file();
610 g_key_file_set_boolean(kfile, sid, att, enabled);
613 static int sensor_get_int(const char *sid, const char *att)
617 kfile = get_sensor_key_file();
618 return g_key_file_get_integer(kfile, sid, att, NULL);
621 static void sensor_set_int(const char *sid, const char *att, int i)
625 kfile = get_sensor_key_file();
627 g_key_file_set_integer(kfile, sid, att, i);
630 char *config_get_sensor_name(const char *sid)
632 return sensor_get_str(sid, ATT_SENSOR_NAME);
635 void config_set_sensor_name(const char *sid, const char *name)
637 sensor_set_str(sid, ATT_SENSOR_NAME, name);
640 void config_set_sensor_color(const char *sid, const struct color *color)
644 scolor = color_to_str(color);
646 sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
652 config_get_sensor_color(const char *sid, const struct color *dft)
657 scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
660 color = str_to_color(scolor);
665 color = color_new(dft->red, dft->green, dft->blue);
666 config_set_sensor_color(sid, color);
674 bool config_is_sensor_enabled(const char *sid)
676 return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
679 void config_set_sensor_enabled(const char *sid, bool enabled)
681 sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
684 int config_get_sensor_alarm_high_threshold(const char *sid)
686 return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
689 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
691 sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
694 int config_get_sensor_alarm_low_threshold(const char *sid)
696 return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
699 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
701 sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
704 bool config_is_appindicator_enabled(const char *sid)
706 return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
709 void config_set_appindicator_enabled(const char *sid, bool enabled)
711 return sensor_set_bool(sid,
712 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
716 int config_get_sensor_position(const char *sid)
718 return sensor_get_int(sid, ATT_SENSOR_POSITION);
721 void config_set_sensor_position(const char *sid, int pos)
723 return sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
726 bool config_get_sensor_alarm_enabled(const char *sid)
728 return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
731 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
733 sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
736 bool config_is_appindicator_label_enabled(const char *sid)
738 return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
741 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
743 sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);