]> err.no Git - mapper/blob - src/mapper.c
Import fork of maemo-mapper.
[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 <locale.h>
36 #include <math.h>
37 #include <errno.h>
38 #include <sys/wait.h>
39 #include <glib/gstdio.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 #include <libxml/parser.h>
48
49 #ifdef WITH_HILDON
50 #include <libosso.h>
51 #include <osso-helplib.h>
52 #include <osso-ic-dbus.h>
53 #include <osso-ic.h>
54 #include <bt-dbus.h>
55 #include <hildon-widgets/hildon-program.h>
56 #include <hildon-widgets/hildon-controlbar.h>
57 #include <hildon-widgets/hildon-note.h>
58 #include <hildon-widgets/hildon-color-button.h>
59 #include <hildon-widgets/hildon-file-chooser-dialog.h>
60 #include <hildon-widgets/hildon-number-editor.h>
61 #include <hildon-widgets/hildon-banner.h>
62 #include <hildon-widgets/hildon-system-sound.h>
63 #include <hildon-widgets/hildon-input-mode-hint.h>
64 #include <device_symbols.h>
65 #endif
66
67 #include <libintl.h>
68 #include <locale.h>
69
70 #include <sqlite3.h>
71
72 #include "utils.h"
73 #include "poi.h"
74 #include "route.h"
75 #include "settings.h"
76 #include "gps.h"
77 #include "map.h"
78 #include "mapper-types.h"
79 #include "bt.h"
80 #include "ui-common.h"
81
82 /* Callbacks */
83 #include "cb.h"
84
85 gfloat UNITS_CONVERT[] = {
86         1.85200,
87         1.15077945,
88         1.f,
89 };
90
91 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
92         {0, 0x0000, 0x0000, 0xc000}
93         ,                       /* COLORABLE_MARK */
94         {0, 0x6000, 0x6000, 0xf800}
95         ,                       /* COLORABLE_MARK_VELOCITY */
96         {0, 0x8000, 0x8000, 0x8000}
97         ,                       /* COLORABLE_MARK_OLD */
98         {0, 0xe000, 0x0000, 0x0000}
99         ,                       /* COLORABLE_TRACK */
100         {0, 0xa000, 0x0000, 0x0000}
101         ,                       /* COLORABLE_TRACK_MARK */
102         {0, 0x7000, 0x0000, 0x0000}
103         ,                       /* COLORABLE_TRACK_BREAK */
104         {0, 0x0000, 0xa000, 0x0000}
105         ,                       /* COLORABLE_ROUTE */
106         {0, 0x0000, 0x8000, 0x0000}
107         ,                       /* COLORABLE_ROUTE_WAY */
108         {0, 0x0000, 0x6000, 0x0000}
109         ,                       /* COLORABLE_ROUTE_BREAK */
110         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
111 };
112
113 /**
114  * Set the connection state.  This function controls all connection-related
115  * banners.
116  */
117 void set_conn_state(ConnState new_conn_state)
118 {
119         printf("%s(%d)\n", __PRETTY_FUNCTION__, new_conn_state);
120
121         switch (_conn_state = new_conn_state) {
122         case RCVR_OFF:
123         case RCVR_FIXED:
124                 if (_connect_banner) {
125                         gtk_widget_destroy(_connect_banner);
126                         _connect_banner = NULL;
127                 }
128                 if (_fix_banner) {
129                         gtk_widget_destroy(_fix_banner);
130                         _fix_banner = NULL;
131                 }
132                 break;
133         case RCVR_DOWN:
134                 if (_fix_banner) {
135                         gtk_widget_destroy(_fix_banner);
136                         _fix_banner = NULL;
137                 }
138                 if (!_connect_banner)
139                         _connect_banner =
140                             hildon_banner_show_animation(_window, NULL,
141                                                          _
142                                                          ("Searching for GPS receiver"));
143                 break;
144         case RCVR_UP:
145                 if (_connect_banner) {
146                         gtk_widget_destroy(_connect_banner);
147                         _connect_banner = NULL;
148                 }
149                 if (!_fix_banner)
150                         _fix_banner =
151                             hildon_banner_show_progress(_window, NULL,
152                                                         _
153                                                         ("Establishing GPS fix"));
154                 break;
155         default:;               /* to quell warning. */
156         }
157
158         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
159 }
160
161 /**
162  * Save state and destroy all non-UI elements created by this program in
163  * preparation for exiting.
164  */
165 void maemo_mapper_destroy(void)
166 {
167         printf("%s()\n", __PRETTY_FUNCTION__);
168
169         if (_curl_sid) {
170                 g_source_remove(_curl_sid);
171                 _curl_sid = 0;
172         }
173         config_save();
174         rcvr_disconnect();
175         /* _program and widgets have already been destroyed. */
176
177         if (_db) {
178                 sqlite3_close(_db);
179                 _db = NULL;
180         }
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 main(gint argc, gchar * argv[])
237 {
238         printf("%s()\n", __PRETTY_FUNCTION__);
239
240         /* Initialize localization. */
241         setlocale(LC_ALL, "");
242         bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
243         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
244         textdomain(GETTEXT_PACKAGE);
245
246         g_thread_init(NULL);
247
248 #ifdef WITH_HILDON
249         /* Initialize _osso. */
250         _osso = osso_initialize("com.gnuite.maemo_mapper", VERSION, TRUE, NULL);
251         if (!_osso) {
252                 g_printerr("osso_initialize failed.\n");
253                 return 1;
254         }
255 #endif
256
257         set_var_defaults();
258
259         gtk_init(&argc, &argv);
260
261         /* Init gconf. */
262         g_type_init();
263         gconf_init(argc, argv, NULL);
264
265         /* Init Gnome-VFS. */
266         gnome_vfs_init();
267
268         /* Init libcurl. */
269         curl_global_init(CURL_GLOBAL_NOTHING);
270
271         maemo_mapper_init(argc, argv);
272
273 #ifdef WITH_HILDON
274         if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
275                 g_printerr("osso_rpc_set_default_cb_f failed.\n");
276                 return 1;
277         }
278 #endif
279
280         gtk_main();
281
282         maemo_mapper_destroy();
283
284 #ifdef WITH_HILDON
285         osso_deinitialize(_osso);
286 #endif
287
288         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
289         return 0;
290 }