]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikmapsourcedefault.c
Add TIFF as an image file type that gets listed in the applied filter.
[andy/viking.git] / src / vikmapsourcedefault.c
index b13089ab395930aec1972601820e766321de1989..3a2579372466d4073662e242ca769fcf5643cedd 100644 (file)
 #include "vikenumtypes.h"
 #include "download.h"
 
 #include "vikenumtypes.h"
 #include "download.h"
 
-static const gchar *map_source_get_copyright (VikMapSource *self);
+static void map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
 static const gchar *map_source_get_license (VikMapSource *self);
 static const gchar *map_source_get_license_url (VikMapSource *self);
 static const GdkPixbuf *map_source_get_logo (VikMapSource *self);
 
 static const gchar *map_source_get_license (VikMapSource *self);
 static const gchar *map_source_get_license_url (VikMapSource *self);
 static const GdkPixbuf *map_source_get_logo (VikMapSource *self);
 
-static guint8 map_source_get_uniq_id (VikMapSource *self);
+static const gchar *map_source_get_name (VikMapSource *self);
+static guint16 map_source_get_uniq_id (VikMapSource *self);
 static const gchar *map_source_get_label (VikMapSource *self);
 static guint16 map_source_get_tilesize_x (VikMapSource *self);
 static guint16 map_source_get_tilesize_y (VikMapSource *self);
 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
 static const gchar *map_source_get_label (VikMapSource *self);
 static guint16 map_source_get_tilesize_x (VikMapSource *self);
 static guint16 map_source_get_tilesize_y (VikMapSource *self);
 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
+static const gchar *map_source_get_file_extension (VikMapSource *self);
 
 
-static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
+static DownloadResult_t _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
 static void * _download_handle_init ( VikMapSource *self );
 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
 
 static void * _download_handle_init ( VikMapSource *self );
 static void _download_handle_cleanup ( VikMapSource *self, void *handle );
 
@@ -54,11 +56,13 @@ struct _VikMapSourceDefaultPrivate
        gchar *license_url;
        GdkPixbuf *logo;
 
        gchar *license_url;
        GdkPixbuf *logo;
 
-       guint8 uniq_id;
+       gchar *name;
+       guint16 uniq_id;
        gchar *label;
        guint16 tilesize_x;
        guint16 tilesize_y;
        VikViewportDrawMode drawmode;
        gchar *label;
        guint16 tilesize_x;
        guint16 tilesize_y;
        VikViewportDrawMode drawmode;
+       gchar *file_extension;
 };
 
 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
 };
 
 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
@@ -68,6 +72,7 @@ enum
 {
   PROP_0,
 
 {
   PROP_0,
 
+  PROP_NAME,
   PROP_ID,
   PROP_LABEL,
   PROP_TILESIZE_X,
   PROP_ID,
   PROP_LABEL,
   PROP_TILESIZE_X,
@@ -76,9 +81,10 @@ enum
   PROP_COPYRIGHT,
   PROP_LICENSE,
   PROP_LICENSE_URL,
   PROP_COPYRIGHT,
   PROP_LICENSE,
   PROP_LICENSE_URL,
+  PROP_FILE_EXTENSION,
 };
 
 };
 
-G_DEFINE_TYPE_EXTENDED (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE, (GTypeFlags)G_TYPE_FLAG_ABSTRACT,);
+G_DEFINE_ABSTRACT_TYPE (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE);
 
 static void
 vik_map_source_default_init (VikMapSourceDefault *object)
 
 static void
 vik_map_source_default_init (VikMapSourceDefault *object)
@@ -91,6 +97,8 @@ vik_map_source_default_init (VikMapSourceDefault *object)
   priv->license = NULL;
   priv->license_url = NULL;
   priv->logo = NULL;
   priv->license = NULL;
   priv->license_url = NULL;
   priv->logo = NULL;
+  priv->name = NULL;
+  priv->file_extension = NULL;
 }
 
 static void
 }
 
 static void
@@ -109,7 +117,11 @@ vik_map_source_default_finalize (GObject *object)
   priv->license_url = NULL;
   g_free (priv->logo);
   priv->license_url = NULL;
   priv->license_url = NULL;
   g_free (priv->logo);
   priv->license_url = NULL;
-       
+  g_free (priv->name);
+  priv->name = NULL;
+  g_free (priv->file_extension);
+  priv->file_extension = NULL;
+
   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
 }
 
   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
 }
 
@@ -124,11 +136,19 @@ vik_map_source_default_set_property (GObject      *object,
 
   switch (property_id)
     {
 
   switch (property_id)
     {
+    case PROP_NAME:
+      // Sanitize the name here for file usage
+      // A simple check just to prevent containing slashes ATM
+      g_free (priv->name);
+      priv->name = g_strdup(g_value_get_string (value));
+      g_strdelimit (priv->name, "\\/", 'x' );
+      break;
+
     case PROP_ID:
       priv->uniq_id = g_value_get_uint (value);
       break;
 
     case PROP_ID:
       priv->uniq_id = g_value_get_uint (value);
       break;
 
-       case PROP_LABEL:
+    case PROP_LABEL:
       g_free (priv->label);
       priv->label = g_strdup(g_value_get_string (value));
       break;
       g_free (priv->label);
       priv->label = g_strdup(g_value_get_string (value));
       break;
@@ -160,6 +180,11 @@ vik_map_source_default_set_property (GObject      *object,
       priv->license_url = g_strdup(g_value_get_string (value));
       break;
 
       priv->license_url = g_strdup(g_value_get_string (value));
       break;
 
+    case PROP_FILE_EXTENSION:
+      g_free (priv->file_extension);
+      priv->file_extension = g_strdup(g_value_get_string(value));
+      break;
+
     default:
       /* We don't have any other property... */
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     default:
       /* We don't have any other property... */
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -178,6 +203,10 @@ vik_map_source_default_get_property (GObject    *object,
 
   switch (property_id)
     {
 
   switch (property_id)
     {
+    case PROP_NAME:
+      g_value_set_string (value, priv->name);
+      break;
+
     case PROP_ID:
       g_value_set_uint (value, priv->uniq_id);
       break;
     case PROP_ID:
       g_value_set_uint (value, priv->uniq_id);
       break;
@@ -210,6 +239,10 @@ vik_map_source_default_get_property (GObject    *object,
       g_value_set_string (value, priv->license_url);
       break;
 
       g_value_set_string (value, priv->license_url);
       break;
 
+    case PROP_FILE_EXTENSION:
+      g_value_set_string (value, priv->file_extension);
+      break;
+
     default:
       /* We don't have any other property... */
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     default:
       /* We don't have any other property... */
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -232,11 +265,13 @@ vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
        parent_class->get_license =     map_source_get_license;
        parent_class->get_license_url = map_source_get_license_url;
        parent_class->get_logo =        map_source_get_logo;
        parent_class->get_license =     map_source_get_license;
        parent_class->get_license_url = map_source_get_license_url;
        parent_class->get_logo =        map_source_get_logo;
+       parent_class->get_name =        map_source_get_name;
        parent_class->get_uniq_id =    map_source_get_uniq_id;
        parent_class->get_label =      map_source_get_label;
        parent_class->get_tilesize_x = map_source_get_tilesize_x;
        parent_class->get_tilesize_y = map_source_get_tilesize_y;
        parent_class->get_drawmode =   map_source_get_drawmode;
        parent_class->get_uniq_id =    map_source_get_uniq_id;
        parent_class->get_label =      map_source_get_label;
        parent_class->get_tilesize_x = map_source_get_tilesize_x;
        parent_class->get_tilesize_y = map_source_get_tilesize_y;
        parent_class->get_drawmode =   map_source_get_drawmode;
+       parent_class->get_file_extension = map_source_get_file_extension;
        parent_class->download =                 _download;
        parent_class->download_handle_init =     _download_handle_init;
        parent_class->download_handle_cleanup =  _download_handle_cleanup;
        parent_class->download =                 _download;
        parent_class->download_handle_init =     _download_handle_init;
        parent_class->download_handle_cleanup =  _download_handle_cleanup;
@@ -246,6 +281,13 @@ vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
        klass->get_hostname = NULL;
        klass->get_download_options = NULL;
 
        klass->get_hostname = NULL;
        klass->get_download_options = NULL;
 
+       pspec = g_param_spec_string ("name",
+                                    "Name",
+                                    "The name of the map that may be used as the file cache directory",
+                                    "Unknown" /* default value */,
+                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_NAME, pspec);
+
        pspec = g_param_spec_uint ("id",
                                   "Id of the tool",
                                "Set the id",
        pspec = g_param_spec_uint ("id",
                                   "Id of the tool",
                                "Set the id",
@@ -292,7 +334,7 @@ vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
                                     "Copyright",
                                     "The copyright of the map source",
                                     NULL,
                                     "Copyright",
                                     "The copyright of the map source",
                                     NULL,
-                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+                                    G_PARAM_READWRITE);
        g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
 
        pspec = g_param_spec_string ("license",
        g_object_class_install_property (object_class, PROP_COPYRIGHT, pspec);
 
        pspec = g_param_spec_string ("license",
@@ -309,19 +351,31 @@ vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
        g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
 
                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
        g_object_class_install_property (object_class, PROP_LICENSE_URL, pspec);
 
+       pspec = g_param_spec_string ("file-extension",
+                                    "File Extension",
+                                    "The file extension of tile files on disk",
+                                    ".png" /* default value */,
+                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_FILE_EXTENSION, pspec);
+
        g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
 
        object_class->finalize = vik_map_source_default_finalize;
 }
 
        g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
 
        object_class->finalize = vik_map_source_default_finalize;
 }
 
-static const gchar *
-map_source_get_copyright (VikMapSource *self)
+static void
+map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
 {
 {
-       g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
+       /* Just ignore bbox and zoom level */
+       g_return_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self));
+
+       g_debug ("%s: %g %g %g %g %g", __FUNCTION__,
+               bbox.south, bbox.north, bbox.east, bbox.west,
+               zoom);
        
        VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
 
        
        VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
 
-       return priv->copyright;
+       (*fct) (data, priv->copyright);
 }
 
 static const gchar *
 }
 
 static const gchar *
@@ -354,10 +408,18 @@ map_source_get_logo (VikMapSource *self)
        return priv->logo;
 }
 
        return priv->logo;
 }
 
-static guint8
+static const gchar *
+map_source_get_name (VikMapSource *self)
+{
+       g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
+       VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
+       return priv->name;
+}
+
+static guint16
 map_source_get_uniq_id (VikMapSource *self)
 {
 map_source_get_uniq_id (VikMapSource *self)
 {
-       g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint8)0);
+       g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
        
        VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
 
        
        VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
 
@@ -404,19 +466,26 @@ map_source_get_drawmode (VikMapSource *self)
        return priv->drawmode;
 }
 
        return priv->drawmode;
 }
 
-static int
+static DownloadResult_t
 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
 {
 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
 {
-   int res;
    gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
    gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
    DownloadMapOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
    gchar *uri = vik_map_source_default_get_uri(VIK_MAP_SOURCE_DEFAULT(self), src);
    gchar *host = vik_map_source_default_get_hostname(VIK_MAP_SOURCE_DEFAULT(self));
    DownloadMapOptions *options = vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self));
-   res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
+   DownloadResult_t res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
    g_free ( uri );
    g_free ( host );
    return res;
 }
 
    g_free ( uri );
    g_free ( host );
    return res;
 }
 
+static const gchar *
+map_source_get_file_extension (VikMapSource *self)
+{
+    g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
+    VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
+    return priv->file_extension;
+}
+
 static void *
 _download_handle_init ( VikMapSource *self )
 {
 static void *
 _download_handle_init ( VikMapSource *self )
 {