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