1 /***************************************************************************
2  *            lpconfig.h
3  *
4  *  Thu Mar 10 15:02:49 2005
5  *  Copyright  2005  Simon Morlat
6  *  Email simon.morlat@linphone.org
7  ****************************************************************************/
8 
9 /*
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Library General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24 
25 #ifndef LPCONFIG_H
26 #define LPCONFIG_H
27 
28 #include "linphone/types.h"
29 
30 /**
31  * @addtogroup misc
32  * @{
33  */
34 
35 /**
36  * Safely downcast a belle_sip_object into LinphoneConfig
37  */
38 #define LINPHONE_CONFIG(obj) BELLE_SIP_CAST(obj, LinphoneConfig);
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * Instantiates a LinphoneConfig object from a user config file.
46  * The caller of this constructor owns a reference. linphone_config_unref() must be called when this object is no longer needed.
47  * @ingroup misc
48  * @param filename the filename of the config file to read to fill the instantiated LinphoneConfig
49  * @see linphone_config_new_with_factory
50  */
51 LINPHONE_PUBLIC LinphoneConfig * linphone_config_new(const char *filename);
52 
53 /**
54  * Instantiates a LinphoneConfig object from a user provided buffer.
55  * The caller of this constructor owns a reference. linphone_config_unref() must be called when this object is no longer needed.
56  * @ingroup misc
57  * @param buffer the buffer from which the LinphoneConfig will be retrieved. We expect the buffer to be null-terminated.
58  * @see linphone_config_new_with_factory
59  * @see linphone_config_new
60  */
61 LINPHONE_PUBLIC LinphoneConfig * linphone_config_new_from_buffer(const char *buffer);
62 
63 /**
64  * Instantiates a LinphoneConfig object from a user config file and a factory config file.
65  * The caller of this constructor owns a reference. linphone_config_unref() must be called when this object is no longer needed.
66  * @ingroup misc
67  * @param config_filename the filename of the user config file to read to fill the instantiated LinphoneConfig
68  * @param factory_config_filename the filename of the factory config file to read to fill the instantiated LinphoneConfig
69  * @see linphone_config_new
70  *
71  * The user config file is read first to fill the LinphoneConfig and then the factory config file is read.
72  * Therefore the configuration parameters defined in the user config file will be overwritten by the parameters
73  * defined in the factory config file.
74  */
75 LINPHONE_PUBLIC LinphoneConfig * linphone_config_new_with_factory(const char *config_filename, const char *factory_config_filename);
76 
77 /**
78  * Reads a user config file and fill the LinphoneConfig with the read config values.
79  * @ingroup misc
80  * @param lpconfig The LinphoneConfig object to fill with the content of the file
81  * @param filename The filename of the config file to read to fill the LinphoneConfig
82  */
83 LINPHONE_PUBLIC LinphoneStatus linphone_config_read_file(LinphoneConfig *lpconfig, const char *filename);
84 
85 /**
86  * Reads a xml config file and fill the LinphoneConfig with the read config dynamic values.
87  * @ingroup misc
88  * @param lpconfig The LinphoneConfig object to fill with the content of the file
89  * @param filename The filename of the config file to read to fill the LinphoneConfig
90  */
91 LINPHONE_PUBLIC const char* linphone_config_load_from_xml_file(LinphoneConfig *lpc, const char *filename);
92 
93 /**
94  * Reads a xml config string and fill the LinphoneConfig with the read config dynamic values.
95  * @ingroup misc
96  * @param lpconfig The LinphoneConfig object to fill with the content of the file
97  * @param buffer The string of the config file to fill the LinphoneConfig
98  * @return 0 in case of success
99  */
100 LINPHONE_PUBLIC LinphoneStatus linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer);
101 
102 /**
103  * Retrieves a configuration item as a string, given its section, key, and default value.
104  *
105  * The default value string is returned if the config item isn't found.
106 **/
107 LINPHONE_PUBLIC const char *linphone_config_get_string(const LinphoneConfig *lpconfig, const char *section, const char *key, const char *default_string);
108 
109 /**
110  * Retrieves a configuration item as a list of strings, given its section, key, and default value.
111  * The default value is returned if the config item is not found.
112  * @param[in] lpconfig A #LinphoneConfig object
113  * @param[in] section The section from which to retrieve a configuration item
114  * @param[in] key The name of the configuration item to retrieve
115  * @param[in] default_list \bctbx_list{const char *}
116  * @return \bctbx_list{const char *}
117  */
118 LINPHONE_PUBLIC bctbx_list_t * linphone_config_get_string_list(const LinphoneConfig *lpconfig, const char *section, const char *key, bctbx_list_t *default_list);
119 
120 /**
121  * Retrieves a configuration item as a range, given its section, key, and default min and max values.
122  *
123  * @return TRUE if the value is successfully parsed as a range, FALSE otherwise.
124  * If FALSE is returned, min and max are filled respectively with default_min and default_max values.
125  */
126 LINPHONE_PUBLIC bool_t linphone_config_get_range(const LinphoneConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max);
127 
128 /**
129  * Retrieves a configuration item as an integer, given its section, key, and default value.
130  *
131  * The default integer value is returned if the config item isn't found.
132 **/
133 LINPHONE_PUBLIC int linphone_config_get_int(const LinphoneConfig *lpconfig,const char *section, const char *key, int default_value);
134 
135 /**
136  * Retrieves a configuration item as a 64 bit integer, given its section, key, and default value.
137  *
138  * The default integer value is returned if the config item isn't found.
139 **/
140 LINPHONE_PUBLIC int64_t linphone_config_get_int64(const LinphoneConfig *lpconfig,const char *section, const char *key, int64_t default_value);
141 
142 /**
143  * Retrieves a configuration item as a float, given its section, key, and default value.
144  *
145  * The default float value is returned if the config item isn't found.
146 **/
147 LINPHONE_PUBLIC float linphone_config_get_float(const LinphoneConfig *lpconfig,const char *section, const char *key, float default_value);
148 
149 /**
150  * Sets a string config item
151 **/
152 LINPHONE_PUBLIC void linphone_config_set_string(LinphoneConfig *lpconfig,const char *section, const char *key, const char *value);
153 
154 /**
155  * Sets a string list config item
156  * @param[in] lpconfig A #LinphoneConfig object
157  * @param[in] section The name of the section to put the configuration item into
158  * @param[in] key The name of the configuration item to set
159  * @param[in] value \bctbx_list{const char *} The value to set
160  */
161 LINPHONE_PUBLIC void linphone_config_set_string_list(LinphoneConfig *lpconfig, const char *section, const char *key, const bctbx_list_t *value);
162 
163 /**
164  * Sets a range config item
165  */
166 LINPHONE_PUBLIC void linphone_config_set_range(LinphoneConfig *lpconfig, const char *section, const char *key, int min_value, int max_value);
167 
168 /**
169  * Sets an integer config item
170 **/
171 LINPHONE_PUBLIC void linphone_config_set_int(LinphoneConfig *lpconfig,const char *section, const char *key, int value);
172 
173 /**
174  * Sets an integer config item, but store it as hexadecimal
175 **/
176 LINPHONE_PUBLIC void linphone_config_set_int_hex(LinphoneConfig *lpconfig,const char *section, const char *key, int value);
177 
178 /**
179  * Sets a 64 bits integer config item
180 **/
181 LINPHONE_PUBLIC void linphone_config_set_int64(LinphoneConfig *lpconfig,const char *section, const char *key, int64_t value);
182 
183 /**
184  * Sets a float config item
185 **/
186 LINPHONE_PUBLIC void linphone_config_set_float(LinphoneConfig *lpconfig,const char *section, const char *key, float value);
187 
188 /**
189  * Writes the config file to disk.
190 **/
191 LINPHONE_PUBLIC LinphoneStatus linphone_config_sync(LinphoneConfig *lpconfig);
192 
193 /**
194  * Returns 1 if a given section is present in the configuration.
195 **/
196 LINPHONE_PUBLIC int linphone_config_has_section(const LinphoneConfig *lpconfig, const char *section);
197 
198 /**
199  * Removes every pair of key,value in a section and remove the section.
200 **/
201 LINPHONE_PUBLIC void linphone_config_clean_section(LinphoneConfig *lpconfig, const char *section);
202 
203 /**
204  * Returns 1 if a given section  with a given key is present in the configuration.
205  * @param[in] lpconfig The LinphoneConfig object
206  * @param[in] section
207  * @param[in] key
208  **/
209 LINPHONE_PUBLIC int linphone_config_has_entry(const LinphoneConfig *lpconfig, const char *section, const char *key);
210 
211 /**
212  * Removes entries for key,value in a section.
213  * @param[in] lpconfig The LinphoneConfig object
214  * @param[in] section
215  * @param[in] key
216  **/
217 LINPHONE_PUBLIC void linphone_config_clean_entry(LinphoneConfig *lpconfig, const char *section, const char *key);
218 
219 /**
220  * Returns the list of sections' names in the LinphoneConfig.
221  * @param[in] lpconfig The LinphoneConfig object
222  * @return a null terminated static array of strings
223 **/
224 LINPHONE_PUBLIC const char** linphone_config_get_sections_names(LinphoneConfig *lpconfig);
225 
226 /**
227  * Call a function for each section present in the configuration.
228 **/
229 void linphone_config_for_each_section(const LinphoneConfig *lpconfig, void (*callback)(const char *section, void *ctx), void *ctx);
230 
231 /**
232  * Call a function for each entry present in a section configuration.
233 **/
234 void linphone_config_for_each_entry(const LinphoneConfig *lpconfig, const char *section, void (*callback)(const char *entry, void *ctx), void *ctx);
235 
236 /*tells whether uncommited (with linphone_config_sync()) modifications exist*/
237 bool_t linphone_config_needs_commit(const LinphoneConfig *lpconfig);
238 
239 LINPHONE_PUBLIC void linphone_config_destroy(LinphoneConfig *cfg);
240 
241 /**
242  * Retrieves a default configuration item as an integer, given its section, key, and default value.
243  * The default integer value is returned if the config item isn't found.
244 **/
245 LINPHONE_PUBLIC int linphone_config_get_default_int(const LinphoneConfig *lpconfig, const char *section, const char *key, int default_value);
246 
247 /**
248  * Retrieves a default configuration item as a 64 bit integer, given its section, key, and default value.
249  * The default integer value is returned if the config item isn't found.
250 **/
251 LINPHONE_PUBLIC int64_t linphone_config_get_default_int64(const LinphoneConfig *lpconfig, const char *section, const char *key, int64_t default_value);
252 
253 /**
254  * Retrieves a default configuration item as a float, given its section, key, and default value.
255  * The default float value is returned if the config item isn't found.
256 **/
257 LINPHONE_PUBLIC float linphone_config_get_default_float(const LinphoneConfig *lpconfig, const char *section, const char *key, float default_value);
258 
259 /**
260  * Retrieves a default configuration item as a string, given its section, key, and default value.
261  * The default value string is returned if the config item isn't found.
262 **/
263 LINPHONE_PUBLIC const char* linphone_config_get_default_string(const LinphoneConfig *lpconfig, const char *section, const char *key, const char *default_value);
264 
265 /**
266  * Retrieves a section parameter item as a string, given its section and key.
267  * The default value string is returned if the config item isn't found.
268 **/
269 LINPHONE_PUBLIC const char* linphone_config_get_section_param_string(const LinphoneConfig *lpconfig, const char *section, const char *key, const char *default_value);
270 
271 /**
272  * increment reference count
273 **/
274 LINPHONE_PUBLIC LinphoneConfig *linphone_config_ref(LinphoneConfig *lpconfig);
275 
276 /**
277  * Decrement reference count, which will eventually free the object.
278 **/
279 LINPHONE_PUBLIC void linphone_config_unref(LinphoneConfig *lpconfig);
280 
281 /**
282  * Write a string in a file placed relatively with the Linphone configuration file.
283  * @param lpconfig LinphoneConfig instance used as a reference
284  * @param filename Name of the file where to write data. The name is relative to the place of the config file
285  * @param data String to write
286  */
287 LINPHONE_PUBLIC void linphone_config_write_relative_file(const LinphoneConfig *lpconfig, const char *filename, const char *data);
288 
289 /**
290  * Read a string from a file placed beside the Linphone configuration file
291  * @param lpconfig LinphoneConfig instance used as a reference
292  * @param filename Name of the file where data will be read from. The name is relative to the place of the config file
293  * @param data Buffer where read string will be stored
294  * @param max_length Length of the buffer
295  * @return 0 on success, -1 on failure
296  * @donotwrap
297  */
298 LINPHONE_PUBLIC LinphoneStatus linphone_config_read_relative_file(const LinphoneConfig *lpconfig, const char *filename, char *data, size_t max_length);
299 
300 /**
301  * @return TRUE if file exists relative to the to the current location
302 **/
303 LINPHONE_PUBLIC bool_t linphone_config_relative_file_exists(const LinphoneConfig *lpconfig, const char *filename);
304 
305 /**
306  * Dumps the LinphoneConfig as XML into a buffer
307  * @param[in] lpconfig The LinphoneConfig object
308  * @return The buffer that contains the XML dump
309 **/
310 LINPHONE_PUBLIC char* linphone_config_dump_as_xml(const LinphoneConfig *lpconfig);
311 
312 /**
313  * Dumps the LinphoneConfig as INI into a buffer
314  * @param[in] lpconfig The LinphoneConfig object
315  * @return The buffer that contains the config dump
316 **/
317 LINPHONE_PUBLIC char* linphone_config_dump(const LinphoneConfig *lpconfig);
318 
319 /**
320  * Retrieves the overwrite flag for a config item
321 **/
322 LINPHONE_PUBLIC bool_t linphone_config_get_overwrite_flag_for_entry(const LinphoneConfig *lpconfig, const char *section, const char *key);
323 
324 /**
325  * Sets the overwrite flag for a config item (used when dumping config as xml)
326 **/
327 LINPHONE_PUBLIC void linphone_config_set_overwrite_flag_for_entry(LinphoneConfig *lpconfig, const char *section, const char *key, bool_t value);
328 
329 /**
330  * Retrieves the overwrite flag for a config section
331 **/
332 LINPHONE_PUBLIC bool_t linphone_config_get_overwrite_flag_for_section(const LinphoneConfig *lpconfig, const char *section);
333 
334 /**
335  * Sets the overwrite flag for a config section (used when dumping config as xml)
336 **/
337 LINPHONE_PUBLIC void linphone_config_set_overwrite_flag_for_section(LinphoneConfig *lpconfig, const char *section, bool_t value);
338 
339 /**
340  * Retrieves the skip flag for a config item
341 **/
342 LINPHONE_PUBLIC bool_t linphone_config_get_skip_flag_for_entry(const LinphoneConfig *lpconfig, const char *section, const char *key);
343 
344 /**
345  * Sets the skip flag for a config item (used when dumping config as xml)
346 **/
347 LINPHONE_PUBLIC void linphone_config_set_skip_flag_for_entry(LinphoneConfig *lpconfig, const char *section, const char *key, bool_t value);
348 
349 /**
350  * Retrieves the skip flag for a config section
351 **/
352 LINPHONE_PUBLIC bool_t linphone_config_get_skip_flag_for_section(const LinphoneConfig *lpconfig, const char *section);
353 
354 /**
355  * Sets the skip flag for a config section (used when dumping config as xml)
356 **/
357 LINPHONE_PUBLIC void linphone_config_set_skip_flag_for_section(LinphoneConfig *lpconfig, const char *section, bool_t value);
358 
359 /**
360  * Converts a config section into a dictionary.
361  * @return a dictionary with all the keys from a section, or NULL if the section doesn't exist
362  */
363 LINPHONE_PUBLIC LinphoneDictionary * lp_config_section_to_dict( const LpConfig* lpconfig, const char* section );
364 
365 /**
366  * Loads a dictionary into a section of the lpconfig. If the section doesn't exist it is created.
367  * Overwrites existing keys, creates non-existing keys.
368  */
369 LINPHONE_PUBLIC void lp_config_load_dict_to_section( LpConfig* lpconfig, const char* section, const LinphoneDictionary* dict);
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 // Define old function names for backward compatibility
376 #define lp_config_new linphone_config_new
377 #define lp_config_new_from_buffer linphone_config_new_from_buffer
378 #define lp_config_new_with_factory linphone_config_new_with_factory
379 #define lp_config_read_file linphone_config_read_file
380 #define lp_config_get_string linphone_config_get_string
381 #define lp_config_get_string_list linphone_config_get_string_list
382 #define lp_config_get_range linphone_config_get_range
383 #define lp_config_get_int linphone_config_get_int
384 #define lp_config_get_int64 linphone_config_get_int64
385 #define lp_config_get_float linphone_config_get_float
386 #define lp_config_set_string linphone_config_set_string
387 #define lp_config_set_string_list linphone_config_set_string_list
388 #define lp_config_set_range linphone_config_set_range
389 #define lp_config_set_int linphone_config_set_int
390 #define lp_config_set_int_hex linphone_config_set_int_hex
391 #define lp_config_set_int64 linphone_config_set_int64
392 #define lp_config_set_float linphone_config_set_float
393 #define lp_config_sync linphone_config_sync
394 #define lp_config_has_section linphone_config_has_section
395 #define lp_config_clean_section linphone_config_clean_section
396 #define lp_config_has_entry linphone_config_has_entry
397 #define lp_config_clean_entry linphone_config_clean_entry
398 #define lp_config_get_sections_names linphone_config_get_sections_names
399 #define lp_config_for_each_section linphone_config_for_each_section
400 #define lp_config_for_each_entry linphone_config_for_each_entry
401 #define lp_config_needs_commit linphone_config_needs_commit
402 #define lp_config_destroy linphone_config_destroy
403 #define lp_config_get_default_int linphone_config_get_default_int
404 #define lp_config_get_default_int64 linphone_config_get_default_int64
405 #define lp_config_get_default_float linphone_config_get_default_float
406 #define lp_config_get_default_string linphone_config_get_default_string
407 #define lp_config_get_section_param_string linphone_config_get_section_param_string
408 #define lp_config_ref linphone_config_ref
409 #define lp_config_unref linphone_config_unref
410 #define lp_config_write_relative_file linphone_config_write_relative_file
411 #define lp_config_read_relative_file linphone_config_read_relative_file
412 #define lp_config_relative_file_exists linphone_config_relative_file_exists
413 #define lp_config_dump_as_xml linphone_config_dump_as_xml
414 #define lp_config_dump linphone_config_dump
415 #define lp_config_get_overwrite_flag_for_entry linphone_config_get_overwrite_flag_for_entry
416 #define lp_config_set_overwrite_flag_for_entry linphone_config_set_overwrite_flag_for_entry
417 #define lp_config_get_overwrite_flag_for_section linphone_config_get_overwrite_flag_for_section
418 #define lp_config_set_overwrite_flag_for_section linphone_config_set_overwrite_flag_for_section
419 #define lp_config_get_skip_flag_for_entry linphone_config_get_skip_flag_for_entry
420 #define lp_config_set_skip_flag_for_entry linphone_config_set_skip_flag_for_entry
421 #define lp_config_get_skip_flag_for_section linphone_config_get_skip_flag_for_section
422 #define lp_config_set_skip_flag_for_section linphone_config_set_skip_flag_for_section
423 
424 /**
425  * @}
426  */
427 
428 #endif
429