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