6c811b6206bf543792d4e66e44d375d4839f2bf1
[platform/core/location/lbs-location.git] / location / map-service / map-service.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-object.h>
27 #include <vconf.h>
28 #include "location-log.h"
29 #include "location-pref.h"
30
31 #include "module-internal.h"
32
33 #include "map-internal.h"
34 #include "map-service.h"
35 #include "location-ielement.h"
36
37 enum {
38         PROP_0,
39         PROP_SERVICE_NAME
40 };
41
42 static void map_service_ielement_interface_init (LocationIElementInterface *iface);
43
44 G_DEFINE_TYPE_WITH_CODE (MapService, map_service, G_TYPE_OBJECT,
45         G_IMPLEMENT_INTERFACE (LOCATION_TYPE_IELEMENT, map_service_ielement_interface_init));
46
47 static void
48 _get_lang (gchar country_code[3], gchar lang_code[3])
49 {
50        if (!country_code || !lang_code) return;
51        gchar* langset = vconf_get_str(VCONFKEY_LANGSET);
52        LOCATION_LOGD("getenv: %s", langset);
53
54        if(langset == NULL){
55               lang_code[0] = 'E';
56               lang_code[1] = 'N';
57               lang_code[2] = '\0';
58               country_code[0] = 'U';
59               country_code[1] = 'S';
60               country_code[2] = '\0';
61        }else{
62               gchar* langset_upper = g_ascii_strup(langset, -1);
63               lang_code[0] = langset_upper[0];
64               lang_code[1] = langset_upper[1];
65               lang_code[2] = '\0';
66               country_code[0] = langset_upper[3];
67               country_code[1] = langset_upper[4];
68               country_code[2] = '\0';
69               g_free(langset_upper);
70        }
71        LOCATION_LOGD("Language: %s, Country: %s", lang_code, country_code);
72 }
73
74
75 static void
76 map_service_dispose (GObject *gobject)
77 {
78         LOCATION_LOGD("map_service_dispose");
79
80         G_OBJECT_CLASS (map_service_parent_class)->dispose (gobject);
81 }
82
83 static void
84 map_service_finalize (GObject *gobject)
85 {
86         LOCATION_LOGD("map_service_finalize");
87         MapServicePrivate* priv = GET_PRIVATE(gobject);
88         module_free(priv->mod, "map-service");
89         priv->mod = NULL;
90         G_OBJECT_CLASS (map_service_parent_class)->finalize (gobject);
91 }
92
93 static void
94 map_service_get_property (GObject *object,
95         guint property_id,
96         GValue *value,
97         GParamSpec *pspec)
98 {
99         MapServicePrivate *priv = GET_PRIVATE (object);
100
101         g_return_if_fail(priv->mod);
102         g_return_if_fail(priv->mod->handler);
103         switch (property_id){
104         case PROP_SERVICE_NAME:{
105                 char* service_name = NULL;
106                 if(priv->mod->ops.get_service_name){
107                         if( LOCATION_ERROR_NONE != priv->mod->ops.get_service_name(priv->mod->handler, &service_name) ){
108                                 service_name = NULL;
109                         }
110                 }
111                 LOCATION_LOGD("Get prop>> Service name: %s", service_name);
112                 g_value_set_string(value, service_name);
113                 g_free(service_name);
114                 break;
115         }
116         default:
117                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118                 break;
119         }
120 }
121
122 static void
123 map_service_ielement_interface_init (LocationIElementInterface *iface)
124 {
125         iface->get_geocode = (TYPE_GET_GEOCODE)map_service_get_geocode;
126         iface->get_geocode_freeform = (TYPE_GET_GEOCODE_FREEFORM)map_service_get_geocode_freeform;
127         iface->get_reversegeocode = (TYPE_GET_REVERSEGEOCODE)map_service_get_reversegeocode;
128         iface->get_geocode_async = (TYPE_GET_GEOCODE_ASYNC)map_service_get_geocode_async;
129         iface->get_geocode_freeform_async = (TYPE_GET_GEOCODE_FREEFORM_ASYNC)map_service_get_geocode_freeform_async;
130         iface->get_reversegeocode_async = (TYPE_GET_REVERSEGEOCODE_ASYNC)map_service_get_reversegeocode_async;
131         iface->search_poi = (TYPE_SEARCH_POI) map_service_search_poi;
132         iface->search_poi_by_area = (TYPE_SEARCH_POI_BY_AREA) map_service_search_poi_by_area;
133         iface->search_poi_by_address = (TYPE_SEARCH_POI_BY_ADDR) map_service_search_poi_by_address;
134         iface->search_poi_by_freeform = (TYPE_SEARCH_POI_BY_FREEFORM) map_service_search_poi_by_freeform;
135         iface->cancel_poi_request = (TYPE_CANCEL_POI_REQUEST) map_service_cancel_poi_request;
136         iface->request_route = (TYPE_REQUEST_ROUTE) map_service_request_route;
137         iface->cancel_route_request = (TYPE_CANCEL_ROUTE_REQUEST) map_service_cancel_route_request;
138         iface->is_supported_map_provider_capability = (TYPE_IS_SUPPORTED_MAP_PROVIDER_CAPABILITY) map_service_is_supported_map_provider_capability;
139         iface->get_map_provider_capability_key = (TYPE_GET_MAP_PROVIDER_CAPABILITY_KEY) map_service_get_map_provider_capability_key;
140
141 }
142
143 static void
144 map_service_init (MapService *self)
145 {
146         LOCATION_LOGD("map_service_init");
147         MapServicePrivate* priv = GET_PRIVATE(self);
148
149         priv->mod = (LocationServiceMod*)module_new("map-service");
150         if(!priv->mod) LOCATION_LOGW("module loading failed");
151         priv->pref = location_pref_new ();
152         if(!priv->pref) LOCATION_LOGW("Service preference failed");
153
154         gchar country[3], lang[3];
155         _get_lang (country, lang);
156         location_pref_set_language (priv->pref, lang);
157         location_pref_set_distance_unit (priv->pref, "MI");
158         location_pref_set_provider_name (priv->pref, "decarta");
159 }
160
161 static void
162 map_service_class_init (MapServiceClass *klass)
163 {
164         LOCATION_LOGD("map_service_class_init");
165         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166         GParamSpec *pspec;
167
168         gobject_class->get_property = map_service_get_property;
169
170         gobject_class->dispose = map_service_dispose;
171         gobject_class->finalize = map_service_finalize;
172
173         g_type_class_add_private (klass, sizeof (MapServicePrivate));
174
175         pspec = g_param_spec_string ("service provider",
176                                 "map service provider name prop",
177                                 "map service provider name",
178                                 DEFAULT_SERVICE,
179                                 G_PARAM_READABLE);
180         g_object_class_install_property (gobject_class,
181                                    PROP_SERVICE_NAME,
182                                    pspec);
183 }
184
185 LocationPreference *
186 map_service_get_pref(GObject *obj)
187 {
188         LOCATION_LOGD("map_service_get_pref");
189         MapServicePrivate* priv = GET_PRIVATE(obj);
190         if (!priv) return NULL;
191         if (!priv->pref) return NULL;
192
193         LocationPreference *pref = location_pref_copy(priv->pref);
194
195         return pref;
196 }
197
198 gboolean
199 map_service_set_pref (GObject *obj, LocationPreference *pref)
200 {
201         LOCATION_LOGD("map_service_set_pref");
202         MapServicePrivate* priv = GET_PRIVATE(obj);
203         if (!priv) return FALSE;
204         if (!priv->pref) location_pref_free(priv->pref);
205
206         priv->pref = location_pref_copy (pref);
207
208         return TRUE;
209 }