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"
33 static void map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
34 static const gchar *map_source_get_license (VikMapSource *self);
35 static const gchar *map_source_get_license_url (VikMapSource *self);
36 static const GdkPixbuf *map_source_get_logo (VikMapSource *self);
38 static const gchar *map_source_get_name (VikMapSource *self);
39 static guint16 map_source_get_uniq_id (VikMapSource *self);
40 static const gchar *map_source_get_label (VikMapSource *self);
41 static guint16 map_source_get_tilesize_x (VikMapSource *self);
42 static guint16 map_source_get_tilesize_y (VikMapSource *self);
43 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
44 static const gchar *map_source_get_file_extension (VikMapSource *self);
46 static DownloadResult_t _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
47 static void * _download_handle_init ( VikMapSource *self );
48 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
50 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
51 struct _VikMapSourceDefaultPrivate
64 VikViewportDrawMode drawmode;
65 gchar *file_extension;
68 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
87 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
90 vik_map_source_default_init (VikMapSourceDefault *object)
92 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
93 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
96 priv->copyright = NULL;
98 priv->license_url = NULL;
101 priv->file_extension = NULL;
105 vik_map_source_default_finalize (GObject *object)
107 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
108 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
110 g_free (priv->label);
112 g_free (priv->copyright);
113 priv->copyright = NULL;
114 g_free (priv->license);
115 priv->license = NULL;
116 g_free (priv->license_url);
117 priv->license_url = NULL;
119 priv->license_url = NULL;
122 g_free (priv->file_extension);
123 priv->file_extension = NULL;
125 G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
129 vik_map_source_default_set_property (GObject *object,
134 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
135 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
140 // Sanitize the name here for file usage
141 // A simple check just to prevent containing slashes ATM
143 priv->name = g_strdup(g_value_get_string (value));
144 g_strdelimit (priv->name, "\\/", 'x' );
148 priv->uniq_id = g_value_get_uint (value);
152 g_free (priv->label);
153 priv->label = g_strdup(g_value_get_string (value));
156 case PROP_TILESIZE_X:
157 priv->tilesize_x = g_value_get_uint (value);
160 case PROP_TILESIZE_Y:
161 priv->tilesize_y = g_value_get_uint (value);
165 priv->drawmode = g_value_get_enum(value);
169 g_free (priv->copyright);
170 priv->copyright = g_strdup(g_value_get_string (value));
174 g_free (priv->license);
175 priv->license = g_strdup(g_value_get_string (value));
178 case PROP_LICENSE_URL:
179 g_free (priv->license_url);
180 priv->license_url = g_strdup(g_value_get_string (value));
183 case PROP_FILE_EXTENSION:
184 g_free (priv->file_extension);
185 priv->file_extension = g_strdup(g_value_get_string(value));
189 /* We don't have any other property... */
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196 vik_map_source_default_get_property (GObject *object,
201 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
202 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
207 g_value_set_string (value, priv->name);
211 g_value_set_uint (value, priv->uniq_id);
215 g_value_set_string (value, priv->label);
218 case PROP_TILESIZE_X:
219 g_value_set_uint (value, priv->tilesize_x);
222 case PROP_TILESIZE_Y:
223 g_value_set_uint (value, priv->tilesize_y);
227 g_value_set_enum (value, priv->drawmode);
231 g_value_set_string (value, priv->copyright);
235 g_value_set_string (value, priv->license);
238 case PROP_LICENSE_URL:
239 g_value_set_string (value, priv->license_url);
242 case PROP_FILE_EXTENSION:
243 g_value_set_string (value, priv->file_extension);
247 /* We don't have any other property... */
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
254 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
256 GObjectClass* object_class = G_OBJECT_CLASS (klass);
257 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
258 GParamSpec *pspec = NULL;
260 object_class->set_property = vik_map_source_default_set_property;
261 object_class->get_property = vik_map_source_default_get_property;
263 /* Overiding methods */
264 parent_class->get_copyright = map_source_get_copyright;
265 parent_class->get_license = map_source_get_license;
266 parent_class->get_license_url = map_source_get_license_url;
267 parent_class->get_logo = map_source_get_logo;
268 parent_class->get_name = map_source_get_name;
269 parent_class->get_uniq_id = map_source_get_uniq_id;
270 parent_class->get_label = map_source_get_label;
271 parent_class->get_tilesize_x = map_source_get_tilesize_x;
272 parent_class->get_tilesize_y = map_source_get_tilesize_y;
273 parent_class->get_drawmode = map_source_get_drawmode;
274 parent_class->get_file_extension = map_source_get_file_extension;
275 parent_class->download = _download;
276 parent_class->download_handle_init = _download_handle_init;
277 parent_class->download_handle_cleanup = _download_handle_cleanup;
279 /* Default implementation of methods */
280 klass->get_uri = NULL;
281 klass->get_hostname = NULL;
282 klass->get_download_options = NULL;
284 pspec = g_param_spec_string ("name",
286 "The name of the map that may be used as the file cache directory",
287 "Unknown" /* default value */,
288 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
289 g_object_class_install_property (object_class, PROP_NAME, pspec);
291 pspec = g_param_spec_uint ("id",
294 0 /* minimum value */,
295 G_MAXUINT /* maximum value */,
296 0 /* default value */,
297 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
298 g_object_class_install_property (object_class, PROP_ID, pspec);
300 pspec = g_param_spec_string ("label",
302 "The label of the map source",
303 "<no-set>" /* default value */,
304 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
305 g_object_class_install_property (object_class, PROP_LABEL, pspec);
307 pspec = g_param_spec_uint ("tilesize-x",
309 "Set the size of the tile (x)",
310 0 /* minimum value */,
311 G_MAXUINT16 /* maximum value */,
312 0 /* default value */,
314 g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
316 pspec = g_param_spec_uint ("tilesize-y",
318 "Set the size of the tile (y)",
319 0 /* minimum value */,
320 G_MAXUINT16 /* maximum value */,
321 0 /* default value */,
323 g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
325 pspec = g_param_spec_enum("drawmode",
327 "The mode used to draw map",
328 VIK_TYPE_VIEWPORT_DRAW_MODE,
329 VIK_VIEWPORT_DRAWMODE_UTM,
331 g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);
333 pspec = g_param_spec_string ("copyright",
335 "The copyright of the map source",
338 g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
340 pspec = g_param_spec_string ("license",
342 "The license of the map source",
344 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
345 g_object_class_install_property (object_class, PROP_LICENSE, pspec);
347 pspec = g_param_spec_string ("license-url",
349 "The URL of the license of the map source",
351 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
352 g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
354 pspec = g_param_spec_string ("file-extension",
356 "The file extension of tile files on disk",
357 ".png" /* default value */,
358 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
359 g_object_class_install_property (object_class, PROP_FILE_EXTENSION, pspec);
361 g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
363 object_class->finalize = vik_map_source_default_finalize;
367 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
369 /* Just ignore bbox and zoom level */
370 g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
372 g_debug ("%s: %g %g %g %g %g", __FUNCTION__,
373 bbox.south, bbox.north, bbox.east, bbox.west,
376 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
378 (*fct) (data, priv->copyright);
382 map_source_get_license (VikMapSource *self)
384 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
386 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
388 return priv->license;
392 map_source_get_license_url (VikMapSource *self)
394 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
396 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
398 return priv->license_url;
401 static const GdkPixbuf *
402 map_source_get_logo (VikMapSource *self)
404 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
406 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
412 map_source_get_name (VikMapSource *self)
414 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
415 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
420 map_source_get_uniq_id (VikMapSource *self)
422 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
424 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
426 return priv->uniq_id;
430 map_source_get_label (VikMapSource *self)
432 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
434 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
440 map_source_get_tilesize_x (VikMapSource *self)
442 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
444 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
446 return priv->tilesize_x;
450 map_source_get_tilesize_y (VikMapSource *self)
452 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
454 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
456 return priv->tilesize_y;
459 static VikViewportDrawMode
460 map_source_get_drawmode (VikMapSource *self)
462 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
464 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
466 return priv->drawmode;
469 static DownloadResult_t
470 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
472 gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
473 gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
474 DownloadMapOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
475 DownloadResult_t res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
482 map_source_get_file_extension (VikMapSource *self)
484 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
485 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
486 return priv->file_extension;
490 _download_handle_init ( VikMapSource *self )
492 return a_download_handle_init ();
497 _download_handle_cleanup ( VikMapSource *self, void *handle )
499 a_download_handle_cleanup ( handle );
503 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
505 VikMapSourceDefaultClass *klass;
506 g_return_val_if_fail (self != NULL, 0);
507 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
508 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
510 g_return_val_if_fail (klass->get_uri != NULL, 0);
512 return (*klass->get_uri)(self, src);
516 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
518 VikMapSourceDefaultClass *klass;
519 g_return_val_if_fail (self != NULL, 0);
520 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
521 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
523 g_return_val_if_fail (klass->get_hostname != NULL, 0);
525 return (*klass->get_hostname)(self);
529 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
531 VikMapSourceDefaultClass *klass;
532 g_return_val_if_fail (self != NULL, 0);
533 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
534 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
536 g_return_val_if_fail (klass->get_download_options != NULL, 0);
538 return (*klass->get_download_options)(self);