1 /*
2     Gpredict: Real-time satellite tracking and orbit prediction program
3 
4     Copyright (C)  2001-2009  Alexandru Csete, OZ9AEC.
5 
6     Authors: Alexandru Csete <oz9aec@gmail.com>
7 
8     Comments, questions and bugreports should be submitted via
9     http://sourceforge.net/projects/gpredict/
10     More details can be found at the project home page:
11 
12             http://gpredict.oz9aec.net/
13 
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18 
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23 
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, visit http://www.fsf.org/
26 */
27 
28 /**
29  * \brief Utilities to read module configuration parameters.
30  *
31  * This file contains utility functions that can can be used by modules
32  * to read configuration parameters from the GKeyFile of the module. If
33  * a parameter does not exist in the GKeyFile the corrersponding value
34  * from sat-cfg will be returned.
35  *
36  * The intended use of these functions is to read parameters when creating
37  * new modules. A module may have it's own configuration for most settings
38  * while for some settings the global/default values may be needed. To avoid
39  * too much repetitive coding in the module implementations, the functions
40  * warp this code into one convenient function call.
41  */
42 
43 #ifdef HAVE_CONFIG_H
44 #include <build-config.h>
45 #endif
46 
47 #include <glib/gi18n.h>
48 #include <gtk/gtk.h>
49 
50 #include "config-keys.h"
51 #include "sat-cfg.h"
52 #include "sat-log.h"
53 
54 
55 /**
56  * \brief Get boolean parameter.
57  * \param f The configuration data for the module.
58  * \param sec Configuration section in the cfg data (see config-keys.h).
59  * \param key Configuration key in the cfg data (see config-keys.h).
60  * \param p SatCfg index to use as fallback.
61  */
mod_cfg_get_bool(GKeyFile * f,const gchar * sec,const gchar * key,sat_cfg_bool_e p)62 gboolean mod_cfg_get_bool(GKeyFile * f, const gchar * sec, const gchar * key,
63                           sat_cfg_bool_e p)
64 {
65     GError         *error = NULL;
66     gboolean        param;
67 
68     /* check whether parameter is present in GKeyFile, otherwise use sat-cfg */
69     if (g_key_file_has_key(f, sec, key, NULL))
70     {
71         param = g_key_file_get_boolean(f, sec, key, &error);
72 
73         if (error != NULL)
74         {
75             sat_log_log(SAT_LOG_LEVEL_INFO,
76                         _("%s: Failed to read boolean (%s)"),
77                         __func__, error->message);
78 
79             g_clear_error(&error);
80 
81             /* get a timeout from global config */
82             param = sat_cfg_get_bool(p);
83         }
84     }
85     else
86     {
87         param = sat_cfg_get_bool(p);
88     }
89 
90     return param;
91 }
92 
93 
mod_cfg_get_int(GKeyFile * f,const gchar * sec,const gchar * key,sat_cfg_int_e p)94 gint mod_cfg_get_int(GKeyFile * f, const gchar * sec, const gchar * key,
95                      sat_cfg_int_e p)
96 {
97     GError         *error = NULL;
98     gint            param;
99 
100     /* check whether parameter is present in GKeyFile */
101     if (g_key_file_has_key(f, sec, key, NULL))
102     {
103         param = g_key_file_get_integer(f, sec, key, &error);
104 
105         if (error != NULL)
106         {
107             sat_log_log(SAT_LOG_LEVEL_WARN,
108                         _("%s: Failed to read integer (%s)"),
109                         __func__, error->message);
110 
111             g_clear_error(&error);
112 
113             /* get a timeout from global config */
114             param = sat_cfg_get_int(p);
115         }
116     }
117     /* get value from sat-cfg */
118     else
119     {
120         param = sat_cfg_get_int(p);
121     }
122 
123     return param;
124 }
125 
126 
mod_cfg_get_str(GKeyFile * f,const gchar * sec,const gchar * key,sat_cfg_str_e p)127 gchar          *mod_cfg_get_str(GKeyFile * f, const gchar * sec,
128                                 const gchar * key, sat_cfg_str_e p)
129 {
130     GError         *error = NULL;
131     gchar          *param;
132 
133     /* check whether parameter is present in GKeyFile, otherwise use sat-cfg */
134     if (g_key_file_has_key(f, sec, key, NULL))
135     {
136         param = g_key_file_get_string(f, sec, key, &error);
137 
138         if (error != NULL)
139         {
140             sat_log_log(SAT_LOG_LEVEL_WARN,
141                         _("%s: Failed to read string (%s)"),
142                         __func__, error->message);
143 
144             g_clear_error(&error);
145 
146             /* get a timeout from global config */
147             param = sat_cfg_get_str(p);
148         }
149     }
150     else
151     {
152         param = sat_cfg_get_str(p);
153     }
154 
155     return param;
156 }
157 
158 /**
159  * \brief Load an integer list into a hash table that uses the
160  * existinence of datain the hash as a boolean.
161  * It loads NULL's into the hash table.
162  */
mod_cfg_get_integer_list_boolean(GKeyFile * cfgdata,const gchar * section,const gchar * key,GHashTable * dest)163 void mod_cfg_get_integer_list_boolean(GKeyFile * cfgdata,
164                                       const gchar * section, const gchar * key,
165                                       GHashTable * dest)
166 {
167     gint           *sats = NULL;
168     gsize           length;
169     GError         *error = NULL;
170     guint           i;
171     guint          *tkey;
172 
173     if (!g_key_file_has_key(cfgdata, section, key, NULL))
174         return;
175 
176     sats = g_key_file_get_integer_list(cfgdata, section, key, &length, &error);
177     if (error != NULL)
178     {
179         sat_log_log(SAT_LOG_LEVEL_WARN,
180                     _("%s: Failed to get integer list: %s"),
181                     __func__, error->message);
182 
183         g_clear_error(&error);
184 
185         /* GLib API says nothing about the contents in case of error */
186         if (sats)
187         {
188             g_free(sats);
189         }
190 
191         return;
192     }
193 
194     /* read each satellite into hash table */
195     for (i = 0; i < length; i++)
196     {
197         tkey = g_new0(guint, 1);
198         *tkey = sats[i];
199         //printf("loading sat %d\n",sats[i]);
200         if (!(g_hash_table_lookup_extended(dest, tkey, NULL, NULL)))
201         {
202             /* just add a one to the value so there is presence indicator */
203             g_hash_table_insert(dest, tkey, NULL);
204         }
205     }
206     g_free(sats);
207 }
208 
209 /**
210  * \brief Convert the "boolean" hash back into an integer list and
211  * save it to the cfgdata.
212  */
mod_cfg_set_integer_list_boolean(GKeyFile * cfgdata,GHashTable * hash,const gchar * cfgsection,const gchar * cfgkey)213 void mod_cfg_set_integer_list_boolean(GKeyFile * cfgdata, GHashTable * hash,
214                                       const gchar * cfgsection,
215                                       const gchar * cfgkey)
216 {
217     gint           *showtrack;
218     gint           *something;
219     gint            i, length;
220     GList          *keys = g_hash_table_get_keys(hash);
221 
222     length = g_list_length(keys);
223     if (g_list_length(keys) > 0)
224     {
225         showtrack = g_try_new0(gint, g_list_length(keys));
226         for (i = 0; i < length; i++)
227         {
228             something = g_list_nth_data(keys, i);
229             showtrack[i] = *something;
230         }
231         g_key_file_set_integer_list(cfgdata,
232                                     cfgsection,
233                                     cfgkey, showtrack, g_list_length(keys));
234     }
235     else
236     {
237         g_key_file_remove_key(cfgdata, cfgsection, cfgkey, NULL);
238     }
239 
240     g_list_free(keys);
241 }
242