]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
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> | |
ddc47a46 | 23 | #include <string.h> |
50a14534 EB |
24 | #include "coords.h" |
25 | #include "vikcoord.h" | |
26 | #include "vikwaypoint.h" | |
67dc0a98 | 27 | #include "globals.h" |
98acb9a1 | 28 | #include "garminsymbols.h" |
6581f246 | 29 | #include "dems.h" |
c9570f86 | 30 | #include <glib/gi18n.h> |
acaf7113 | 31 | |
50a14534 EB |
32 | VikWaypoint *vik_waypoint_new() |
33 | { | |
6b2f262e | 34 | VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) ); |
8541f2cf | 35 | wp->altitude = VIK_DEFAULT_ALTITUDE; |
c9570f86 | 36 | wp->name = g_strdup(_("Waypoint")); |
50a14534 EB |
37 | return wp; |
38 | } | |
39 | ||
c9570f86 RN |
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 | ||
27d267f4 | 46 | wp->name = g_strdup(name); |
c9570f86 RN |
47 | } |
48 | ||
50a14534 EB |
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 | ||
6b2f262e RN |
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 | ||
415b0e21 RN |
78 | void vik_waypoint_set_url(VikWaypoint *wp, const gchar *url) |
79 | { | |
80 | if ( wp->url ) | |
81 | g_free ( wp->url ); | |
82 | ||
83 | if ( url && url[0] != '\0' ) | |
84 | wp->url = g_strdup(url); | |
85 | else | |
86 | wp->url = NULL; | |
87 | } | |
88 | ||
50a14534 EB |
89 | void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image) |
90 | { | |
91 | if ( wp->image ) | |
92 | g_free ( wp->image ); | |
93 | ||
94 | if ( image && image[0] != '\0' ) | |
95 | wp->image = g_strdup(image); | |
96 | else | |
97 | wp->image = NULL; | |
ba45ceea | 98 | // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn |
50a14534 EB |
99 | } |
100 | ||
acaf7113 AF |
101 | void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname) |
102 | { | |
fffcc269 SE |
103 | const gchar *hashed_symname; |
104 | ||
acaf7113 AF |
105 | if ( wp->symbol ) |
106 | g_free ( wp->symbol ); | |
107 | ||
98acb9a1 RN |
108 | // NB symbol_pixbuf is just a reference, so no need to free it |
109 | ||
110 | if ( symname && symname[0] != '\0' ) { | |
fffcc269 SE |
111 | hashed_symname = a_get_hashed_sym ( symname ); |
112 | if ( hashed_symname ) | |
113 | symname = hashed_symname; | |
114 | wp->symbol = g_strdup ( symname ); | |
98acb9a1 RN |
115 | wp->symbol_pixbuf = a_get_wp_sym ( wp->symbol ); |
116 | } | |
117 | else { | |
acaf7113 | 118 | wp->symbol = NULL; |
98acb9a1 RN |
119 | wp->symbol_pixbuf = NULL; |
120 | } | |
acaf7113 AF |
121 | } |
122 | ||
50a14534 EB |
123 | void vik_waypoint_free(VikWaypoint *wp) |
124 | { | |
125 | if ( wp->comment ) | |
126 | g_free ( wp->comment ); | |
6b2f262e RN |
127 | if ( wp->description ) |
128 | g_free ( wp->description ); | |
415b0e21 RN |
129 | if ( wp->url ) |
130 | g_free ( wp->url ); | |
50a14534 EB |
131 | if ( wp->image ) |
132 | g_free ( wp->image ); | |
acaf7113 AF |
133 | if ( wp->symbol ) |
134 | g_free ( wp->symbol ); | |
50a14534 EB |
135 | g_free ( wp ); |
136 | } | |
137 | ||
138 | VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp) | |
139 | { | |
140 | VikWaypoint *new_wp = vik_waypoint_new(); | |
a601900a RN |
141 | new_wp->coord = wp->coord; |
142 | new_wp->visible = wp->visible; | |
143 | new_wp->altitude = wp->altitude; | |
f2ef466d RN |
144 | new_wp->has_timestamp = wp->has_timestamp; |
145 | new_wp->timestamp = wp->timestamp; | |
c9570f86 | 146 | vik_waypoint_set_name(new_wp,wp->name); |
50a14534 | 147 | vik_waypoint_set_comment(new_wp,wp->comment); |
6b2f262e | 148 | vik_waypoint_set_description(new_wp,wp->description); |
415b0e21 | 149 | vik_waypoint_set_url(new_wp,wp->url); |
50a14534 | 150 | vik_waypoint_set_image(new_wp,wp->image); |
acaf7113 | 151 | vik_waypoint_set_symbol(new_wp,wp->symbol); |
50a14534 EB |
152 | return new_wp; |
153 | } | |
acaf7113 | 154 | |
6581f246 RN |
155 | /** |
156 | * vik_waypoint_apply_dem_data: | |
157 | * @wp: The Waypoint to operate on | |
158 | * @skip_existing: When TRUE, don't change the elevation if the waypoint already has a value | |
159 | * | |
160 | * Set elevation data for a waypoint using available DEM information | |
161 | * | |
162 | * Returns: TRUE if the waypoint was updated | |
163 | */ | |
164 | gboolean vik_waypoint_apply_dem_data ( VikWaypoint *wp, gboolean skip_existing ) | |
165 | { | |
166 | gboolean updated = FALSE; | |
167 | if ( !(skip_existing && wp->altitude != VIK_DEFAULT_ALTITUDE) ) { | |
168 | gint16 elev = a_dems_get_elev_by_coord ( &(wp->coord), VIK_DEM_INTERPOL_BEST ); | |
169 | if ( elev != VIK_DEM_INVALID_ELEVATION ) { | |
170 | wp->altitude = (gdouble)elev; | |
171 | updated = TRUE; | |
172 | } | |
173 | } | |
174 | return updated; | |
175 | } | |
176 | ||
c9570f86 RN |
177 | /* |
178 | * Take a Waypoint and convert it into a byte array | |
179 | */ | |
ddc47a46 AF |
180 | void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen) |
181 | { | |
182 | GByteArray *b = g_byte_array_new(); | |
183 | guint len; | |
184 | ||
c9570f86 RN |
185 | // This creates space for fixed sized members like gints and whatnot |
186 | // and copies that amount of data from the waypoint to byte array | |
ddc47a46 AF |
187 | g_byte_array_append(b, (guint8 *)wp, sizeof(*wp)); |
188 | ||
c9570f86 RN |
189 | // This allocates space for variant sized strings |
190 | // and copies that amount of data from the waypoint to byte array | |
ddc47a46 AF |
191 | #define vwm_append(s) \ |
192 | len = (s) ? strlen(s)+1 : 0; \ | |
193 | g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \ | |
194 | if (s) g_byte_array_append(b, (guint8 *)s, len); | |
195 | ||
c9570f86 | 196 | vwm_append(wp->name); |
ddc47a46 | 197 | vwm_append(wp->comment); |
6b2f262e | 198 | vwm_append(wp->description); |
415b0e21 | 199 | vwm_append(wp->url); |
ddc47a46 AF |
200 | vwm_append(wp->image); |
201 | vwm_append(wp->symbol); | |
202 | ||
203 | *data = b->data; | |
204 | *datalen = b->len; | |
205 | g_byte_array_free(b, FALSE); | |
206 | #undef vwm_append | |
207 | } | |
208 | ||
c9570f86 RN |
209 | /* |
210 | * Take a byte array and convert it into a Waypoint | |
211 | */ | |
ddc47a46 AF |
212 | VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen) |
213 | { | |
214 | guint len; | |
215 | VikWaypoint *new_wp = vik_waypoint_new(); | |
6b2f262e | 216 | // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...) |
ddc47a46 AF |
217 | memcpy(new_wp, data, sizeof(*new_wp)); |
218 | data += sizeof(*new_wp); | |
219 | ||
6b2f262e | 220 | // Now the variant sized strings... |
ddc47a46 AF |
221 | #define vwu_get(s) \ |
222 | len = *(guint *)data; \ | |
223 | data += sizeof(len); \ | |
224 | if (len) { \ | |
225 | (s) = g_strdup((gchar *)data); \ | |
226 | } else { \ | |
227 | (s) = NULL; \ | |
228 | } \ | |
229 | data += len; | |
230 | ||
c9570f86 | 231 | vwu_get(new_wp->name); |
ddc47a46 | 232 | vwu_get(new_wp->comment); |
6b2f262e | 233 | vwu_get(new_wp->description); |
415b0e21 | 234 | vwu_get(new_wp->url); |
ddc47a46 AF |
235 | vwu_get(new_wp->image); |
236 | vwu_get(new_wp->symbol); | |
237 | ||
238 | return new_wp; | |
239 | #undef vwu_get | |
240 | } | |
241 |