]> err.no Git - mapper/blob - src/mapper.c
Use common dbus system connection
[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 "mapper.h"
58 #include "hildon-mapper.h"
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 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
76
77 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
78         {0, 0x0000, 0x0000, 0xc000}
79         ,                       /* COLORABLE_MARK */
80         {0, 0x6000, 0x6000, 0xf800}
81         ,                       /* COLORABLE_MARK_VELOCITY */
82         {0, 0x8000, 0x8000, 0x8000}
83         ,                       /* COLORABLE_MARK_OLD */
84         {0, 0xe000, 0x0000, 0x0000}
85         ,                       /* COLORABLE_TRACK */
86         {0, 0xa000, 0x0000, 0x0000}
87         ,                       /* COLORABLE_TRACK_MARK */
88         {0, 0x7000, 0x0000, 0x0000}
89         ,                       /* COLORABLE_TRACK_BREAK */
90         {0, 0x0000, 0xa000, 0x0000}
91         ,                       /* COLORABLE_ROUTE */
92         {0, 0x0000, 0x8000, 0x0000}
93         ,                       /* COLORABLE_ROUTE_WAY */
94         {0, 0x0000, 0x6000, 0x0000}
95         ,                       /* COLORABLE_ROUTE_BREAK */
96         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
97 };
98
99 enum {
100         MAPPER_INIT_START=0,
101         MAPPER_INIT_MISC,
102         MAPPER_INIT_CONFIG,
103         MAPPER_INIT_GPS,
104         MAPPER_INIT_VOICE,
105         MAPPER_INIT_DB,
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  * Save state and destroy all non-UI elements created by this program in
118  * preparation for exiting.
119  */
120 static void 
121 mapper_destroy(void)
122 {
123 config_save();
124 config_save_repo();
125 rcvr_disconnect();
126 map_download_deinit();
127 if (_curl_sid) {
128         g_source_remove(_curl_sid);
129         _curl_sid = 0;
130 }
131 #if defined (WITH_GST) && defined (WITH_ESPEAK)
132 speak_deinit();
133 #endif
134 osm_deinit();
135 db_close(&_db);
136 track_deinit();
137 route_deinit();
138
139 gnome_vfs_shutdown();
140 #ifdef WITH_OSSO
141 osso_deinitialize(_osso);
142 #endif
143 curl_global_cleanup();
144 }
145
146 static gint 
147 mapper_osso_init(void)
148 {
149 #ifdef WITH_OSSO
150 /* Initialize _osso. */
151 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
152 if (!_osso) {
153         g_printerr("osso_initialize failed.\n");
154         return 1;
155 }
156 #endif
157 return 0;
158 }
159
160 static gint 
161 mapper_osso_cb_init(void)
162 {
163 gchar *filter_string;
164
165 #ifdef WITH_OSSO
166 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
167         g_printerr("osso_rpc_set_default_cb_f failed.\n");
168         return 1;
169 }
170 #endif
171
172 #ifdef WITH_OSSO_IC
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
178 /* add the callback */
179 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
180 osso_iap_cb(iap_callback);
181 #endif
182
183 return 0;
184 }
185
186 static void 
187 timezone_init(void)
188 {
189 time_t time1;
190 struct tm time2;
191
192 time1 = time(NULL);
193 localtime_r(&time1, &time2);
194 _gmtoffset = time2.tm_gmtoff;
195 }
196
197 static gboolean
198 mapper_init(gpointer data)
199 {
200 GError *error = NULL;
201 gboolean ret=TRUE;
202
203 switch (mis) {
204         case MAPPER_INIT_START:
205                 _conn_state = RCVR_OFF;
206                 curl_global_init(CURL_GLOBAL_NOTHING);
207                 gnome_vfs_init();
208                 timezone_init();
209                 gpx_init();
210                 mapper_init_variables();
211                 track_init();
212                 route_init();
213                 mis=MAPPER_INIT_CONFIG;
214         break;
215         case MAPPER_INIT_CONFIG:
216                 config_init();
217                 map_download_init();
218                 mis=MAPPER_INIT_GPS;
219         break;
220         case MAPPER_INIT_GPS:
221                 gps_init();
222                 mis=MAPPER_INIT_DB;
223         break;
224         case MAPPER_INIT_DB:
225                 osm_init();
226                 mis=MAPPER_INIT_VOICE;
227         break;
228         case MAPPER_INIT_VOICE:
229                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
230                 if (speak_init("en",_voice_speed,_voice_pitch)==FALSE) {
231                         g_printerr("Espeak init failed\n");
232                         popup_error(_window, "Speech init failed. Disabled.");
233                 }
234                 #endif
235                 mis=MAPPER_INIT_MISC;
236         break;
237         case MAPPER_INIT_MISC:
238                 /* Initialize D-Bus system connection. */
239                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
240                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
241                         error = NULL;
242                 }
243
244                 /* XXX: Move this */
245                 #ifdef WITH_OSSO
246                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
247                 #endif
248
249                 #ifdef WITH_HILDON_DBUS_BT
250                 if (NULL == (_rfcomm_req_proxy = dbus_g_proxy_new_for_name(dbus_conn, BTCOND_SERVICE, BTCOND_REQ_PATH, BTCOND_REQ_INTERFACE))) {
251                         g_printerr("Failed to open connection to %s.\n", BTCOND_REQ_INTERFACE);
252                         popup_error(_window, "Bluetooth connection handling failed.");
253                 }
254                 #endif
255                 mis=MAPPER_INIT_UI;
256         break;
257         case MAPPER_INIT_UI:
258                 mapper_ui_init();
259                 mis=MAPPER_INIT_GOTO;
260         break;
261         case MAPPER_INIT_GOTO:
262                 if (map_goto_position(&_home)==FALSE)
263                         g_printerr("Home is not set.\n");
264                 mis=MAPPER_INIT_DONE;
265         break;
266         case MAPPER_INIT_DONE:
267                 progress_dialog_remove(init_dialog);
268                 return FALSE;
269         break;
270 }
271
272 gtk_progress_bar_pulse(GTK_PROGRESS(init_progress));
273 return ret;
274 }
275
276 gint 
277 main(gint argc, gchar * argv[])
278 {
279 /* Initialize localization. */
280 setlocale(LC_ALL, "");
281 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
282 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
283 textdomain(GETTEXT_PACKAGE);
284
285 g_thread_init(NULL);
286 g_type_init();
287 g_set_application_name("Mapper");
288 gtk_init(&argc, &argv);
289 if (mapper_osso_init()!=0)
290         return 1;
291 if (mapper_osso_cb_init()!=0)
292         return 1;
293 #if defined (WITH_GST)
294 gst_init(&argc, &argv);
295 #endif
296
297 init_progress=gtk_progress_bar_new();
298 init_dialog=progress_dialog("Mapper is loading...",init_progress);
299 g_idle_add((GSourceFunc)mapper_init, NULL);
300 gtk_main();
301 mapper_destroy();
302
303 return 0;
304 }