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