]> err.no Git - mapper/blob - src/mapper.c
Merge branch 'master' of git+ssh://tal.org/home/git/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 <math.h>
36 #include <errno.h>
37 #include <sys/wait.h>
38 #include <glib/gstdio.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-glib.h>
46 #include <libgnomevfs/gnome-vfs.h>
47 #include <curl/multi.h>
48 #include <gconf/gconf-client.h>
49 #include <libintl.h>
50 #include <locale.h>
51
52 #include "hildon-mapper.h"
53
54 #include "utils.h"
55 #include "mapper-types.h"
56 #include "settings.h"
57 #include "gps.h"
58 #include "map.h"
59 #include "route.h"
60 #include "track.h"
61 #include "bt.h"
62 #include "ui-common.h"
63 #include "db.h"
64 #include "osm-db.h"
65 #include "cb.h"
66 #include "speak.h"
67
68 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
69
70 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
71         {0, 0x0000, 0x0000, 0xc000}
72         ,                       /* COLORABLE_MARK */
73         {0, 0x6000, 0x6000, 0xf800}
74         ,                       /* COLORABLE_MARK_VELOCITY */
75         {0, 0x8000, 0x8000, 0x8000}
76         ,                       /* COLORABLE_MARK_OLD */
77         {0, 0xe000, 0x0000, 0x0000}
78         ,                       /* COLORABLE_TRACK */
79         {0, 0xa000, 0x0000, 0x0000}
80         ,                       /* COLORABLE_TRACK_MARK */
81         {0, 0x7000, 0x0000, 0x0000}
82         ,                       /* COLORABLE_TRACK_BREAK */
83         {0, 0x0000, 0xa000, 0x0000}
84         ,                       /* COLORABLE_ROUTE */
85         {0, 0x0000, 0x8000, 0x0000}
86         ,                       /* COLORABLE_ROUTE_WAY */
87         {0, 0x0000, 0x6000, 0x0000}
88         ,                       /* COLORABLE_ROUTE_BREAK */
89         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
90 };
91
92
93 /**
94  * Save state and destroy all non-UI elements created by this program in
95  * preparation for exiting.
96  */
97 void mapper_destroy(void)
98 {
99         printf("%s()\n", __PRETTY_FUNCTION__);
100
101         if (_curl_sid) {
102                 g_source_remove(_curl_sid);
103                 _curl_sid = 0;
104         }
105         config_save();
106         config_save_repo();
107         rcvr_disconnect();
108         /* _program and widgets have already been destroyed. */
109
110         osm_deinit();
111         db_close(&_db);
112
113         MACRO_PATH_FREE(_track);
114         MACRO_PATH_FREE(_route);
115
116         /* Clean up CURL. */
117         if (_curl_multi) {
118                 CURL *curr;
119                 CURLMsg *msg;
120                 gint num_transfers, num_msgs;
121
122                 /* First, remove all downloads from _pui_tree. */
123                 g_tree_destroy(_pui_tree);
124
125                 /* Finish up all downloads. */
126                 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(_curl_multi, &num_transfers) || num_transfers) {
127                         /* XXX: should inform the user why it's taking so damn long... */
128                 }
129
130                 /* Close all finished files. */
131                 while ((msg = curl_multi_info_read(_curl_multi, &num_msgs))) {
132                         if (msg->msg == CURLMSG_DONE) {
133                                 /* This is a map download. */
134                                 ProgressUpdateInfo *pui = g_hash_table_lookup(_pui_by_easy, msg->easy_handle);
135                                 g_queue_push_head(_curl_easy_queue, msg->easy_handle);
136                                 g_hash_table_remove(_pui_by_easy, msg->easy_handle);
137                                 fclose(pui->file);
138                                 curl_multi_remove_handle(_curl_multi, msg->easy_handle);
139                         }
140                 }
141
142                 while ((curr = g_queue_pop_tail(_curl_easy_queue)))
143                         curl_easy_cleanup(curr);
144
145                 curl_multi_cleanup(_curl_multi);
146                 _curl_multi = NULL;
147
148                 g_queue_free(_curl_easy_queue);
149                 g_tree_destroy(_downloading_tree);
150                 g_hash_table_destroy(_pui_by_easy);
151         }
152
153         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
154 }
155
156 void set_var_defaults(void)
157 {
158 _conn_state = RCVR_OFF;
159 }
160
161 gint mapper_osso_init(void)
162 {
163 #ifdef WITH_OSSO
164 /* Initialize _osso. */
165 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
166 if (!_osso) {
167         g_printerr("osso_initialize failed.\n");
168         return 1;
169 }
170 #endif
171 return 0;
172 }
173
174 gint mapper_osso_cb_init(void)
175 {
176 #ifdef WITH_OSSO
177 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
178         g_printerr("osso_rpc_set_default_cb_f failed.\n");
179         return 1;
180 }
181 #endif
182 return 0;
183 }
184
185 void timezone_init(void)
186 {
187 time_t time1;
188 struct tm time2;
189
190 time1 = time(NULL);
191 localtime_r(&time1, &time2);
192 _gmtoffset = time2.tm_gmtoff;
193 }
194
195 gint main(gint argc, gchar * argv[])
196 {
197         printf("%s()\n", __PRETTY_FUNCTION__);
198
199         /* Initialize localization. */
200         setlocale(LC_ALL, "");
201         bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
202         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
203         textdomain(GETTEXT_PACKAGE);
204
205         g_thread_init(NULL);
206         g_type_init();
207 #ifdef WITH_OSSO
208         if (mapper_osso_init()!=0)
209                 return 1;
210 #endif
211         set_var_defaults();     
212 #ifdef WITH_GST
213         gst_init(&argc, &argv);
214         speak_init();
215 #endif
216         gtk_init(&argc, &argv);
217         gconf_init(argc, argv, NULL);
218         gnome_vfs_init();
219         curl_global_init(CURL_GLOBAL_NOTHING);
220         timezone_init();
221         gpx_init();
222         mapper_init(argc, argv);
223 #ifdef WITH_OSSO
224         if (mapper_osso_cb_init()!=0)
225                 return 1;
226 #endif
227         gtk_main();
228         mapper_destroy();
229 #ifdef WITH_GST
230         speak_deinit();
231 #endif
232 #ifdef WITH_OSSO
233         osso_deinitialize(_osso);
234 #endif
235         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
236         return 0;
237 }