1 /*
2   Gpredict: Real-time satellite tracking and orbit prediction program
3 
4   Copyright (C)  2001-2017  Alexandru Csete, OZ9AEC.
5 
6   Comments, questions and bugreports should be submitted via
7   http://sourceforge.net/projects/gpredict/
8   More details can be found at the project home page:
9 
10   http://gpredict.oz9aec.net/
11 
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2 of the License, or
15   (at your option) any later version.
16 
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21 
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, visit http://www.fsf.org/
24 */
25 /**
26  * @defgroup satcfg Read, manage and store gpredict configuration.
27  *
28  * The purpose with this module is to centralise the access to the gpredict.cfg
29  * configuration file and also to have a central place where the min, max and
30  * default values are defined.
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include <build-config.h>
35 #endif
36 #include <glib/gi18n.h>
37 #include <gtk/gtk.h>
38 
39 #include "compat.h"
40 #include "config-keys.h"
41 #include "gpredict-utils.h"
42 #include "gtk-polar-view.h"
43 #include "gtk-sat-module.h"
44 #include "gtk-sat-list.h"
45 #include "gtk-single-sat.h"
46 #include "sat-cfg.h"
47 #include "sat-log.h"
48 #include "sat-pass-dialogs.h"
49 
50 /* *INDENT-OFF* */
51 #define LIST_COLUMNS_DEFAULTS (SAT_LIST_FLAG_NAME |\
52     SAT_LIST_FLAG_AZ   |\
53     SAT_LIST_FLAG_EL   |\
54     SAT_LIST_FLAG_RANGE |\
55     SAT_LIST_FLAG_DIR |\
56     SAT_LIST_FLAG_NEXT_EVENT |\
57     SAT_LIST_FLAG_ALT |\
58     SAT_LIST_FLAG_ORBIT)
59 
60 #define SINGLE_PASS_COL_DEFAULTS (SINGLE_PASS_FLAG_TIME |\
61     SINGLE_PASS_FLAG_AZ |\
62     SINGLE_PASS_FLAG_EL |\
63     SINGLE_PASS_FLAG_RANGE |\
64     SINGLE_PASS_FLAG_FOOTPRINT)
65 
66 #define MULTI_PASS_COL_DEFAULTS (MULTI_PASS_FLAG_AOS_TIME |\
67     MULTI_PASS_FLAG_LOS_TIME |\
68     MULTI_PASS_FLAG_DURATION |\
69     MULTI_PASS_FLAG_AOS_AZ |\
70     MULTI_PASS_FLAG_MAX_EL |\
71     MULTI_PASS_FLAG_LOS_AZ)
72 
73 #define SINGLE_SAT_FIELD_DEF (SINGLE_SAT_FLAG_AZ |\
74     SINGLE_SAT_FLAG_EL |\
75     SINGLE_SAT_FLAG_RANGE |\
76     SINGLE_SAT_FLAG_RANGE_RATE |\
77     SINGLE_SAT_FLAG_NEXT_EVENT |\
78     SINGLE_SAT_FLAG_SSP |\
79     SINGLE_SAT_FLAG_FOOTPRINT |\
80     SINGLE_SAT_FLAG_ALT |\
81     SINGLE_SAT_FLAG_VEL |\
82     SINGLE_SAT_FLAG_DOPPLER |\
83     SINGLE_SAT_FLAG_LOSS |\
84     SINGLE_SAT_FLAG_DELAY |\
85     SINGLE_SAT_FLAG_MA |\
86     SINGLE_SAT_FLAG_PHASE |\
87     SINGLE_SAT_FLAG_ORBIT |\
88     SINGLE_SAT_FLAG_VISIBILITY)
89 /* *INDENT-ON* */
90 
91 /** Structure representing a boolean value */
92 typedef struct {
93     gchar          *group;      /*!< The configration group */
94     gchar          *key;        /*!< The configuration key */
95     gboolean        defval;     /*!< The default value */
96 } sat_cfg_bool_t;
97 
98 /** Structure representing an integer value */
99 typedef struct {
100     gchar          *group;      /*!< The configration group */
101     gchar          *key;        /*!< The configuration key */
102     gint            defval;     /*!< The default value */
103 } sat_cfg_int_t;
104 
105 /** Structure representing a string value */
106 typedef struct {
107     gchar          *group;      /*!< The configration group */
108     gchar          *key;        /*!< The configuration key */
109     gchar          *defval;     /*!< The default value */
110 } sat_cfg_str_t;
111 
112 /** Array containing the boolean configuration values */
113 sat_cfg_bool_t  sat_cfg_bool[SAT_CFG_BOOL_NUM] = {
114     {"GLOBAL", "USE_LOCAL_TIME", FALSE},
115     {"GLOBAL", "USE_NSEW", FALSE},
116     {"GLOBAL", "USE_IMPERIAL", FALSE},
117     {"GLOBAL", "MAIN_WIN_POS", FALSE},
118     {"GLOBAL", "MOD_WIN_POS", FALSE},
119     {"GLOBAL", "MOD_STATE", FALSE},
120     {"MODULES", "RULES_HINT", FALSE},
121     {"MODULES", "MAP_QTH_INFO", TRUE},
122     {"MODULES", "MAP_NEXT_EVENT", TRUE},
123     {"MODULES", "MAP_CURSOR_TRACK", FALSE},
124     {"MODULES", "MAP_SHOW_GRID", TRUE},
125     {"MODULES", "MAP_KEEP_RATIO", FALSE},
126     {"MODULES", "POLAR_QTH_INFO", TRUE},
127     {"MODULES", "POLAR_NEXT_EVENT", TRUE},
128     {"MODULES", "POLAR_CURSOR_TRACK", TRUE},
129     {"MODULES", "POLAR_EXTRA_AZ_TICKS", FALSE},
130     {"MODULES", "POLAR_SHOW_TRACK_AUTO", FALSE},
131     {"TLE", "SERVER_AUTH", FALSE},
132     {"TLE", "PROXY_AUTH", FALSE},
133     {"TLE", "ADD_NEW_SATS", TRUE},
134     {"LOG", "KEEP_LOG_FILES", FALSE},
135     {"PREDICT", "USE_REAL_T0", FALSE}
136 };
137 
138 /** Array containing the integer configuration parameters */
139 sat_cfg_int_t   sat_cfg_int[SAT_CFG_INT_NUM] = {
140     {"VERSION", "MAJOR", 1},
141     {"VERSION", "MINOR", 4},
142     {"MODULES", "DATA_TIMEOUT", 300},
143     {"MODULES", "LAYOUT", 2},   /* FIXME */
144     {"MODULES", "VIEW_1", GTK_SAT_MOD_VIEW_MAP},        /* FIXME */
145     {"MODULES", "VIEW_2", GTK_SAT_MOD_VIEW_POLAR},      /* FIXME */
146     {"MODULES", "VIEW_3", GTK_SAT_MOD_VIEW_SINGLE},     /* FIXME */
147     {"GLOBAL", "CURRENT_PAGE", -1},     /* FIXME */
148     {"GLOBAL", "WARP", 1},
149     {"MODULES", "LIST_REFRESH", 1},
150     {"MODULES", "LIST_COLUMNS", LIST_COLUMNS_DEFAULTS},
151     {"MODULES", "MAP_CENTER", 0},
152     {"MODULES", "MAP_REFRESH", 10},
153     {"MODULES", "MAP_INFO_COLOUR", 0x00FF00FF},
154     {"MODULES", "MAP_INFO_BGD_COLOUR", 0x000000FF},
155     {"MODULES", "MAP_QTH_COLOUR", 0x00FFFFFF},
156     {"MODULES", "MAP_SAT_COLOUR", 0xF0F000FF},
157     {"MODULES", "MAP_SELECTED_SAT_COLOUR", 0xFFFFFFFF},
158     {"MODULES", "MAP_COV_AREA_COLOUR", 0xFFFFFF1F},
159     {"MODULES", "MAP_GRID_COLOUR", 0x7F7F7FC8},
160     {"MODULES", "MAP_TERMINATOR_COLOUR", 0xFFFF0080},
161     {"MODULES", "MAP_EARTH_SHADOW_COLOUR", 0x00000060},
162     {"MODULES", "MAP_TICK_COLOUR", 0x7F7F7FC8},
163     {"MODULES", "MAP_TRACK_COLOUR", 0xFF1200BB},
164     {"MODULES", "MAP_TRACK_NUM", 3},
165     {"MODULES", "MAP_SHADOW_ALPHA", 0xDD},
166     {"MODULES", "POLAR_REFRESH", 3},
167     {"MODULES", "POLAR_CHART_ORIENT", POLAR_VIEW_NESW},
168     {"MODULES", "POLAR_BGD_COLOUR", 0xFFFFFFFF},
169     {"MODULES", "POLAR_AXIS_COLOUR", 0x0F0F0F7F},
170     {"MODULES", "POLAR_TICK_COLOUR", 0x007F00FF},
171     {"MODULES", "POLAR_SAT_COLOUR", 0x8F0000FF},
172     {"MODULES", "POLAR_SELECTED_SAT_COL", 0xFF0D0BFF},
173     {"MODULES", "POLAR_TRACK_COLOUR", 0x0000FFFF},
174     {"MODULES", "POLAR_INFO_COLOUR", 0x00007FFF},
175     {"MODULES", "SINGLE_SAT_REFRESH", 1},
176     {"MODULES", "SINGLE_SAT_FIELDS", SINGLE_SAT_FIELD_DEF},
177     {"MODULES", "SINGLE_SAT_SELECTED", 0},
178     {"MODULES", "EVENT_LIST_REFRESH", 1},
179     {"PREDICT", "MINIMUM_ELEV", 5},
180     {"PREDICT", "NUMBER_OF_PASSES", 10},
181     {"PREDICT", "LOOK_AHEAD", 3},
182     {"PREDICT", "TIME_RESOLUTION", 10},
183     {"PREDICT", "NUMBER_OF_ENTRIES", 20},
184     {"PREDICT", "SINGLE_PASS_COL", SINGLE_PASS_COL_DEFAULTS},
185     {"PREDICT", "MULTI_PASS_COL", MULTI_PASS_COL_DEFAULTS},
186     {"PREDICT", "SAVE_FORMAT", 0},
187     {"PREDICT", "SAVE_CONTENTS", 0},
188     {"PREDICT", "TWILIGHT_THRESHOLD", -6},
189     {"SKY_AT_GLANCE", "TIME_SPAN_HOURS", 8},
190     {"SKY_AT_GLANCE", "COLOUR_01", 0x3c46c8},
191     {"SKY_AT_GLANCE", "COLOUR_02", 0x00500a},
192     {"SKY_AT_GLANCE", "COLOUR_03", 0xd5472b},
193     {"SKY_AT_GLANCE", "COLOUR_04", 0xd06b38},
194     {"SKY_AT_GLANCE", "COLOUR_05", 0xcf477a},
195     {"SKY_AT_GLANCE", "COLOUR_06", 0xbf041f},
196     {"SKY_AT_GLANCE", "COLOUR_07", 0x688522},
197     {"SKY_AT_GLANCE", "COLOUR_08", 0x0420bf},
198     {"SKY_AT_GLANCE", "COLOUR_09", 0xa304bf},
199     {"SKY_AT_GLANCE", "COLOUR_10", 0x04bdbf},
200     {"GLOBAL", "WINDOW_POS_X", 0},
201     {"GLOBAL", "WINDOW_POS_Y", 0},
202     {"GLOBAL", "WINDOW_WIDTH", 700},
203     {"GLOBAL", "WINDOW_HEIGHT", 700},
204     {"GLOBAL", "HTML_BROWSER_TYPE", 0},
205     {"TLE", "AUTO_UPDATE_FREQ", 2},     /* weekly, see tle_auto_upd_freq_t */
206     {"TLE", "AUTO_UPDATE_ACTION", 1},   /* notify, see tle_auto_upd_action_t */
207     {"TLE", "LAST_UPDATE", 0},
208     {"LOG", "CLEAN_AGE", 0},    /* 0 = Never clean */
209     {"LOG", "LEVEL", 2}
210 };
211 
212 /** Array containing the string configuration values */
213 sat_cfg_str_t   sat_cfg_str[SAT_CFG_STR_NUM] = {
214     {"GLOBAL", "TIME_FORMAT", "%Y/%m/%d %H:%M:%S"},
215     {"GLOBAL", "DEFAULT_QTH", "sample.qth"},
216     {"GLOBAL", "OPEN_MODULES", "Amateur"},
217     {"GLOBAL", "HTML_BROWSER", NULL},
218     {"MODULES", "GRID", "1;0;2;0;1;2;0;1;1;2;3;1;2;1;2"},
219     {"MODULES", "MAP_FILE", "nasa-bmng-07_1024.jpg"},
220     {"MODULES", "MAP_FONT", "Sans 8"},
221     {"MODULES", "POLAR_FONT", "Sans 10"},
222     {"TRSP", "SERVER", "https://db.satnogs.org/api/"},
223     {"TRSP", "FREQ_FILE", "transmitters/?format=json"},
224     {"TRSP", "MODE_FILE", "modes/?format=json"},
225     {"TRSP", "PROXY", NULL},
226     {"TLE", "SERVER", "http://www.celestrak.com/NORAD/elements/"},
227     {"TLE", "FILES", "amateur.txt;cubesat.txt;visual.txt;weather.txt"},
228     {"TLE", "PROXY", NULL},
229     {"TLE", "URLS",
230      "http://www.amsat.org/amsat/ftp/keps/current/nasabare.txt;"
231      "http://www.celestrak.com/NORAD/elements/amateur.txt;"
232      "http://www.celestrak.com/NORAD/elements/cubesat.txt;"
233      "http://www.celestrak.com/NORAD/elements/galileo.txt;"
234      "http://www.celestrak.com/NORAD/elements/glo-ops.txt;"
235      "http://www.celestrak.com/NORAD/elements/gps-ops.txt;"
236      "http://www.celestrak.com/NORAD/elements/iridium.txt;"
237      "http://www.celestrak.com/NORAD/elements/iridium-NEXT.txt;"
238      "http://www.celestrak.com/NORAD/elements/molniya.txt;"
239      "http://www.celestrak.com/NORAD/elements/noaa.txt;"
240      "http://www.celestrak.com/NORAD/elements/science.txt;"
241      "http://www.celestrak.com/NORAD/elements/tle-new.txt;"
242      "http://www.celestrak.com/NORAD/elements/visual.txt;"
243      "http://www.celestrak.com/NORAD/elements/weather.txt"},
244     {"TLE", "FILE_DIR", NULL},
245     {"PREDICT", "SAVE_DIR", NULL}
246 };
247 
248 /* The configuration data buffer */
249 static GKeyFile *config = NULL;
250 
251 /**
252  * Load configuration data.
253  * @return 0 if everything OK, 1 otherwise.
254  *
255  * This function reads the configuration data from gpredict.cfg into
256  * memory. This function must be called very early at program start.
257  *
258  * The the configuration data in memory is already "loaded" the data will
259  * be ereased first.
260  */
sat_cfg_load()261 guint sat_cfg_load()
262 {
263     gchar          *keyfile, *confdir;
264     GError         *error = NULL;
265 
266     if (config != NULL)
267         sat_cfg_close();
268 
269     /* load the configuration file */
270     config = g_key_file_new();
271     confdir = get_user_conf_dir();
272     keyfile = g_strconcat(confdir, G_DIR_SEPARATOR_S, "gpredict.cfg", NULL);
273     g_free(confdir);
274     g_key_file_load_from_file(config, keyfile, G_KEY_FILE_KEEP_COMMENTS,
275                               &error);
276     g_free(keyfile);
277 
278     if (error != NULL)
279     {
280         sat_log_log(SAT_LOG_LEVEL_WARN,
281                     _("%s: Error reading config file (%s)"),
282                     __func__, error->message);
283         sat_log_log(SAT_LOG_LEVEL_WARN,
284                     _("%s: Using built-in defaults"), __func__);
285         g_clear_error(&error);
286         return 1;
287     }
288     else
289     {
290         sat_log_log(SAT_LOG_LEVEL_DEBUG, _("%s: Everything OK."), __func__);
291     }
292 
293     /* config version compatibility */
294     guint16         cfg_ver;
295 
296     cfg_ver = sat_cfg_get_int(SAT_CFG_INT_VERSION_MAJOR) << 8 |
297         sat_cfg_get_int(SAT_CFG_INT_VERSION_MINOR);
298 
299     /* if config version is < 1.1; reset SAT_CFG_STR_TLE_FILES */
300     if (cfg_ver < 0x0101)
301     {
302         sat_cfg_reset_str(SAT_CFG_STR_TLE_FILES);
303         sat_cfg_set_int(SAT_CFG_INT_VERSION_MAJOR, 1);
304         sat_cfg_set_int(SAT_CFG_INT_VERSION_MINOR, 4);
305     }
306     else if (cfg_ver < 0x0104)
307     {
308         /* Version 1.4 replaces TLE_SERVER and TLE_FILES with TLE_URLS,
309          * so import TLE_SERVER and FILES if they exist.
310          */
311         gchar          *urls = NULL;
312         gchar          *buff;
313         gchar          *tle_srv = sat_cfg_get_str(SAT_CFG_STR_TLE_SERVER);
314         gchar          *tle_fstr = sat_cfg_get_str(SAT_CFG_STR_TLE_FILES);
315         gchar         **tle_fvec = g_strsplit(tle_fstr, ";", 0);
316         unsigned int    i;
317 
318         for (i = 0; i < g_strv_length(tle_fvec); i++)
319         {
320             if (i > 0)
321             {
322                 buff = g_strdup_printf("%s;%s/%s", urls, tle_srv, tle_fvec[i]);
323                 g_free(urls);
324             }
325             else
326             {
327                 buff = g_strdup_printf("%s/%s", tle_srv, tle_fvec[i]);
328             }
329             urls = g_strdup(buff);
330             g_free(buff);
331         }
332         if (urls)
333             sat_cfg_set_str(SAT_CFG_STR_TLE_URLS, urls);
334         //sat_cfg_reset_str(SAT_CFG_STR_TLE_SERVER);
335         //sat_cfg_reset_str(SAT_CFG_STR_TLE_FILES);
336         g_free(urls);
337         g_free(tle_srv);
338         g_free(tle_fstr);
339         g_strfreev(tle_fvec);
340 
341         sat_cfg_set_int(SAT_CFG_INT_VERSION_MAJOR, 1);
342         sat_cfg_set_int(SAT_CFG_INT_VERSION_MINOR, 4);
343     }
344 
345     return 0;
346 }
347 
348 /**
349  * Save configuration data.
350  * @return 0 on success, 1 if an error occured.
351  *
352  * This function saves the configuration data currently stored in
353  * memory to the gpredict.cfg file.
354  */
sat_cfg_save()355 guint sat_cfg_save()
356 {
357     gchar          *keyfile;
358     gchar          *confdir;
359     guint           err = 0;
360 
361     confdir = get_user_conf_dir();
362     keyfile = g_strconcat(confdir, G_DIR_SEPARATOR_S, "gpredict.cfg", NULL);
363     err = gpredict_save_key_file(config, keyfile);
364     g_free(confdir);
365 
366     return err;
367 }
368 
369 /**
370  * Close configuration module.
371  *
372  * This function cleans up the memory allocated to the storage and
373  * management of configuration data. Note: configuration data will
374  * no be accessible after call to this function, unless sat_cfg_load
375  * is called again. This function should only be called when the
376  * program exits.
377  */
sat_cfg_close()378 void sat_cfg_close()
379 {
380     if (config != NULL)
381     {
382         g_key_file_free(config);
383         config = NULL;
384     }
385 }
386 
387 /** Get boolean value */
sat_cfg_get_bool(sat_cfg_bool_e param)388 gboolean sat_cfg_get_bool(sat_cfg_bool_e param)
389 {
390     gboolean        value = FALSE;
391     GError         *error = NULL;
392 
393     if (param < SAT_CFG_BOOL_NUM)
394     {
395         if (config == NULL)
396         {
397             sat_log_log(SAT_LOG_LEVEL_ERROR,
398                         _("%s: Module not initialised\n"), __func__);
399 
400             /* return default value */
401             value = sat_cfg_bool[param].defval;
402         }
403         else
404         {
405             /* fetch value */
406             value = g_key_file_get_boolean(config,
407                                            sat_cfg_bool[param].group,
408                                            sat_cfg_bool[param].key, &error);
409 
410             if (error != NULL)
411             {
412                 g_clear_error(&error);
413                 value = sat_cfg_bool[param].defval;
414             }
415         }
416 
417     }
418     else
419     {
420         sat_log_log(SAT_LOG_LEVEL_ERROR,
421                     _("%s: Unknown BOOL param index (%d)\n"), __func__, param);
422     }
423 
424     return value;
425 }
426 
427 /** Get default value of boolean parameter */
sat_cfg_get_bool_def(sat_cfg_bool_e param)428 gboolean sat_cfg_get_bool_def(sat_cfg_bool_e param)
429 {
430     gboolean        value = FALSE;
431 
432     if (param < SAT_CFG_BOOL_NUM)
433     {
434         value = sat_cfg_bool[param].defval;
435     }
436     else
437     {
438         sat_log_log(SAT_LOG_LEVEL_ERROR,
439                     _("%s: Unknown BOOL param index (%d)\n"), __func__, param);
440     }
441 
442     return value;
443 }
444 
445 /**
446  * Store a boolean configuration value.
447  * @param param The parameter to store.
448  * @param value The value of the parameter.
449  *
450  * This function stores a boolean configuration value in the configuration
451  * table.
452  */
sat_cfg_set_bool(sat_cfg_bool_e param,gboolean value)453 void sat_cfg_set_bool(sat_cfg_bool_e param, gboolean value)
454 {
455     if (param < SAT_CFG_BOOL_NUM)
456     {
457         if (config == NULL)
458         {
459             sat_log_log(SAT_LOG_LEVEL_ERROR,
460                         _("%s: Module not initialised\n"), __func__);
461         }
462         else
463         {
464             g_key_file_set_boolean(config,
465                                    sat_cfg_bool[param].group,
466                                    sat_cfg_bool[param].key, value);
467         }
468     }
469     else
470     {
471         sat_log_log(SAT_LOG_LEVEL_ERROR,
472                     _("%s: Unknown BOOL param index (%d)\n"), __func__, param);
473     }
474 }
475 
sat_cfg_reset_bool(sat_cfg_bool_e param)476 void sat_cfg_reset_bool(sat_cfg_bool_e param)
477 {
478     if (param < SAT_CFG_BOOL_NUM)
479     {
480         if (config == NULL)
481         {
482             sat_log_log(SAT_LOG_LEVEL_ERROR,
483                         _("%s: Module not initialised\n"), __func__);
484         }
485         else
486         {
487             g_key_file_remove_key(config,
488                                   sat_cfg_bool[param].group,
489                                   sat_cfg_bool[param].key, NULL);
490         }
491 
492     }
493     else
494     {
495         sat_log_log(SAT_LOG_LEVEL_ERROR,
496                     _("%s: Unknown BOOL param index (%d)\n"), __func__, param);
497     }
498 }
499 
500 /**
501  * Get string value
502  *
503  * Return a newly allocated gchar * which must be freed when no longer needed.
504  */
sat_cfg_get_str(sat_cfg_str_e param)505 gchar          *sat_cfg_get_str(sat_cfg_str_e param)
506 {
507     gchar          *value;
508     GError         *error = NULL;
509 
510     if (param < SAT_CFG_STR_NUM)
511     {
512         if (config == NULL)
513         {
514             sat_log_log(SAT_LOG_LEVEL_ERROR,
515                         _("%s: Module not initialised\n"), __func__);
516 
517             /* return default value */
518             value = g_strdup(sat_cfg_str[param].defval);
519         }
520         else
521         {
522             /* fetch value */
523             value = g_key_file_get_string(config,
524                                           sat_cfg_str[param].group,
525                                           sat_cfg_str[param].key, &error);
526             if (error != NULL)
527             {
528                 g_clear_error(&error);
529                 value = g_strdup(sat_cfg_str[param].defval);
530             }
531         }
532     }
533     else
534     {
535         sat_log_log(SAT_LOG_LEVEL_ERROR,
536                     _("%s: Unknown STR param index (%d)\n"), __func__, param);
537 
538         value = g_strdup("ERROR");
539     }
540 
541     return value;
542 }
543 
544 /**
545  * Get default value of string parameter
546  *
547  * Returns a newly allocated gchar * which must be freed when no longer needed.
548  */
sat_cfg_get_str_def(sat_cfg_str_e param)549 gchar          *sat_cfg_get_str_def(sat_cfg_str_e param)
550 {
551     gchar          *value;
552 
553     if (param < SAT_CFG_STR_NUM)
554     {
555         value = g_strdup(sat_cfg_str[param].defval);
556     }
557     else
558     {
559         sat_log_log(SAT_LOG_LEVEL_ERROR,
560                     _("%s: Unknown STR param index (%d)\n"), __func__, param);
561 
562         value = g_strdup("ERROR");
563     }
564 
565     return value;
566 }
567 
568 /** Store a str configuration value. */
sat_cfg_set_str(sat_cfg_str_e param,const gchar * value)569 void sat_cfg_set_str(sat_cfg_str_e param, const gchar * value)
570 {
571     if (param < SAT_CFG_STR_NUM)
572     {
573         if (config == NULL)
574         {
575             sat_log_log(SAT_LOG_LEVEL_ERROR,
576                         _("%s: Module not initialised\n"), __func__);
577         }
578         else
579         {
580             if (value)
581             {
582                 g_key_file_set_string(config,
583                                       sat_cfg_str[param].group,
584                                       sat_cfg_str[param].key, value);
585             }
586             else
587             {
588                 /* remove key from config */
589                 g_key_file_remove_key(config,
590                                       sat_cfg_str[param].group,
591                                       sat_cfg_str[param].key, NULL);
592             }
593         }
594     }
595     else
596     {
597         sat_log_log(SAT_LOG_LEVEL_ERROR,
598                     _("%s: Unknown STR param index (%d)\n"), __func__, param);
599     }
600 }
601 
sat_cfg_reset_str(sat_cfg_str_e param)602 void sat_cfg_reset_str(sat_cfg_str_e param)
603 {
604 
605     if (param < SAT_CFG_STR_NUM)
606     {
607         if (config == NULL)
608         {
609             sat_log_log(SAT_LOG_LEVEL_ERROR,
610                         _("%s: Module not initialised\n"), __func__);
611         }
612         else
613         {
614             g_key_file_remove_key(config,
615                                   sat_cfg_str[param].group,
616                                   sat_cfg_str[param].key, NULL);
617         }
618 
619     }
620     else
621     {
622         sat_log_log(SAT_LOG_LEVEL_ERROR,
623                     _("%s: Unknown STR param index (%d)\n"), __func__, param);
624     }
625 }
626 
sat_cfg_get_int(sat_cfg_int_e param)627 gint sat_cfg_get_int(sat_cfg_int_e param)
628 {
629     gint            value = 0;
630     GError         *error = NULL;
631 
632     if (param < SAT_CFG_INT_NUM)
633     {
634         if (config == NULL)
635         {
636             sat_log_log(SAT_LOG_LEVEL_ERROR,
637                         _("%s: Module not initialised\n"), __func__);
638 
639             /* return default value */
640             value = sat_cfg_int[param].defval;
641         }
642         else
643         {
644             /* fetch value */
645             value = g_key_file_get_integer(config,
646                                            sat_cfg_int[param].group,
647                                            sat_cfg_int[param].key, &error);
648 
649             if (error != NULL)
650             {
651                 g_clear_error(&error);
652                 value = sat_cfg_int[param].defval;
653             }
654         }
655 
656     }
657     else
658     {
659         sat_log_log(SAT_LOG_LEVEL_ERROR,
660                     _("%s: Unknown INT param index (%d)\n"), __func__, param);
661     }
662 
663     return value;
664 }
665 
sat_cfg_get_int_def(sat_cfg_int_e param)666 gint sat_cfg_get_int_def(sat_cfg_int_e param)
667 {
668     gint            value = 0;
669 
670     if (param < SAT_CFG_INT_NUM)
671     {
672         value = sat_cfg_int[param].defval;
673     }
674     else
675     {
676         sat_log_log(SAT_LOG_LEVEL_ERROR,
677                     _("%s: Unknown INT param index (%d)\n"), __func__, param);
678     }
679 
680     return value;
681 }
682 
sat_cfg_set_int(sat_cfg_int_e param,gint value)683 void sat_cfg_set_int(sat_cfg_int_e param, gint value)
684 {
685     if (param < SAT_CFG_INT_NUM)
686     {
687         if (config == NULL)
688         {
689             sat_log_log(SAT_LOG_LEVEL_ERROR,
690                         _("%s: Module not initialised\n"), __func__);
691         }
692         else
693         {
694             g_key_file_set_integer(config,
695                                    sat_cfg_int[param].group,
696                                    sat_cfg_int[param].key, value);
697         }
698 
699     }
700     else
701     {
702         sat_log_log(SAT_LOG_LEVEL_ERROR,
703                     _("%s: Unknown INT param index (%d)\n"), __func__, param);
704     }
705 }
706 
sat_cfg_reset_int(sat_cfg_int_e param)707 void sat_cfg_reset_int(sat_cfg_int_e param)
708 {
709     if (param < SAT_CFG_INT_NUM)
710     {
711         if (config == NULL)
712         {
713             sat_log_log(SAT_LOG_LEVEL_ERROR,
714                         _("%s: Module not initialised\n"), __func__);
715         }
716         else
717         {
718             g_key_file_remove_key(config,
719                                   sat_cfg_int[param].group,
720                                   sat_cfg_int[param].key, NULL);
721         }
722 
723     }
724     else
725     {
726         sat_log_log(SAT_LOG_LEVEL_ERROR,
727                     _("%s: Unknown INT param index (%d)\n"), __func__, param);
728     }
729 }
730