]> git.street.me.uk Git - andy/viking.git/blobdiff - src/bingmapsource.c
Merge pull request #32 from davidedelvento/installnotes
[andy/viking.git] / src / bingmapsource.c
index add04fc2e0efa5f0a92c24531330cf493bfd6231..0c9fd99587ea46545bb3a2dd9faf4696057afc62 100644 (file)
@@ -55,8 +55,8 @@
 /* Format for URL */
 #define URL_ATTR_FMT "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial/0,0?zl=1&mapVersion=v1&key=%s&include=ImageryProviders&output=xml"
 
-static gchar *_get_uri ( VikMapSourceDefault *self, MapCoord *src );
-static gchar *_get_hostname ( VikMapSourceDefault *self );
+static gchar *bget_uri ( VikMapSourceDefault *self, MapCoord *src );
+static gchar *bget_hostname ( VikMapSourceDefault *self );
 static void _get_copyright (VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
 static const GdkPixbuf *_get_logo ( VikMapSource *self );
 static int _load_attributions ( BingMapSource *self );
@@ -79,6 +79,7 @@ struct _BingMapSourcePrivate
        GList *attributions;
        /* Current attribution, when parsing */
        gchar *attribution;
+       gboolean loading_attributions;
 };
 
 /* The pixbuf to store the logo */
@@ -109,6 +110,7 @@ bing_map_source_init (BingMapSource *self)
        priv->api_key = NULL;
        priv->attributions = NULL;
        priv->attribution = NULL;
+       priv->loading_attributions = FALSE;
 }
 
 static void
@@ -201,8 +203,8 @@ bing_map_source_class_init (BingMapSourceClass *klass)
        /* Overiding methods */
        object_class->set_property = _set_property;
        object_class->get_property = _get_property;
-       grandparent_class->get_uri = _get_uri;
-       grandparent_class->get_hostname = _get_hostname;
+       grandparent_class->get_uri = bget_uri;
+       grandparent_class->get_hostname = bget_hostname;
        base_class->get_logo      = _get_logo;
        base_class->get_copyright = _get_copyright;
 
@@ -258,7 +260,7 @@ compute_quad_tree(int zoom, int tilex, int tiley)
 }
 
 static gchar *
-_get_uri( VikMapSourceDefault *self, MapCoord *src )
+bget_uri( VikMapSourceDefault *self, MapCoord *src )
 {
        g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
 
@@ -270,7 +272,7 @@ _get_uri( VikMapSourceDefault *self, MapCoord *src )
 } 
 
 static gchar *
-_get_hostname( VikMapSourceDefault *self )
+bget_hostname( VikMapSourceDefault *self )
 {
        g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
 
@@ -297,7 +299,11 @@ _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(V
        /* Loop over all known attributions */
        GList *attribution = priv->attributions;
        if (attribution == NULL && g_strcmp0 ("<no-set>", priv->api_key)) {
-               _async_load_attributions (BING_MAP_SOURCE (self));
+               if ( ! priv->loading_attributions )
+                       _async_load_attributions (BING_MAP_SOURCE (self));
+               else
+                       // Wait until attributions loaded before processing them
+                       return;
        }
        while (attribution != NULL) {
                struct _Attribution *current = (struct _Attribution*)attribution->data;
@@ -314,7 +320,7 @@ _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(V
 
 /* Called for open tags <foo bar="baz"> */
 static void
-_start_element (GMarkupParseContext *context,
+bstart_element (GMarkupParseContext *context,
                 const gchar         *element_name,
                 const gchar        **attribute_names,
                 const gchar        **attribute_values,
@@ -326,7 +332,7 @@ _start_element (GMarkupParseContext *context,
        const gchar *element = g_markup_parse_context_get_element (context);
        if (strcmp (element, "CoverageArea") == 0) {
                /* New Attribution */
-               struct _Attribution *attribution = g_malloc (sizeof(struct _Attribution));
+               struct _Attribution *attribution = g_malloc0 (sizeof(struct _Attribution));
                priv->attributions = g_list_append (priv->attributions, attribution);
                attribution->attribution = g_strdup (priv->attribution);
        }
@@ -335,7 +341,7 @@ _start_element (GMarkupParseContext *context,
 /* Called for character data */
 /* text is not nul-terminated */
 static void
-_text (GMarkupParseContext *context,
+btext (GMarkupParseContext *context,
        const gchar         *text,
        gsize                text_len,  
        gpointer             user_data,
@@ -351,33 +357,31 @@ _text (GMarkupParseContext *context,
        int len = g_slist_length ((GSList *)stack);
 
        const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
-       
        if (strcmp (element, "Attribution") == 0) {
                g_free (priv->attribution);
                priv->attribution = g_strdup (textl);
-       } else if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
-               if (strcmp (element, "ZoomMin") == 0) {
-                       attribution->minZoom = atoi (textl);
-               } else if (strcmp (element, "ZoomMax") == 0) {
-                       attribution->maxZoom = atoi (textl);
-               }
-       } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
-               if (strcmp (element, "SouthLatitude") == 0) {
-                       attribution->bounds.south = g_ascii_strtod (textl, NULL);
-               } else if (strcmp (element, "WestLongitude") == 0) {
-                       attribution->bounds.west = g_ascii_strtod (textl, NULL);
-               } else if (strcmp (element, "NorthLatitude") == 0) {
-                       attribution->bounds.north = g_ascii_strtod (textl, NULL);
-               } else if (strcmp (element, "EastLongitude") == 0) {
-                       attribution->bounds.east = g_ascii_strtod (textl, NULL);
+       }
+       else {
+               if ( attribution ) {
+                       if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
+                               if (strcmp (element, "ZoomMin") == 0) {
+                                       attribution->minZoom = atoi (textl);
+                               } else if (strcmp (element, "ZoomMax") == 0) {
+                                       attribution->maxZoom = atoi (textl);
+                               }
+                       } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
+                               if (strcmp (element, "SouthLatitude") == 0) {
+                                       attribution->bounds.south = g_ascii_strtod (textl, NULL);
+                               } else if (strcmp (element, "WestLongitude") == 0) {
+                                       attribution->bounds.west = g_ascii_strtod (textl, NULL);
+                               } else if (strcmp (element, "NorthLatitude") == 0) {
+                                       attribution->bounds.north = g_ascii_strtod (textl, NULL);
+                               } else if (strcmp (element, "EastLongitude") == 0) {
+                                       attribution->bounds.east = g_ascii_strtod (textl, NULL);
+                               }
+                       }
                }
        }
-       if (attribution)
-               g_debug("Current attribution %s from %d to %d %g %g %g %g",
-                       attribution->attribution,
-                       attribution->minZoom, attribution->maxZoom,
-                       attribution->bounds.south, attribution->bounds.north, attribution->bounds.west, attribution->bounds.east);
-
        g_free(textl);
 }
 
@@ -396,9 +400,9 @@ _parse_file_for_attributions(BingMapSource *self, gchar *filename)
                return FALSE;
        
        /* setup context parse (ie callbacks) */
-       xml_parser.start_element = &_start_element;
+       xml_parser.start_element = &bstart_element;
        xml_parser.end_element = NULL;
-       xml_parser.text = &_text;
+       xml_parser.text = &btext;
        xml_parser.passthrough = NULL;
        xml_parser.error = NULL;
        
@@ -438,6 +442,15 @@ _parse_file_for_attributions(BingMapSource *self, gchar *filename)
        xml_context = NULL;
        fclose (file);
 
+       if (vik_debug) {
+               GList *attribution = priv->attributions;
+               while (attribution != NULL) {
+                       struct _Attribution *aa = (struct _Attribution*)attribution->data;
+                       g_debug ("Bing Attribution: %s from %d to %d %g %g %g %g", aa->attribution, aa->minZoom, aa->maxZoom, aa->bounds.south, aa->bounds.north, aa->bounds.east, aa->bounds.west);
+                       attribution = attribution->next;
+               }
+       }
+
        return TRUE;
 }
 
@@ -447,32 +460,34 @@ _load_attributions ( BingMapSource *self )
        int ret = 0;  /* OK */
 
        BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
+       priv->loading_attributions = TRUE;
        gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
 
        gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
+       if ( !tmpname ) {
+               ret = -1;
+               goto done;
+       }
 
        g_debug("%s: %s", __FUNCTION__, tmpname);
        if (!_parse_file_for_attributions(self, tmpname)) {
                ret = -1;
-               goto done;
        }
 
+       (void)g_remove(tmpname);
+       g_free(tmpname);
 done:
+       priv->loading_attributions = FALSE;
        g_free(uri);
-       g_remove(tmpname);
-       g_free(tmpname);
        return ret;
 }
 
-static int
+static gboolean
 _emit_update ( gpointer data )
 {
-       gdk_threads_enter();
-       /* TODO
-       vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
-       */
-       gdk_threads_leave();
-       return 0;
+       //if ( data )
+       //      vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
+       return FALSE;
 }
 
 static int
@@ -484,10 +499,9 @@ _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
                return -1; /* Abort thread */
 
        /* Emit update */
-       /* As we are on a download thread,
-        * it's better to fire the update from the main loop.
-        */
-       g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
+       // As we are on a download thread, must the update GUI from the main loop
+       // TODO: have reference to the window/viewport/layerspanel to update...
+       (void)gdk_threads_add_idle ( _emit_update, NULL );
 
        return 0;
 }
@@ -495,13 +509,14 @@ _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
 static void
 _async_load_attributions ( BingMapSource *self )
 {
-       a_background_thread ( /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
-                           _("Bing attribution Loading"),
-                           (vik_thr_func) _load_attributions_thread,
-                           self,
-                           NULL,
-                           NULL,
-                           1 );
+       a_background_thread ( BACKGROUND_POOL_REMOTE,
+                             /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
+                             _("Bing attribution Loading"),
+                             (vik_thr_func) _load_attributions_thread,
+                             self,
+                             NULL,
+                             NULL,
+                             1 );
      
 }
 
@@ -527,6 +542,8 @@ bing_map_source_new_with_id (guint16 id, const gchar *label, const gchar *key)
                                                "url", "/tiles/a%s.jpeg?g=587",
                                                "api-key", key,
                                                "check-file-server-time", TRUE,
+                                               "zoom-min", 0,
+                                               "zoom-max", 19, // NB: Might be regionally different rather than the same across the world
                                                "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
                                                "license", "Microsoft Bing Maps Specific",
                                                "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",