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