]> err.no Git - mapper/blob - src/mapper.c
Call osm_init/osm_deinit
[mapper] / src / mapper.c
1 /*
2  * This file is part of maemo-mapper
3  *
4  * Copyright (C) 2006-2007 John Costigan.
5  *
6  * POI and GPS-Info code originally written by Cezary Jackiewicz.
7  *
8  * Default map data provided by http://www.openstreetmap.org/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #define _GNU_SOURCE
26
27 #define _(String) gettext(String)
28
29 #include <config.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <stddef.h>
35 #include <math.h>
36 #include <errno.h>
37 #include <sys/wait.h>
38 #include <glib/gstdio.h>
39 #include <gst/gst.h>
40 #include <gtk/gtk.h>
41 #include <fcntl.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <dbus/dbus-glib.h>
44 #include <libgnomevfs/gnome-vfs.h>
45 #include <curl/multi.h>
46 #include <gconf/gconf-client.h>
47
48 #ifdef WITH_HILDON
49 #include <libosso.h>
50 #include <osso-helplib.h>
51 #include <osso-ic-dbus.h>
52 #include <osso-ic.h>
53 #include <bt-dbus.h>
54 #include <hildon-widgets/hildon-program.h>
55 #include <hildon-widgets/hildon-controlbar.h>
56 #include <hildon-widgets/hildon-note.h>
57 #include <hildon-widgets/hildon-color-button.h>
58 #include <hildon-widgets/hildon-file-chooser-dialog.h>
59 #include <hildon-widgets/hildon-number-editor.h>
60 #include <hildon-widgets/hildon-banner.h>
61 #include <hildon-widgets/hildon-system-sound.h>
62 #include <hildon-widgets/hildon-input-mode-hint.h>
63 #include <device_symbols.h>
64 #include "maemo-osso.h"
65 #endif
66
67 #include <libintl.h>
68 #include <locale.h>
69
70 #include "utils.h"
71 #include "poi.h"
72 #include "route.h"
73 #include "mapper-types.h"
74 #include "settings.h"
75 #include "gps.h"
76 #include "map.h"
77 #include "bt.h"
78 #include "ui-common.h"
79 #include "db.h"
80 #include "cb.h"
81
82 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
83
84 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
85         {0, 0x0000, 0x0000, 0xc000}
86         ,                       /* COLORABLE_MARK */
87         {0, 0x6000, 0x6000, 0xf800}
88         ,                       /* COLORABLE_MARK_VELOCITY */
89         {0, 0x8000, 0x8000, 0x8000}
90         ,                       /* COLORABLE_MARK_OLD */
91         {0, 0xe000, 0x0000, 0x0000}
92         ,                       /* COLORABLE_TRACK */
93         {0, 0xa000, 0x0000, 0x0000}
94         ,                       /* COLORABLE_TRACK_MARK */
95         {0, 0x7000, 0x0000, 0x0000}
96         ,                       /* COLORABLE_TRACK_BREAK */
97         {0, 0x0000, 0xa000, 0x0000}
98         ,                       /* COLORABLE_ROUTE */
99         {0, 0x0000, 0x8000, 0x0000}
100         ,                       /* COLORABLE_ROUTE_WAY */
101         {0, 0x0000, 0x6000, 0x0000}
102         ,                       /* COLORABLE_ROUTE_BREAK */
103         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
104 };
105
106 /**
107  * Set the connection state.  This function controls all connection-related
108  * banners.
109  */
110 void set_conn_state(ConnState new_conn_state)
111 {
112         printf("%s(%d)\n", __PRETTY_FUNCTION__, new_conn_state);
113
114         switch (_conn_state = new_conn_state) {
115         case RCVR_OFF:
116         case RCVR_FIXED:
117 #ifdef WITH_HILDON
118                 if (_connect_banner) {
119                         gtk_widget_destroy(_connect_banner);
120                         _connect_banner = NULL;
121                 }
122                 if (_fix_banner) {
123                         gtk_widget_destroy(_fix_banner);
124                         _fix_banner = NULL;
125                 }
126 #else
127
128 #endif
129                 break;
130         case RCVR_DOWN:
131 #ifdef WITH_HILDON
132                 if (_fix_banner) {
133                         gtk_widget_destroy(_fix_banner);
134                         _fix_banner = NULL;
135                 }
136                 if (!_connect_banner)
137                         _connect_banner = hildon_banner_show_animation(_window, NULL,           
138                                          _("Searching for GPS receiver"));
139 #else
140
141 #endif
142                 break;
143         case RCVR_UP:
144 #ifdef WITH_HILDON
145                 if (_connect_banner) {
146                         gtk_widget_destroy(_connect_banner);
147                         _connect_banner = NULL;
148                 }
149                 if (!_fix_banner)
150                         _fix_banner = hildon_banner_show_progress(_window, NULL,
151                                                         _("Establishing GPS fix"));
152 #else
153
154 #endif
155                 break;
156         default:;               /* to quell warning. */
157         }
158
159         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
160 }
161
162 /**
163  * Save state and destroy all non-UI elements created by this program in
164  * preparation for exiting.
165  */
166 void mapper_destroy(void)
167 {
168         printf("%s()\n", __PRETTY_FUNCTION__);
169
170         if (_curl_sid) {
171                 g_source_remove(_curl_sid);
172                 _curl_sid = 0;
173         }
174         config_save();
175         config_save_repo();
176         rcvr_disconnect();
177         /* _program and widgets have already been destroyed. */
178
179         osm_deinit();
180         db_close(&_db);
181
182         MACRO_PATH_FREE(_track);
183         MACRO_PATH_FREE(_route);
184
185         /* Clean up CURL. */
186         if (_curl_multi) {
187                 CURL *curr;
188                 CURLMsg *msg;
189                 gint num_transfers, num_msgs;
190
191                 /* First, remove all downloads from _pui_tree. */
192                 g_tree_destroy(_pui_tree);
193
194                 /* Finish up all downloads. */
195                 while (CURLM_CALL_MULTI_PERFORM
196                        == curl_multi_perform(_curl_multi, &num_transfers)
197                        || num_transfers) {
198                 }
199
200                 /* Close all finished files. */
201                 while ((msg = curl_multi_info_read(_curl_multi, &num_msgs))) {
202                         if (msg->msg == CURLMSG_DONE) {
203                                 /* This is a map download. */
204                                 ProgressUpdateInfo *pui =
205                                     g_hash_table_lookup(_pui_by_easy,
206                                                         msg->easy_handle);
207                                 g_queue_push_head(_curl_easy_queue,
208                                                   msg->easy_handle);
209                                 g_hash_table_remove(_pui_by_easy,
210                                                     msg->easy_handle);
211                                 fclose(pui->file);
212                                 curl_multi_remove_handle(_curl_multi,
213                                                          msg->easy_handle);
214                         }
215                 }
216
217                 while ((curr = g_queue_pop_tail(_curl_easy_queue)))
218                         curl_easy_cleanup(curr);
219
220                 curl_multi_cleanup(_curl_multi);
221                 _curl_multi = NULL;
222
223                 g_queue_free(_curl_easy_queue);
224                 g_tree_destroy(_downloading_tree);
225                 g_hash_table_destroy(_pui_by_easy);
226         }
227
228         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
229 }
230
231 void set_var_defaults(void)
232 {
233 _conn_state = RCVR_OFF;
234 }
235
236 gint mapper_osso_init(void)
237 {
238 #ifdef WITH_HILDON
239 /* Initialize _osso. */
240 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
241 if (!_osso) {
242         g_printerr("osso_initialize failed.\n");
243         return 1;
244 }
245 #endif
246 return 0;
247 }
248
249 gint mapper_osso_cb_init(void)
250 {
251 #ifdef WITH_HILDON
252 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
253         g_printerr("osso_rpc_set_default_cb_f failed.\n");
254         return 1;
255 }
256 #endif
257 return 0;
258 }
259
260 void timezone_init(void)
261 {
262 time_t time1;
263 struct tm time2;
264
265 time1 = time(NULL);
266 localtime_r(&time1, &time2);
267 _gmtoffset = time2.tm_gmtoff;
268 }
269
270 gint main(gint argc, gchar * argv[])
271 {
272         printf("%s()\n", __PRETTY_FUNCTION__);
273
274         /* Initialize localization. */
275         setlocale(LC_ALL, "");
276         bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
277         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
278         textdomain(GETTEXT_PACKAGE);
279
280         g_thread_init(NULL);
281         g_type_init();
282         if (mapper_osso_init()!=0)
283                 return 1;
284         set_var_defaults();     
285         gst_init(&argc, &argv);
286         gtk_init(&argc, &argv);
287         gconf_init(argc, argv, NULL);
288         gnome_vfs_init();
289         curl_global_init(CURL_GLOBAL_NOTHING);
290         timezone_init();
291         gpx_init();
292         mapper_init(argc, argv);
293         if (mapper_osso_cb_init()!=0)
294                 return 1;
295         gtk_main();
296         mapper_destroy();
297 #ifdef WITH_HILDON
298         osso_deinitialize(_osso);
299 #endif
300         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
301         return 0;
302 }