]> err.no Git - mapper/blob - src/mapper.c
Add (untested) libconic support
[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 #define _GNU_SOURCE
27
28 #include <config.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <stddef.h>
34 #include <math.h>
35 #include <errno.h>
36 #include <sys/wait.h>
37 #include <glib/gstdio.h>
38 #include <glib/gi18n.h>
39 #ifdef WITH_GST
40 #include <gst/gst.h>
41 #endif
42 #include <gtk/gtk.h>
43 #include <fcntl.h>
44 #include <gdk/gdkkeysyms.h>
45 #include <dbus/dbus.h>
46 #include <dbus/dbus-glib.h>
47 #include <libgnomevfs/gnome-vfs.h>
48 #include <curl/multi.h>
49 #include <gconf/gconf-client.h>
50 #include <libintl.h>
51 #include <locale.h>
52
53 #ifdef WITH_HILDON_DBUS_BT
54 #include <bt-dbus.h>
55 #endif
56
57 #include "hildon-mapper.h"
58
59 #include "utils.h"
60 #include "mapper-types.h"
61 #include "settings.h"
62 #include "gps.h"
63 #include "map.h"
64 #include "route.h"
65 #include "track.h"
66 #include "bt.h"
67 #include "ui-common.h"
68 #include "db.h"
69 #include "osm-db.h"
70 #include "cb.h"
71 #include "speak.h"
72 #include "gpx.h"
73 #include "config-gconf.h"
74
75 DBusConnection *dbus_conn;
76
77 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
78
79 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
80         {0, 0x0000, 0x0000, 0xc000}
81         ,                       /* COLORABLE_MARK */
82         {0, 0x6000, 0x6000, 0xf800}
83         ,                       /* COLORABLE_MARK_VELOCITY */
84         {0, 0x8000, 0x8000, 0x8000}
85         ,                       /* COLORABLE_MARK_OLD */
86         {0, 0xe000, 0x0000, 0x0000}
87         ,                       /* COLORABLE_TRACK */
88         {0, 0xa000, 0x0000, 0x0000}
89         ,                       /* COLORABLE_TRACK_MARK */
90         {0, 0x7000, 0x0000, 0x0000}
91         ,                       /* COLORABLE_TRACK_BREAK */
92         {0, 0x0000, 0xa000, 0x0000}
93         ,                       /* COLORABLE_ROUTE */
94         {0, 0x0000, 0x8000, 0x0000}
95         ,                       /* COLORABLE_ROUTE_WAY */
96         {0, 0x0000, 0x6000, 0x0000}
97         ,                       /* COLORABLE_ROUTE_BREAK */
98         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
99 };
100
101 enum {
102         MAPPER_INIT_START=0,
103         MAPPER_INIT_MISC,
104         MAPPER_INIT_CONFIG,
105         MAPPER_INIT_GPS,
106         MAPPER_INIT_VOICE,
107         MAPPER_INIT_DB,
108         MAPPER_INIT_UI,
109         MAPPER_INIT_GOTO,
110         MAPPER_INIT_DONE
111 } mapper_init_state;
112
113 guint mis=MAPPER_INIT_START;
114
115 static GtkWidget *init_progress;
116 static GtkWidget *init_dialog;
117
118 /**
119  * Save state and destroy all non-UI elements created by this program in
120  * preparation for exiting.
121  */
122 static void 
123 mapper_destroy(void)
124 {
125 config_save();
126 config_save_repo();
127 rcvr_disconnect();
128 map_download_deinit();
129 if (_curl_sid) {
130         g_source_remove(_curl_sid);
131         _curl_sid = 0;
132 }
133 #if defined (WITH_GST) && defined (WITH_ESPEAK)
134 speak_deinit();
135 #endif
136 osm_deinit();
137 db_close(&_db);
138 track_deinit();
139 route_deinit();
140
141 gnome_vfs_shutdown();
142 #ifdef WITH_OSSO
143 osso_deinitialize(_osso);
144 #endif
145 curl_global_cleanup();
146 }
147
148 static gint 
149 mapper_osso_init(void)
150 {
151 #ifdef WITH_OSSO
152 /* Initialize _osso. */
153 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
154 if (!_osso) {
155         g_printerr("osso_initialize failed.\n");
156         return 1;
157 }
158 #endif
159 return 0;
160 }
161
162 static gint 
163 mapper_osso_cb_init(void)
164 {
165 gchar *filter_string;
166
167 #ifdef WITH_OSSO
168 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
169         g_printerr("osso_rpc_set_default_cb_f failed.\n");
170         return 1;
171 }
172
173 filter_string = g_strdup_printf("interface=%s", ICD_DBUS_INTERFACE);
174 /* add match */
175 dbus_bus_add_match(dbus_conn, filter_string, NULL);
176 g_free(filter_string);
177 #endif
178
179 /* add the callback */
180 #ifdef WITH_OSSO_IC
181 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
182 osso_iap_cb(iap_callback);
183 #endif
184
185 return 0;
186 }
187
188 static void 
189 timezone_init(void)
190 {
191 time_t time1;
192 struct tm time2;
193
194 time1 = time(NULL);
195 localtime_r(&time1, &time2);
196 _gmtoffset = time2.tm_gmtoff;
197 }
198
199 static gboolean
200 mapper_init(gpointer data)
201 {
202 GError *error = NULL;
203 gboolean ret=TRUE;
204
205 switch (mis) {
206         case MAPPER_INIT_START:
207                 _conn_state = RCVR_OFF;
208                 curl_global_init(CURL_GLOBAL_NOTHING);
209                 gnome_vfs_init();
210                 timezone_init();
211                 gpx_init();
212                 mapper_init_variables();
213                 track_init();
214                 route_init();
215                 mis=MAPPER_INIT_CONFIG;
216         break;
217         case MAPPER_INIT_CONFIG:
218                 config_init();
219                 map_download_init();
220                 mis=MAPPER_INIT_GPS;
221         break;
222         case MAPPER_INIT_GPS:
223                 gps_init();
224                 mis=MAPPER_INIT_DB;
225         break;
226         case MAPPER_INIT_DB:
227                 osm_init();
228                 mis=MAPPER_INIT_VOICE;
229         break;
230         case MAPPER_INIT_VOICE:
231                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
232                 speak_init("en",_voice_speed,_voice_pitch);
233                 #endif
234                 mis=MAPPER_INIT_MISC;
235         break;
236         case MAPPER_INIT_MISC:
237                 /* Initialize D-Bus system connection. */
238                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
239                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
240                         error = NULL;
241                 }
242
243                 /* XXX: Move this */
244                 #ifdef WITH_OSSO
245                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
246                 #endif
247
248                 #ifdef WITH_HILDON_DBUS_BT
249                 if (NULL == (_rfcomm_req_proxy = dbus_g_proxy_new_for_name(dbus_conn, BTCOND_SERVICE, BTCOND_REQ_PATH, BTCOND_REQ_INTERFACE))) {
250                         g_printerr("Failed to open connection to %s.\n", BTCOND_REQ_INTERFACE);
251                 }
252                 #endif
253                 mis=MAPPER_INIT_UI;
254         break;
255         case MAPPER_INIT_UI:
256                 mapper_ui_init();
257                 mis=MAPPER_INIT_GOTO;
258         break;
259         case MAPPER_INIT_GOTO:
260                 if (map_goto_position(&_home)==FALSE)
261                         g_printf("Home is not set\n");
262                 mis=MAPPER_INIT_DONE;
263         break;
264         case MAPPER_INIT_DONE:
265                 progress_dialog_remove(init_dialog);
266                 return FALSE;
267         break;
268 }
269
270 gtk_progress_bar_pulse(GTK_PROGRESS(init_progress));
271 return ret;
272 }
273
274 gint 
275 main(gint argc, gchar * argv[])
276 {
277 /* Initialize localization. */
278 setlocale(LC_ALL, "");
279 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
280 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
281 textdomain(GETTEXT_PACKAGE);
282
283 g_thread_init(NULL);
284 g_type_init();
285 g_set_application_name("Mapper");
286 gtk_init(&argc, &argv);
287 if (mapper_osso_init()!=0)
288         return 1;
289 if (mapper_osso_cb_init()!=0)
290         return 1;
291 #if defined (WITH_GST)
292 gst_init(&argc, &argv);
293 #endif
294
295 init_progress=gtk_progress_bar_new();
296 init_dialog=progress_dialog("Mapper starting...",init_progress);
297 g_idle_add((GSourceFunc)mapper_init, NULL);
298 gtk_main();
299 mapper_destroy();
300
301 return 0;
302 }