]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikslippymapsource.c
Rename struct DownloadOptions to DownloadMapOptions
[andy/viking.git] / src / vikslippymapsource.c
index 6511ad2d81a8572550244197a805bf21f3816505..c04ef485d5f669e3ffdac6f0a328b12b4a9416aa 100644 (file)
 
 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
-static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn );
+static int _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 gboolean _supports_if_modified_since (VikMapSource *self );
 
 static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
 static gchar *_get_hostname( VikSlippyMapSource *self );
-static DownloadOptions *_get_download_options( VikSlippyMapSource *self );
-
-/* FIXME Huge gruik */
-static DownloadOptions slippy_options = { NULL, 0, a_check_map_file };
+static DownloadMapOptions *_get_download_options( VikSlippyMapSource *self );
 
 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
 struct _VikSlippyMapSourcePrivate
 {
   gchar *hostname;
   gchar *url;
+  DownloadMapOptions options;
 };
 
 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
 
+/* properties */
+enum
+{
+  PROP_0,
+
+  PROP_HOSTNAME,
+  PROP_URL,
+  PROP_REFERER,
+  PROP_FOLLOW_LOCATION,
+  PROP_CHECK_FILE_SERVER_TIME,
+};
+
 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
 
 static void
-vik_slippy_map_source_init (VikSlippyMapSource *object)
+vik_slippy_map_source_init (VikSlippyMapSource *self)
 {
-       /* initialize the object here */
-       // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_x = 256;
-       // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_y = 256;
-       // FIXME VIK_MAP_SOURCE_DEFAULT(self)->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
+  /* initialize the object here */
+  VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
+
+  priv->hostname = NULL;
+  priv->url = NULL;
+  priv->options.referer = NULL;
+  priv->options.follow_location = 0;
+  priv->options.check_file = a_check_map_file;
+  priv->options.check_file_server_time = FALSE;
+
+  g_object_set (G_OBJECT (self),
+                "tilesize-x", 256,
+                "tilesize-y", 256,
+                "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
+                NULL);
 }
 
 static void
 vik_slippy_map_source_finalize (GObject *object)
 {
-       /* TODO: Add deinitalization code here */
+  VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
+  VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
+
+  g_free (priv->hostname);
+  priv->hostname = NULL;
+  g_free (priv->url);
+  priv->url = NULL;
+  g_free (priv->options.referer);
+  priv->options.referer = NULL;
+
+  G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
+}
+
+static void
+vik_slippy_map_source_set_property (GObject      *object,
+                                    guint         property_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
+  VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
+
+  switch (property_id)
+    {
+    case PROP_HOSTNAME:
+      g_free (priv->hostname);
+      priv->hostname = g_value_dup_string (value);
+      break;
+
+    case PROP_URL:
+      g_free (priv->url);
+      priv->url = g_value_dup_string (value);
+      break;
+
+    case PROP_REFERER:
+      g_free (priv->options.referer);
+      priv->options.referer = g_value_dup_string (value);
+      break;
+
+    case PROP_FOLLOW_LOCATION:
+      priv->options.follow_location = g_value_get_long (value);
+      break;
+
+    case PROP_CHECK_FILE_SERVER_TIME:
+      priv->options.check_file_server_time = g_value_get_boolean (value);
+      break;
+
+    default:
+      /* We don't have any other property... */
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
 
-       G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
+static void
+vik_slippy_map_source_get_property (GObject    *object,
+                                    guint       property_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
+  VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
+
+  switch (property_id)
+    {
+    case PROP_HOSTNAME:
+      g_value_set_string (value, priv->hostname);
+      break;
+
+    case PROP_URL:
+      g_value_set_string (value, priv->url);
+      break;
+
+    case PROP_REFERER:
+      g_value_set_string (value, priv->options.referer);
+      break;
+
+    case PROP_FOLLOW_LOCATION:
+      g_value_set_long (value, priv->options.follow_location);
+      break;
+
+    case PROP_CHECK_FILE_SERVER_TIME:
+      g_value_set_boolean (value, priv->options.check_file_server_time);
+      break;
+         
+    default:
+      /* We don't have any other property... */
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
 }
 
 static void
@@ -71,17 +182,61 @@ vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
 {
        GObjectClass* object_class = G_OBJECT_CLASS (klass);
        VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
+       GParamSpec *pspec = NULL;
+               
+       object_class->set_property = vik_slippy_map_source_set_property;
+    object_class->get_property = vik_slippy_map_source_get_property;
 
        /* Overiding methods */
        parent_class->coord_to_mapcoord =        _coord_to_mapcoord;
        parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
        parent_class->download =                 _download;
+       parent_class->download_handle_init =     _download_handle_init;
+       parent_class->download_handle_cleanup =  _download_handle_cleanup;
+       parent_class->supports_if_modified_since = _supports_if_modified_since;
        
        /* Default implementation of methods */
        klass->get_uri = _get_uri;
        klass->get_hostname = _get_hostname;
        klass->get_download_options = _get_download_options;
+
+       pspec = g_param_spec_string ("hostname",
+                                    "Hostname",
+                                    "The hostname of the map server",
+                                    "<no-set>" /* default value */,
+                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
+
+       pspec = g_param_spec_string ("url",
+                                    "URL",
+                                    "The template of the tiles' URL",
+                                    "<no-set>" /* default value */,
+                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_URL, pspec);
+
+       pspec = g_param_spec_string ("referer",
+                                    "Referer",
+                                    "The REFERER string to use in HTTP request",
+                                    NULL /* default value */,
+                                    G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_REFERER, pspec);
+       
+       pspec = g_param_spec_long ("follow-location",
+                                  "Follow location",
+                               "Specifies the number of retries to follow a redirect while downloading a page",
+                               0  /* minimum value */,
+                               G_MAXLONG /* maximum value */,
+                               0  /* default value */,
+                               G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
        
+       pspec = g_param_spec_boolean ("check-file-server-time",
+                                     "Check file server time",
+                                  "Age of current cache before redownloading tile",
+                                  FALSE  /* default value */,
+                                  G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
+       g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
+
        g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
        
        object_class->finalize = vik_slippy_map_source_finalize;
@@ -95,13 +250,23 @@ static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5),
 
 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
 
+static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
+static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
+
 #define ERROR_MARGIN 0.01
-static guint8 slippy_zoom ( gdouble mpp ) {
+static gint slippy_zoom ( gdouble mpp ) {
   gint i;
   for ( i = 0; i < num_scales; i++ ) {
-    if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
+    if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
       return i;
+    }
   }
+  for ( i = 0; i < num_scales_neg; i++ ) {
+    if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
+      return -i;
+    }
+  }
+
   return 255;
 }
 
@@ -131,7 +296,7 @@ vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
        return (*klass->get_hostname)(self);
 }
 
-DownloadOptions *
+DownloadMapOptions *
 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
 {
        VikSlippyMapSourceClass *klass;
@@ -144,6 +309,15 @@ vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
        return (*klass->get_download_options)(self);
 }
 
+gboolean
+_supports_if_modified_since (VikMapSource *self)
+{
+       g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
+       
+    VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
+       
+       return priv->options.check_file_server_time;
+}
 static gboolean
 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
 {
@@ -166,25 +340,42 @@ _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdo
 static void
 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
 {
-  gdouble socalled_mpp = GZ(src->scale);
+  gdouble socalled_mpp;
+  if (src->scale >= 0)
+    socalled_mpp = GZ(src->scale);
+  else
+    socalled_mpp = 1.0/GZ(-src->scale);
   dest->mode = VIK_COORD_LATLON;
   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
 }
 
 static int
-_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
+_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
 {
    int res;
    gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
    gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
-   DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
-   res = a_http_download_get_url ( host, uri, dest_fn, options );
+   DownloadMapOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
+   res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
    g_free ( uri );
    g_free ( host );
    return res;
 }
 
+static void *
+_download_handle_init ( VikMapSource *self )
+{
+   return a_download_handle_init ();
+}
+
+
+static void
+_download_handle_cleanup ( VikMapSource *self, void *handle )
+{
+   return a_download_handle_cleanup ( handle );
+}
+
 static gchar *
 _get_uri( VikSlippyMapSource *self, MapCoord *src )
 {
@@ -204,22 +395,18 @@ _get_hostname( VikSlippyMapSource *self )
        return g_strdup( priv->hostname );
 }
 
-static DownloadOptions *
+static DownloadMapOptions *
 _get_download_options( VikSlippyMapSource *self )
 {
        g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
        
-       return &slippy_options;
+       VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
+       return &(priv->options);
 }
 
 VikSlippyMapSource *
-vik_slippy_map_source_new_with_id (guint8 id, const gchar *hostname, const gchar *url)
+vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
 {
-       VikSlippyMapSource *ret = g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE, NULL);
-       
-    VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(ret);
-       // FIXME VIK_MAP_SOURCE_DEFAULT(ret)->uniq_id = id;
-       priv->hostname = g_strdup(hostname);
-       priv->url = g_strdup(url);
-       return ret;
-}
\ No newline at end of file
+       return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
+                           "id", id, "label", label, "hostname", hostname, "url", url, NULL);
+}