]> err.no Git - mapper/blob - src/mapper.c
Parse directly but update informations in callbacks only if we got a sentence that...
[mapper] / src / mapper.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * POI and GPS-Info code originally written by Cezary Jackiewicz.
8  *
9  * Default map data provided by http://www.openstreetmap.org/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <stddef.h>
32 #include <math.h>
33 #include <errno.h>
34 #include <sys/wait.h>
35 #include <glib/gstdio.h>
36 #include <glib/gi18n.h>
37 #ifdef WITH_GST
38 #include <gst/gst.h>
39 #endif
40 #include <gtk/gtk.h>
41 #include <fcntl.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <dbus/dbus.h>
44 #include <dbus/dbus-glib.h>
45 #include <libgnomevfs/gnome-vfs.h>
46 #include <curl/multi.h>
47 #include <gconf/gconf-client.h>
48 #include <libintl.h>
49 #include <locale.h>
50
51 #ifdef WITH_HILDON_DBUS_BT
52 #include <bt-dbus.h>
53 #endif
54
55 #include "mapper.h"
56 #include "hildon-mapper.h"
57 #include "utils.h"
58 #include "mapper-types.h"
59 #include "settings.h"
60 #include "gps.h"
61 #include "gps-conn.h"
62 #include "map.h"
63 #include "route.h"
64 #include "track.h"
65 #include "ui-common.h"
66 #include "db.h"
67 #include "osm-db.h"
68 #include "poi.h"
69 #include "cb.h"
70 #include "speak.h"
71 #include "gpx.h"
72 #include "config-gconf.h"
73
74 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
75
76 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
77         {0, 0x0000, 0x0000, 0xc000}
78         ,                       /* COLORABLE_MARK */
79         {0, 0x6000, 0x6000, 0xf800}
80         ,                       /* COLORABLE_MARK_VELOCITY */
81         {0, 0x8000, 0x8000, 0x8000}
82         ,                       /* COLORABLE_MARK_OLD */
83         {0, 0xe000, 0x0000, 0x0000}
84         ,                       /* COLORABLE_TRACK */
85         {0, 0xa000, 0x0000, 0x0000}
86         ,                       /* COLORABLE_TRACK_MARK */
87         {0, 0x7000, 0x0000, 0x0000}
88         ,                       /* COLORABLE_TRACK_BREAK */
89         {0, 0x0000, 0xa000, 0x0000}
90         ,                       /* COLORABLE_ROUTE */
91         {0, 0x0000, 0x8000, 0x0000}
92         ,                       /* COLORABLE_ROUTE_WAY */
93         {0, 0x0000, 0x6000, 0x0000}
94         ,                       /* COLORABLE_ROUTE_BREAK */
95         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
96 };
97
98 enum {
99         MAPPER_INIT_START=0,
100         MAPPER_INIT_MISC,
101         MAPPER_INIT_CONFIG,
102         MAPPER_INIT_DB,
103         MAPPER_INIT_POI,
104         MAPPER_INIT_OSM,
105         MAPPER_INIT_VOICE,
106         MAPPER_INIT_UI,
107         MAPPER_INIT_GOTO,
108         MAPPER_INIT_DONE
109 } mapper_init_state;
110
111 guint mis=MAPPER_INIT_START;
112
113 static GtkWidget *init_progress;
114 static GtkWidget *init_dialog;
115
116 /**
117  * Initialize arrays
118  */
119 static void 
120 variables_init(void)
121 {
122 UNITS_TEXT[UNITS_KM] = _("km");
123 UNITS_TEXT[UNITS_MI] = _("mi.");
124 UNITS_TEXT[UNITS_NM] = _("n.m.");
125
126 INFO_FONT_TEXT[INFO_FONT_XXSMALL] = "xx-small";
127 INFO_FONT_TEXT[INFO_FONT_XSMALL] = "x-small";
128 INFO_FONT_TEXT[INFO_FONT_SMALL] = "small";
129 INFO_FONT_TEXT[INFO_FONT_MEDIUM] = "medium";
130 INFO_FONT_TEXT[INFO_FONT_LARGE] = "large";
131 INFO_FONT_TEXT[INFO_FONT_XLARGE] = "x-large";
132 INFO_FONT_TEXT[INFO_FONT_XXLARGE] = "xx-large";
133
134 CUSTOM_KEY_GCONF[CUSTOM_KEY_UP] = GCONF_KEY_PREFIX "/key_up";
135 CUSTOM_KEY_GCONF[CUSTOM_KEY_DOWN] = GCONF_KEY_PREFIX "/key_down";
136 CUSTOM_KEY_GCONF[CUSTOM_KEY_LEFT] = GCONF_KEY_PREFIX "/key_left";
137 CUSTOM_KEY_GCONF[CUSTOM_KEY_RIGHT] = GCONF_KEY_PREFIX "/key_right";
138 CUSTOM_KEY_GCONF[CUSTOM_KEY_SELECT] = GCONF_KEY_PREFIX "/key_select";
139 CUSTOM_KEY_GCONF[CUSTOM_KEY_INCREASE] = GCONF_KEY_PREFIX "/key_increase";
140 CUSTOM_KEY_GCONF[CUSTOM_KEY_DECREASE] = GCONF_KEY_PREFIX "/key_decrease";
141 CUSTOM_KEY_GCONF[CUSTOM_KEY_FULLSCREEN] = GCONF_KEY_PREFIX "/key_fullscreen";
142 CUSTOM_KEY_GCONF[CUSTOM_KEY_ESC] = GCONF_KEY_PREFIX "/key_esc";
143
144 CUSTOM_KEY_ICON[CUSTOM_KEY_UP] = HWK_BUTTON_UP;
145 CUSTOM_KEY_ICON[CUSTOM_KEY_LEFT] = HWK_BUTTON_LEFT;
146 CUSTOM_KEY_ICON[CUSTOM_KEY_DOWN] = HWK_BUTTON_DOWN;
147 CUSTOM_KEY_ICON[CUSTOM_KEY_RIGHT] = HWK_BUTTON_RIGHT;
148 CUSTOM_KEY_ICON[CUSTOM_KEY_SELECT] = HWK_BUTTON_SELECT;
149 CUSTOM_KEY_ICON[CUSTOM_KEY_INCREASE] = HWK_BUTTON_INCREASE;
150 CUSTOM_KEY_ICON[CUSTOM_KEY_DECREASE] = HWK_BUTTON_DECREASE;
151 CUSTOM_KEY_ICON[CUSTOM_KEY_FULLSCREEN] = HWK_BUTTON_VIEW;
152 CUSTOM_KEY_ICON[CUSTOM_KEY_ESC] = HWK_BUTTON_CANCEL;
153
154 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_UP] = CUSTOM_ACTION_PAN_NORTH;
155 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_LEFT] = CUSTOM_ACTION_PAN_WEST;
156 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DOWN] = CUSTOM_ACTION_PAN_SOUTH;
157 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_RIGHT] = CUSTOM_ACTION_PAN_EAST;
158 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_SELECT] = CUSTOM_ACTION_TOGGLE_AUTOCENTER;
159 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_INCREASE] = CUSTOM_ACTION_ZOOM_IN;
160 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DECREASE] = CUSTOM_ACTION_ZOOM_OUT;
161 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_FULLSCREEN] = CUSTOM_ACTION_TOGGLE_FULLSCREEN;
162 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_ESC] = CUSTOM_ACTION_TOGGLE_TRACKS;
163
164 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_NORTH] = _("Pan North");
165 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_WEST] = _("Pan West");
166 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_SOUTH] = _("Pan South");
167 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_EAST] = _("Pan East");
168 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_AUTOCENTER] = _("Toggle Auto-Center");
169 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_FULLSCREEN] = _("Toggle Fullscreen");
170 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_IN] = _("Zoom In");
171 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_OUT] = _("Zoom Out");
172 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_TRACKS] = _("Toggle Tracks");
173 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SCALE] = _("Toggle Scale");
174 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_POI] = _("Toggle POIs");
175 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_CHANGE_REPO] = _("Select Next Repository");
176 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTNEXT] = _("Show Distance to Next Waypoint");
177 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTLAST] = _("Show Distance to End of Route");
178 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_BREAK] = _("Insert Track Break");
179 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTLAST] = _("Show Distance from Last Break");
180 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTFIRST] = _("Show Distance from Beginning");
181 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPS] = _("Toggle GPS");
182 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPSINFO] = _("Toggle GPS Info");
183 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SPEEDLIMIT] = _("Toggle Speed Limit");
184
185 COLORABLE_GCONF[COLORABLE_MARK] = GCONF_KEY_PREFIX "/color_mark";
186 COLORABLE_GCONF[COLORABLE_MARK_VELOCITY] = GCONF_KEY_PREFIX "/color_mark_velocity";
187 COLORABLE_GCONF[COLORABLE_MARK_OLD] = GCONF_KEY_PREFIX "/color_mark_old";
188 COLORABLE_GCONF[COLORABLE_TRACK] = GCONF_KEY_PREFIX "/color_track";
189 COLORABLE_GCONF[COLORABLE_TRACK_MARK] = GCONF_KEY_PREFIX "/color_track_mark";
190 COLORABLE_GCONF[COLORABLE_TRACK_BREAK] = GCONF_KEY_PREFIX "/color_track_break";
191 COLORABLE_GCONF[COLORABLE_ROUTE] = GCONF_KEY_PREFIX "/color_route";
192 COLORABLE_GCONF[COLORABLE_ROUTE_WAY] = GCONF_KEY_PREFIX "/color_route_way";
193 COLORABLE_GCONF[COLORABLE_ROUTE_BREAK] = GCONF_KEY_PREFIX "/color_route_break";
194 COLORABLE_GCONF[COLORABLE_POI] = GCONF_KEY_PREFIX "/color_poi";
195 }
196
197 /**
198  * Save state and destroy all non-UI elements created by this program in
199  * preparation for exiting.
200  */
201 static void 
202 mapper_destroy(void)
203 {
204 config_save();
205 config_save_repo();
206 map_download_deinit();
207 map_poi_deinit();
208 #if defined (WITH_GST) && defined (WITH_ESPEAK)
209 speak_deinit();
210 #endif
211 gps_disconnect(_gps);
212 gps_free(_gps);
213 osm_deinit();
214 db_close(&_db);
215 track_deinit();
216 route_deinit();
217
218 gnome_vfs_shutdown();
219 #ifdef WITH_OSSO
220 osso_deinitialize(_osso);
221 #endif
222 curl_global_cleanup();
223 }
224
225 static gint 
226 mapper_osso_init(void)
227 {
228 #ifdef WITH_OSSO
229 /* Initialize _osso. */
230 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
231 if (!_osso) {
232         g_printerr("osso_initialize failed.\n");
233         return 1;
234 }
235 #endif
236 return 0;
237 }
238
239 static gint 
240 mapper_osso_cb_init(void)
241 {
242 gchar *filter_string;
243
244 #ifdef WITH_OSSO
245 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
246         g_printerr("osso_rpc_set_default_cb_f failed.\n");
247         return 1;
248 }
249 #endif
250
251 #ifdef WITH_OSSO_IC
252 filter_string = g_strdup_printf("interface=%s", ICD_DBUS_INTERFACE);
253 /* add match */
254 dbus_bus_add_match(dbus_conn, filter_string, NULL);
255 g_free(filter_string);
256
257 /* add the callback */
258 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
259 osso_iap_cb(iap_callback);
260 #endif
261
262 return 0;
263 }
264
265 static void 
266 timezone_init(void)
267 {
268 time_t time1;
269 struct tm time2;
270
271 time1 = time(NULL);
272 localtime_r(&time1, &time2);
273 _gmtoffset = time2.tm_gmtoff;
274 }
275
276 static gboolean
277 mapper_init(gpointer data)
278 {
279 GError *error = NULL;
280 gboolean ret=TRUE;
281 gfloat p=0;
282 gchar *w="Starting";
283
284 switch (mis) {
285         case MAPPER_INIT_START:
286                 curl_global_init(CURL_GLOBAL_NOTHING);
287                 gnome_vfs_init();
288                 /* XXX: Load GPS configuration, then create gps */
289                 _gps=gps_new(GPS_IO_SIMULATION);
290                 _gps->connection_retry=gps_retry_connection;
291                 _gps->connection_error=NULL;
292                 _gps->connection_progress=gps_conn_set_progress;
293                 _gps->update_location=gps_location_update;
294                 _gps->update_info=gps_info_update;
295
296                 timezone_init();
297                 gpx_init();
298                 variables_init();
299                 latlon_init();
300                 track_init();
301                 route_init();
302                 mis=MAPPER_INIT_CONFIG;
303                 p=0.1;
304                 w="Init";
305         break;
306         case MAPPER_INIT_CONFIG:
307                 config_init();
308                 map_download_init();
309                 mis=MAPPER_INIT_DB;
310                 p=0.2;
311                 w="Config";
312         break;
313         case MAPPER_INIT_DB:
314                 if (db_connect(&_db, _mapper_db)) {
315                         mis=MAPPER_INIT_POI;
316                 } else {
317                         mis=MAPPER_INIT_VOICE;
318                 }
319                 p=0.5;
320                 w="Database";
321         break;
322         case MAPPER_INIT_POI:
323                 poi_init(&_db);
324                 mis=MAPPER_INIT_OSM;
325                 p=0.6;
326                 w="POIs";
327         break;
328         case MAPPER_INIT_OSM:
329                 osm_init(&_db);
330                 mis=MAPPER_INIT_VOICE;
331                 p=0.8;
332                 w="OSM";
333         break;
334         case MAPPER_INIT_VOICE:
335                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
336                 if (speak_init("en",_voice_speed,_voice_pitch)==FALSE) {
337                         g_printerr("Espeak init failed\n");
338                         popup_error(_window, "Speech init failed. Disabled.");
339                 }
340                 #endif
341                 mis=MAPPER_INIT_MISC;
342                 p=0.85;
343                 w="Speech";
344         break;
345         case MAPPER_INIT_MISC:
346                 /* Initialize D-Bus system connection. */
347                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
348                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
349                         popup_error(_window, "Failed to connect to D-Bus.");
350                         error = NULL;
351                 }
352
353                 /* XXX: Move this */
354                 #ifdef WITH_OSSO
355                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
356                 #endif
357
358                 mis=MAPPER_INIT_UI;
359                 p=0.9;
360                 w="Misc";
361         break;
362         case MAPPER_INIT_UI:
363                 mapper_ui_init();
364                 mis=MAPPER_INIT_GOTO;
365                 p=0.99;
366                 w="UI";
367         break;
368         case MAPPER_INIT_GOTO:
369                 if (map_goto_position(&_home)==FALSE)
370                         g_printerr("Home is not set.\n");
371                 else
372                         map_center_unit(_center.unitx, _center.unity);
373                 mis=MAPPER_INIT_DONE;
374                 p=1.0;
375                 w="Done";
376         break;
377         case MAPPER_INIT_DONE:
378                 progress_dialog_remove(init_dialog);
379                 if (_enable_gps)   
380                         gps_connect_now(_gps);
381                 return FALSE;
382         break;
383 }
384
385 gtk_progress_bar_set_fraction(GTK_PROGRESS(init_progress), p);
386 gtk_progress_bar_set_text(GTK_PROGRESS(init_progress), w);
387 return ret;
388 }
389
390 gint 
391 main(gint argc, gchar * argv[])
392 {
393 /* Initialize localization. */
394 setlocale(LC_ALL, "");
395 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
396 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
397 textdomain(GETTEXT_PACKAGE);
398
399 g_thread_init(NULL);
400 g_type_init();
401 g_set_application_name("Mapper");
402 gtk_init(&argc, &argv);
403 if (mapper_osso_init()!=0)
404         return 1;
405 if (mapper_osso_cb_init()!=0)
406         return 1;
407 #if defined (WITH_GST)
408 gst_init(&argc, &argv);
409 #endif
410
411 init_progress=gtk_progress_bar_new();
412 init_dialog=progress_dialog("Mapper is loading...",init_progress);
413 g_idle_add((GSourceFunc)mapper_init, NULL);
414 gtk_main();
415 mapper_destroy();
416
417 return 0;
418 }