Git init
[framework/location/libslp-location.git] / tests / address-sample.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 #include <gconf/gconf-client.h>
23 #include <location/location.h>
24
25 static GMainLoop *loop = NULL;
26
27 #define GCONF_PROXY_MODE            "/system/proxy/mode"
28 #define GCONF_HTTP_PROXY_HOST       "/system/http_proxy/host"
29 #define GCONF_HTTP_PROXY_PORT       "/system/http_proxy/port"
30 #define ENV_HTTP_PROXY              "http_proxy"
31
32 static gboolean
33 exit_program (gpointer data)
34 {
35         g_main_loop_quit (loop);
36         g_debug ("Quit g_main_loop");
37         return FALSE;
38 }
39
40 static void
41 cb_address (LocationError error,
42         LocationAddress *addr,
43         LocationAccuracy *acc,
44         gpointer userdata)
45 {
46         if (error != LOCATION_ERROR_NONE) {
47                 g_debug("cb_address failed: error=%d\n", error);
48                 return;
49         }
50         g_debug ("ASYNC>> location_get_address_async> %s %s %s %s %s %s %s",
51                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
52         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
53 }
54
55 static void
56 cb_service_disabled (GObject *self,
57         guint status,
58         gpointer userdata)
59 {
60         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
61 }
62
63 static void
64 cb_position_from_address (LocationError error, LocationPosition *pos, LocationAccuracy *acc, gpointer userdata)
65 {
66         if (error != LOCATION_ERROR_NONE) {
67                 g_debug("cb_position_from_address failed: error=%d\n", error);
68                 return;
69         }
70         g_debug ("ASYNC>> location_get_position_from_address_async> time: %d, lat: %f, long: %f, alt: %f, status: %d",
71                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
72         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
73 }
74
75 static void
76 cb_position_from_freeformed_address (LocationError error, LocationPosition *pos, LocationAccuracy *acc, gpointer userdata)
77 {
78         if (error != LOCATION_ERROR_NONE) {
79                 g_debug("cb_position_from_freeformed_address failed: error=%d\n", error);
80                 return;
81         }
82         g_debug ("ASYNC>> location_get_position_from_freeformed_address_async> time: %d, lat: %f, long: %f, alt: %f, status: %d",
83                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
84         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
85 }
86
87 static void
88 cb_address_from_position (LocationError error, LocationAddress *addr, LocationAccuracy *acc, gpointer userdata)
89 {
90         if (error != LOCATION_ERROR_NONE) {
91                 g_debug("cb_address_from_position failed: error=%d\n", error);
92                 return;
93         }
94         g_debug ("ASYNC>> location_get_address_from_position_async> %s %s %s %s %s %s %s",
95                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
96         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
97 }
98
99 static void
100 cb_service_enabled (GObject *self,
101         guint status,
102         gpointer userdata)
103 {
104         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
105
106         LocationAddress *addr = NULL;
107         LocationAccuracy *acc = NULL;
108         LocationObject *loc = (LocationObject*)userdata;
109
110         LocationError err = location_get_address(loc, &addr, &acc);
111         if (LOCATION_ERROR_NONE == err) {
112                 g_debug ("SYNC>> location_get_address() success> %s %s %s %s %s %s %s",
113                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
114                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
115                 location_address_free(addr);
116                 location_accuracy_free(acc);
117         } else g_warning ("SYNC>> location_get_address() failed> error code:%d", err);
118
119         err = location_get_address_async(loc, cb_address, loc);
120         if (LOCATION_ERROR_NONE == err) g_debug("location_get_address_async() success");
121         else g_warning ("location_get_address_async() failed> error code:%d", err);
122 }
123
124 static gboolean
125 async_request (gpointer loc)
126 {
127         LocationAddress *addr = location_address_new ("1", "Post Street", NULL, "san jose", "ca", NULL, "95113");
128         LocationError err = location_get_position_from_address_async(loc, addr, cb_position_from_address, loc);
129         if (LOCATION_ERROR_NONE == err)
130                 g_debug("location_get_position_from_address_async() success");
131         else g_warning ("location_get_position_from_address_async() failed> error code:%d", err);
132         location_address_free (addr);
133
134         gchar *addr_str = g_strdup("4 N 2nd Street 95113");
135         err = location_get_position_from_freeformed_address_async(loc, addr_str, cb_position_from_freeformed_address, loc);
136         if (LOCATION_ERROR_NONE == err)
137                 g_debug("location_get_position_from_freeformed_address_async() success");
138         else g_warning ("location_get_position_from_freeformed_address_async() failed> error code:%d", err);
139         g_free(addr_str);
140
141         LocationPosition *pos = location_position_new (0, 37.3322, -121.8720, 0, LOCATION_STATUS_2D_FIX);
142         err = location_get_address_from_position_async(loc, pos, cb_address_from_position, loc);
143         if (LOCATION_ERROR_NONE == err)
144                 g_debug("location_get_address_from_position_async() success");
145         else g_warning ("location_get_address_from_position_async() failed> error code:%d", err);
146         location_position_free (pos);
147         return FALSE;
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153         LocationObject *loc = NULL;
154
155         // If application is executed by AUL, this is not needed.
156         g_setenv("PKG_NAME", "org.tizen.address-sample", 1);
157
158         g_type_init();
159         location_init ();
160         loop = g_main_loop_new (NULL, TRUE);
161
162         loc  = location_new (LOCATION_METHOD_GPS);
163         if (!loc) {
164                 g_warning("location_new failed");
165                 return -1;
166         }
167         LocationPosition *pos = NULL;
168         LocationAccuracy *acc = NULL;
169         LocationAddress *addr = NULL;
170
171         addr = location_address_new ("1", "Post Street", NULL, "san jose", "ca", NULL, "95113");
172         LocationError err = location_get_position_from_address(loc, addr, &pos, &acc);
173         if (LOCATION_ERROR_NONE == err) {
174                 g_debug ("SYNC>> location_get_position_from_address() success> time: %d, lat: %f, long: %f, alt: %f, status: %d",
175                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
176                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
177                 location_position_free (pos);
178                 location_accuracy_free (acc);
179         } else g_warning ("SYNC>>>location_get_position_from_address() failed> error code:%d", err);
180         location_address_free (addr);
181
182         char* addr_str = g_strdup("4 N 2nd Street 95113");
183         err = location_get_position_from_freeformed_address(loc, addr_str, &pos, &acc);
184         if (LOCATION_ERROR_NONE == err) {
185                 g_debug ("SYNC>> location_get_position_from_freeformed_address() success> time: %d, lat: %f, long: %f, alt: %f, status: %d",
186                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
187                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
188                 location_position_free (pos);
189                 location_accuracy_free (acc);
190         } else g_warning ("SYNC>> location_get_position_from_freeformed_address() failed> error code:%d", err);
191         g_free(addr_str);
192
193         pos = location_position_new (0, 37.3322, -121.8720, 0, LOCATION_STATUS_2D_FIX);
194         err = location_get_address_from_position(loc, pos, &addr, &acc);
195         if (LOCATION_ERROR_NONE == err) {
196                 g_debug ("SYNC>> location_get_address_from_position() success> %s %s %s %s %s %s %s",
197                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
198                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
199                 location_address_free(addr);
200                 location_accuracy_free(acc);
201         } else g_warning ("SYNC>> location_get_address_from_position() failed> error code:%d", err);
202         location_position_free (pos);
203
204         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
205         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
206
207         g_timeout_add_seconds (3, async_request, loc);
208         if( LOCATION_ERROR_NONE != location_start (loc) ){
209                 g_debug("location_start failed");
210                 return -1;
211         }
212
213         g_timeout_add_seconds (60, exit_program, NULL);
214         g_main_loop_run (loop);
215
216         location_stop (loc);
217         location_free (loc);
218
219         return 0;
220 }