added support of smooth curves setting
[psensor.git] / src / cfg.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include <locale.h>
29 #include <libintl.h>
30 #define _(str) gettext(str)
31
32 #include <cfg.h>
33 #include <pio.h>
34 #include <plog.h>
35
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";
49
50 /* Update interval of the measures of the sensors */
51 static const char *KEY_SENSOR_UPDATE_INTERVAL
52 = "sensor-update-interval";
53
54 /* Graph settings */
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";
64
65 static const char *DEFAULT_GRAPH_FOREGROUND_COLOR = "#000000000000";
66
67 static const char *KEY_ALPHA_CHANNEL_ENABLED = "graph-alpha-channel-enabled";
68
69 /* Inteface settings */
70 static const char *KEY_INTERFACE_SENSORLIST_POSITION
71 = "interface-sensorlist-position";
72
73 static const char *KEY_INTERFACE_WINDOW_DECORATION_DISABLED
74 = "interface-window-decoration-disabled";
75
76 static const char *KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED
77 = "interface-window-keep-below-enabled";
78
79 static const char *KEY_INTERFACE_MENU_BAR_DISABLED
80 = "interface-menu-bar-disabled";
81
82 static const char *KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED
83 = "interface-unity-launcher-count-disabled";
84
85 static const char *KEY_INTERFACE_HIDE_ON_STARTUP
86 = "interface-hide-on-startup";
87
88 static const char *KEY_INTERFACE_WINDOW_RESTORE_ENABLED
89 = "interface-window-restore-enabled";
90
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";
95
96 static const char *KEY_INTERFACE_WINDOW_DIVIDER_POS
97 = "interface-window-divider-pos";
98
99 static const char *KEY_INTERFACE_TEMPERATURE_UNIT
100 = "interface-temperature-unit";
101
102 /* Sensor logging settings */
103 static const char *KEY_SLOG_ENABLED = "slog-enabled";
104 static const char *KEY_SLOG_INTERVAL = "slog-interval";
105
106 /* Path to the script called when a notification is raised */
107 static const char *KEY_NOTIFICATION_SCRIPT = "notif-script";
108
109 static GSettings *settings;
110
111 static char *user_dir;
112
113 static GKeyFile *key_file;
114
115 static char *sensor_config_path;
116
117 static void (*slog_enabled_cbk)(void *);
118
119 static char *get_string(const char *key)
120 {
121         return g_settings_get_string(settings, key);
122 }
123
124 static void set_string(const char *key, const char *str)
125 {
126         g_settings_set_string(settings, key, str);
127 }
128
129 static void set_bool(const char *k, bool b)
130 {
131         g_settings_set_boolean(settings, k, b);
132 }
133
134 static bool get_bool(const char *k)
135 {
136         return g_settings_get_boolean(settings, k);
137 }
138
139 static void set_int(const char *k, int i)
140 {
141         g_settings_set_int(settings, k, i);
142 }
143
144 static double get_double(const char *k)
145 {
146         return g_settings_get_double(settings, k);
147 }
148
149 static void set_double(const char *k, double d)
150 {
151         g_settings_set_double(settings, k, d);
152 }
153
154 static int get_int(const char *k)
155 {
156         return g_settings_get_int(settings, k);
157 }
158
159 char *config_get_notif_script()
160 {
161         char *str;
162
163         str =  get_string(KEY_NOTIFICATION_SCRIPT);
164         if (str && !strlen(str)) {
165                 free(str);
166                 str = NULL;
167         }
168
169         return str;
170 }
171
172 void config_set_notif_script(const char *str)
173 {
174         if (str && strlen(str) > 0)
175                 set_string(KEY_NOTIFICATION_SCRIPT, str);
176         else
177                 set_string(KEY_NOTIFICATION_SCRIPT, "");
178 }
179
180 static struct color *get_background_color()
181 {
182         char *scolor;
183         struct color *c;
184
185         scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR);
186
187         c = str_to_color(scolor);
188         free(scolor);
189
190         if (!c)
191                 return color_new(1, 1, 1);
192
193         return c;
194 }
195
196 static struct color *get_foreground_color()
197 {
198         char *scolor;
199         struct color *c;
200
201         scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR);
202
203         c = str_to_color(scolor);
204         free(scolor);
205
206         if (!c)
207                 return color_new(0, 0, 0);
208
209         return c;
210 }
211
212 static bool is_alpha_channel_enabled()
213 {
214         return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
215 }
216
217 static void set_alpha_channeld_enabled(bool b)
218 {
219         set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
220 }
221
222 static enum sensorlist_position get_sensorlist_position()
223 {
224         return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
225 }
226
227 static void set_sensorlist_position(enum sensorlist_position pos)
228 {
229         set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
230 }
231
232 static double get_graph_background_alpha()
233 {
234         return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
235 }
236
237 static void set_graph_background_alpha(double alpha)
238 {
239         set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
240 }
241
242 static void set_background_color(const struct color *color)
243 {
244         char *scolor;
245
246         scolor = color_to_str(color);
247         if (!scolor)
248                 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
249
250         set_string(KEY_GRAPH_BACKGROUND_COLOR, scolor);
251
252         free(scolor);
253 }
254
255 static void set_foreground_color(const struct color *color)
256 {
257         char *str;
258
259         str = color_to_str(color);
260         if (!str)
261                 str = strdup(DEFAULT_GRAPH_FOREGROUND_COLOR);
262
263         set_string(KEY_GRAPH_FOREGROUND_COLOR, str);
264
265         free(str);
266 }
267
268 bool is_slog_enabled()
269 {
270         return get_bool(KEY_SLOG_ENABLED);
271 }
272
273 static void set_slog_enabled(bool enabled)
274 {
275         set_bool(KEY_SLOG_ENABLED, enabled);
276 }
277
278 static void slog_enabled_changed_cbk(GSettings *settings,
279                                      gchar *key,
280                                      gpointer data)
281 {
282         if (slog_enabled_cbk)
283                 slog_enabled_cbk(data);
284 }
285
286 void config_set_slog_enabled_changed_cbk(void (*cbk)(void *), void *data)
287 {
288         log_fct_enter();
289
290         slog_enabled_cbk = cbk;
291
292         g_signal_connect_after(settings,
293                                "changed::slog-enabled",
294                                G_CALLBACK(slog_enabled_changed_cbk),
295                                data);
296
297         log_fct_exit();
298 }
299
300 int config_get_slog_interval()
301 {
302         return get_int(KEY_SLOG_INTERVAL);
303 }
304
305 static void set_slog_interval(int interval)
306 {
307         if (interval <= 0)
308                 interval = 300;
309
310         set_int(KEY_SLOG_INTERVAL, interval);
311 }
312
313 static bool is_window_decoration_enabled()
314 {
315         return !get_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED);
316 }
317
318 static bool is_window_keep_below_enabled()
319 {
320         return get_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED);
321 }
322
323 static void set_window_decoration_enabled(bool enabled)
324 {
325         set_bool(KEY_INTERFACE_WINDOW_DECORATION_DISABLED, !enabled);
326 }
327
328 static void set_window_keep_below_enabled(bool enabled)
329 {
330         set_bool(KEY_INTERFACE_WINDOW_KEEP_BELOW_ENABLED, enabled);
331 }
332
333 bool config_is_smooth_curves_enabled()
334 {
335         return get_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED);
336 }
337
338 void config_set_smooth_curves_enabled(bool b)
339 {
340         set_bool(KEY_GRAPH_SMOOTH_CURVES_ENABLED, b);
341 }
342
343
344 static void init()
345 {
346         log_fct_enter();
347
348         if (!settings)
349                 settings = g_settings_new("psensor");
350
351         log_fct_exit();
352 }
353
354 void config_cleanup()
355 {
356         config_sync();
357
358         if (settings) {
359                 g_settings_sync();
360                 g_object_unref(settings);
361                 settings = NULL;
362         }
363
364         if (user_dir) {
365                 free(user_dir);
366                 user_dir = NULL;
367         }
368
369         if (key_file) {
370                 g_key_file_free(key_file);
371                 key_file = NULL;
372         }
373
374         if (sensor_config_path) {
375                 free(sensor_config_path);
376                 sensor_config_path = NULL;
377         }
378
379         slog_enabled_cbk = NULL;
380 }
381
382 struct config *config_load()
383 {
384         struct config *c;
385
386         init();
387
388         c = malloc(sizeof(struct config));
389
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();
399
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;
404
405         c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
406         if (c->graph_update_interval < 1)
407                 c->graph_update_interval = 1;
408
409         c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
410
411         if (c->graph_monitoring_duration < 1)
412                 c->graph_monitoring_duration = 10;
413
414         c->sensor_values_max_length
415             = (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
416
417         if (c->sensor_values_max_length < 3)
418                 c->sensor_values_max_length = 3;
419
420         c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
421
422         c->unity_launcher_count_disabled
423                 = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
424
425         c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
426
427         c->window_restore_enabled
428                 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
429
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);
434
435         c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
436
437         if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
438                 c->window_w = 800;
439                 c->window_h = 200;
440         }
441
442         c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
443
444         return c;
445 }
446
447 void config_save(const struct config *c)
448 {
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);
458
459         set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
460
461         set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
462
463         set_int(KEY_SENSOR_UPDATE_INTERVAL, c->sensor_update_interval);
464
465         set_bool(KEY_INTERFACE_MENU_BAR_DISABLED, c->menu_bar_disabled);
466
467         set_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED,
468                  c->unity_launcher_count_disabled);
469
470         set_bool(KEY_INTERFACE_HIDE_ON_STARTUP, c->hide_on_startup);
471
472         set_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED,
473                  c->window_restore_enabled);
474
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);
479
480         set_int(KEY_INTERFACE_WINDOW_DIVIDER_POS, c->window_divider_pos);
481
482         set_int(KEY_INTERFACE_TEMPERATURE_UNIT, c->temperature_unit);
483 }
484
485 const char *get_psensor_user_dir()
486 {
487         const char *home;
488
489         log_fct_enter();
490
491         if (!user_dir) {
492                 home = getenv("HOME");
493
494                 if (!home)
495                         return NULL;
496
497                 user_dir = path_append(home, ".psensor");
498
499                 if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
500                         log_err(_("Failed to create the directory %s: %s"),
501                                 user_dir,
502                                 strerror(errno));
503
504                         free(user_dir);
505                         user_dir = NULL;
506                 }
507         }
508
509         log_fct_exit();
510
511         return user_dir;
512 }
513
514 static const char *get_sensor_config_path()
515 {
516         const char *dir;
517
518         if (!sensor_config_path) {
519                 dir = get_psensor_user_dir();
520
521                 if (dir)
522                         sensor_config_path = path_append(dir, "psensor.cfg");
523         }
524
525         return sensor_config_path;
526 }
527
528 static GKeyFile *get_sensor_key_file()
529 {
530         int ret;
531         GError *err;
532         const char *path;
533
534         if (!key_file) {
535                 path = get_sensor_config_path();
536
537                 key_file = g_key_file_new();
538
539                 err = NULL;
540                 ret = g_key_file_load_from_file(key_file,
541                                                 path,
542                                                 G_KEY_FILE_KEEP_COMMENTS
543                                                 | G_KEY_FILE_KEEP_TRANSLATIONS,
544                                                 &err);
545
546                 if (!ret)
547                         log_warn(_("Failed to load configuration file %s: %s"),
548                                  path,
549                                  err->message);
550         }
551
552         return key_file;
553 }
554
555 static void save_sensor_key_file()
556 {
557         GKeyFile *kfile;
558         const char *path;
559         char *data;
560
561         log_fct_enter();
562
563         kfile = get_sensor_key_file();
564
565         data = g_key_file_to_data(kfile, NULL, NULL);
566
567         path = get_sensor_config_path();
568
569         if (!g_file_set_contents(path, data, -1, NULL))
570                 log_err(_("Failed to save configuration file %s."), path);
571
572         free(data);
573
574         log_fct_exit();
575 }
576
577 void config_sync()
578 {
579         log_fct_enter();
580         if (settings)
581                 g_settings_sync();
582         save_sensor_key_file();
583         log_fct_exit();
584 }
585
586 static void sensor_set_str(const char *sid, const char *att, const char *str)
587 {
588         GKeyFile *kfile;
589
590         kfile = get_sensor_key_file();
591         g_key_file_set_string(kfile, sid, att, str);
592 }
593
594 static char *sensor_get_str(const char *sid, const char *att)
595 {
596         GKeyFile *kfile;
597
598         kfile = get_sensor_key_file();
599         return g_key_file_get_string(kfile, sid, att, NULL);
600 }
601
602 static bool sensor_get_bool(const char *sid, const char *att)
603 {
604         GKeyFile *kfile;
605
606         kfile = get_sensor_key_file();
607         return g_key_file_get_boolean(kfile, sid, att, NULL);
608 }
609
610 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
611 {
612         GKeyFile *kfile;
613
614         kfile = get_sensor_key_file();
615
616         g_key_file_set_boolean(kfile, sid, att, enabled);
617 }
618
619 static int sensor_get_int(const char *sid, const char *att)
620 {
621         GKeyFile *kfile;
622
623         kfile = get_sensor_key_file();
624         return g_key_file_get_integer(kfile, sid, att, NULL);
625 }
626
627 static void sensor_set_int(const char *sid, const char *att, int i)
628 {
629         GKeyFile *kfile;
630
631         kfile = get_sensor_key_file();
632
633         g_key_file_set_integer(kfile, sid, att, i);
634 }
635
636 char *config_get_sensor_name(const char *sid)
637 {
638         return sensor_get_str(sid, ATT_SENSOR_NAME);
639 }
640
641 void config_set_sensor_name(const char *sid, const char *name)
642 {
643         sensor_set_str(sid, ATT_SENSOR_NAME, name);
644 }
645
646 void config_set_sensor_color(const char *sid, const struct color *color)
647 {
648         char *scolor;
649
650         scolor = color_to_str(color);
651
652         sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
653
654         free(scolor);
655 }
656
657 struct color *
658 config_get_sensor_color(const char *sid, const struct color *dft)
659 {
660         char *scolor;
661         struct color *color;
662
663         scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
664
665         if (scolor)
666                 color = str_to_color(scolor);
667         else
668                 color = NULL;
669
670         if (!color) {
671                 color = color_new(dft->red, dft->green, dft->blue);
672                 config_set_sensor_color(sid, color);
673         }
674
675         free(scolor);
676
677         return color;
678 }
679
680 bool config_is_sensor_graph_enabled(const char *sid)
681 {
682         return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
683 }
684
685 void config_set_sensor_graph_enabled(const char *sid, bool enabled)
686 {
687         sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
688 }
689
690 int config_get_sensor_alarm_high_threshold(const char *sid)
691 {
692         return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
693 }
694
695 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
696 {
697         sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
698 }
699
700 int config_get_sensor_alarm_low_threshold(const char *sid)
701 {
702         return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
703 }
704
705 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
706 {
707         sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
708 }
709
710 bool config_is_appindicator_enabled(const char *sid)
711 {
712         return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
713 }
714
715 void config_set_appindicator_enabled(const char *sid, bool enabled)
716 {
717         return sensor_set_bool(sid,
718                                ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
719                                !enabled);
720 }
721
722 int config_get_sensor_position(const char *sid)
723 {
724         return sensor_get_int(sid, ATT_SENSOR_POSITION);
725 }
726
727 void config_set_sensor_position(const char *sid, int pos)
728 {
729         return sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
730 }
731
732 bool config_get_sensor_alarm_enabled(const char *sid)
733 {
734         return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
735 }
736
737 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
738 {
739         sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
740 }
741
742 bool config_is_sensor_enabled(const char *sid)
743 {
744         return !sensor_get_bool(sid, ATT_SENSOR_HIDE);
745 }
746
747 void config_set_sensor_enabled(const char *sid, bool enabled)
748 {
749         sensor_set_bool(sid, ATT_SENSOR_HIDE, !enabled);
750 }
751
752 bool config_is_appindicator_label_enabled(const char *sid)
753 {
754         return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
755 }
756
757 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
758 {
759         sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);
760 }
761
762 GSettings *config_get_GSettings()
763 {
764         return settings;
765 }