Fix:Core:Replace g_warning with dbg(0, some wince fixes
[navit-package] / navit / vehicle.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib.h>
23 #include "config.h"
24 #include "debug.h"
25 #include "coord.h"
26 #include "item.h"
27 #include "log.h"
28 #include "callback.h"
29 #include "plugin.h"
30 #include "vehicle.h"
31
32 struct vehicle {
33         char *name;
34         struct vehicle_priv *priv;
35         struct vehicle_methods meth;
36         struct callback_list *cbl;
37         struct log *nmea_log, *gpx_log;
38 };
39
40 static void
41 vehicle_log_nmea(struct vehicle *this_, struct log *log)
42 {
43         struct attr pos_attr;
44         if (!this_->meth.position_attr_get)
45                 return;
46         if (!this_->meth.position_attr_get(this_->priv, attr_position_nmea, &pos_attr))
47                 return;
48         log_write(log, pos_attr.u.str, strlen(pos_attr.u.str));
49 }
50
51 static void
52 vehicle_log_gpx(struct vehicle *this_, struct log *log)
53 {
54         struct attr pos_attr;
55         char buffer[256];
56 #ifndef __CEGCC__
57         GTimeVal time; 
58         g_get_current_time(&time); 
59 #endif
60
61         if (!this_->meth.position_attr_get)
62                 return;
63         if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
64                 return;
65 #ifndef __CEGCC__
66         sprintf(buffer,"<trkpt lat=\"%f\" lon=\"%f\">\n\t<time>%s</time>\n</trkpt>\n",
67                 pos_attr.u.coord_geo->lat, pos_attr.u.coord_geo->lng, g_time_val_to_iso8601(&time));
68 #else
69         sprintf(buffer,"<trkpt lat=\"%f\" lon=\"%f\">\n</trkpt>\n",
70                 pos_attr.u.coord_geo->lat, pos_attr.u.coord_geo->lng);
71 #endif
72         log_write(log, buffer, strlen(buffer));
73 }
74
75
76 static void
77 vehicle_log_textfile(struct vehicle *this_, struct log *log)
78 {
79         struct attr pos_attr;
80         char buffer[256];
81         if (!this_->meth.position_attr_get)
82                 return;
83         if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
84                 return;
85         sprintf(buffer,"%f %f type=trackpoint\n", pos_attr.u.coord_geo->lng, pos_attr.u.coord_geo->lat);
86         log_write(log, buffer, strlen(buffer));
87 }
88
89 static int
90 vehicle_add_log(struct vehicle *this_, struct log *log)
91 {
92         struct callback *cb;
93         struct attr type_attr;
94         if (!log_get_attr(log, attr_type, &type_attr, NULL))
95                 return 1;
96
97         if (!strcmp(type_attr.u.str, "nmea")) {
98                 cb=callback_new_2(callback_cast(vehicle_log_nmea), this_, log);
99         } else if (!strcmp(type_attr.u.str, "gpx")) {
100                 char *header =
101                     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gpx version=\"1.0\" creator=\"Navit http://navit.sourceforge.net\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n<trk>\n<trkseg>\n";
102                 char *trailer = "</trkseg>\n</trk>\n</gpx>\n";
103                 log_set_header(log, header, strlen(header));
104                 log_set_trailer(log, trailer, strlen(trailer));
105                 cb=callback_new_2(callback_cast(vehicle_log_gpx), this_, log);
106         } else if (!strcmp(type_attr.u.str, "textfile")) {
107                 char *header = "type=track\n";
108                 log_set_header(log, header, strlen(header));
109                 cb=callback_new_2(callback_cast(vehicle_log_textfile), this_, log);
110         } else
111                 return 1;
112         callback_list_add(this_->cbl, cb);
113         return 0;
114 }
115
116 struct vehicle *
117 vehicle_new(struct attr **attrs)
118 {
119         struct vehicle *this_;
120         struct attr *source;
121         struct vehicle_priv *(*vehicletype_new) (struct vehicle_methods *
122                                                  meth,
123                                                  struct callback_list *
124                                                  cbl,
125                                                  struct attr ** attrs);
126         char *type, *colon;
127         struct attr *name;
128
129         dbg(1, "enter\n");
130         source = attr_search(attrs, NULL, attr_source);
131         if (!source) {
132                 dbg(0, "no source\n");
133                 return NULL;
134         }
135
136         type = g_strdup(source->u.str);
137         colon = index(type, ':');
138         if (colon)
139                 *colon = '\0';
140         dbg(1, "source='%s' type='%s'\n", source->u.str, type);
141
142         vehicletype_new = plugin_get_vehicle_type(type);
143         if (!vehicletype_new) {
144                 dbg(0, "invalid type '%s'\n", type);
145                 return NULL;
146         }
147         this_ = g_new0(struct vehicle, 1);
148         this_->cbl = callback_list_new();
149         this_->priv = vehicletype_new(&this_->meth, this_->cbl, attrs);
150         if (!this_->priv) {
151                 dbg(0, "vehicletype_new failed\n");
152                 callback_list_destroy(this_->cbl);
153                 g_free(this_);
154                 return NULL;
155         }
156         dbg(1, "leave\n");
157
158         if ((name=attr_search(attrs, NULL, attr_name))) {
159                 this_->name=g_strdup(name->u.str);
160         } else {
161                 this_->name=g_strdup("Noname");
162         }
163         return this_;
164 }
165
166 int
167 vehicle_get_attr(struct vehicle *this_, enum attr_type type, struct attr *attr)
168 {
169         switch (type) {
170         case attr_name:
171                 attr->u.str=this_->name;
172                 break;
173         default:
174                 if (this_->meth.position_attr_get) {
175                         return this_->meth.position_attr_get(this_->priv, type, attr);
176                 } else {
177                         return 0;
178                 }
179         }
180         attr->type=type;
181         return 1;
182 }
183
184 int
185 vehicle_set_attr(struct vehicle *this_, struct attr *attr,
186                  struct attr **attrs)
187 {
188         if (this_->meth.set_attr)
189                 return this_->meth.set_attr(this_->priv, attr, attrs);
190         return 0;
191 }
192
193 int
194 vehicle_add_attr(struct vehicle *this_, struct attr *attr)
195 {
196         switch (attr->type) {
197         case attr_callback:
198                 callback_list_add(this_->cbl, attr->u.callback);
199                 break;
200         case attr_log:
201                 return vehicle_add_log(this_, attr->u.log);
202         default:
203                 return 0;
204         }
205         return 1;
206 }
207
208 int
209 vehicle_remove_attr(struct vehicle *this_, struct attr *attr)
210 {
211         switch (attr->type) {
212         case attr_callback:
213                 callback_list_remove(this_->cbl, attr->u.callback);
214                 break;
215         default:
216                 return 0;
217         }
218         return 1;
219 }
220
221 void
222 vehicle_destroy(struct vehicle *this_)
223 {
224         callback_list_destroy(this_->cbl);
225         if(this_->name) g_free(this_->name);
226         g_free(this_);
227 }