36b89f0c1572a03382552d0d82cf114a217f45ed
[platform/core/api/maps-service.git] / src / api / maps_place_filter.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include "maps_error.h"
19 #include "maps_place_filter.h"
20 #include "maps_extra_types.h"
21 #include "maps_util.h"
22 #include "maps_condition.h"
23
24 typedef struct _maps_place_filter_s
25 {
26         maps_item_hashtable_h table;
27 } maps_place_filter_s;
28
29 /*----------------------------------------------------------------------------*/
30
31 EXPORT_API int maps_place_filter_create(maps_place_filter_h *filter)
32 {
33         if (!maps_condition_check_maps_feature())
34                 return MAPS_ERROR_NOT_SUPPORTED;
35         if (!filter)
36                 return MAPS_ERROR_INVALID_PARAMETER;
37
38         *filter = (maps_place_filter_h) g_slice_new0(maps_place_filter_s);
39         if (*filter == NULL) {
40                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
41                 return MAPS_ERROR_OUT_OF_MEMORY;
42         }
43
44         maps_place_filter_s *f = (maps_place_filter_s *) (*filter);
45         maps_item_hashtable_create(&f->table);
46
47         return MAPS_ERROR_NONE;
48 }
49
50 EXPORT_API int maps_place_filter_destroy(maps_place_filter_h filter)
51 {
52         if (!maps_condition_check_maps_feature())
53                 return MAPS_ERROR_NOT_SUPPORTED;
54         if (!filter)
55                 return MAPS_ERROR_INVALID_PARAMETER;
56
57         maps_place_filter_s *f = (maps_place_filter_s *) filter;
58
59         if (f->table)
60                 maps_item_hashtable_destroy(f->table);
61
62         g_slice_free(maps_place_filter_s, filter);
63         return MAPS_ERROR_NONE;
64 }
65
66 EXPORT_API int maps_place_filter_clone(const maps_place_filter_h origin,
67                                                                 maps_place_filter_h *cloned)
68 {
69         if (!maps_condition_check_maps_feature())
70                 return MAPS_ERROR_NOT_SUPPORTED;
71         if (!cloned || !origin)
72                 return MAPS_ERROR_INVALID_PARAMETER;
73
74         int error = MAPS_ERROR_NONE;
75         do {
76                 error = maps_place_filter_create(cloned);
77                 if (!(*cloned) || (error != MAPS_ERROR_NONE))
78                         break;
79
80                 maps_place_filter_s *f = (maps_place_filter_s *) origin;
81
82                 if (f->table) {
83                         maps_place_filter_s *f_cloned =
84                                 (maps_place_filter_s *) (*cloned);
85                         if (f_cloned->table)
86                                 maps_item_hashtable_destroy(f_cloned->table);
87
88                         error = maps_item_hashtable_clone(f->table, &f_cloned->table);
89                         if (error != MAPS_ERROR_NONE)
90                                 break;
91                 }
92
93                 return MAPS_ERROR_NONE;
94         } while (false);
95
96         maps_place_filter_destroy(*cloned);
97         *cloned = NULL;
98         return error;
99 }
100
101 /*----------------------------------------------------------------------------*/
102
103 EXPORT_API int maps_place_filter_get(const maps_place_filter_h filter,
104                                                                 const char *key, char **value)
105 {
106         if (!maps_condition_check_maps_feature())
107                 return MAPS_ERROR_NOT_SUPPORTED;
108         if (!filter)
109                 return MAPS_ERROR_INVALID_PARAMETER;
110         if (!((maps_place_filter_s *) filter)->table)
111                 return MAPS_ERROR_NOT_FOUND;
112         return maps_item_hashtable_get_string(((maps_place_filter_s *) filter)->table,
113                 key, value);
114 }
115
116 EXPORT_API int maps_place_filter_foreach_property(const maps_place_filter_h filter,
117                                                                 maps_place_filter_properties_cb callback,
118                                                                 void *user_data)
119 {
120         if (!maps_condition_check_maps_feature())
121                 return MAPS_ERROR_NOT_SUPPORTED;
122         if (!filter || !callback)
123                 return MAPS_ERROR_INVALID_PARAMETER;
124         if (!((maps_place_filter_s *) filter)->table)
125                 return MAPS_ERROR_NOT_FOUND;
126         return maps_item_hashtable_foreach(((maps_place_filter_s *) filter)->table,
127                 callback, user_data);
128 }
129
130 EXPORT_API int maps_place_filter_get_keyword(const maps_place_filter_h filter,
131                                                                 char **keyword)
132 {
133         if (!maps_condition_check_maps_feature())
134                 return MAPS_ERROR_NOT_SUPPORTED;
135         if (!filter || !keyword)
136                 return MAPS_ERROR_INVALID_PARAMETER;
137         if (!((maps_place_filter_s *) filter)->table)
138                 return MAPS_ERROR_NOT_FOUND;
139         return maps_item_hashtable_get_string(((maps_place_filter_s *) filter)->table,
140                 "MAPS_PLACE_FILTER_KEYWORD", keyword);
141 }
142
143 EXPORT_API int maps_place_filter_get_place_name(const maps_place_filter_h filter,
144                                                                 char **place_name)
145 {
146         if (!maps_condition_check_maps_feature())
147                 return MAPS_ERROR_NOT_SUPPORTED;
148         if (!filter || !place_name)
149                 return MAPS_ERROR_INVALID_PARAMETER;
150         if (!((maps_place_filter_s *) filter)->table)
151                 return MAPS_ERROR_NOT_FOUND;
152         return maps_item_hashtable_get_string(((maps_place_filter_s *) filter)->table,
153                 "MAPS_PLACE_FILTER_PLACE_NAME", place_name);
154 }
155
156 EXPORT_API int maps_place_filter_get_category(const maps_place_filter_h filter,
157                                                                 maps_place_category_h *category)
158 {
159         if (!maps_condition_check_maps_feature())
160                 return MAPS_ERROR_NOT_SUPPORTED;
161         if (!filter || !category)
162                 return MAPS_ERROR_INVALID_PARAMETER;
163         if (!((maps_place_filter_s *) filter)->table)
164                 return MAPS_ERROR_NOT_FOUND;
165         return maps_item_hashtable_get(((maps_place_filter_s *) filter)->table,
166                 "MAPS_PLACE_FILTER_CATEGORY", (void **) category);
167 }
168
169 EXPORT_API int maps_place_filter_get_place_address(const maps_place_filter_h filter,
170                                                                 char **place_address)
171 {
172         if (!maps_condition_check_maps_feature())
173                 return MAPS_ERROR_NOT_SUPPORTED;
174         if (!filter || !place_address)
175                 return MAPS_ERROR_INVALID_PARAMETER;
176         if (!((maps_place_filter_s *) filter)->table)
177                 return MAPS_ERROR_NOT_FOUND;
178         return maps_item_hashtable_get_string(((maps_place_filter_s *) filter)->table,
179                 "MAPS_PLACE_FILTER_PLACE_ADDRESS", place_address);
180 }
181
182 /*----------------------------------------------------------------------------*/
183
184 EXPORT_API int maps_place_filter_set(maps_place_filter_h filter,
185                                                                 const char *key, const char *value)
186 {
187         if (!maps_condition_check_maps_feature())
188                 return MAPS_ERROR_NOT_SUPPORTED;
189         if (!filter)
190                 return MAPS_ERROR_INVALID_PARAMETER;
191         return maps_item_hashtable_set_string(((maps_place_filter_s *) filter)->table,
192                 key, value);
193 }
194
195 EXPORT_API int maps_place_filter_set_keyword(maps_place_filter_h filter,
196                                                                 const char *keyword)
197 {
198         if (!maps_condition_check_maps_feature())
199                 return MAPS_ERROR_NOT_SUPPORTED;
200         if (!filter || !keyword)
201                 return MAPS_ERROR_INVALID_PARAMETER;
202         return maps_item_hashtable_set_string(((maps_place_filter_s *) filter)->table,
203                 "MAPS_PLACE_FILTER_KEYWORD", keyword);
204 }
205
206 EXPORT_API int maps_place_filter_set_place_name(maps_place_filter_h filter,
207                                                                 const char *place_name)
208 {
209         if (!maps_condition_check_maps_feature())
210                 return MAPS_ERROR_NOT_SUPPORTED;
211         if (!filter || !place_name)
212                 return MAPS_ERROR_INVALID_PARAMETER;
213         return maps_item_hashtable_set_string(((maps_place_filter_s *) filter)->table,
214                 "MAPS_PLACE_FILTER_PLACE_NAME", place_name);
215 }
216
217 EXPORT_API int maps_place_filter_set_category(maps_place_filter_h filter,
218                                                                 const maps_place_category_h category)
219 {
220         if (!maps_condition_check_maps_feature())
221                 return MAPS_ERROR_NOT_SUPPORTED;
222         if (!filter || !category)
223                 return MAPS_ERROR_INVALID_PARAMETER;
224         return maps_item_hashtable_set(((maps_place_filter_s *) filter)->table,
225                 "MAPS_PLACE_FILTER_CATEGORY", (void **) category,
226                 maps_place_category_clone, maps_place_category_destroy);
227 }
228
229 EXPORT_API int maps_place_filter_set_place_address(maps_place_filter_h filter,
230                                                                 const char *place_address)
231 {
232         if (!maps_condition_check_maps_feature())
233                 return MAPS_ERROR_NOT_SUPPORTED;
234         if (!filter || !place_address)
235                 return MAPS_ERROR_INVALID_PARAMETER;
236         return maps_item_hashtable_set_string(((maps_place_filter_s *) filter)->table,
237                 "MAPS_PLACE_FILTER_PLACE_ADDRESS", place_address);
238 }