]>
Commit | Line | Data |
---|---|---|
3a4b95f6 GB |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking | |
82aa018d | 4 | * Copyright (C) 2009-2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
3a4b95f6 GB |
5 | * |
6 | * viking is free software: you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * viking is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
14 | * See the GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
aa69d106 GB |
20 | /** |
21 | * SECTION:vikmapsourcedefault | |
22 | * @short_description: the base class implementing most of generic features | |
23 | * | |
24 | * The #VikMapSourceDefault class is the base class implementing most of | |
25 | * generic feature, using properties or reducing complexity of some | |
26 | * functions. | |
27 | */ | |
28 | ||
3a4b95f6 | 29 | #include "vikmapsourcedefault.h" |
31f3c5e7 | 30 | #include "vikenumtypes.h" |
f66ea3ec | 31 | #include "download.h" |
3a4b95f6 | 32 | |
68b1d6c0 | 33 | static void map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(void*,const gchar*), void *data); |
53ac8302 GB |
34 | static const gchar *map_source_get_license (VikMapSource *self); |
35 | static const gchar *map_source_get_license_url (VikMapSource *self); | |
26336cf0 | 36 | static const GdkPixbuf *map_source_get_logo (VikMapSource *self); |
82aa018d | 37 | |
3a4b95f6 | 38 | static guint8 map_source_get_uniq_id (VikMapSource *self); |
db03733a | 39 | static const gchar *map_source_get_label (VikMapSource *self); |
3a4b95f6 GB |
40 | static guint16 map_source_get_tilesize_x (VikMapSource *self); |
41 | static guint16 map_source_get_tilesize_y (VikMapSource *self); | |
42 | static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self); | |
43 | ||
f66ea3ec GB |
44 | static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle ); |
45 | static void * _download_handle_init ( VikMapSource *self ); | |
46 | static void _download_handle_cleanup ( VikMapSource *self, void *handle ); | |
47 | ||
3a4b95f6 GB |
48 | typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate; |
49 | struct _VikMapSourceDefaultPrivate | |
50 | { | |
82aa018d GB |
51 | /* legal stuff */ |
52 | gchar *copyright; | |
53ac8302 GB |
53 | gchar *license; |
54 | gchar *license_url; | |
26336cf0 GB |
55 | GdkPixbuf *logo; |
56 | ||
3a4b95f6 | 57 | guint8 uniq_id; |
db03733a | 58 | gchar *label; |
3a4b95f6 GB |
59 | guint16 tilesize_x; |
60 | guint16 tilesize_y; | |
61 | VikViewportDrawMode drawmode; | |
62 | }; | |
63 | ||
64 | #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate)) | |
65 | ||
31f3c5e7 GB |
66 | /* properties */ |
67 | enum | |
68 | { | |
69 | PROP_0, | |
70 | ||
71 | PROP_ID, | |
db03733a | 72 | PROP_LABEL, |
31f3c5e7 GB |
73 | PROP_TILESIZE_X, |
74 | PROP_TILESIZE_Y, | |
75 | PROP_DRAWMODE, | |
82aa018d | 76 | PROP_COPYRIGHT, |
53ac8302 GB |
77 | PROP_LICENSE, |
78 | PROP_LICENSE_URL, | |
31f3c5e7 | 79 | }; |
3a4b95f6 GB |
80 | |
81 | G_DEFINE_TYPE_EXTENDED (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE, (GTypeFlags)G_TYPE_FLAG_ABSTRACT,); | |
82 | ||
83 | static void | |
84 | vik_map_source_default_init (VikMapSourceDefault *object) | |
85 | { | |
db03733a GB |
86 | VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object); |
87 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self); | |
88 | ||
89 | priv->label = NULL; | |
82aa018d | 90 | priv->copyright = NULL; |
53ac8302 GB |
91 | priv->license = NULL; |
92 | priv->license_url = NULL; | |
26336cf0 | 93 | priv->logo = NULL; |
3a4b95f6 GB |
94 | } |
95 | ||
96 | static void | |
97 | vik_map_source_default_finalize (GObject *object) | |
98 | { | |
db03733a GB |
99 | VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object); |
100 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self); | |
3a4b95f6 | 101 | |
db03733a GB |
102 | g_free (priv->label); |
103 | priv->label = NULL; | |
82aa018d GB |
104 | g_free (priv->copyright); |
105 | priv->copyright = NULL; | |
53ac8302 GB |
106 | g_free (priv->license); |
107 | priv->license = NULL; | |
108 | g_free (priv->license_url); | |
109 | priv->license_url = NULL; | |
26336cf0 GB |
110 | g_free (priv->logo); |
111 | priv->license_url = NULL; | |
db03733a GB |
112 | |
113 | G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object); | |
3a4b95f6 GB |
114 | } |
115 | ||
31f3c5e7 GB |
116 | static void |
117 | vik_map_source_default_set_property (GObject *object, | |
118 | guint property_id, | |
119 | const GValue *value, | |
120 | GParamSpec *pspec) | |
121 | { | |
122 | VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object); | |
123 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self); | |
124 | ||
125 | switch (property_id) | |
126 | { | |
127 | case PROP_ID: | |
128 | priv->uniq_id = g_value_get_uint (value); | |
129 | break; | |
130 | ||
9f58c4b4 | 131 | case PROP_LABEL: |
db03733a GB |
132 | g_free (priv->label); |
133 | priv->label = g_strdup(g_value_get_string (value)); | |
134 | break; | |
135 | ||
31f3c5e7 GB |
136 | case PROP_TILESIZE_X: |
137 | priv->tilesize_x = g_value_get_uint (value); | |
138 | break; | |
139 | ||
140 | case PROP_TILESIZE_Y: | |
141 | priv->tilesize_y = g_value_get_uint (value); | |
142 | break; | |
143 | ||
144 | case PROP_DRAWMODE: | |
145 | priv->drawmode = g_value_get_enum(value); | |
146 | break; | |
147 | ||
82aa018d GB |
148 | case PROP_COPYRIGHT: |
149 | g_free (priv->copyright); | |
150 | priv->copyright = g_strdup(g_value_get_string (value)); | |
151 | break; | |
152 | ||
53ac8302 GB |
153 | case PROP_LICENSE: |
154 | g_free (priv->license); | |
155 | priv->license = g_strdup(g_value_get_string (value)); | |
156 | break; | |
157 | ||
158 | case PROP_LICENSE_URL: | |
159 | g_free (priv->license_url); | |
160 | priv->license_url = g_strdup(g_value_get_string (value)); | |
161 | break; | |
162 | ||
31f3c5e7 GB |
163 | default: |
164 | /* We don't have any other property... */ | |
165 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | static void | |
171 | vik_map_source_default_get_property (GObject *object, | |
172 | guint property_id, | |
173 | GValue *value, | |
174 | GParamSpec *pspec) | |
175 | { | |
176 | VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object); | |
177 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self); | |
178 | ||
179 | switch (property_id) | |
180 | { | |
181 | case PROP_ID: | |
182 | g_value_set_uint (value, priv->uniq_id); | |
183 | break; | |
184 | ||
db03733a GB |
185 | case PROP_LABEL: |
186 | g_value_set_string (value, priv->label); | |
187 | break; | |
188 | ||
31f3c5e7 GB |
189 | case PROP_TILESIZE_X: |
190 | g_value_set_uint (value, priv->tilesize_x); | |
191 | break; | |
192 | ||
193 | case PROP_TILESIZE_Y: | |
194 | g_value_set_uint (value, priv->tilesize_y); | |
195 | break; | |
196 | ||
197 | case PROP_DRAWMODE: | |
198 | g_value_set_enum (value, priv->drawmode); | |
199 | break; | |
200 | ||
82aa018d GB |
201 | case PROP_COPYRIGHT: |
202 | g_value_set_string (value, priv->copyright); | |
203 | break; | |
204 | ||
53ac8302 GB |
205 | case PROP_LICENSE: |
206 | g_value_set_string (value, priv->license); | |
207 | break; | |
208 | ||
209 | case PROP_LICENSE_URL: | |
210 | g_value_set_string (value, priv->license_url); | |
211 | break; | |
212 | ||
31f3c5e7 GB |
213 | default: |
214 | /* We don't have any other property... */ | |
215 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
3a4b95f6 GB |
220 | static void |
221 | vik_map_source_default_class_init (VikMapSourceDefaultClass *klass) | |
222 | { | |
223 | GObjectClass* object_class = G_OBJECT_CLASS (klass); | |
224 | VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass); | |
31f3c5e7 GB |
225 | GParamSpec *pspec = NULL; |
226 | ||
227 | object_class->set_property = vik_map_source_default_set_property; | |
228 | object_class->get_property = vik_map_source_default_get_property; | |
229 | ||
3a4b95f6 | 230 | /* Overiding methods */ |
82aa018d | 231 | parent_class->get_copyright = map_source_get_copyright; |
53ac8302 GB |
232 | parent_class->get_license = map_source_get_license; |
233 | parent_class->get_license_url = map_source_get_license_url; | |
26336cf0 | 234 | parent_class->get_logo = map_source_get_logo; |
3a4b95f6 | 235 | parent_class->get_uniq_id = map_source_get_uniq_id; |
db03733a | 236 | parent_class->get_label = map_source_get_label; |
3a4b95f6 GB |
237 | parent_class->get_tilesize_x = map_source_get_tilesize_x; |
238 | parent_class->get_tilesize_y = map_source_get_tilesize_y; | |
239 | parent_class->get_drawmode = map_source_get_drawmode; | |
f66ea3ec GB |
240 | parent_class->download = _download; |
241 | parent_class->download_handle_init = _download_handle_init; | |
242 | parent_class->download_handle_cleanup = _download_handle_cleanup; | |
243 | ||
244 | /* Default implementation of methods */ | |
245 | klass->get_uri = NULL; | |
246 | klass->get_hostname = NULL; | |
247 | klass->get_download_options = NULL; | |
3a4b95f6 | 248 | |
31f3c5e7 GB |
249 | pspec = g_param_spec_uint ("id", |
250 | "Id of the tool", | |
251 | "Set the id", | |
252 | 0 /* minimum value */, | |
9593ef7d | 253 | G_MAXUINT /* maximum value */, |
31f3c5e7 GB |
254 | 0 /* default value */, |
255 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
256 | g_object_class_install_property (object_class, PROP_ID, pspec); | |
257 | ||
db03733a GB |
258 | pspec = g_param_spec_string ("label", |
259 | "Label", | |
260 | "The label of the map source", | |
261 | "<no-set>" /* default value */, | |
262 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
263 | g_object_class_install_property (object_class, PROP_LABEL, pspec); | |
264 | ||
31f3c5e7 GB |
265 | pspec = g_param_spec_uint ("tilesize-x", |
266 | "TileSizeX", | |
267 | "Set the size of the tile (x)", | |
268 | 0 /* minimum value */, | |
269 | G_MAXUINT16 /* maximum value */, | |
270 | 0 /* default value */, | |
9f58c4b4 | 271 | G_PARAM_CONSTRUCT | G_PARAM_READWRITE); |
31f3c5e7 GB |
272 | g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec); |
273 | ||
274 | pspec = g_param_spec_uint ("tilesize-y", | |
275 | "TileSizeY", | |
276 | "Set the size of the tile (y)", | |
277 | 0 /* minimum value */, | |
278 | G_MAXUINT16 /* maximum value */, | |
279 | 0 /* default value */, | |
9f58c4b4 | 280 | G_PARAM_CONSTRUCT | G_PARAM_READWRITE); |
31f3c5e7 GB |
281 | g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec); |
282 | ||
283 | pspec = g_param_spec_enum("drawmode", | |
284 | "Drawmode", | |
285 | "The mode used to draw map", | |
286 | VIK_TYPE_VIEWPORT_DRAW_MODE, | |
287 | VIK_VIEWPORT_DRAWMODE_UTM, | |
288 | G_PARAM_READWRITE); | |
289 | g_object_class_install_property(object_class, PROP_DRAWMODE, pspec); | |
290 | ||
82aa018d GB |
291 | pspec = g_param_spec_string ("copyright", |
292 | "Copyright", | |
293 | "The copyright of the map source", | |
294 | NULL, | |
9f58c4b4 | 295 | G_PARAM_READWRITE); |
82aa018d GB |
296 | g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec); |
297 | ||
53ac8302 GB |
298 | pspec = g_param_spec_string ("license", |
299 | "License", | |
300 | "The license of the map source", | |
301 | NULL, | |
302 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
303 | g_object_class_install_property (object_class, PROP_LICENSE, pspec); | |
304 | ||
305 | pspec = g_param_spec_string ("license-url", | |
306 | "License URL", | |
307 | "The URL of the license of the map source", | |
308 | NULL, | |
309 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
310 | g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec); | |
311 | ||
3a4b95f6 GB |
312 | g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate)); |
313 | ||
314 | object_class->finalize = vik_map_source_default_finalize; | |
315 | } | |
316 | ||
68b1d6c0 GB |
317 | static void |
318 | map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(void*,const gchar*), void *data) | |
82aa018d | 319 | { |
68b1d6c0 GB |
320 | /* Just ignore bbox and zoom level */ |
321 | g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self)); | |
322 | ||
323 | g_debug ("%s: %g %g %g %g %g", __FUNCTION__, | |
324 | bbox.south, bbox.north, bbox.east, bbox.west, | |
325 | zoom); | |
82aa018d GB |
326 | |
327 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
328 | ||
68b1d6c0 | 329 | (*fct) (data, priv->copyright); |
82aa018d GB |
330 | } |
331 | ||
53ac8302 GB |
332 | static const gchar * |
333 | map_source_get_license (VikMapSource *self) | |
334 | { | |
335 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL); | |
336 | ||
337 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
338 | ||
339 | return priv->license; | |
340 | } | |
341 | ||
342 | static const gchar * | |
343 | map_source_get_license_url (VikMapSource *self) | |
344 | { | |
345 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL); | |
346 | ||
347 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
348 | ||
349 | return priv->license_url; | |
350 | } | |
351 | ||
26336cf0 GB |
352 | static const GdkPixbuf * |
353 | map_source_get_logo (VikMapSource *self) | |
354 | { | |
355 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL); | |
356 | ||
357 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
358 | ||
359 | return priv->logo; | |
360 | } | |
361 | ||
3a4b95f6 GB |
362 | static guint8 |
363 | map_source_get_uniq_id (VikMapSource *self) | |
364 | { | |
3a84e537 GB |
365 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint8)0); |
366 | ||
367 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
3a4b95f6 GB |
368 | |
369 | return priv->uniq_id; | |
370 | } | |
371 | ||
db03733a GB |
372 | static const gchar * |
373 | map_source_get_label (VikMapSource *self) | |
374 | { | |
375 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL); | |
376 | ||
377 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); | |
378 | ||
379 | return priv->label; | |
380 | } | |
381 | ||
3a4b95f6 GB |
382 | static guint16 |
383 | map_source_get_tilesize_x (VikMapSource *self) | |
384 | { | |
3a84e537 GB |
385 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0); |
386 | ||
3a4b95f6 | 387 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); |
3a4b95f6 GB |
388 | |
389 | return priv->tilesize_x; | |
390 | } | |
391 | ||
392 | static guint16 | |
393 | map_source_get_tilesize_y (VikMapSource *self) | |
394 | { | |
3a84e537 GB |
395 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0); |
396 | ||
3a4b95f6 | 397 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); |
3a4b95f6 GB |
398 | |
399 | return priv->tilesize_y; | |
400 | } | |
401 | ||
402 | static VikViewportDrawMode | |
403 | map_source_get_drawmode (VikMapSource *self) | |
404 | { | |
3a84e537 GB |
405 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0); |
406 | ||
3a4b95f6 | 407 | VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self); |
3a4b95f6 GB |
408 | |
409 | return priv->drawmode; | |
410 | } | |
f66ea3ec GB |
411 | |
412 | static int | |
413 | _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle ) | |
414 | { | |
415 | int res; | |
416 | gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src); | |
417 | gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self)); | |
418 | DownloadMapOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)); | |
419 | res = a_http_download_get_url ( host, uri, dest_fn, options, handle ); | |
420 | g_free ( uri ); | |
421 | g_free ( host ); | |
422 | return res; | |
423 | } | |
424 | ||
425 | static void * | |
426 | _download_handle_init ( VikMapSource *self ) | |
427 | { | |
428 | return a_download_handle_init (); | |
429 | } | |
430 | ||
431 | ||
432 | static void | |
433 | _download_handle_cleanup ( VikMapSource *self, void *handle ) | |
434 | { | |
41810542 | 435 | a_download_handle_cleanup ( handle ); |
f66ea3ec GB |
436 | } |
437 | ||
438 | gchar * | |
439 | vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src ) | |
440 | { | |
441 | VikMapSourceDefaultClass *klass; | |
442 | g_return_val_if_fail (self != NULL, 0); | |
443 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0); | |
444 | klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self); | |
445 | ||
446 | g_return_val_if_fail (klass->get_uri != NULL, 0); | |
447 | ||
448 | return (*klass->get_uri)(self, src); | |
449 | } | |
450 | ||
451 | gchar * | |
452 | vik_map_source_default_get_hostname( VikMapSourceDefault *self ) | |
453 | { | |
454 | VikMapSourceDefaultClass *klass; | |
455 | g_return_val_if_fail (self != NULL, 0); | |
456 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0); | |
457 | klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self); | |
458 | ||
459 | g_return_val_if_fail (klass->get_hostname != NULL, 0); | |
460 | ||
461 | return (*klass->get_hostname)(self); | |
462 | } | |
463 | ||
464 | DownloadMapOptions * | |
465 | vik_map_source_default_get_download_options( VikMapSourceDefault *self ) | |
466 | { | |
467 | VikMapSourceDefaultClass *klass; | |
468 | g_return_val_if_fail (self != NULL, 0); | |
469 | g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0); | |
470 | klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self); | |
471 | ||
472 | g_return_val_if_fail (klass->get_download_options != NULL, 0); | |
473 | ||
474 | return (*klass->get_download_options)(self); | |
475 | } |