1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2009-2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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/>.
21 * SECTION:vikmapsourcedefault
22 * @short_description: the base class implementing most of generic features
24 * The #VikMapSourceDefault class is the base class implementing most of
25 * generic feature, using properties or reducing complexity of some
29 #include "vikmapsourcedefault.h"
30 #include "vikenumtypes.h"
34 static void map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
35 static const gchar *map_source_get_license (VikMapSource *self);
36 static const gchar *map_source_get_license_url (VikMapSource *self);
37 static const GdkPixbuf *map_source_get_logo (VikMapSource *self);
39 static const gchar *map_source_get_name (VikMapSource *self);
40 static guint16 map_source_get_uniq_id (VikMapSource *self);
41 static const gchar *map_source_get_label (VikMapSource *self);
42 static guint16 map_source_get_tilesize_x (VikMapSource *self);
43 static guint16 map_source_get_tilesize_y (VikMapSource *self);
44 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
45 static const gchar *map_source_get_file_extension (VikMapSource *self);
47 static DownloadResult_t _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
48 static void * _download_handle_init ( VikMapSource *self );
49 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
51 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
52 struct _VikMapSourceDefaultPrivate
65 VikViewportDrawMode drawmode;
66 gchar *file_extension;
69 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
88 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
91 vik_map_source_default_init (VikMapSourceDefault *object)
93 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
94 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
97 priv->copyright = NULL;
99 priv->license_url = NULL;
102 priv->file_extension = NULL;
106 vik_map_source_default_finalize (GObject *object)
108 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
109 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
111 g_free (priv->label);
113 g_free (priv->copyright);
114 priv->copyright = NULL;
115 g_free (priv->license);
116 priv->license = NULL;
117 g_free (priv->license_url);
118 priv->license_url = NULL;
120 priv->license_url = NULL;
123 g_free (priv->file_extension);
124 priv->file_extension = NULL;
126 G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
130 vik_map_source_default_set_property (GObject *object,
135 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
136 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
141 // Sanitize the name here for file usage
142 // A simple check just to prevent containing slashes ATM
144 priv->name = g_strdup(g_value_get_string (value));
145 g_strdelimit (priv->name, "\\/", 'x' );
149 priv->uniq_id = g_value_get_uint (value);
153 g_free (priv->label);
154 priv->label = g_strdup(g_value_get_string (value));
157 case PROP_TILESIZE_X:
158 priv->tilesize_x = g_value_get_uint (value);
161 case PROP_TILESIZE_Y:
162 priv->tilesize_y = g_value_get_uint (value);
166 priv->drawmode = g_value_get_enum(value);
170 g_free (priv->copyright);
171 priv->copyright = g_strdup(g_value_get_string (value));
175 g_free (priv->license);
176 priv->license = g_strdup(g_value_get_string (value));
179 case PROP_LICENSE_URL:
180 g_free (priv->license_url);
181 priv->license_url = g_strdup(g_value_get_string (value));
184 case PROP_FILE_EXTENSION:
185 g_free (priv->file_extension);
186 priv->file_extension = g_strdup(g_value_get_string(value));
190 /* We don't have any other property... */
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
197 vik_map_source_default_get_property (GObject *object,
202 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
203 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
208 g_value_set_string (value, priv->name);
212 g_value_set_uint (value, priv->uniq_id);
216 g_value_set_string (value, priv->label);
219 case PROP_TILESIZE_X:
220 g_value_set_uint (value, priv->tilesize_x);
223 case PROP_TILESIZE_Y:
224 g_value_set_uint (value, priv->tilesize_y);
228 g_value_set_enum (value, priv->drawmode);
232 g_value_set_string (value, priv->copyright);
236 g_value_set_string (value, priv->license);
239 case PROP_LICENSE_URL:
240 g_value_set_string (value, priv->license_url);
243 case PROP_FILE_EXTENSION:
244 g_value_set_string (value, priv->file_extension);
248 /* We don't have any other property... */
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
255 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
257 GObjectClass* object_class = G_OBJECT_CLASS (klass);
258 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
259 GParamSpec *pspec = NULL;
261 object_class->set_property = vik_map_source_default_set_property;
262 object_class->get_property = vik_map_source_default_get_property;
264 /* Overiding methods */
265 parent_class->get_copyright = map_source_get_copyright;
266 parent_class->get_license = map_source_get_license;
267 parent_class->get_license_url = map_source_get_license_url;
268 parent_class->get_logo = map_source_get_logo;
269 parent_class->get_name = map_source_get_name;
270 parent_class->get_uniq_id = map_source_get_uniq_id;
271 parent_class->get_label = map_source_get_label;
272 parent_class->get_tilesize_x = map_source_get_tilesize_x;
273 parent_class->get_tilesize_y = map_source_get_tilesize_y;
274 parent_class->get_drawmode = map_source_get_drawmode;
275 parent_class->get_file_extension = map_source_get_file_extension;
276 parent_class->download = _download;
277 parent_class->download_handle_init = _download_handle_init;
278 parent_class->download_handle_cleanup = _download_handle_cleanup;
280 /* Default implementation of methods */
281 klass->get_uri = NULL;
282 klass->get_hostname = NULL;
283 klass->get_download_options = NULL;
285 pspec = g_param_spec_string ("name",
287 "The name of the map that may be used as the file cache directory",
288 "Unknown" /* default value */,
289 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
290 g_object_class_install_property (object_class, PROP_NAME, pspec);
292 pspec = g_param_spec_uint ("id",
295 0 /* minimum value */,
296 G_MAXUINT /* maximum value */,
297 0 /* default value */,
298 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
299 g_object_class_install_property (object_class, PROP_ID, pspec);
301 pspec = g_param_spec_string ("label",
303 "The label of the map source",
304 "<no-set>" /* default value */,
305 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
306 g_object_class_install_property (object_class, PROP_LABEL, pspec);
308 pspec = g_param_spec_uint ("tilesize-x",
310 "Set the size of the tile (x)",
311 0 /* minimum value */,
312 G_MAXUINT16 /* maximum value */,
313 0 /* default value */,
315 g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
317 pspec = g_param_spec_uint ("tilesize-y",
319 "Set the size of the tile (y)",
320 0 /* minimum value */,
321 G_MAXUINT16 /* maximum value */,
322 0 /* default value */,
324 g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
326 pspec = g_param_spec_enum("drawmode",
328 "The mode used to draw map",
329 VIK_TYPE_VIEWPORT_DRAW_MODE,
330 VIK_VIEWPORT_DRAWMODE_UTM,
332 g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);
334 pspec = g_param_spec_string ("copyright",
336 "The copyright of the map source",
339 g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
341 pspec = g_param_spec_string ("license",
343 "The license of the map source",
345 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
346 g_object_class_install_property (object_class, PROP_LICENSE, pspec);
348 pspec = g_param_spec_string ("license-url",
350 "The URL of the license of the map source",
352 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
353 g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
355 pspec = g_param_spec_string ("file-extension",
357 "The file extension of tile files on disk",
358 ".png" /* default value */,
359 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
360 g_object_class_install_property (object_class, PROP_FILE_EXTENSION, pspec);
362 g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
364 object_class->finalize = vik_map_source_default_finalize;
368 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
370 /* Just ignore bbox and zoom level */
371 g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
373 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
375 (*fct) (data, priv->copyright);
379 map_source_get_license (VikMapSource *self)
381 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
383 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
385 return priv->license;
389 map_source_get_license_url (VikMapSource *self)
391 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
393 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
395 return priv->license_url;
398 static const GdkPixbuf *
399 map_source_get_logo (VikMapSource *self)
401 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
403 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
409 map_source_get_name (VikMapSource *self)
411 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
412 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
417 map_source_get_uniq_id (VikMapSource *self)
419 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
421 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
423 return priv->uniq_id;
427 map_source_get_label (VikMapSource *self)
429 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
431 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
437 map_source_get_tilesize_x (VikMapSource *self)
439 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
441 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
443 return priv->tilesize_x;
447 map_source_get_tilesize_y (VikMapSource *self)
449 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
451 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
453 return priv->tilesize_y;
456 static VikViewportDrawMode
457 map_source_get_drawmode (VikMapSource *self)
459 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
461 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
463 return priv->drawmode;
466 static DownloadResult_t
467 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
469 gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
470 gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
471 DownloadFileOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
472 DownloadResult_t res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
479 map_source_get_file_extension (VikMapSource *self)
481 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
482 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
483 return priv->file_extension;
487 _download_handle_init ( VikMapSource *self )
489 return a_download_handle_init ();
494 _download_handle_cleanup ( VikMapSource *self, void *handle )
496 a_download_handle_cleanup ( handle );
500 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
502 VikMapSourceDefaultClass *klass;
503 g_return_val_if_fail (self != NULL, 0);
504 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
505 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
507 g_return_val_if_fail (klass->get_uri != NULL, 0);
509 return (*klass->get_uri)(self, src);
513 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
515 VikMapSourceDefaultClass *klass;
516 g_return_val_if_fail (self != NULL, 0);
517 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
518 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
520 g_return_val_if_fail (klass->get_hostname != NULL, 0);
522 return (*klass->get_hostname)(self);
525 DownloadFileOptions *
526 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
528 VikMapSourceDefaultClass *klass;
529 g_return_val_if_fail (self != NULL, 0);
530 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
531 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
533 g_return_val_if_fail (klass->get_download_options != NULL, 0);
535 return (*klass->get_download_options)(self);
539 vik_map_source_default_get_url_display( VikMapSourceDefault *self, MapCoord *src )
541 VikMapSourceDefaultClass *klass;
542 g_return_val_if_fail (self != NULL, 0);
543 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
544 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
546 g_return_val_if_fail (klass->get_uri != NULL, 0);
547 g_return_val_if_fail (klass->get_hostname != NULL, 0);
549 gchar *newstr = NULL;
550 gchar *hostname = (*klass->get_hostname)(self);
551 gchar *url = (*klass->get_uri)(self, src);
552 if ( hostname && strlen(hostname)>1 ) {
553 if ( strstr (hostname, "://") == NULL ) {
555 newstr = g_strdup_printf ( "http://%s", hostname );
558 newstr = g_strdup ( hostname );
561 if ( url && strlen(url)>1 ) {
563 gchar *tmp = g_strdup ( newstr );
564 newstr = g_strdup_printf ( "%s%s", newstr, url );
568 newstr = g_strdup ( url );