]> git.street.me.uk Git - andy/viking.git/blob - src/vikmapsourcedefault.c
Add Show/Hide all Layers within an Aggregate Layer.
[andy/viking.git] / src / vikmapsourcedefault.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking
4  * Copyright (C) 2009-2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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
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
29 #include "vikmapsourcedefault.h"
30 #include "vikenumtypes.h"
31 #include "download.h"
32
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);
37
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);
43
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
48 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
49 struct _VikMapSourceDefaultPrivate
50 {
51         /* legal stuff */
52         gchar *copyright;
53         gchar *license;
54         gchar *license_url;
55         GdkPixbuf *logo;
56
57         guint8 uniq_id;
58         gchar *label;
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
66 /* properties */
67 enum
68 {
69   PROP_0,
70
71   PROP_ID,
72   PROP_LABEL,
73   PROP_TILESIZE_X,
74   PROP_TILESIZE_Y,
75   PROP_DRAWMODE,
76   PROP_COPYRIGHT,
77   PROP_LICENSE,
78   PROP_LICENSE_URL,
79 };
80
81 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
82
83 static void
84 vik_map_source_default_init (VikMapSourceDefault *object)
85 {
86   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
87   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
88
89   priv->label = NULL;
90   priv->copyright = NULL;
91   priv->license = NULL;
92   priv->license_url = NULL;
93   priv->logo = NULL;
94 }
95
96 static void
97 vik_map_source_default_finalize (GObject *object)
98 {
99   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
100   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
101
102   g_free (priv->label);
103   priv->label = NULL;
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;
110   g_free (priv->logo);
111   priv->license_url = NULL;
112         
113   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
114 }
115
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
131     case PROP_LABEL:
132       g_free (priv->label);
133       priv->label = g_strdup(g_value_get_string (value));
134       break;
135
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
148     case PROP_COPYRIGHT:
149       g_free (priv->copyright);
150       priv->copyright = g_strdup(g_value_get_string (value));
151       break;
152
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
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
185     case PROP_LABEL:
186       g_value_set_string (value, priv->label);
187       break;
188
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
201     case PROP_COPYRIGHT:
202       g_value_set_string (value, priv->copyright);
203       break;
204
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
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
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);
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         
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;
243
244         /* Default implementation of methods */
245         klass->get_uri = NULL;
246         klass->get_hostname = NULL;
247         klass->get_download_options = NULL;
248
249         pspec = g_param_spec_uint ("id",
250                                    "Id of the tool",
251                                "Set the 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);
257
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
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 */,
271                                G_PARAM_READWRITE);
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 */,
280                                G_PARAM_READWRITE);
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
291         pspec = g_param_spec_string ("copyright",
292                                      "Copyright",
293                                      "The copyright of the map source",
294                                      NULL,
295                                      G_PARAM_READWRITE);
296         g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
297
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
312         g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
313
314         object_class->finalize = vik_map_source_default_finalize;
315 }
316
317 static void
318 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
319 {
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);
326         
327         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
328
329         (*fct) (data, priv->copyright);
330 }
331
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
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
362 static guint8
363 map_source_get_uniq_id (VikMapSource *self)
364 {
365         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint8)0);
366         
367         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
368
369         return priv->uniq_id;
370 }
371
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
382 static guint16
383 map_source_get_tilesize_x (VikMapSource *self)
384 {
385         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
386
387     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
388
389         return priv->tilesize_x;
390 }
391
392 static guint16
393 map_source_get_tilesize_y (VikMapSource *self)
394 {
395         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
396
397     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
398
399         return priv->tilesize_y;
400 }
401
402 static VikViewportDrawMode
403 map_source_get_drawmode (VikMapSource *self)
404 {
405         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
406
407     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
408
409         return priv->drawmode;
410 }
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 {
435    a_download_handle_cleanup ( handle );
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 }