upload tizen1.0 source
[framework/location/libslp-location.git] / location / map-service / location-map-pref.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27 #include "location-map-types.h"
28 #include "map-service.h"
29 #include "location-map-pref.h"
30
31 struct _LocationMapPref {
32         gchar *provider_name;                ///< Name of the service provier
33     gchar *language;            ///< Language of the service preference.
34     gchar *country;                             ///< Country of the service preference.
35     gchar *distance_unit;          ///< Distance unit of the service preference.
36     GHashTable *properties;       ///< properties of the service preference.
37 };
38
39 EXPORT_API GList *
40 location_map_pref_get_property_key (const LocationMapPref *pref)
41 {
42         g_return_val_if_fail (pref, NULL);
43
44         if (!pref->properties) return NULL;
45
46         return g_hash_table_get_keys (pref->properties);
47 }
48
49 EXPORT_API gchar *
50 location_map_pref_get_language (const LocationMapPref *pref)
51 {
52         g_return_val_if_fail (pref, NULL);
53
54         return pref->language;
55 }
56
57 EXPORT_API gchar *
58 location_map_pref_get_country (const LocationMapPref *pref)
59 {
60         g_return_val_if_fail (pref, NULL);
61
62         return pref->country;
63 }
64
65 EXPORT_API gchar *
66 location_map_pref_get_distance_unit (const LocationMapPref *pref)
67 {
68         g_return_val_if_fail (pref, NULL);
69
70         return pref->distance_unit;
71 }
72
73 EXPORT_API gconstpointer
74 location_map_pref_get_property (const LocationMapPref *pref, gconstpointer key)
75 {
76         g_return_val_if_fail (pref, NULL);
77         g_return_val_if_fail (key, NULL);
78         if (!pref->properties) return NULL;
79
80         return (gconstpointer) g_hash_table_lookup (pref->properties, key);
81 }
82
83 EXPORT_API gchar *
84 location_map_pref_get_provider_name (const LocationMapPref *pref)
85 {
86         g_return_val_if_fail (pref, NULL);
87
88         return pref->provider_name;
89 }
90
91 EXPORT_API gboolean
92 location_map_pref_set_provider_name (LocationMapPref *pref, const gchar *name)
93 {
94         g_return_val_if_fail (pref, FALSE);
95
96         if (pref->provider_name) {
97                 g_free (pref->provider_name);
98                 pref->provider_name = NULL;
99         }
100
101         if (name) pref->provider_name = g_strdup ((gchar *)name);
102
103         return TRUE;
104 }
105
106 EXPORT_API gboolean
107 location_map_pref_set_language (LocationMapPref *pref, const gchar * language)
108 {
109         g_return_val_if_fail (pref, FALSE);
110
111         if (pref->language) {
112                 g_free (pref->language);
113                 pref->language = NULL;
114         }
115
116         if (language) pref->language = g_strdup(language);
117
118         return TRUE;
119 }
120
121 EXPORT_API gboolean
122 location_map_pref_set_country (LocationMapPref *pref, const gchar *country)
123 {
124         g_return_val_if_fail (pref, FALSE);
125
126         if (pref->country) {
127                 g_free (pref->country);
128                 pref->country = NULL;
129         }
130
131         if (country) pref->country = g_strdup(country);
132
133         return TRUE;
134 }
135
136
137 EXPORT_API gboolean
138 location_map_pref_set_distance_unit (LocationMapPref *pref, const gchar * unit)
139 {
140         g_return_val_if_fail (pref, FALSE);
141
142         if (pref->distance_unit) {
143                 g_free (pref->distance_unit);
144                 pref->distance_unit = NULL;
145         }
146
147         if (unit) pref->distance_unit = g_strdup (unit);
148
149         return TRUE;
150 }
151
152 EXPORT_API gboolean
153 location_map_pref_set_property (LocationMapPref *pref, gconstpointer key, gconstpointer value)
154 {
155         g_return_val_if_fail (pref, FALSE);
156         g_return_val_if_fail (key, FALSE);
157         if (!pref->properties) return FALSE;
158
159         if (value) {
160                 gchar *re_key = g_strdup (key);
161                 gchar *re_val = g_strdup (value);
162                 g_hash_table_insert (pref->properties, re_key, re_val);
163         } else g_hash_table_remove (pref->properties, key);
164
165         return TRUE;
166 }
167
168 EXPORT_API LocationMapPref *
169 location_map_pref_new (void)
170 {
171         LocationMapPref *pref = g_slice_new0(LocationMapPref);
172         if (!pref) return NULL;
173
174         pref->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
175
176         return pref;
177 }
178
179 static void property_copy (gpointer key, gpointer value, gpointer user_data)
180 {
181         g_return_if_fail (key);
182         g_return_if_fail (value);
183         g_return_if_fail (user_data);
184
185         GHashTable *properties = (GHashTable *) user_data;
186
187         gchar *re_key = g_strdup (key);
188         gchar *re_val = g_strdup (value);
189         g_hash_table_insert (properties, re_key, re_val);
190 }
191
192 EXPORT_API LocationMapPref *
193 location_map_pref_copy (LocationMapPref *pref)
194 {
195         g_return_val_if_fail (pref, NULL);
196
197         LocationMapPref *new_pref = location_map_pref_new();
198         if (!new_pref) return NULL;
199
200         location_map_pref_set_provider_name (new_pref, location_map_pref_get_provider_name(pref));
201         location_map_pref_set_language (new_pref, location_map_pref_get_language(pref));
202         location_map_pref_set_distance_unit (new_pref, location_map_pref_get_distance_unit(pref));
203
204
205         g_hash_table_foreach (pref->properties, property_copy, new_pref->properties);
206
207         return new_pref;
208 }
209
210 EXPORT_API void
211 location_map_pref_free (LocationMapPref * pref)
212 {
213         g_return_if_fail(pref);
214
215         location_map_pref_set_provider_name (pref, NULL);
216         location_map_pref_set_language (pref, NULL);
217         location_map_pref_set_distance_unit (pref, NULL);
218         g_hash_table_destroy (pref->properties);
219
220         g_slice_free (LocationMapPref, pref);
221         pref = NULL;
222 }