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 guint8 map_source_get_uniq_id (VikMapSource *self);
39 static const gchar *map_source_get_label (VikMapSource *self);
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);
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 );
48 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
49 struct _VikMapSourceDefaultPrivate
61 VikViewportDrawMode drawmode;
64 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
81 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
84 vik_map_source_default_init (VikMapSourceDefault *object)
86 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
87 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
90 priv->copyright = NULL;
92 priv->license_url = NULL;
97 vik_map_source_default_finalize (GObject *object)
99 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
100 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
102 g_free (priv->label);
104 g_free (priv->copyright);
105 priv->copyright = NULL;
106 g_free (priv->license);
107 priv->license = NULL;
108 g_free (priv->license_url);
109 priv->license_url = NULL;
111 priv->license_url = NULL;
113 G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
117 vik_map_source_default_set_property (GObject *object,
122 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
123 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
128 priv->uniq_id = g_value_get_uint (value);
132 g_free (priv->label);
133 priv->label = g_strdup(g_value_get_string (value));
136 case PROP_TILESIZE_X:
137 priv->tilesize_x = g_value_get_uint (value);
140 case PROP_TILESIZE_Y:
141 priv->tilesize_y = g_value_get_uint (value);
145 priv->drawmode = g_value_get_enum(value);
149 g_free (priv->copyright);
150 priv->copyright = g_strdup(g_value_get_string (value));
154 g_free (priv->license);
155 priv->license = g_strdup(g_value_get_string (value));
158 case PROP_LICENSE_URL:
159 g_free (priv->license_url);
160 priv->license_url = g_strdup(g_value_get_string (value));
164 /* We don't have any other property... */
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
171 vik_map_source_default_get_property (GObject *object,
176 VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
177 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
182 g_value_set_uint (value, priv->uniq_id);
186 g_value_set_string (value, priv->label);
189 case PROP_TILESIZE_X:
190 g_value_set_uint (value, priv->tilesize_x);
193 case PROP_TILESIZE_Y:
194 g_value_set_uint (value, priv->tilesize_y);
198 g_value_set_enum (value, priv->drawmode);
202 g_value_set_string (value, priv->copyright);
206 g_value_set_string (value, priv->license);
209 case PROP_LICENSE_URL:
210 g_value_set_string (value, priv->license_url);
214 /* We don't have any other property... */
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
221 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
223 GObjectClass* object_class = G_OBJECT_CLASS (klass);
224 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
225 GParamSpec *pspec = NULL;
227 object_class->set_property = vik_map_source_default_set_property;
228 object_class->get_property = vik_map_source_default_get_property;
230 /* Overiding methods */
231 parent_class->get_copyright = map_source_get_copyright;
232 parent_class->get_license = map_source_get_license;
233 parent_class->get_license_url = map_source_get_license_url;
234 parent_class->get_logo = map_source_get_logo;
235 parent_class->get_uniq_id = map_source_get_uniq_id;
236 parent_class->get_label = map_source_get_label;
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;
240 parent_class->download = _download;
241 parent_class->download_handle_init = _download_handle_init;
242 parent_class->download_handle_cleanup = _download_handle_cleanup;
244 /* Default implementation of methods */
245 klass->get_uri = NULL;
246 klass->get_hostname = NULL;
247 klass->get_download_options = NULL;
249 pspec = g_param_spec_uint ("id",
252 0 /* minimum value */,
253 G_MAXUINT /* maximum value */,
254 0 /* default value */,
255 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
256 g_object_class_install_property (object_class, PROP_ID, pspec);
258 pspec = g_param_spec_string ("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);
265 pspec = g_param_spec_uint ("tilesize-x",
267 "Set the size of the tile (x)",
268 0 /* minimum value */,
269 G_MAXUINT16 /* maximum value */,
270 0 /* default value */,
271 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
272 g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
274 pspec = g_param_spec_uint ("tilesize-y",
276 "Set the size of the tile (y)",
277 0 /* minimum value */,
278 G_MAXUINT16 /* maximum value */,
279 0 /* default value */,
280 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
281 g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
283 pspec = g_param_spec_enum("drawmode",
285 "The mode used to draw map",
286 VIK_TYPE_VIEWPORT_DRAW_MODE,
287 VIK_VIEWPORT_DRAWMODE_UTM,
289 g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);
291 pspec = g_param_spec_string ("copyright",
293 "The copyright of the map source",
296 g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
298 pspec = g_param_spec_string ("license",
300 "The license of the map source",
302 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
303 g_object_class_install_property (object_class, PROP_LICENSE, pspec);
305 pspec = g_param_spec_string ("license-url",
307 "The URL of the license of the map source",
309 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
310 g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
312 g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
314 object_class->finalize = vik_map_source_default_finalize;
318 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
320 /* Just ignore bbox and zoom level */
321 g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
323 g_debug ("%s: %g %g %g %g %g", __FUNCTION__,
324 bbox.south, bbox.north, bbox.east, bbox.west,
327 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
329 (*fct) (data, priv->copyright);
333 map_source_get_license (VikMapSource *self)
335 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
337 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
339 return priv->license;
343 map_source_get_license_url (VikMapSource *self)
345 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
347 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
349 return priv->license_url;
352 static const GdkPixbuf *
353 map_source_get_logo (VikMapSource *self)
355 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
357 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
363 map_source_get_uniq_id (VikMapSource *self)
365 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint8)0);
367 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
369 return priv->uniq_id;
373 map_source_get_label (VikMapSource *self)
375 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
377 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
383 map_source_get_tilesize_x (VikMapSource *self)
385 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
387 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
389 return priv->tilesize_x;
393 map_source_get_tilesize_y (VikMapSource *self)
395 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
397 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
399 return priv->tilesize_y;
402 static VikViewportDrawMode
403 map_source_get_drawmode (VikMapSource *self)
405 g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
407 VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
409 return priv->drawmode;
413 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
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 );
426 _download_handle_init ( VikMapSource *self )
428 return a_download_handle_init ();
433 _download_handle_cleanup ( VikMapSource *self, void *handle )
435 a_download_handle_cleanup ( handle );
439 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
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);
446 g_return_val_if_fail (klass->get_uri != NULL, 0);
448 return (*klass->get_uri)(self, src);
452 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
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);
459 g_return_val_if_fail (klass->get_hostname != NULL, 0);
461 return (*klass->get_hostname)(self);
465 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
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);
472 g_return_val_if_fail (klass->get_download_options != NULL, 0);
474 return (*klass->get_download_options)(self);