merge from private
[platform/core/location/location-module.git] / modules / osm / location-osm.c
1 /*
2  * location-module
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 #include <glib.h>
23
24 #include <location.h>
25 #include <location-module.h>
26 #include <location-types.h>
27 #include <location-map-service.h>
28 #include <location-map-pref.h>
29 #include "location-osm-geocode.h"
30 #include "location-osm-route.h"
31 #include "location-osm-poi.h"
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include "log.h"
37
38 static void
39 _unref_geoclue(OsmHandle* handle)
40 {
41         if (handle->geocoder) {
42                 g_object_unref (handle->geocoder);
43                 handle->geocoder = NULL;
44         }
45         if (handle->rev_geocoder) {
46                 g_object_unref (handle->rev_geocoder);
47                 handle->rev_geocoder = NULL;
48         }
49         if (handle->poi) {
50                 g_object_unref (handle->poi);
51                 handle->poi = NULL;
52         }
53 }
54
55 static gboolean
56 _ref_geoclue(OsmHandle* handle)
57 {
58         gchar *service, *path;
59         service = g_strdup_printf ("org.freedesktop.Geoclue.Providers.Nominatim");
60         path = g_strdup_printf ("/org/freedesktop/Geoclue/Providers/Nominatim");
61
62         if (!handle->geocoder) handle->geocoder = geoclue_geocode_new (service, path);
63         if (!handle->rev_geocoder) handle->rev_geocoder = geoclue_reverse_geocode_new (service, path);
64         if (!handle->poi) handle->poi = geoclue_poi_new (service, path);
65
66         if(!handle->geocoder || !handle->rev_geocoder || !handle->poi){
67                 MOD_LOGW ("Error while creating Geoclue object.");
68                 _unref_geoclue(handle);
69                 return FALSE;
70         }
71         return TRUE;
72 }
73
74 static int
75 get_geocode (gpointer handle,
76         const LocationAddress *addr,
77         const LocationMapPref *svc_pref,
78         GList **pos_list,
79         GList **acc_list)
80 {
81         OsmHandle *osm = (OsmHandle *)handle;
82         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
83         g_return_val_if_fail(addr, LOCATION_ERROR_PARAMETER);
84         g_return_val_if_fail(pos_list, LOCATION_ERROR_PARAMETER);
85         g_return_val_if_fail(acc_list, LOCATION_ERROR_PARAMETER);
86
87         return osm_get_geocode(osm, addr, pos_list, acc_list);
88 }
89
90
91 static int
92 get_geocode_async (gpointer handle,
93         const LocationAddress *addr,
94         const LocationMapPref *svc_pref,
95         const LocationGeocodePreference *pref,
96         LocationPositionCB callback,
97         gpointer userdata,
98         guint *req_id)
99 {
100         OsmHandle *osm = (OsmHandle *)handle;
101         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
102         g_return_val_if_fail(addr, LOCATION_ERROR_PARAMETER);
103         g_return_val_if_fail(callback, LOCATION_ERROR_PARAMETER);
104         g_return_val_if_fail(pref, LOCATION_ERROR_PARAMETER);
105
106         //TODO: treat req_id to cancel async request.
107
108         return osm_get_geocode_async(osm, addr, callback, userdata);
109 }
110
111 static int
112 get_geocode_freetext(gpointer handle,
113         const gchar *addr,
114         const LocationMapPref *svc_pref,
115         GList **pos_list,
116         GList **acc_list)
117 {
118         OsmHandle *osm = (OsmHandle *)handle;
119         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
120         g_return_val_if_fail(addr, LOCATION_ERROR_PARAMETER);
121         g_return_val_if_fail(pos_list, LOCATION_ERROR_PARAMETER);
122         g_return_val_if_fail(acc_list, LOCATION_ERROR_PARAMETER);
123
124         return osm_get_geocode_freetext(osm, addr, pos_list, acc_list);
125 }
126
127
128 static int
129 get_geocode_freetext_async (gpointer handle,
130         const gchar* addr,
131         const LocationMapPref *svc_pref,
132         const LocationGeocodePreference *pref,
133         LocationPositionCB callback,
134         gpointer userdata,
135         guint *req_id)
136 {
137         OsmHandle *osm = (OsmHandle *)handle;
138         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
139         g_return_val_if_fail(addr, LOCATION_ERROR_PARAMETER);
140         g_return_val_if_fail(callback, LOCATION_ERROR_PARAMETER);
141
142         //TODO: treat GeocodePreference and req_id to cancel async request.
143
144         return osm_get_geocode_freetext_async(osm, addr, callback, userdata);
145 }
146
147 static int
148 get_reverse_geocode(gpointer handle,
149         const LocationPosition *pos,
150         const LocationMapPref *svc_pref,
151         LocationAddress **addr,
152         LocationAccuracy **acc)
153 {
154         OsmHandle *osm = (OsmHandle *)handle;
155         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
156         g_return_val_if_fail(pos, LOCATION_ERROR_PARAMETER);
157         g_return_val_if_fail(addr, LOCATION_ERROR_PARAMETER);
158         g_return_val_if_fail(acc, LOCATION_ERROR_PARAMETER);
159
160         return osm_get_reverse_geocode(osm, pos, addr, acc);
161 }
162
163 static int
164 get_reverse_geocode_async (gpointer handle,
165         const LocationPosition *pos,
166         const LocationMapPref *svc_pref,
167         LocationAddressCB callback,
168         gpointer userdata,
169         guint *req_id)
170 {
171         OsmHandle *osm = (OsmHandle *)handle;
172         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
173         g_return_val_if_fail(pos, LOCATION_ERROR_PARAMETER);
174         g_return_val_if_fail(callback, LOCATION_ERROR_PARAMETER);
175         g_return_val_if_fail(req_id, LOCATION_ERROR_PARAMETER);
176
177         //TODO: treat req_id to cancel async request.
178
179         return osm_get_reverse_geocode_async(osm, pos, callback, userdata);
180 }
181
182 static int
183 search_poi (gpointer handle,
184         const LocationPOIFilter *filter,
185         const LocationPosition *position,
186         const LocationMapPref *svc_pref,
187         const LocationPOIPreference *pref,
188         LocationPOICB cb, const gpointer user_data, guint * req_id)
189 {
190         OsmHandle *osm = (OsmHandle *)handle;
191         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
192         g_return_val_if_fail(filter, LOCATION_ERROR_PARAMETER);
193         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
194         g_return_val_if_fail(pref, LOCATION_ERROR_PARAMETER);
195         g_return_val_if_fail(cb, LOCATION_ERROR_PARAMETER);
196         g_return_val_if_fail(req_id, LOCATION_ERROR_PARAMETER);
197
198
199         return osm_search_poi_by_position(osm, filter, position, svc_pref, pref, cb, user_data, req_id);
200 }
201
202 static int
203 search_poi_by_area (gpointer handle,
204         const LocationPOIFilter *filter,
205         const LocationBoundary *boundary,
206         const LocationMapPref *svc_pref,
207         const LocationPOIPreference *pref,
208         LocationPOICB cb, const gpointer user_data, guint * req_id)
209 {
210         OsmHandle *osm = (OsmHandle *)handle;
211         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
212         g_return_val_if_fail(filter, LOCATION_ERROR_PARAMETER);
213         g_return_val_if_fail(boundary, LOCATION_ERROR_PARAMETER);
214         g_return_val_if_fail(pref, LOCATION_ERROR_PARAMETER);
215         g_return_val_if_fail(cb, LOCATION_ERROR_PARAMETER);
216         g_return_val_if_fail(req_id, LOCATION_ERROR_PARAMETER);
217         g_return_val_if_fail(boundary->type == LOCATION_BOUNDARY_RECT, LOCATION_ERROR_NOT_SUPPORTED);
218
219         return osm_search_poi_by_area(osm, filter, boundary, svc_pref, pref, cb, user_data, req_id);;
220 }
221
222 static int
223 search_poi_by_address (gpointer handle,
224         const LocationPOIFilter *filter,
225         const LocationAddress *address,
226         const LocationMapPref *svc_pref,
227         const LocationPOIPreference *pref,
228         LocationPOICB cb, const gpointer user_data, guint * req_id)
229 {
230         return LOCATION_ERROR_NOT_SUPPORTED;
231 }
232
233 static int
234 search_poi_by_freeform (gpointer handle,
235         const LocationPOIFilter * filter,
236         const gchar *freeform,
237         const LocationMapPref *svc_pref,
238         const LocationPOIPreference *pref,
239         LocationPOICB cb, const gpointer user_data, guint *req_id)
240 {
241         return LOCATION_ERROR_NOT_SUPPORTED;
242 }
243
244 static int
245 cancel_poi_request (gpointer handle, guint req_id)
246 {
247         return LOCATION_ERROR_NOT_SUPPORTED;
248 }
249
250 static int
251 request_route (gpointer handle,
252                 const LocationPosition *origin,
253                 const LocationPosition *destination,
254                 GList *waypoint,
255                 const LocationMapPref *svc_pref,
256                 const LocationRoutePreference *pref,
257                 LocationRouteCB cb, const gpointer user_data, guint * req_id)
258 {
259         OsmHandle *osm = (OsmHandle *)handle;
260         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
261
262         return osm_request_route(origin, destination, waypoint, svc_pref, pref, cb, user_data, req_id);
263 }
264
265 static int
266 cancel_route_request (gpointer handle, guint req_id)
267 {
268         OsmHandle *osm = (OsmHandle *) handle;
269         g_return_val_if_fail (osm, LOCATION_ERROR_NOT_AVAILABLE);
270
271         return osm_cancel_route_request (req_id);
272 }
273
274 static gboolean
275 is_supported_provider_capability (gpointer handle,
276                 LocationMapServiceType type)
277 {
278         return FALSE;
279 }
280
281 static int
282 get_provider_capability_key (gpointer handle,
283                 LocationMapServiceType type, GList **key)
284 {
285         g_return_val_if_fail (key, LOCATION_ERROR_PARAMETER);
286         *key = NULL;
287         return LOCATION_ERROR_NOT_SUPPORTED;
288 }
289
290 static int
291 get_service_name (gpointer handle,
292         gchar **service_name)
293 {
294         OsmHandle *osm = (OsmHandle *)handle;
295         g_return_val_if_fail(osm, LOCATION_ERROR_NOT_AVAILABLE);
296         g_return_val_if_fail (service_name, LOCATION_ERROR_PARAMETER);
297         *service_name = g_strdup (osm->service_name);
298         return LOCATION_ERROR_NONE;
299 }
300
301 LOCATION_MODULE_API gpointer
302 init (LocModServiceOps* ops)
303 {
304         MOD_LOGD("init");
305         g_return_val_if_fail(ops, NULL);
306         OsmHandle *handle = g_new0 (OsmHandle, 1);
307         if (!_ref_geoclue(handle)) {
308                 g_free (handle);
309                 return NULL;
310         }
311         handle->service_name = g_strdup ("osm");
312         ops->get_service_name = get_service_name;
313         ops->get_geocode = get_geocode;
314         ops->get_geocode_freetext = get_geocode_freetext;
315         ops->get_reverse_geocode = get_reverse_geocode;
316         ops->get_geocode_async = get_geocode_async;
317         ops->get_geocode_freetext_async = get_geocode_freetext_async;
318         ops->get_reverse_geocode_async = get_reverse_geocode_async;
319         ops->search_poi = search_poi;
320         ops->search_poi_by_area = search_poi_by_area;
321         ops->search_poi_by_address = search_poi_by_address;
322         ops->search_poi_by_freeform = search_poi_by_freeform;
323         ops->cancel_poi_request = cancel_poi_request;
324         ops->request_route = request_route;
325         ops->cancel_route_request = cancel_route_request;
326         ops->is_supported_provider_capability = is_supported_provider_capability;
327         ops->get_provider_capability_key = get_provider_capability_key;
328
329         return (gpointer)handle;
330 }
331
332 LOCATION_MODULE_API void
333 shutdown (gpointer handle)
334 {
335         MOD_LOGD("shutdown");
336         g_return_if_fail(handle);
337         OsmHandle *osm = (OsmHandle *)handle;
338         g_free (osm->service_name);
339         _unref_geoclue(osm);
340         g_free (osm);
341 }