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);
45 static DownloadResult_t _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
46 static void * _download_handle_init ( VikMapSource *self );
47 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
49 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
50 struct _VikMapSourceDefaultPrivate
63 VikViewportDrawMode drawmode;
66 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
84 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
87 vik_map_source_default_init (VikMapSourceDefault *object)
89 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
90 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
93 priv->copyright = NULL;
95 priv->license_url = NULL;
101 vik_map_source_default_finalize (GObject *object)
103 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
104 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
106 g_free (priv->label);
108 g_free (priv->copyright);
109 priv->copyright = NULL;
110 g_free (priv->license);
111 priv->license = NULL;
112 g_free (priv->license_url);
113 priv->license_url = NULL;
115 priv->license_url = NULL;
119 G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
123 vik_map_source_default_set_property (GObject *object,
128 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
129 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
134 // Sanitize the name here for file usage
135 // A simple check just to prevent containing slashes ATM
137 priv->name = g_strdup(g_value_get_string (value));
138 g_strdelimit (priv->name, "\\/", 'x' );
142 priv->uniq_id = g_value_get_uint (value);
146 g_free (priv->label);
147 priv->label = g_strdup(g_value_get_string (value));
150 case PROP_TILESIZE_X:
151 priv->tilesize_x = g_value_get_uint (value);
154 case PROP_TILESIZE_Y:
155 priv->tilesize_y = g_value_get_uint (value);
159 priv->drawmode = g_value_get_enum(value);
163 g_free (priv->copyright);
164 priv->copyright = g_strdup(g_value_get_string (value));
168 g_free (priv->license);
169 priv->license = g_strdup(g_value_get_string (value));
172 case PROP_LICENSE_URL:
173 g_free (priv->license_url);
174 priv->license_url = g_strdup(g_value_get_string (value));
178 /* We don't have any other property... */
179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185 vik_map_source_default_get_property (GObject *object,
190 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
191 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
196 g_value_set_string (value, priv->name);
200 g_value_set_uint (value, priv->uniq_id);
204 g_value_set_string (value, priv->label);
207 case PROP_TILESIZE_X:
208 g_value_set_uint (value, priv->tilesize_x);
211 case PROP_TILESIZE_Y:
212 g_value_set_uint (value, priv->tilesize_y);
216 g_value_set_enum (value, priv->drawmode);
220 g_value_set_string (value, priv->copyright);
224 g_value_set_string (value, priv->license);
227 case PROP_LICENSE_URL:
228 g_value_set_string (value, priv->license_url);
232 /* We don't have any other property... */
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
239 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
241 GObjectClass* object_class = G_OBJECT_CLASS (klass);
242 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
243 GParamSpec *pspec = NULL;
245 object_class->set_property = vik_map_source_default_set_property;
246 object_class->get_property = vik_map_source_default_get_property;
248 /* Overiding methods */
249 parent_class->get_copyright = map_source_get_copyright;
250 parent_class->get_license = map_source_get_license;
251 parent_class->get_license_url = map_source_get_license_url;
252 parent_class->get_logo = map_source_get_logo;
253 parent_class->get_name = map_source_get_name;
254 parent_class->get_uniq_id = map_source_get_uniq_id;
255 parent_class->get_label = map_source_get_label;
256 parent_class->get_tilesize_x = map_source_get_tilesize_x;
257 parent_class->get_tilesize_y = map_source_get_tilesize_y;
258 parent_class->get_drawmode = map_source_get_drawmode;
259 parent_class->download = _download;
260 parent_class->download_handle_init = _download_handle_init;
261 parent_class->download_handle_cleanup = _download_handle_cleanup;
263 /* Default implementation of methods */
264 klass->get_uri = NULL;
265 klass->get_hostname = NULL;
266 klass->get_download_options = NULL;
268 pspec = g_param_spec_string ("name",
270 "The name of the map that may be used as the file cache directory",
271 "Unknown" /* default value */,
272 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
273 g_object_class_install_property (object_class, PROP_NAME, pspec);
275 pspec = g_param_spec_uint ("id",
278 0 /* minimum value */,
279 G_MAXUINT /* maximum value */,
280 0 /* default value */,
281 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
282 g_object_class_install_property (object_class, PROP_ID, pspec);
284 pspec = g_param_spec_string ("label",
286 "The label of the map source",
287 "<no-set>" /* default value */,
288 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
289 g_object_class_install_property (object_class, PROP_LABEL, pspec);
291 pspec = g_param_spec_uint ("tilesize-x",
293 "Set the size of the tile (x)",
294 0 /* minimum value */,
295 G_MAXUINT16 /* maximum value */,
296 0 /* default value */,
298 g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
300 pspec = g_param_spec_uint ("tilesize-y",
302 "Set the size of the tile (y)",
303 0 /* minimum value */,
304 G_MAXUINT16 /* maximum value */,
305 0 /* default value */,
307 g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
309 pspec = g_param_spec_enum("drawmode",
311 "The mode used to draw map",
312 VIK_TYPE_VIEWPORT_DRAW_MODE,
313 VIK_VIEWPORT_DRAWMODE_UTM,
315 g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);
317 pspec = g_param_spec_string ("copyright",
319 "The copyright of the map source",
322 g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
324 pspec = g_param_spec_string ("license",
326 "The license of the map source",
328 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
329 g_object_class_install_property (object_class, PROP_LICENSE, pspec);
331 pspec = g_param_spec_string ("license-url",
333 "The URL of the license of the map source",
335 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
336 g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
338 g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
340 object_class->finalize = vik_map_source_default_finalize;
344 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
346 /* Just ignore bbox and zoom level */
347 g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
349 g_debug ("%s: %g %g %g %g %g", __FUNCTION__,
350 bbox.south, bbox.north, bbox.east, bbox.west,
353 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
355 (*fct) (data, priv->copyright);
359 map_source_get_license (VikMapSource *self)
361 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
363 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
365 return priv->license;
369 map_source_get_license_url (VikMapSource *self)
371 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
373 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
375 return priv->license_url;
378 static const GdkPixbuf *
379 map_source_get_logo (VikMapSource *self)
381 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
383 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
389 map_source_get_name (VikMapSource *self)
391 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
392 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
397 map_source_get_uniq_id (VikMapSource *self)
399 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
401 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
403 return priv->uniq_id;
407 map_source_get_label (VikMapSource *self)
409 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
411 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
417 map_source_get_tilesize_x (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->tilesize_x;
427 map_source_get_tilesize_y (VikMapSource *self)
429 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
431 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
433 return priv->tilesize_y;
436 static VikViewportDrawMode
437 map_source_get_drawmode (VikMapSource *self)
439 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
441 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
443 return priv->drawmode;
446 static DownloadResult_t
447 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
449 gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
450 gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
451 DownloadMapOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
452 DownloadResult_t res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
459 _download_handle_init ( VikMapSource *self )
461 return a_download_handle_init ();
466 _download_handle_cleanup ( VikMapSource *self, void *handle )
468 a_download_handle_cleanup ( handle );
472 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
474 VikMapSourceDefaultClass *klass;
475 g_return_val_if_fail (self != NULL, 0);
476 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
477 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
479 g_return_val_if_fail (klass->get_uri != NULL, 0);
481 return (*klass->get_uri)(self, src);
485 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
487 VikMapSourceDefaultClass *klass;
488 g_return_val_if_fail (self != NULL, 0);
489 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
490 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
492 g_return_val_if_fail (klass->get_hostname != NULL, 0);
494 return (*klass->get_hostname)(self);
498 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
500 VikMapSourceDefaultClass *klass;
501 g_return_val_if_fail (self != NULL, 0);
502 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
503 klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
505 g_return_val_if_fail (klass->get_download_options != NULL, 0);
507 return (*klass->get_download_options)(self);