Release Tizen2.0 beta
[platform/core/location/lbs-location.git] / location / map-service / location-poi.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 "location-log.h"
27 #include "location-types.h"
28 #include "location-map-pref.h"
29
30 #include "map-service.h"
31
32 #include "location-poi.h"
33
34 struct _LocationPOIPreference {
35         guint max_result_cnt;                            ///< Maximum number of results
36         LocationPOIPrefSortOrder sort_order;             ///< Sort order
37         gchar* item;                        ///< Sory by item
38         GHashTable *properties;
39 };
40
41 struct _LocationPOIFilter {
42         // "CATEGORY", "KEYWORD", "POINAME"
43         GHashTable *properties;
44 };
45
46 EXPORT_API guint
47 location_poi_pref_get_max_result (const LocationPOIPreference *pref)
48 {
49         g_return_val_if_fail(pref, 0);
50
51         return pref->max_result_cnt;
52 }
53
54 EXPORT_API gchar *
55 location_poi_pref_get_sort_by (const LocationPOIPreference *pref)
56 {
57         g_return_val_if_fail(pref, NULL);
58
59         return pref->item;
60 }
61
62 EXPORT_API LocationPOIPrefSortOrder
63 location_poi_pref_get_sort_order (const LocationPOIPreference *pref)
64 {
65         g_return_val_if_fail(pref, LOCATION_POI_PRE_SO_NONE);
66
67         return pref->sort_order;
68 }
69
70 EXPORT_API GList *
71 location_poi_pref_get_property_key (const LocationPOIPreference *pref)
72 {
73         g_return_val_if_fail (pref, NULL);
74         if (!pref->properties) return NULL;
75
76         return g_hash_table_get_keys (pref->properties);
77 }
78
79 EXPORT_API gpointer
80 location_poi_pref_get_property (const LocationPOIPreference *pref, gconstpointer key)
81 {
82         g_return_val_if_fail (pref, NULL);
83         g_return_val_if_fail (key, NULL);
84
85         if (!pref->properties) return NULL;
86
87         return g_hash_table_lookup (pref->properties, key);
88 }
89
90
91 EXPORT_API gboolean
92 location_poi_pref_set_max_result (LocationPOIPreference *pref, guint max_num)
93 {
94         g_return_val_if_fail(pref, FALSE);
95         g_return_val_if_fail(max_num > 0, FALSE);
96
97         pref->max_result_cnt = max_num;
98
99         return TRUE;
100 }
101
102 EXPORT_API gboolean
103 location_poi_pref_set_sort_by(LocationPOIPreference * pref, const gchar * item)
104 {
105         g_return_val_if_fail(pref, FALSE);
106
107         if (pref->item) {
108                 g_free(pref->item);
109                 pref->item = NULL;
110         }
111
112         if (item) pref->item = g_strdup(item);
113
114         return TRUE;
115 }
116
117 EXPORT_API gboolean
118 location_poi_pref_set_sort_order (LocationPOIPreference *pref, LocationPOIPrefSortOrder sort_order)
119 {
120         g_return_val_if_fail(pref, FALSE);
121
122         if (sort_order < LOCATION_POI_PREF_SO_ASC || sort_order > LOCATION_POI_PREF_SO_DESC) return FALSE;
123
124         pref->sort_order = sort_order;
125
126         return TRUE;
127 }
128
129 EXPORT_API gboolean
130 location_poi_pref_set_property (LocationPOIPreference *pref, gconstpointer key, gconstpointer value)
131 {
132         g_return_val_if_fail(pref, FALSE);
133         g_return_val_if_fail(key, FALSE);
134         g_return_val_if_fail(pref->properties, FALSE);
135
136         if (value) {
137                 gchar *re_key = g_strdup (key);
138                 gchar *re_val = g_strdup (value);
139                 g_hash_table_insert (pref->properties, re_key, re_val);
140         } else  g_hash_table_remove (pref->properties, key);
141
142         return TRUE;
143 }
144
145 EXPORT_API LocationPOIPreference *
146 location_poi_pref_new (void)
147 {
148         LocationPOIPreference *pref = g_slice_new0 (LocationPOIPreference);
149         g_return_val_if_fail(pref, NULL);
150
151         pref->sort_order = LOCATION_POI_PREF_SO_ASC;
152         pref->max_result_cnt = 25;
153         pref->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
154
155         return pref;
156 }
157
158 static void poi_pref_property_copy_cb (gpointer key, gpointer value, gpointer user_data)
159 {
160         g_return_if_fail (key);
161         g_return_if_fail (value);
162         g_return_if_fail (user_data);
163
164         LocationPOIPreference *pref = (LocationPOIPreference *) user_data;
165         if (pref->properties) {
166                 gchar *re_key = g_strdup (key);
167                 gchar *re_val = g_strdup (value);
168                 g_hash_table_insert (pref->properties, re_key, re_val);
169         }
170 }
171
172 EXPORT_API LocationPOIPreference *
173 location_poi_pref_copy (LocationPOIPreference *pref)
174 {
175         LocationPOIPreference *new_pref = location_poi_pref_new();
176         g_return_val_if_fail (new_pref, NULL);
177
178         location_poi_pref_set_sort_by(new_pref, location_poi_pref_get_sort_by(pref));
179         location_poi_pref_set_sort_order(new_pref, location_poi_pref_get_sort_order(pref));
180         location_poi_pref_set_max_result(new_pref, location_poi_pref_get_max_result(pref));
181
182         g_hash_table_foreach (pref->properties, poi_pref_property_copy_cb ,new_pref);
183
184         return new_pref;
185 }
186
187 EXPORT_API void
188 location_poi_pref_free (LocationPOIPreference * pref)
189 {
190         g_return_if_fail(pref);
191
192         location_poi_pref_set_sort_by(pref, NULL);
193         if (pref->properties) g_hash_table_destroy (pref->properties);
194
195         g_slice_free(LocationPOIPreference, pref);
196 }
197
198 EXPORT_API gboolean
199 location_poi_filter_set (const LocationPOIFilter *filter, gconstpointer key, gconstpointer value)
200 {
201         g_return_val_if_fail(filter, FALSE);
202         g_return_val_if_fail(key, FALSE);
203
204         if (filter->properties) {
205                 if (value) {
206                         gchar *re_key = g_strdup (key);
207                         gchar *re_val = g_strdup (value);
208                         g_hash_table_insert (filter->properties, re_key, re_val);
209                 } else  g_hash_table_remove (filter->properties, key);
210         }
211         else
212                 return FALSE;
213
214         return TRUE;
215 }
216
217 EXPORT_API gpointer
218 location_poi_filter_get (const LocationPOIFilter *filter, gconstpointer key)
219 {
220         g_return_val_if_fail(filter, NULL);
221         g_return_val_if_fail(key, NULL);
222
223         if (filter->properties) {
224                 return g_hash_table_lookup (filter->properties, key);
225         }
226         return NULL;
227 }
228
229 EXPORT_API GList *
230 location_poi_filter_get_key (const LocationPOIFilter *filter)
231 {
232         g_return_val_if_fail(filter, NULL);
233
234         if (filter->properties) {
235                 return g_hash_table_get_keys (filter->properties);
236         }
237
238         return NULL;
239 }
240
241 EXPORT_API LocationPOIFilter *
242 location_poi_filter_new (void)
243 {
244         LocationPOIFilter *filter = g_slice_new0(LocationPOIFilter);
245         g_return_val_if_fail (filter, NULL);
246
247         filter->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
248
249         return filter;
250 }
251
252 static void poi_filter_copy_cb (gpointer key, gpointer value, gpointer user_data)
253 {
254         g_return_if_fail (key);
255         g_return_if_fail (value);
256         g_return_if_fail (user_data);
257
258         LocationPOIFilter *filter = (LocationPOIFilter *) user_data;
259         if (filter->properties) {
260                 gchar *re_key = g_strdup (key);
261                 gchar *re_val = g_strdup (value);
262                 g_hash_table_insert (filter->properties, re_key, re_val);
263         }
264 }
265
266 EXPORT_API LocationPOIFilter *
267 location_poi_filter_copy (LocationPOIFilter *filter)
268 {
269         g_return_val_if_fail (filter, NULL);
270
271         LocationPOIFilter * new_filter = location_poi_filter_new();
272         g_return_val_if_fail (new_filter, NULL);
273
274         if (new_filter->properties) g_hash_table_foreach (filter->properties, poi_filter_copy_cb, new_filter);
275
276         return new_filter;
277 }
278
279 EXPORT_API void
280 location_poi_filter_free (LocationPOIFilter *filter)
281 {
282         g_return_if_fail(filter);
283
284         if (filter->properties) {
285                 g_hash_table_destroy (filter->properties);
286                 filter->properties = NULL;
287         }
288         g_slice_free(LocationPOIFilter, filter);
289 }