Tizen 2.1 base
[platform/core/location/location-module.git] / modules / osm / location-osm-poi.c
1 /*
2  * location-module
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *          Genie Kim <daejins.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 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <vconf.h>
27 #include <location.h>
28 #include <location-map-service.h>
29 #include <location-map-service-ext.h>
30 #include <geoclue/geoclue-poi.h>
31 #include <geoclue/geoclue-landmark.h>
32 #include "log.h"
33 #include "location-osm.h"
34 #include "location-osm-poi.h"
35
36 static int g_req_id = 0;
37
38 static void
39 _get_lang (gchar country_code[3], gchar lang_code[3])
40 {
41         if (!country_code || !lang_code) return;
42                 gchar* langset = vconf_get_str(VCONFKEY_LANGSET);
43                 MOD_LOGW("getenv: %s", langset);
44
45                 if(langset == NULL){
46                         lang_code[0] = 'E';
47                         lang_code[1] = 'N';
48                         lang_code[2] = '\0';
49                         country_code[0] = 'U';
50                         country_code[1] = 'S';
51                         country_code[2] = '\0';
52                 }else{
53                         gchar* langset_upper = g_ascii_strup(langset, -1);
54                         lang_code[0] = langset_upper[0];
55                         lang_code[1] = langset_upper[1];
56                         lang_code[2] = '\0';
57                         country_code[0] = langset_upper[3];
58                         country_code[1] = langset_upper[4];
59                         country_code[2] = '\0';
60                         g_free(langset_upper);
61                 }
62                 MOD_LOGW("Language: %s, Country: %s", lang_code, country_code);
63 }
64
65 static void
66 _free_landmark(gpointer data)
67 {
68         g_return_if_fail (data);
69
70         LocationLandmark *landmark = (LocationLandmark *)data;
71         location_landmark_free(landmark);
72 }
73
74
75 static void
76 _poi_callback(GeocluePoi *poi, int count, GPtrArray *landmark, GError *error, gpointer userdata)
77 {
78         OsmHandleData *data = (OsmHandleData *)userdata;
79
80         g_return_if_fail(data);
81         g_return_if_fail(data->poi_cb);
82
83         int req_id = data->req_id;
84         int err = LOCATION_ERROR_NONE;
85         GValueArray *vals = NULL;
86         GList *landmark_list = NULL;
87         LocationLandmark *land = NULL;
88         GeoclueLandmark landmark_data = {0, };
89         LocationBoundary *bounding_box = NULL;
90         LocationPosition *left_top = NULL;
91         LocationPosition *right_bottom = NULL;
92
93         int idx = 0;
94         if (!landmark || error) {
95                 MOD_LOGE("Landmark[%p] is NULL or Error occurs", landmark);
96                 err = LOCATION_ERROR_UNKNOWN;
97         }
98         else {
99                 MOD_LOGE("Count [%d]", count);
100                 for (; idx < count; idx++) {
101                         vals = (GValueArray *) g_ptr_array_index(landmark, idx);
102                         if (!vals) {
103                                 break;
104                         }
105
106                         landmark_data.id = g_value_get_int (g_value_array_get_nth(vals, 0));
107                         landmark_data.rank = g_value_get_int (g_value_array_get_nth(vals, 1));
108                         landmark_data.lat = g_value_get_double (g_value_array_get_nth(vals, 2));
109                         landmark_data.lon = g_value_get_double (g_value_array_get_nth(vals, 3));
110                         landmark_data.boundary_left = g_value_get_double (g_value_array_get_nth(vals, 4));
111                         landmark_data.boundary_top = g_value_get_double (g_value_array_get_nth(vals, 5));
112                         landmark_data.boundary_right = g_value_get_double (g_value_array_get_nth(vals, 6));
113                         landmark_data.boundary_bottom = g_value_get_double (g_value_array_get_nth(vals, 7));
114                         landmark_data.name = g_value_dup_string (g_value_array_get_nth(vals, 8));
115                         landmark_data.icon = g_value_dup_string (g_value_array_get_nth(vals, 9));
116                         landmark_data.house = g_value_dup_string (g_value_array_get_nth(vals, 10));
117                         landmark_data.road = g_value_dup_string (g_value_array_get_nth(vals, 11));
118                         landmark_data.village = g_value_dup_string (g_value_array_get_nth(vals, 12));
119                         landmark_data.suburb = g_value_dup_string (g_value_array_get_nth(vals, 13));
120                         landmark_data.postcode = g_value_dup_string (g_value_array_get_nth(vals, 14));
121                         landmark_data.city = g_value_dup_string (g_value_array_get_nth(vals, 15));
122                         landmark_data.county = g_value_dup_string (g_value_array_get_nth(vals, 16));
123                         landmark_data.country = g_value_dup_string (g_value_array_get_nth(vals, 17));
124                         landmark_data.country_code = g_value_dup_string (g_value_array_get_nth(vals, 18));
125
126                         land = location_landmark_new();
127
128                         location_landmark_set_id (land, (guint)landmark_data.id);
129                         location_landmark_set_priority(land, (guint)landmark_data.rank);
130                         location_landmark_set_position (land, location_position_new (0, landmark_data.lat, landmark_data.lon, 0.0, LOCATION_STATUS_2D_FIX));
131
132                         location_landmark_set_name (land, landmark_data.name);
133                         location_landmark_set_address (land, location_address_new (NULL, landmark_data.house, landmark_data.village, landmark_data.city, landmark_data.county, NULL, landmark_data.postcode, NULL, landmark_data.country_code, NULL));
134                         location_landmark_set_property (land, "ICON", landmark_data.icon);
135
136                         if (!landmark_data.boundary_left || landmark_data.boundary_top || landmark_data.boundary_right || landmark_data.boundary_bottom) {
137                                 left_top = location_position_new (0, landmark_data.boundary_left, landmark_data.boundary_top, 0.0, LOCATION_STATUS_2D_FIX);
138                                 right_bottom = location_position_new (0, landmark_data.boundary_right, landmark_data.boundary_bottom, 0.0, LOCATION_STATUS_2D_FIX);
139                                 bounding_box = location_boundary_new_for_rect (left_top, right_bottom);
140                                 if (bounding_box) {
141                                         location_landmark_set_bounding_box (land, bounding_box);
142                                         location_boundary_free(bounding_box);
143                                 }
144
145                                 if (left_top) location_position_free (left_top);
146                                 if (right_bottom) location_position_free (right_bottom);
147
148                                 left_top = NULL;
149                                 right_bottom = NULL;
150                                 bounding_box = NULL;
151                         }
152
153
154                         landmark_list = g_list_append (landmark_list, land);
155                 }
156         }
157
158         MOD_LOGW ("Calling LocationPOICB");
159         data->poi_cb (err, req_id, landmark_list, NULL, NULL, data->userdata);
160
161         g_list_free_full (landmark_list, _free_landmark);
162 }
163
164 int
165 osm_search_poi_by_position(OsmHandle *osm,
166                         const LocationPOIFilter         *filter,
167                         const LocationPosition          *position,
168                         const LocationMapPref   *svc_pref,
169                         const LocationPOIPreference     *pref,
170                         LocationPOICB                   cb,
171                         const gpointer                  user_data,
172                         guint                           *req_id)
173 {
174         int left = 0.0, top = 0.0, right = 0.0, bottom = 0.0;
175         *req_id = ++g_req_id;
176
177         OsmHandleData *data = g_new0 (OsmHandleData, 1);
178         data->req_id = *req_id;
179         data->poi_cb = cb;
180         data->userdata = user_data;
181
182         gchar *keyword = NULL, *lang = NULL, *country_code = NULL;
183         int max_result = 0;
184
185         keyword = location_poi_filter_get(filter, "KEYWORD");
186         if (!keyword) {
187                 MOD_LOGW("OSM supports only keyword filter.");
188                 return LOCATION_ERROR_PARAMETER;
189
190         }
191
192         lang = location_map_pref_get_language (svc_pref);
193         country_code = location_map_pref_get_country(svc_pref);
194         max_result = location_poi_pref_get_max_result(pref);
195
196         geoclue_poi_search_by_position_async (osm->poi, keyword, lang, country_code, max_result, left, top, right, bottom, _poi_callback, data);
197
198         return LOCATION_ERROR_NONE;
199 }
200
201 int
202 osm_search_poi_by_area(OsmHandle *osm,
203                         const LocationPOIFilter         *filter,
204                         const LocationBoundary          *boundary,
205                         const LocationMapPref   *svc_pref,
206                         const LocationPOIPreference     *pref,
207                         LocationPOICB                   cb,
208                         const gpointer                  user_data,
209                         guint                           *req_id)
210 {
211         *req_id = ++g_req_id;
212
213         OsmHandleData *data = g_new0 (OsmHandleData, 1);
214
215         gchar sys_country[3] = {0, }, sys_lang[3] = {0, };
216         gchar *keyword = NULL, *lang = NULL, *country_code = NULL;
217         int max_result = 0;
218         gdouble left = 0.0, top = 0.0, right = 0.0, bottom = 0.0;
219
220         data->req_id = *req_id;
221         data->poi_cb = cb;
222         data->userdata = user_data;
223
224         _get_lang (sys_country, sys_lang);
225         keyword = location_poi_filter_get(filter, "KEYWORD");
226         if (!keyword) {
227                 MOD_LOGW("OSM supports only keyword filter.");
228                 return LOCATION_ERROR_PARAMETER;
229
230         }
231
232         lang = location_map_pref_get_language (svc_pref);
233 //      if (!lang) lang = sys_lang;
234
235         country_code = location_map_pref_get_country(svc_pref);
236 //      if (!country_code) country_code = sys_country;
237
238         max_result = location_poi_pref_get_max_result(pref);
239
240         LocationPosition *left_top = boundary->rect.left_top;
241         LocationPosition *right_bottom = boundary->rect.right_bottom;
242
243         left = left_top->latitude;
244         top = left_top->longitude;
245         right = right_bottom->latitude;
246         bottom = right_bottom->longitude;
247
248         geoclue_poi_search_by_position_async (osm->poi, keyword, lang, country_code, max_result, left, top, right, bottom, _poi_callback, data);
249
250         return LOCATION_ERROR_NONE;
251 }