]> git.street.me.uk Git - andy/viking.git/blob - src/vikmapsourcedefault.c
Improve tile info dialog message to print out the filename even when cache file is...
[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 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
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 );
48
49 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
50 struct _VikMapSourceDefaultPrivate
51 {
52         /* legal stuff */
53         gchar *copyright;
54         gchar *license;
55         gchar *license_url;
56         GdkPixbuf *logo;
57
58         gchar *name;
59         guint16 uniq_id;
60         gchar *label;
61         guint16 tilesize_x;
62         guint16 tilesize_y;
63         VikViewportDrawMode drawmode;
64 };
65
66 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
67
68 /* properties */
69 enum
70 {
71   PROP_0,
72
73   PROP_NAME,
74   PROP_ID,
75   PROP_LABEL,
76   PROP_TILESIZE_X,
77   PROP_TILESIZE_Y,
78   PROP_DRAWMODE,
79   PROP_COPYRIGHT,
80   PROP_LICENSE,
81   PROP_LICENSE_URL,
82 };
83
84 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
85
86 static void
87 vik_map_source_default_init (VikMapSourceDefault *object)
88 {
89   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
90   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
91
92   priv->label = NULL;
93   priv->copyright = NULL;
94   priv->license = NULL;
95   priv->license_url = NULL;
96   priv->logo = NULL;
97   priv->name = NULL;
98 }
99
100 static void
101 vik_map_source_default_finalize (GObject *object)
102 {
103   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
104   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
105
106   g_free (priv->label);
107   priv->label = NULL;
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;
114   g_free (priv->logo);
115   priv->license_url = NULL;
116   g_free (priv->name);
117   priv->name = NULL;
118
119   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
120 }
121
122 static void
123 vik_map_source_default_set_property (GObject      *object,
124                                      guint         property_id,
125                                      const GValue *value,
126                                      GParamSpec   *pspec)
127 {
128   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
129   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
130
131   switch (property_id)
132     {
133     case PROP_NAME:
134       // Sanitize the name here for file usage
135       // A simple check just to prevent containing slashes ATM
136       g_free (priv->name);
137       priv->name = g_strdup(g_value_get_string (value));
138       g_strdelimit (priv->name, "\\/", 'x' );
139       break;
140
141     case PROP_ID:
142       priv->uniq_id = g_value_get_uint (value);
143       break;
144
145     case PROP_LABEL:
146       g_free (priv->label);
147       priv->label = g_strdup(g_value_get_string (value));
148       break;
149
150     case PROP_TILESIZE_X:
151       priv->tilesize_x = g_value_get_uint (value);
152       break;
153
154     case PROP_TILESIZE_Y:
155       priv->tilesize_y = g_value_get_uint (value);
156       break;
157
158     case PROP_DRAWMODE:
159       priv->drawmode = g_value_get_enum(value);
160       break;
161
162     case PROP_COPYRIGHT:
163       g_free (priv->copyright);
164       priv->copyright = g_strdup(g_value_get_string (value));
165       break;
166
167     case PROP_LICENSE:
168       g_free (priv->license);
169       priv->license = g_strdup(g_value_get_string (value));
170       break;
171
172     case PROP_LICENSE_URL:
173       g_free (priv->license_url);
174       priv->license_url = g_strdup(g_value_get_string (value));
175       break;
176
177     default:
178       /* We don't have any other property... */
179       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
180       break;
181     }
182 }
183
184 static void
185 vik_map_source_default_get_property (GObject    *object,
186                                      guint       property_id,
187                                      GValue     *value,
188                                      GParamSpec *pspec)
189 {
190   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
191   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
192
193   switch (property_id)
194     {
195     case PROP_NAME:
196       g_value_set_string (value, priv->name);
197       break;
198
199     case PROP_ID:
200       g_value_set_uint (value, priv->uniq_id);
201       break;
202
203     case PROP_LABEL:
204       g_value_set_string (value, priv->label);
205       break;
206
207     case PROP_TILESIZE_X:
208       g_value_set_uint (value, priv->tilesize_x);
209       break;
210
211     case PROP_TILESIZE_Y:
212       g_value_set_uint (value, priv->tilesize_y);
213       break;
214
215     case PROP_DRAWMODE:
216       g_value_set_enum (value, priv->drawmode);
217       break;
218
219     case PROP_COPYRIGHT:
220       g_value_set_string (value, priv->copyright);
221       break;
222
223     case PROP_LICENSE:
224       g_value_set_string (value, priv->license);
225       break;
226
227     case PROP_LICENSE_URL:
228       g_value_set_string (value, priv->license_url);
229       break;
230
231     default:
232       /* We don't have any other property... */
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
234       break;
235     }
236 }
237
238 static void
239 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
240 {
241         GObjectClass* object_class = G_OBJECT_CLASS (klass);
242         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
243     GParamSpec *pspec = NULL;
244         
245         object_class->set_property = vik_map_source_default_set_property;
246     object_class->get_property = vik_map_source_default_get_property;
247         
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;
262
263         /* Default implementation of methods */
264         klass->get_uri = NULL;
265         klass->get_hostname = NULL;
266         klass->get_download_options = NULL;
267
268         pspec = g_param_spec_string ("name",
269                                      "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);
274
275         pspec = g_param_spec_uint ("id",
276                                    "Id of the tool",
277                                "Set the 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);
283
284         pspec = g_param_spec_string ("label",
285                                      "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);
290
291         pspec = g_param_spec_uint ("tilesize-x",
292                                    "TileSizeX",
293                                "Set the size of the tile (x)",
294                                0  /* minimum value */,
295                                G_MAXUINT16 /* maximum value */,
296                                0  /* default value */,
297                                G_PARAM_READWRITE);
298         g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
299
300         pspec = g_param_spec_uint ("tilesize-y",
301                                    "TileSizeY",
302                                "Set the size of the tile (y)",
303                                0  /* minimum value */,
304                                G_MAXUINT16 /* maximum value */,
305                                0  /* default value */,
306                                G_PARAM_READWRITE);
307         g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
308
309         pspec = g_param_spec_enum("drawmode",
310                               "Drawmode",
311                               "The mode used to draw map",
312                               VIK_TYPE_VIEWPORT_DRAW_MODE,
313                               VIK_VIEWPORT_DRAWMODE_UTM,
314                               G_PARAM_READWRITE);
315     g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);                                    
316
317         pspec = g_param_spec_string ("copyright",
318                                      "Copyright",
319                                      "The copyright of the map source",
320                                      NULL,
321                                      G_PARAM_READWRITE);
322         g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
323
324         pspec = g_param_spec_string ("license",
325                                      "License",
326                                      "The license of the map source",
327                                      NULL,
328                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
329         g_object_class_install_property (object_class, PROP_LICENSE, pspec);
330
331         pspec = g_param_spec_string ("license-url",
332                                      "License URL",
333                                      "The URL of the license of the map source",
334                                      NULL,
335                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
336         g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
337
338         g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
339
340         object_class->finalize = vik_map_source_default_finalize;
341 }
342
343 static void
344 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
345 {
346         /* Just ignore bbox and zoom level */
347         g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
348
349         g_debug ("%s: %g %g %g %g %g", __FUNCTION__,
350                 bbox.south, bbox.north, bbox.east, bbox.west,
351                 zoom);
352         
353         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
354
355         (*fct) (data, priv->copyright);
356 }
357
358 static const gchar *
359 map_source_get_license (VikMapSource *self)
360 {
361         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
362         
363         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
364
365         return priv->license;
366 }
367
368 static const gchar *
369 map_source_get_license_url (VikMapSource *self)
370 {
371         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
372         
373         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
374
375         return priv->license_url;
376 }
377
378 static const GdkPixbuf *
379 map_source_get_logo (VikMapSource *self)
380 {
381         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
382
383         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
384
385         return priv->logo;
386 }
387
388 static const gchar *
389 map_source_get_name (VikMapSource *self)
390 {
391         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
392         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
393         return priv->name;
394 }
395
396 static guint16
397 map_source_get_uniq_id (VikMapSource *self)
398 {
399         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
400         
401         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
402
403         return priv->uniq_id;
404 }
405
406 static const gchar *
407 map_source_get_label (VikMapSource *self)
408 {
409         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
410         
411         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
412
413         return priv->label;
414 }
415
416 static guint16
417 map_source_get_tilesize_x (VikMapSource *self)
418 {
419         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
420
421     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
422
423         return priv->tilesize_x;
424 }
425
426 static guint16
427 map_source_get_tilesize_y (VikMapSource *self)
428 {
429         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
430
431     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
432
433         return priv->tilesize_y;
434 }
435
436 static VikViewportDrawMode
437 map_source_get_drawmode (VikMapSource *self)
438 {
439         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
440
441     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
442
443         return priv->drawmode;
444 }
445
446 static DownloadResult_t
447 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
448 {
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 );
453    g_free ( uri );
454    g_free ( host );
455    return res;
456 }
457
458 static void *
459 _download_handle_init ( VikMapSource *self )
460 {
461    return a_download_handle_init ();
462 }
463
464
465 static void
466 _download_handle_cleanup ( VikMapSource *self, void *handle )
467 {
468    a_download_handle_cleanup ( handle );
469 }
470
471 gchar *
472 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
473 {
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);
478
479         g_return_val_if_fail (klass->get_uri != NULL, 0);
480
481         return (*klass->get_uri)(self, src);
482 }
483
484 gchar *
485 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
486 {
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);
491
492         g_return_val_if_fail (klass->get_hostname != NULL, 0);
493
494         return (*klass->get_hostname)(self);
495 }
496
497 DownloadMapOptions *
498 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
499 {
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);
504
505         g_return_val_if_fail (klass->get_download_options != NULL, 0);
506
507         return (*klass->get_download_options)(self);
508 }