]> git.street.me.uk Git - andy/viking.git/blob - src/vikmapsourcedefault.c
Really empty GPS realtime layers.
[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 #include "string.h"
33
34 static void map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
35 static const gchar *map_source_get_license (VikMapSource *self);
36 static const gchar *map_source_get_license_url (VikMapSource *self);
37 static const GdkPixbuf *map_source_get_logo (VikMapSource *self);
38
39 static const gchar *map_source_get_name (VikMapSource *self);
40 static guint16 map_source_get_uniq_id (VikMapSource *self);
41 static const gchar *map_source_get_label (VikMapSource *self);
42 static guint16 map_source_get_tilesize_x (VikMapSource *self);
43 static guint16 map_source_get_tilesize_y (VikMapSource *self);
44 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
45 static const gchar *map_source_get_file_extension (VikMapSource *self);
46
47 static DownloadResult_t _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
48 static void * _download_handle_init ( VikMapSource *self );
49 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
50
51 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
52 struct _VikMapSourceDefaultPrivate
53 {
54         /* legal stuff */
55         gchar *copyright;
56         gchar *license;
57         gchar *license_url;
58         GdkPixbuf *logo;
59
60         gchar *name;
61         guint16 uniq_id;
62         gchar *label;
63         guint16 tilesize_x;
64         guint16 tilesize_y;
65         VikViewportDrawMode drawmode;
66         gchar *file_extension;
67 };
68
69 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
70
71 /* properties */
72 enum
73 {
74   PROP_0,
75
76   PROP_NAME,
77   PROP_ID,
78   PROP_LABEL,
79   PROP_TILESIZE_X,
80   PROP_TILESIZE_Y,
81   PROP_DRAWMODE,
82   PROP_COPYRIGHT,
83   PROP_LICENSE,
84   PROP_LICENSE_URL,
85   PROP_FILE_EXTENSION,
86 };
87
88 G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
89
90 static void
91 vik_map_source_default_init (VikMapSourceDefault *object)
92 {
93   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
94   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
95
96   priv->label = NULL;
97   priv->copyright = NULL;
98   priv->license = NULL;
99   priv->license_url = NULL;
100   priv->logo = NULL;
101   priv->name = NULL;
102   priv->file_extension = NULL;
103 }
104
105 static void
106 vik_map_source_default_finalize (GObject *object)
107 {
108   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
109   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
110
111   g_free (priv->label);
112   priv->label = NULL;
113   g_free (priv->copyright);
114   priv->copyright = NULL;
115   g_free (priv->license);
116   priv->license = NULL;
117   g_free (priv->license_url);
118   priv->license_url = NULL;
119   g_free (priv->logo);
120   priv->license_url = NULL;
121   g_free (priv->name);
122   priv->name = NULL;
123   g_free (priv->file_extension);
124   priv->file_extension = NULL;
125
126   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
127 }
128
129 static void
130 vik_map_source_default_set_property (GObject      *object,
131                                      guint         property_id,
132                                      const GValue *value,
133                                      GParamSpec   *pspec)
134 {
135   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
136   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
137
138   switch (property_id)
139     {
140     case PROP_NAME:
141       // Sanitize the name here for file usage
142       // A simple check just to prevent containing slashes ATM
143       g_free (priv->name);
144       priv->name = g_strdup(g_value_get_string (value));
145       g_strdelimit (priv->name, "\\/", 'x' );
146       break;
147
148     case PROP_ID:
149       priv->uniq_id = g_value_get_uint (value);
150       break;
151
152     case PROP_LABEL:
153       g_free (priv->label);
154       priv->label = g_strdup(g_value_get_string (value));
155       break;
156
157     case PROP_TILESIZE_X:
158       priv->tilesize_x = g_value_get_uint (value);
159       break;
160
161     case PROP_TILESIZE_Y:
162       priv->tilesize_y = g_value_get_uint (value);
163       break;
164
165     case PROP_DRAWMODE:
166       priv->drawmode = g_value_get_enum(value);
167       break;
168
169     case PROP_COPYRIGHT:
170       g_free (priv->copyright);
171       priv->copyright = g_strdup(g_value_get_string (value));
172       break;
173
174     case PROP_LICENSE:
175       g_free (priv->license);
176       priv->license = g_strdup(g_value_get_string (value));
177       break;
178
179     case PROP_LICENSE_URL:
180       g_free (priv->license_url);
181       priv->license_url = g_strdup(g_value_get_string (value));
182       break;
183
184     case PROP_FILE_EXTENSION:
185       g_free (priv->file_extension);
186       priv->file_extension = g_strdup(g_value_get_string(value));
187       break;
188
189     default:
190       /* We don't have any other property... */
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
192       break;
193     }
194 }
195
196 static void
197 vik_map_source_default_get_property (GObject    *object,
198                                      guint       property_id,
199                                      GValue     *value,
200                                      GParamSpec *pspec)
201 {
202   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
203   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
204
205   switch (property_id)
206     {
207     case PROP_NAME:
208       g_value_set_string (value, priv->name);
209       break;
210
211     case PROP_ID:
212       g_value_set_uint (value, priv->uniq_id);
213       break;
214
215     case PROP_LABEL:
216       g_value_set_string (value, priv->label);
217       break;
218
219     case PROP_TILESIZE_X:
220       g_value_set_uint (value, priv->tilesize_x);
221       break;
222
223     case PROP_TILESIZE_Y:
224       g_value_set_uint (value, priv->tilesize_y);
225       break;
226
227     case PROP_DRAWMODE:
228       g_value_set_enum (value, priv->drawmode);
229       break;
230
231     case PROP_COPYRIGHT:
232       g_value_set_string (value, priv->copyright);
233       break;
234
235     case PROP_LICENSE:
236       g_value_set_string (value, priv->license);
237       break;
238
239     case PROP_LICENSE_URL:
240       g_value_set_string (value, priv->license_url);
241       break;
242
243     case PROP_FILE_EXTENSION:
244       g_value_set_string (value, priv->file_extension);
245       break;
246
247     default:
248       /* We don't have any other property... */
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
250       break;
251     }
252 }
253
254 static void
255 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
256 {
257         GObjectClass* object_class = G_OBJECT_CLASS (klass);
258         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
259     GParamSpec *pspec = NULL;
260         
261         object_class->set_property = vik_map_source_default_set_property;
262     object_class->get_property = vik_map_source_default_get_property;
263         
264         /* Overiding methods */
265         parent_class->get_copyright =   map_source_get_copyright;
266         parent_class->get_license =     map_source_get_license;
267         parent_class->get_license_url = map_source_get_license_url;
268         parent_class->get_logo =        map_source_get_logo;
269         parent_class->get_name =        map_source_get_name;
270         parent_class->get_uniq_id =    map_source_get_uniq_id;
271         parent_class->get_label =      map_source_get_label;
272         parent_class->get_tilesize_x = map_source_get_tilesize_x;
273         parent_class->get_tilesize_y = map_source_get_tilesize_y;
274         parent_class->get_drawmode =   map_source_get_drawmode;
275         parent_class->get_file_extension = map_source_get_file_extension;
276         parent_class->download =                 _download;
277         parent_class->download_handle_init =     _download_handle_init;
278         parent_class->download_handle_cleanup =  _download_handle_cleanup;
279
280         /* Default implementation of methods */
281         klass->get_uri = NULL;
282         klass->get_hostname = NULL;
283         klass->get_download_options = NULL;
284
285         pspec = g_param_spec_string ("name",
286                                      "Name",
287                                      "The name of the map that may be used as the file cache directory",
288                                      "Unknown" /* default value */,
289                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
290         g_object_class_install_property (object_class, PROP_NAME, pspec);
291
292         pspec = g_param_spec_uint ("id",
293                                    "Id of the tool",
294                                "Set the id",
295                                0  /* minimum value */,
296                                G_MAXUINT /* maximum value */,
297                                0  /* default value */,
298                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
299         g_object_class_install_property (object_class, PROP_ID, pspec);
300
301         pspec = g_param_spec_string ("label",
302                                      "Label",
303                                      "The label of the map source",
304                                      "<no-set>" /* default value */,
305                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
306         g_object_class_install_property (object_class, PROP_LABEL, pspec);
307
308         pspec = g_param_spec_uint ("tilesize-x",
309                                    "TileSizeX",
310                                "Set the size of the tile (x)",
311                                0  /* minimum value */,
312                                G_MAXUINT16 /* maximum value */,
313                                0  /* default value */,
314                                G_PARAM_READWRITE);
315         g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
316
317         pspec = g_param_spec_uint ("tilesize-y",
318                                    "TileSizeY",
319                                "Set the size of the tile (y)",
320                                0  /* minimum value */,
321                                G_MAXUINT16 /* maximum value */,
322                                0  /* default value */,
323                                G_PARAM_READWRITE);
324         g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
325
326         pspec = g_param_spec_enum("drawmode",
327                               "Drawmode",
328                               "The mode used to draw map",
329                               VIK_TYPE_VIEWPORT_DRAW_MODE,
330                               VIK_VIEWPORT_DRAWMODE_UTM,
331                               G_PARAM_READWRITE);
332     g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);                                    
333
334         pspec = g_param_spec_string ("copyright",
335                                      "Copyright",
336                                      "The copyright of the map source",
337                                      NULL,
338                                      G_PARAM_READWRITE);
339         g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
340
341         pspec = g_param_spec_string ("license",
342                                      "License",
343                                      "The license of the map source",
344                                      NULL,
345                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
346         g_object_class_install_property (object_class, PROP_LICENSE, pspec);
347
348         pspec = g_param_spec_string ("license-url",
349                                      "License URL",
350                                      "The URL of the license of the map source",
351                                      NULL,
352                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
353         g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
354
355         pspec = g_param_spec_string ("file-extension",
356                                      "File Extension",
357                                      "The file extension of tile files on disk",
358                                      ".png" /* default value */,
359                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
360         g_object_class_install_property (object_class, PROP_FILE_EXTENSION, pspec);
361
362         g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
363
364         object_class->finalize = vik_map_source_default_finalize;
365 }
366
367 static void
368 map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
369 {
370         /* Just ignore bbox and zoom level */
371         g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
372
373         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
374
375         (*fct) (data, priv->copyright);
376 }
377
378 static const gchar *
379 map_source_get_license (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->license;
386 }
387
388 static const gchar *
389 map_source_get_license_url (VikMapSource *self)
390 {
391         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
392         
393         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
394
395         return priv->license_url;
396 }
397
398 static const GdkPixbuf *
399 map_source_get_logo (VikMapSource *self)
400 {
401         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
402
403         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
404
405         return priv->logo;
406 }
407
408 static const gchar *
409 map_source_get_name (VikMapSource *self)
410 {
411         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
412         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
413         return priv->name;
414 }
415
416 static guint16
417 map_source_get_uniq_id (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->uniq_id;
424 }
425
426 static const gchar *
427 map_source_get_label (VikMapSource *self)
428 {
429         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
430         
431         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
432
433         return priv->label;
434 }
435
436 static guint16
437 map_source_get_tilesize_x (VikMapSource *self)
438 {
439         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
440
441     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
442
443         return priv->tilesize_x;
444 }
445
446 static guint16
447 map_source_get_tilesize_y (VikMapSource *self)
448 {
449         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
450
451     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
452
453         return priv->tilesize_y;
454 }
455
456 static VikViewportDrawMode
457 map_source_get_drawmode (VikMapSource *self)
458 {
459         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
460
461     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
462
463         return priv->drawmode;
464 }
465
466 static DownloadResult_t
467 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
468 {
469    gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
470    gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
471    DownloadFileOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
472    DownloadResult_t res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
473    g_free ( uri );
474    g_free ( host );
475    return res;
476 }
477
478 static const gchar *
479 map_source_get_file_extension (VikMapSource *self)
480 {
481     g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
482     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
483     return priv->file_extension;
484 }
485
486 static void *
487 _download_handle_init ( VikMapSource *self )
488 {
489    return a_download_handle_init ();
490 }
491
492
493 static void
494 _download_handle_cleanup ( VikMapSource *self, void *handle )
495 {
496    a_download_handle_cleanup ( handle );
497 }
498
499 gchar *
500 vik_map_source_default_get_uri( VikMapSourceDefault *self, MapCoord *src )
501 {
502         VikMapSourceDefaultClass *klass;
503         g_return_val_if_fail (self != NULL, 0);
504         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
505         klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
506
507         g_return_val_if_fail (klass->get_uri != NULL, 0);
508
509         return (*klass->get_uri)(self, src);
510 }
511
512 gchar *
513 vik_map_source_default_get_hostname( VikMapSourceDefault *self )
514 {
515         VikMapSourceDefaultClass *klass;
516         g_return_val_if_fail (self != NULL, 0);
517         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
518         klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
519
520         g_return_val_if_fail (klass->get_hostname != NULL, 0);
521
522         return (*klass->get_hostname)(self);
523 }
524
525 DownloadFileOptions *
526 vik_map_source_default_get_download_options( VikMapSourceDefault *self )
527 {
528         VikMapSourceDefaultClass *klass;
529         g_return_val_if_fail (self != NULL, 0);
530         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
531         klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
532
533         g_return_val_if_fail (klass->get_download_options != NULL, 0);
534
535         return (*klass->get_download_options)(self);
536 }
537
538 gchar *
539 vik_map_source_default_get_url_display( VikMapSourceDefault *self, MapCoord *src )
540 {
541         VikMapSourceDefaultClass *klass;
542         g_return_val_if_fail (self != NULL, 0);
543         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT (self), 0);
544         klass = VIK_MAP_SOURCE_DEFAULT_GET_CLASS(self);
545
546         g_return_val_if_fail (klass->get_uri != NULL, 0);
547         g_return_val_if_fail (klass->get_hostname != NULL, 0);
548
549         gchar *newstr = NULL;
550         gchar *hostname = (*klass->get_hostname)(self);
551         gchar *url = (*klass->get_uri)(self, src);
552         if ( hostname && strlen(hostname)>1 ) {
553                 if ( strstr (hostname, "://") == NULL ) {
554                         //prepend http
555                         newstr = g_strdup_printf ( "http://%s", hostname );
556                 }
557                 else {
558                         newstr = g_strdup ( hostname );
559                 }
560         }
561         if ( url && strlen(url)>1 ) {
562                 if ( newstr ) {
563                         gchar *tmp = g_strdup ( newstr );
564                         newstr = g_strdup_printf ( "%s%s", newstr, url );
565                         g_free ( tmp );
566                 }
567                 else {
568                         newstr = g_strdup ( url );
569                 }
570         }
571
572         return newstr;
573 }