]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <glib.h> | |
23 | #include <string.h> | |
24 | #include "coords.h" | |
25 | #include "vikcoord.h" | |
26 | #include "vikwaypoint.h" | |
27 | #include "globals.h" | |
28 | #include "garminsymbols.h" | |
29 | #include "dems.h" | |
30 | #include <glib/gi18n.h> | |
31 | ||
32 | VikWaypoint *vik_waypoint_new() | |
33 | { | |
34 | VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) ); | |
35 | wp->altitude = VIK_DEFAULT_ALTITUDE; | |
36 | wp->name = g_strdup(_("Waypoint")); | |
37 | return wp; | |
38 | } | |
39 | ||
40 | // Hmmm tempted to put in new constructor | |
41 | void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name) | |
42 | { | |
43 | if ( wp->name ) | |
44 | g_free ( wp->name ); | |
45 | ||
46 | wp->name = g_strdup(name); | |
47 | } | |
48 | ||
49 | void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment) | |
50 | { | |
51 | if ( wp->comment ) | |
52 | g_free ( wp->comment ); | |
53 | wp->comment = comment; | |
54 | } | |
55 | ||
56 | void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment) | |
57 | { | |
58 | if ( wp->comment ) | |
59 | g_free ( wp->comment ); | |
60 | ||
61 | if ( comment && comment[0] != '\0' ) | |
62 | wp->comment = g_strdup(comment); | |
63 | else | |
64 | wp->comment = NULL; | |
65 | } | |
66 | ||
67 | void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description) | |
68 | { | |
69 | if ( wp->description ) | |
70 | g_free ( wp->description ); | |
71 | ||
72 | if ( description && description[0] != '\0' ) | |
73 | wp->description = g_strdup(description); | |
74 | else | |
75 | wp->description = NULL; | |
76 | } | |
77 | ||
78 | void vik_waypoint_set_source(VikWaypoint *wp, const gchar *source) | |
79 | { | |
80 | if ( wp->source ) | |
81 | g_free ( wp->source ); | |
82 | ||
83 | if ( source && source[0] != '\0' ) | |
84 | wp->source = g_strdup(source); | |
85 | else | |
86 | wp->source = NULL; | |
87 | } | |
88 | ||
89 | void vik_waypoint_set_type(VikWaypoint *wp, const gchar *type) | |
90 | { | |
91 | if ( wp->type ) | |
92 | g_free ( wp->type ); | |
93 | ||
94 | if ( type && type[0] != '\0' ) | |
95 | wp->type = g_strdup(type); | |
96 | else | |
97 | wp->type = NULL; | |
98 | } | |
99 | ||
100 | void vik_waypoint_set_url(VikWaypoint *wp, const gchar *url) | |
101 | { | |
102 | if ( wp->url ) | |
103 | g_free ( wp->url ); | |
104 | ||
105 | if ( url && url[0] != '\0' ) | |
106 | wp->url = g_strdup(url); | |
107 | else | |
108 | wp->url = NULL; | |
109 | } | |
110 | ||
111 | void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image) | |
112 | { | |
113 | if ( wp->image ) | |
114 | g_free ( wp->image ); | |
115 | ||
116 | if ( image && image[0] != '\0' ) | |
117 | wp->image = g_strdup(image); | |
118 | else | |
119 | wp->image = NULL; | |
120 | // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn | |
121 | } | |
122 | ||
123 | void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname) | |
124 | { | |
125 | const gchar *hashed_symname; | |
126 | ||
127 | if ( wp->symbol ) | |
128 | g_free ( wp->symbol ); | |
129 | ||
130 | // NB symbol_pixbuf is just a reference, so no need to free it | |
131 | ||
132 | if ( symname && symname[0] != '\0' ) { | |
133 | hashed_symname = a_get_hashed_sym ( symname ); | |
134 | if ( hashed_symname ) | |
135 | symname = hashed_symname; | |
136 | wp->symbol = g_strdup ( symname ); | |
137 | wp->symbol_pixbuf = a_get_wp_sym ( wp->symbol ); | |
138 | } | |
139 | else { | |
140 | wp->symbol = NULL; | |
141 | wp->symbol_pixbuf = NULL; | |
142 | } | |
143 | } | |
144 | ||
145 | void vik_waypoint_free(VikWaypoint *wp) | |
146 | { | |
147 | if ( wp->comment ) | |
148 | g_free ( wp->comment ); | |
149 | if ( wp->description ) | |
150 | g_free ( wp->description ); | |
151 | if ( wp->source ) | |
152 | g_free ( wp->source ); | |
153 | if ( wp->type ) | |
154 | g_free ( wp->type ); | |
155 | if ( wp->url ) | |
156 | g_free ( wp->url ); | |
157 | if ( wp->image ) | |
158 | g_free ( wp->image ); | |
159 | if ( wp->symbol ) | |
160 | g_free ( wp->symbol ); | |
161 | g_free ( wp ); | |
162 | } | |
163 | ||
164 | VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp) | |
165 | { | |
166 | VikWaypoint *new_wp = vik_waypoint_new(); | |
167 | new_wp->coord = wp->coord; | |
168 | new_wp->visible = wp->visible; | |
169 | new_wp->altitude = wp->altitude; | |
170 | new_wp->has_timestamp = wp->has_timestamp; | |
171 | new_wp->timestamp = wp->timestamp; | |
172 | vik_waypoint_set_name(new_wp,wp->name); | |
173 | vik_waypoint_set_comment(new_wp,wp->comment); | |
174 | vik_waypoint_set_description(new_wp,wp->description); | |
175 | vik_waypoint_set_source(new_wp,wp->source); | |
176 | vik_waypoint_set_type(new_wp,wp->type); | |
177 | vik_waypoint_set_url(new_wp,wp->url); | |
178 | vik_waypoint_set_image(new_wp,wp->image); | |
179 | vik_waypoint_set_symbol(new_wp,wp->symbol); | |
180 | return new_wp; | |
181 | } | |
182 | ||
183 | /** | |
184 | * vik_waypoint_apply_dem_data: | |
185 | * @wp: The Waypoint to operate on | |
186 | * @skip_existing: When TRUE, don't change the elevation if the waypoint already has a value | |
187 | * | |
188 | * Set elevation data for a waypoint using available DEM information | |
189 | * | |
190 | * Returns: TRUE if the waypoint was updated | |
191 | */ | |
192 | gboolean vik_waypoint_apply_dem_data ( VikWaypoint *wp, gboolean skip_existing ) | |
193 | { | |
194 | gboolean updated = FALSE; | |
195 | if ( !(skip_existing && wp->altitude != VIK_DEFAULT_ALTITUDE) ) { | |
196 | gint16 elev = a_dems_get_elev_by_coord ( &(wp->coord), VIK_DEM_INTERPOL_BEST ); | |
197 | if ( elev != VIK_DEM_INVALID_ELEVATION ) { | |
198 | wp->altitude = (gdouble)elev; | |
199 | updated = TRUE; | |
200 | } | |
201 | } | |
202 | return updated; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Take a Waypoint and convert it into a byte array | |
207 | */ | |
208 | void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen) | |
209 | { | |
210 | GByteArray *b = g_byte_array_new(); | |
211 | guint len; | |
212 | ||
213 | // This creates space for fixed sized members like gints and whatnot | |
214 | // and copies that amount of data from the waypoint to byte array | |
215 | g_byte_array_append(b, (guint8 *)wp, sizeof(*wp)); | |
216 | ||
217 | // This allocates space for variant sized strings | |
218 | // and copies that amount of data from the waypoint to byte array | |
219 | #define vwm_append(s) \ | |
220 | len = (s) ? strlen(s)+1 : 0; \ | |
221 | g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \ | |
222 | if (s) g_byte_array_append(b, (guint8 *)s, len); | |
223 | ||
224 | vwm_append(wp->name); | |
225 | vwm_append(wp->comment); | |
226 | vwm_append(wp->description); | |
227 | vwm_append(wp->source); | |
228 | vwm_append(wp->type); | |
229 | vwm_append(wp->url); | |
230 | vwm_append(wp->image); | |
231 | vwm_append(wp->symbol); | |
232 | ||
233 | *data = b->data; | |
234 | *datalen = b->len; | |
235 | g_byte_array_free(b, FALSE); | |
236 | #undef vwm_append | |
237 | } | |
238 | ||
239 | /* | |
240 | * Take a byte array and convert it into a Waypoint | |
241 | */ | |
242 | VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen) | |
243 | { | |
244 | guint len; | |
245 | VikWaypoint *new_wp = vik_waypoint_new(); | |
246 | // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...) | |
247 | memcpy(new_wp, data, sizeof(*new_wp)); | |
248 | data += sizeof(*new_wp); | |
249 | ||
250 | // Now the variant sized strings... | |
251 | #define vwu_get(s) \ | |
252 | len = *(guint *)data; \ | |
253 | data += sizeof(len); \ | |
254 | if (len) { \ | |
255 | (s) = g_strdup((gchar *)data); \ | |
256 | } else { \ | |
257 | (s) = NULL; \ | |
258 | } \ | |
259 | data += len; | |
260 | ||
261 | vwu_get(new_wp->name); | |
262 | vwu_get(new_wp->comment); | |
263 | vwu_get(new_wp->description); | |
264 | vwu_get(new_wp->source); | |
265 | vwu_get(new_wp->type); | |
266 | vwu_get(new_wp->url); | |
267 | vwu_get(new_wp->image); | |
268 | vwu_get(new_wp->symbol); | |
269 | ||
270 | return new_wp; | |
271 | #undef vwu_get | |
272 | } | |
273 |