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";
47 static const char *ATT_SENSOR_POSITION = "position";
49 static const char *KEY_SENSOR_UPDATE_INTERVAL
50 = "/apps/psensor/sensor/update_interval";
52 static const char *KEY_GRAPH_UPDATE_INTERVAL
53 = "/apps/psensor/graph/update_interval";
55 static const char *KEY_GRAPH_MONITORING_DURATION
56 = "/apps/psensor/graph/monitoring_duration";
58 static const char *KEY_GRAPH_BACKGROUND_COLOR
59 = "/apps/psensor/graph/background_color";
61 static const char *DEFAULT_GRAPH_BACKGROUND_COLOR = "#e8f4e8f4a8f5";
63 static const char *KEY_GRAPH_BACKGROUND_ALPHA
64 = "/apps/psensor/graph/background_alpha";
66 static const char *KEY_GRAPH_FOREGROUND_COLOR
67 = "/apps/psensor/graph/foreground_color";
68 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
70 static const char *KEY_ALPHA_CHANNEL_ENABLED
71 = "/apps/psensor/graph/alpha_channel_enabled";
73 static const char *KEY_INTERFACE_SENSORLIST_POSITION
74 = "/apps/psensor/interface/sensorlist_position";
76 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
77 = "/apps/psensor/interface/window_decoration_disabled";
79 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
80 = "/apps/psensor/interface/window_keep_below_enabled";
82 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
83 = "/apps/psensor/interface/menu_bar_disabled";
85 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
86 = "/apps/psensor/interface/unity_launcher_count_disabled";
88 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
89 = "/apps/psensor/interface/hide_on_startup";
91 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
92 = "/apps/psensor/interface/window_restore_enabled";
94 static const char *KEY_INTERFACE_WINDOW_X = "/apps/psensor/interface/window_x";
95 static const char *KEY_INTERFACE_WINDOW_Y = "/apps/psensor/interface/window_y";
96 static const char *KEY_INTERFACE_WINDOW_W = "/apps/psensor/interface/window_w";
97 static const char *KEY_INTERFACE_WINDOW_H = "/apps/psensor/interface/window_h";
99 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
100 = "/apps/psensor/interface/window_divider_pos";
102 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
103 = "/apps/psensor/interface/temperature_unit";
105 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
106 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
108 static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
110 static GConfClient *client;
112 static char *user_dir;
114 static GKeyFile *key_file;
116 static char *sensor_config_path;
118 static char *get_string(const char *key, const char *default_value)
122 value = gconf_client_get_string(client, key, NULL);
125 value = strdup(default_value);
126 gconf_client_set_string(client, key, default_value, NULL);
132 char *config_get_notif_script()
136 str = gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
137 if (str && !strlen(str)) {
145 void config_set_notif_script(const char *str)
147 if (str && strlen(str) > 0)
148 gconf_client_set_string(client,
149 KEY_NOTIFICATION_SCRIPT, str, NULL);
151 gconf_client_set_string(client,
152 KEY_NOTIFICATION_SCRIPT, "", NULL);
155 static struct color *get_background_color()
160 scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR,
161 DEFAULT_GRAPH_BACKGROUND_COLOR);
163 c = str_to_color(scolor);
167 return color_new(0xffff, 0xffff, 0xffff);
172 static struct color *get_foreground_color()
177 scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR,
178 DEFAULT_GRAPH_FOREGROUND_COLOR);
180 c = str_to_color(scolor);
184 return color_new(0x0000, 0x0000, 0x0000);
189 static bool is_alpha_channel_enabled()
191 return gconf_client_get_bool(client, KEY_ALPHA_CHANNEL_ENABLED, NULL);
194 static enum sensorlist_position get_sensorlist_position()
196 return gconf_client_get_int(client,
197 KEY_INTERFACE_SENSORLIST_POSITION, NULL);
200 static void set_sensorlist_position(enum sensorlist_position pos)
202 gconf_client_set_int(client,
203 KEY_INTERFACE_SENSORLIST_POSITION, pos, NULL);
206 static double get_graph_background_alpha()
210 a = gconf_client_get_float(client, KEY_GRAPH_BACKGROUND_ALPHA, NULL);
212 gconf_client_set_float(client,
213 KEY_GRAPH_BACKGROUND_ALPHA, 1.0, NULL);
217 static void set_graph_background_alpha(double alpha)
219 gconf_client_set_float(client, KEY_GRAPH_BACKGROUND_ALPHA, alpha, NULL);
222 static void set_background_color(const struct color *color)
226 scolor = color_to_str(color);
228 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
230 gconf_client_set_string(client,
231 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
236 static void set_foreground_color(const struct color *color)
240 str = color_to_str(color);
242 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
244 gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
249 bool is_slog_enabled()
251 return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
254 static void set_slog_enabled(bool enabled)
256 gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
259 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
261 log_debug("config_slog_enabled_notify_add");
262 gconf_client_add_dir(client,
264 GCONF_CLIENT_PRELOAD_NONE,
266 gconf_client_notify_add(client,
274 int config_get_slog_interval()
278 res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
286 static void set_slog_interval(int interval)
291 gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
294 static bool is_window_decoration_enabled()
296 return !gconf_client_get_bool(client,
297 KEY_INTERFACE_WINDOW_DECORATION_DISABLED,
301 static bool is_window_keep_below_enabled()
303 return gconf_client_get_bool(client,
304 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
308 static void set_window_decoration_enabled(bool enabled)
310 gconf_client_set_bool
312 KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled, NULL);
315 static void set_window_keep_below_enabled(bool enabled)
317 gconf_client_set_bool(client,
318 KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED,
323 * Initializes the GConf client.
328 client = gconf_client_get_default();
331 void config_cleanup()
334 g_object_unref(client);
346 g_key_file_free(key_file);
350 if (sensor_config_path) {
351 free(sensor_config_path);
352 sensor_config_path = NULL;
356 struct config *config_load()
362 c = malloc(sizeof(struct config));
364 c->graph_bgcolor = get_background_color();
365 c->graph_fgcolor = get_foreground_color();
366 c->graph_bg_alpha = get_graph_background_alpha();
367 c->alpha_channel_enabled = is_alpha_channel_enabled();
368 c->sensorlist_position = get_sensorlist_position();
369 c->window_decoration_enabled = is_window_decoration_enabled();
370 c->window_keep_below_enabled = is_window_keep_below_enabled();
371 c->slog_enabled = is_slog_enabled();
372 c->slog_interval = config_get_slog_interval();
374 c->sensor_update_interval
375 = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
376 if (c->sensor_update_interval < 1)
377 c->sensor_update_interval = 1;
379 c->graph_update_interval
380 = gconf_client_get_int(client, KEY_GRAPH_UPDATE_INTERVAL, NULL);
381 if (c->graph_update_interval < 1)
382 c->graph_update_interval = 1;
384 c->graph_monitoring_duration
385 = gconf_client_get_int(client, KEY_GRAPH_MONITORING_DURATION, NULL);
387 if (c->graph_monitoring_duration < 1)
388 c->graph_monitoring_duration = 10;
390 c->sensor_values_max_length
392 (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
393 if (c->sensor_values_max_length < 3)
394 c->sensor_values_max_length = 3;
397 = gconf_client_get_bool(client,
398 KEY_INTERFACE_MENU_BAR_DISABLED,
401 c->unity_launcher_count_disabled
402 = gconf_client_get_bool
404 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
408 = gconf_client_get_bool(client,
409 KEY_INTERFACE_HIDE_ON_STARTUP,
412 c->window_restore_enabled
413 = gconf_client_get_bool(client,
414 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
417 c->window_x = gconf_client_get_int(client,
418 KEY_INTERFACE_WINDOW_X,
420 c->window_y = gconf_client_get_int(client,
421 KEY_INTERFACE_WINDOW_Y,
423 c->window_w = gconf_client_get_int(client,
424 KEY_INTERFACE_WINDOW_W,
426 c->window_h = gconf_client_get_int(client,
427 KEY_INTERFACE_WINDOW_H,
429 c->window_divider_pos
430 = gconf_client_get_int(client,
431 KEY_INTERFACE_WINDOW_DIVIDER_POS,
434 if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
439 c->temperature_unit = gconf_client_get_int
440 (client, KEY_INTERFACE_TEMPERATURE_UNIT, NULL);
445 void config_save(const struct config *c)
447 set_background_color(c->graph_bgcolor);
448 set_foreground_color(c->graph_fgcolor);
449 set_graph_background_alpha(c->graph_bg_alpha);
450 set_sensorlist_position(c->sensorlist_position);
451 set_window_decoration_enabled(c->window_decoration_enabled);
452 set_window_keep_below_enabled(c->window_keep_below_enabled);
453 set_slog_enabled(c->slog_enabled);
454 set_slog_interval(c->slog_interval);
456 gconf_client_set_int(client,
457 KEY_GRAPH_UPDATE_INTERVAL,
458 c->graph_update_interval, NULL);
460 gconf_client_set_int(client,
461 KEY_GRAPH_MONITORING_DURATION,
462 c->graph_monitoring_duration, NULL);
464 gconf_client_set_int(client,
465 KEY_SENSOR_UPDATE_INTERVAL,
466 c->sensor_update_interval, NULL);
468 gconf_client_set_bool(client,
469 KEY_INTERFACE_MENU_BAR_DISABLED,
470 c->menu_bar_disabled, NULL);
472 gconf_client_set_bool(client,
473 KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
474 c->unity_launcher_count_disabled, NULL);
476 gconf_client_set_bool(client,
477 KEY_INTERFACE_HIDE_ON_STARTUP,
478 c->hide_on_startup, NULL);
480 gconf_client_set_bool(client,
481 KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
482 c->window_restore_enabled,
485 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_X, c->window_x, NULL);
486 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_Y, c->window_y, NULL);
487 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_W, c->window_w, NULL);
488 gconf_client_set_int(client, KEY_INTERFACE_WINDOW_H, c->window_h, NULL);
490 gconf_client_set_int(client,
491 KEY_INTERFACE_WINDOW_DIVIDER_POS,
492 c->window_divider_pos,
495 gconf_client_set_int(client,
496 KEY_INTERFACE_TEMPERATURE_UNIT,
501 const char *get_psensor_user_dir()
508 home = getenv("HOME");
513 user_dir = path_append(home, ".psensor");
515 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
516 log_err(_("Failed to create the directory %s: %s"),
530 static const char *get_sensor_config_path()
534 if (!sensor_config_path) {
535 dir = get_psensor_user_dir();
538 sensor_config_path = path_append(dir, "psensor.cfg");
541 return sensor_config_path;
544 static GKeyFile *get_sensor_key_file()
551 path = get_sensor_config_path();
553 key_file = g_key_file_new();
556 ret = g_key_file_load_from_file(key_file,
558 G_KEY_FILE_KEEP_COMMENTS
559 | G_KEY_FILE_KEEP_TRANSLATIONS,
563 if (err->code == G_KEY_FILE_ERROR_NOT_FOUND) {
564 log_fct(_("The configuration file "
567 log_err(_("Failed to parse configuration "
577 static void save_sensor_key_file()
585 kfile = get_sensor_key_file();
587 data = g_key_file_to_data(kfile, NULL, NULL);
589 path = get_sensor_config_path();
591 if (!g_file_set_contents(path, data, -1, NULL))
592 log_err(_("Failed to save configuration file %s."), path);
602 save_sensor_key_file();
607 config_sensor_set_string(const char *sid, const char *att, const char *str)
611 kfile = get_sensor_key_file();
612 g_key_file_set_string(kfile, sid, att, str);
615 static char *config_sensor_get_string(const char *sid, const char *att)
619 kfile = get_sensor_key_file();
620 return g_key_file_get_string(kfile, sid, att, NULL);
623 static bool config_sensor_get_bool(const char *sid, const char *att)
628 kfile = get_sensor_key_file();
629 return g_key_file_get_boolean(kfile, sid, att, NULL);
633 config_sensor_set_bool(const char *sid, const char *att, bool enabled)
637 kfile = get_sensor_key_file();
639 g_key_file_set_boolean(kfile, sid, att, enabled);
642 static int config_sensor_get_int(const char *sid, const char *att)
647 kfile = get_sensor_key_file();
648 return g_key_file_get_integer(kfile, sid, att, NULL);
652 config_sensor_set_int(const char *sid, const char *att, int i)
656 kfile = get_sensor_key_file();
658 g_key_file_set_integer(kfile, sid, att, i);
661 char *config_get_sensor_name(const char *sid)
663 return config_sensor_get_string(sid, ATT_SENSOR_NAME);
666 void config_set_sensor_name(const char *sid, const char *name)
668 config_sensor_set_string(sid, ATT_SENSOR_NAME, name);
671 void config_set_sensor_color(const char *sid, const struct color *color)
675 scolor = color_to_str(color);
677 config_sensor_set_string(sid, ATT_SENSOR_COLOR, scolor);
683 config_get_sensor_color(const char *sid, const struct color *dft)
688 scolor = config_sensor_get_string(sid, ATT_SENSOR_COLOR);
691 color = str_to_color(scolor);
696 color = color_new(dft->red, dft->green, dft->blue);
697 config_set_sensor_color(sid, color);
705 bool config_is_sensor_enabled(const char *sid)
707 return config_sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
710 void config_set_sensor_enabled(const char *sid, bool enabled)
712 config_sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
715 int config_get_sensor_alarm_high_threshold(const char *sid)
717 return config_sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
720 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
722 config_sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
725 int config_get_sensor_alarm_low_threshold(const char *sid)
727 return config_sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
730 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
732 config_sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
735 bool config_is_appindicator_enabled(const char *sid)
737 return !config_sensor_get_bool(sid,
738 ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
741 void config_set_appindicator_enabled(const char *sid, bool enabled)
743 return config_sensor_set_bool(sid,
744 ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
748 int config_get_sensor_position(const char *sid)
750 return config_sensor_get_int(sid, ATT_SENSOR_POSITION);
753 void config_set_sensor_position(const char *sid, int pos)
755 return config_sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
758 bool config_get_sensor_alarm_enabled(const char *sid)
760 return config_sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
763 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
765 config_sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
768 bool config_is_appindicator_label_enabled(const char *sid)
770 return config_sensor_get_bool(sid,
771 ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
774 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
776 config_sensor_set_bool(sid,
777 ATT_SENSOR_APPINDICATOR_LABEL_ENABLED,