added support of "string" properties.
[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 = "/apps/psensor/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 = "/apps/psensor/slog/enabled";
108 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/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 GConfClient *client;
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 char *get_string(const char *key)
123 {
124         return g_settings_get_string(settings, key);
125 }
126
127 static void set_string(const char *key, const char *str)
128 {
129         g_settings_set_string(settings, key, str);
130 }
131
132 static void set_bool(const char *k, bool b)
133 {
134         g_settings_set_boolean(settings, k, b);
135 }
136
137 static bool get_bool(const char *k)
138 {
139         return g_settings_get_boolean(settings, k);
140 }
141
142 static void set_int(const char *k, int i)
143 {
144         g_settings_set_int(settings, k, i);
145 }
146
147 static double get_double(const char *k)
148 {
149         return g_settings_get_double(settings, k);
150 }
151
152 static void set_double(const char *k, double d)
153 {
154         g_settings_set_double(settings, k, d);
155 }
156
157 static int get_int(const char *k)
158 {
159         return g_settings_get_int(settings, k);
160 }
161
162 char *config_get_notif_script()
163 {
164         char *str;
165
166         str =  get_string(KEY_NOTIFICATION_SCRIPT);
167         if (str && !strlen(str)) {
168                 free(str);
169                 str = NULL;
170         }
171
172         return str;
173 }
174
175 void config_set_notif_script(const char *str)
176 {
177         if (str && strlen(str) > 0)
178                 set_string(KEY_NOTIFICATION_SCRIPT, str);
179         else
180                 set_string(KEY_NOTIFICATION_SCRIPT, "");
181 }
182
183 static struct color *get_background_color()
184 {
185         char *scolor;
186         struct color *c;
187
188         scolor = get_string(KEY_GRAPH_BACKGROUND_COLOR);
189
190         c = str_to_color(scolor);
191         free(scolor);
192
193         if (!c)
194                 return color_new(0xffff, 0xffff, 0xffff);
195
196         return c;
197 }
198
199 static struct color *get_foreground_color()
200 {
201         char *scolor;
202         struct color *c;
203
204         scolor = get_string(KEY_GRAPH_FOREGROUND_COLOR);
205
206         c = str_to_color(scolor);
207         free(scolor);
208
209         if (!c)
210                 return color_new(0x0000, 0x0000, 0x0000);
211
212         return c;
213 }
214
215 static bool is_alpha_channel_enabled()
216 {
217         return get_bool(KEY_ALPHA_CHANNEL_ENABLED);
218 }
219
220 static void set_alpha_channeld_enabled(bool b)
221 {
222         set_bool(KEY_ALPHA_CHANNEL_ENABLED, b);
223 }
224
225 static enum sensorlist_position get_sensorlist_position()
226 {
227         return get_int(KEY_INTERFACE_SENSORLIST_POSITION);
228 }
229
230 static void set_sensorlist_position(enum sensorlist_position pos)
231 {
232         set_int(KEY_INTERFACE_SENSORLIST_POSITION, pos);
233 }
234
235 static double get_graph_background_alpha()
236 {
237         return get_double(KEY_GRAPH_BACKGROUND_ALPHA);
238 }
239
240 static void set_graph_background_alpha(double alpha)
241 {
242         set_double(KEY_GRAPH_BACKGROUND_ALPHA, alpha);
243 }
244
245 static void set_background_color(const struct color *color)
246 {
247         char *scolor;
248
249         scolor = color_to_str(color);
250         if (!scolor)
251                 scolor = strdup(DEFAULT_GRAPH_BACKGROUND_COLOR);
252
253         gconf_client_set_string(client,
254                                 KEY_GRAPH_BACKGROUND_COLOR, scolor, NULL);
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         gconf_client_set_string(client, KEY_GRAPH_FOREGROUND_COLOR, str, NULL);
268
269         free(str);
270 }
271
272 bool is_slog_enabled()
273 {
274         return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
275 }
276
277 static void set_slog_enabled(bool enabled)
278 {
279         gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
280 }
281
282 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
283 {
284         log_debug("config_slog_enabled_notify_add");
285         gconf_client_add_dir(client,
286                              KEY_SLOG_ENABLED,
287                              GCONF_CLIENT_PRELOAD_NONE,
288                              NULL);
289         gconf_client_notify_add(client,
290                                 KEY_SLOG_ENABLED,
291                                 cbk,
292                                 data,
293                                 NULL,
294                                 NULL);
295 }
296
297 int config_get_slog_interval()
298 {
299         int res;
300
301         res = gconf_client_get_int(client, KEY_SLOG_INTERVAL, NULL);
302
303         if (res <= 0)
304                 return 300;
305         else
306                 return res;
307 }
308
309 static void set_slog_interval(int interval)
310 {
311         if (interval <= 0)
312                 interval = 300;
313
314         gconf_client_set_int(client, KEY_SLOG_INTERVAL, interval, NULL);
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 /*
338  * Initializes the GConf client.
339  */
340 static void init()
341 {
342         if (!client)
343                 client = gconf_client_get_default();
344
345         if (!settings)
346                 settings = g_settings_new("psensor");
347 }
348
349 void config_cleanup()
350 {
351         config_sync();
352
353         if (client) {
354                 g_object_unref(client);
355                 client = NULL;
356         }
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
380 struct config *config_load()
381 {
382         struct config *c;
383
384         init();
385
386         c = malloc(sizeof(struct config));
387
388         c->graph_bgcolor = get_background_color();
389         c->graph_fgcolor = get_foreground_color();
390         c->graph_bg_alpha = get_graph_background_alpha();
391         c->alpha_channel_enabled = is_alpha_channel_enabled();
392         c->sensorlist_position = get_sensorlist_position();
393         c->window_decoration_enabled = is_window_decoration_enabled();
394         c->window_keep_below_enabled = is_window_keep_below_enabled();
395         c->slog_enabled = is_slog_enabled();
396         c->slog_interval = config_get_slog_interval();
397
398         c->sensor_update_interval
399             = gconf_client_get_int(client, KEY_SENSOR_UPDATE_INTERVAL, NULL);
400         if (c->sensor_update_interval < 1)
401                 c->sensor_update_interval = 1;
402
403         c->graph_update_interval = get_int(KEY_GRAPH_UPDATE_INTERVAL);
404         if (c->graph_update_interval < 1)
405                 c->graph_update_interval = 1;
406
407         c->graph_monitoring_duration = get_int(KEY_GRAPH_MONITORING_DURATION);
408
409         if (c->graph_monitoring_duration < 1)
410                 c->graph_monitoring_duration = 10;
411
412         c->sensor_values_max_length
413             = (c->graph_monitoring_duration * 60) / c->sensor_update_interval;
414
415         if (c->sensor_values_max_length < 3)
416                 c->sensor_values_max_length = 3;
417
418         c->menu_bar_disabled = get_bool(KEY_INTERFACE_MENU_BAR_DISABLED);
419
420         c->unity_launcher_count_disabled
421                 = get_bool(KEY_INTERFACE_UNITY_LAUNCHER_COUNT_DISABLED);
422
423         c->hide_on_startup = get_bool(KEY_INTERFACE_HIDE_ON_STARTUP);
424
425         c->window_restore_enabled
426                 = get_bool(KEY_INTERFACE_WINDOW_RESTORE_ENABLED);
427
428         c->window_x = get_int(KEY_INTERFACE_WINDOW_X);
429         c->window_y = get_int(KEY_INTERFACE_WINDOW_Y);
430         c->window_w = get_int(KEY_INTERFACE_WINDOW_W);
431         c->window_h = get_int(KEY_INTERFACE_WINDOW_H);
432
433         c->window_divider_pos = get_int(KEY_INTERFACE_WINDOW_DIVIDER_POS);
434
435         if (!c->window_restore_enabled || !c->window_w || !c->window_h) {
436                 c->window_w = 800;
437                 c->window_h = 200;
438         }
439
440         c->temperature_unit = get_int(KEY_INTERFACE_TEMPERATURE_UNIT);
441
442         return c;
443 }
444
445 void config_save(const struct config *c)
446 {
447         set_alpha_channeld_enabled(c->alpha_channel_enabled);
448         set_background_color(c->graph_bgcolor);
449         set_foreground_color(c->graph_fgcolor);
450         set_graph_background_alpha(c->graph_bg_alpha);
451         set_sensorlist_position(c->sensorlist_position);
452         set_window_decoration_enabled(c->window_decoration_enabled);
453         set_window_keep_below_enabled(c->window_keep_below_enabled);
454         set_slog_enabled(c->slog_enabled);
455         set_slog_interval(c->slog_interval);
456
457         set_int(KEY_GRAPH_UPDATE_INTERVAL, c->graph_update_interval);
458
459         set_int(KEY_GRAPH_MONITORING_DURATION, c->graph_monitoring_duration);
460
461         gconf_client_set_int(client,
462                              KEY_SENSOR_UPDATE_INTERVAL,
463                              c->sensor_update_interval, NULL);
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                         if (err->code == G_KEY_FILE_ERROR_NOT_FOUND) {
548                                 log_fct(_("The configuration file "
549                                           "does not exist."));
550                         } else {
551                                 log_err(_("Failed to parse configuration "
552                                           "file: %s"),
553                                         path);
554                         }
555                 }
556         }
557
558         return key_file;
559 }
560
561 static void save_sensor_key_file()
562 {
563         GKeyFile *kfile;
564         const char *path;
565         char *data;
566
567         log_fct_enter();
568
569         kfile = get_sensor_key_file();
570
571         data = g_key_file_to_data(kfile, NULL, NULL);
572
573         path = get_sensor_config_path();
574
575         if (!g_file_set_contents(path, data, -1, NULL))
576                 log_err(_("Failed to save configuration file %s."), path);
577
578         free(data);
579
580         log_fct_exit();
581 }
582
583 void config_sync()
584 {
585         log_fct_enter();
586         if (settings)
587                 g_settings_sync();
588         save_sensor_key_file();
589         log_fct_exit();
590 }
591
592 static void sensor_set_str(const char *sid, const char *att, const char *str)
593 {
594         GKeyFile *kfile;
595
596         kfile = get_sensor_key_file();
597         g_key_file_set_string(kfile, sid, att, str);
598 }
599
600 static char *sensor_get_str(const char *sid, const char *att)
601 {
602         GKeyFile *kfile;
603
604         kfile = get_sensor_key_file();
605         return g_key_file_get_string(kfile, sid, att, NULL);
606 }
607
608 static bool sensor_get_bool(const char *sid, const char *att)
609 {
610         GKeyFile *kfile;
611
612         kfile = get_sensor_key_file();
613         return g_key_file_get_boolean(kfile, sid, att, NULL);
614 }
615
616 static void sensor_set_bool(const char *sid, const char *att, bool enabled)
617 {
618         GKeyFile *kfile;
619
620         kfile = get_sensor_key_file();
621
622         g_key_file_set_boolean(kfile, sid, att, enabled);
623 }
624
625 static int sensor_get_int(const char *sid, const char *att)
626 {
627         GKeyFile *kfile;
628
629         kfile = get_sensor_key_file();
630         return g_key_file_get_integer(kfile, sid, att, NULL);
631 }
632
633 static void sensor_set_int(const char *sid, const char *att, int i)
634 {
635         GKeyFile *kfile;
636
637         kfile = get_sensor_key_file();
638
639         g_key_file_set_integer(kfile, sid, att, i);
640 }
641
642 char *config_get_sensor_name(const char *sid)
643 {
644         return sensor_get_str(sid, ATT_SENSOR_NAME);
645 }
646
647 void config_set_sensor_name(const char *sid, const char *name)
648 {
649         sensor_set_str(sid, ATT_SENSOR_NAME, name);
650 }
651
652 void config_set_sensor_color(const char *sid, const struct color *color)
653 {
654         char *scolor;
655
656         scolor = color_to_str(color);
657
658         sensor_set_str(sid, ATT_SENSOR_COLOR, scolor);
659
660         free(scolor);
661 }
662
663 struct color *
664 config_get_sensor_color(const char *sid, const struct color *dft)
665 {
666         char *scolor;
667         struct color *color;
668
669         scolor = sensor_get_str(sid, ATT_SENSOR_COLOR);
670
671         if (scolor)
672                 color = str_to_color(scolor);
673         else
674                 color = NULL;
675
676         if (!color) {
677                 color = color_new(dft->red, dft->green, dft->blue);
678                 config_set_sensor_color(sid, color);
679         }
680
681         free(scolor);
682
683         return color;
684 }
685
686 bool config_is_sensor_enabled(const char *sid)
687 {
688         return sensor_get_bool(sid, ATT_SENSOR_GRAPH_ENABLED);
689 }
690
691 void config_set_sensor_enabled(const char *sid, bool enabled)
692 {
693         sensor_set_bool(sid, ATT_SENSOR_GRAPH_ENABLED, enabled);
694 }
695
696 int config_get_sensor_alarm_high_threshold(const char *sid)
697 {
698         return sensor_get_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD);
699 }
700
701 void config_set_sensor_alarm_high_threshold(const char *sid, int threshold)
702 {
703         sensor_set_int(sid, ATT_SENSOR_ALARM_HIGH_THRESHOLD, threshold);
704 }
705
706 int config_get_sensor_alarm_low_threshold(const char *sid)
707 {
708         return sensor_get_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD);
709 }
710
711 void config_set_sensor_alarm_low_threshold(const char *sid, int threshold)
712 {
713         sensor_set_int(sid, ATT_SENSOR_ALARM_LOW_THRESHOLD, threshold);
714 }
715
716 bool config_is_appindicator_enabled(const char *sid)
717 {
718         return !sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_MENU_DISABLED);
719 }
720
721 void config_set_appindicator_enabled(const char *sid, bool enabled)
722 {
723         return sensor_set_bool(sid,
724                                ATT_SENSOR_APPINDICATOR_MENU_DISABLED,
725                                !enabled);
726 }
727
728 int config_get_sensor_position(const char *sid)
729 {
730         return sensor_get_int(sid, ATT_SENSOR_POSITION);
731 }
732
733 void config_set_sensor_position(const char *sid, int pos)
734 {
735         return sensor_set_int(sid, ATT_SENSOR_POSITION, pos);
736 }
737
738 bool config_get_sensor_alarm_enabled(const char *sid)
739 {
740         return sensor_get_bool(sid, ATT_SENSOR_ALARM_ENABLED);
741 }
742
743 void config_set_sensor_alarm_enabled(const char *sid, bool enabled)
744 {
745         sensor_set_bool(sid, ATT_SENSOR_ALARM_ENABLED, enabled);
746 }
747
748 bool config_is_appindicator_label_enabled(const char *sid)
749 {
750         return sensor_get_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
751 }
752
753 void config_set_appindicator_label_enabled(const char *sid, bool enabled)
754 {
755         sensor_set_bool(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED, enabled);
756 }