1 /*
2 linphone
3 Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 
20 #include "linphone/core.h"
21 #include "linphone/lpconfig.h"
22 #include "private.h"
23 
24 #include <belle-sip/belle-sip.h>
25 #include <belle-sip/object.h>
26 #include <belle-sip/dict.h>
27 
28 
29 /**
30  * @addtogroup misc
31  * @{
32 **/
33 
34 
linphone_dictionary_new()35 LinphoneDictionary* linphone_dictionary_new()
36 {
37 	return belle_sip_dict_create();
38 }
39 
linphone_dictionary_clone(const LinphoneDictionary * src)40 LinphoneDictionary* linphone_dictionary_clone(const LinphoneDictionary* src)
41 {
42 	LinphoneDictionary* cloned = linphone_dictionary_new();
43 	if( cloned ){
44 		belle_sip_dict_clone(src, cloned);
45 	}
46 	return cloned;
47 }
48 
linphone_dictionary_ref(LinphoneDictionary * obj)49 LinphoneDictionary* linphone_dictionary_ref(LinphoneDictionary* obj)
50 {
51 	return BELLE_SIP_DICT(belle_sip_object_ref(obj));
52 }
53 
linphone_dictionary_unref(LinphoneDictionary * obj)54 void linphone_dictionary_unref(LinphoneDictionary *obj)
55 {
56 	belle_sip_object_unref(obj);
57 }
58 
linphone_dictionary_set_int(LinphoneDictionary * obj,const char * key,int value)59 void linphone_dictionary_set_int(LinphoneDictionary* obj, const char* key, int value)
60 {
61 	belle_sip_dict_set_int(obj, key, value);
62 }
63 
linphone_dictionary_get_int(LinphoneDictionary * obj,const char * key,int default_value)64 int linphone_dictionary_get_int(LinphoneDictionary* obj, const char* key, int default_value)
65 {
66 	return belle_sip_dict_get_int(obj, key, default_value);
67 }
68 
linphone_dictionary_set_string(LinphoneDictionary * obj,const char * key,const char * value)69 void linphone_dictionary_set_string(LinphoneDictionary* obj, const char* key, const char*value)
70 {
71 	belle_sip_dict_set_string(obj, key, value);
72 }
73 
linphone_dictionary_get_string(LinphoneDictionary * obj,const char * key,const char * default_value)74 const char* linphone_dictionary_get_string(LinphoneDictionary* obj, const char* key, const char* default_value)
75 {
76 	return belle_sip_dict_get_string(obj, key, default_value);
77 }
78 
linphone_dictionary_set_int64(LinphoneDictionary * obj,const char * key,int64_t value)79 void linphone_dictionary_set_int64(LinphoneDictionary* obj, const char* key, int64_t value)
80 {
81 	belle_sip_dict_set_int64(obj, key, value);
82 }
83 
linphone_dictionary_get_int64(LinphoneDictionary * obj,const char * key,int64_t default_value)84 int64_t linphone_dictionary_get_int64(LinphoneDictionary* obj, const char* key, int64_t default_value)
85 {
86 	return belle_sip_dict_get_int64(obj, key, default_value);
87 }
88 
linphone_dictionary_remove(LinphoneDictionary * obj,const char * key)89 LinphoneStatus linphone_dictionary_remove(LinphoneDictionary* obj, const char* key)
90 {
91 	return belle_sip_dict_remove(obj, key);
92 }
93 
linphone_dictionary_clear(LinphoneDictionary * obj)94 void linphone_dictionary_clear(LinphoneDictionary* obj)
95 {
96 	belle_sip_dict_clear(obj);
97 }
98 
linphone_dictionary_haskey(const LinphoneDictionary * obj,const char * key)99 LinphoneStatus linphone_dictionary_haskey(const LinphoneDictionary* obj, const char* key)
100 {
101 	return belle_sip_dict_haskey(obj, key);
102 }
103 
linphone_dictionary_foreach(const LinphoneDictionary * obj,void (* apply_func)(const char *,void *,void *),void * userdata)104 void linphone_dictionary_foreach(const LinphoneDictionary* obj, void (*apply_func)(const char*, void*, void*), void* userdata)
105 {
106 	belle_sip_dict_foreach(obj, apply_func, userdata);
107 }
108 
109 struct lp_config_to_dict {
110 	const char*         section;
111 	const LpConfig*     config;
112 	LinphoneDictionary* dict;
113 };
114 
lp_config_section_to_dict_cb(const char * key,struct lp_config_to_dict * userdata)115 static void lp_config_section_to_dict_cb(const char*key, struct lp_config_to_dict* userdata)
116 {
117 	const char* value = lp_config_get_string(userdata->config, userdata->section, key, "");
118 	linphone_dictionary_set_string(userdata->dict, key, value);
119 }
120 
lp_config_section_to_dict(const LpConfig * lpconfig,const char * section)121 LinphoneDictionary* lp_config_section_to_dict(const LpConfig* lpconfig, const char* section)
122 {
123 	LinphoneDictionary* dict = NULL;
124 	struct lp_config_to_dict fd;
125 	fd.config = lpconfig;
126 	fd.section = section;
127 
128 	dict = linphone_dictionary_new();
129 	fd.dict = dict;
130 
131 	lp_config_for_each_entry(lpconfig, section,
132 							 (void (*)(const char*, void*))lp_config_section_to_dict_cb,
133 							 &fd);
134 
135 	return dict;
136 }
137 
138 struct lp_config_from_dict {
139 	const char* section;
140 	LpConfig*   config;
141 };
142 
lp_config_dict_dump_cb(const char * key,void * value,void * userdata)143 static void lp_config_dict_dump_cb( const char* key, void* value, void* userdata)
144 {
145 	struct lp_config_from_dict* fd= (struct lp_config_from_dict*)userdata;
146 	lp_config_set_string(fd->config, fd->section, key, (const char*)value);
147 }
148 
lp_config_load_dict_to_section(LpConfig * lpconfig,const char * section,const LinphoneDictionary * dict)149 void lp_config_load_dict_to_section(LpConfig* lpconfig, const char* section, const LinphoneDictionary* dict)
150 {
151 	struct lp_config_from_dict pvdata = { section, lpconfig };
152 	linphone_dictionary_foreach(dict,lp_config_dict_dump_cb, &pvdata);
153 }
154 
155 
156 
157 /**
158  * @}
159 **/
160