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