]> Pileus Git - grits/commitdiff
Convert GisWms to use GisHttp
authorAndy Spencer <andy753421@gmail.com>
Fri, 5 Feb 2010 13:31:12 +0000 (13:31 +0000)
committerAndy Spencer <andy753421@gmail.com>
Fri, 5 Feb 2010 13:31:29 +0000 (13:31 +0000)
src/data/gis-http.c
src/data/gis-http.h
src/data/gis-wms.c
src/data/gis-wms.h
src/plugins/elev.c
src/plugins/map.c
src/plugins/sat.c
src/tile-test.c

index ad21baf5ea03d0436894fefd3e021ad55982dd7a..0b29c4dd9f55208a7aad64559ac5c5853db09d46 100644 (file)
 
 GisHttp *gis_http_new(const gchar *prefix)
 {
-       GisHttp *http = g_new0(GisHttp, 1);
-       http->prefix = g_strdup(prefix);
-       http->soup = soup_session_sync_new();
-       g_object_set(http->soup, "user-agent", PACKAGE_STRING, NULL);
-       return http;
+       GisHttp *self = g_new0(GisHttp, 1);
+       self->soup = soup_session_sync_new();
+       self->prefix = g_strdup(prefix);
+       g_object_set(self->soup, "user-agent", PACKAGE_STRING, NULL);
+       return self;
+}
+
+void gis_http_free(GisHttp *self)
+{
+       g_object_unref(self->soup);
+       g_free(self->prefix);
+       g_free(self);
 }
 
 /* For passing data to the chunck callback */
index 051df0bfb7540cdd8b1857c066fd04274c032b32..c042a42a7a6e32ce306a478d9c8ed7daaf53c364 100644 (file)
@@ -24,8 +24,8 @@
 #include "gis-data.h"
 
 typedef struct _GisHttp {
-       gchar *prefix;
        SoupSession *soup;
+       gchar *prefix;
 } GisHttp;
 
 /**
@@ -34,6 +34,12 @@ typedef struct _GisHttp {
  */
 GisHttp *gis_http_new(const gchar *prefix);
 
+/**
+ * Free data associated with the GisHttp
+ * @param http the GisHttp to free
+ */
+void gis_http_free(GisHttp *http);
+
 /**
  * @param http      GisHttp connection to use
  * @param uri       The uri to fetch
index defedb3cd7b516dad3f440c37940df77fdbec004..23bc3f74e43c43e802c815ad066d14289e1db560 100644 (file)
@@ -63,6 +63,7 @@
 #include <glib.h>
 
 #include "gis-wms.h"
+#include "gis-http.h"
 
 static gchar *_make_uri(GisWms *wms, GisTile *tile)
 {
@@ -89,75 +90,42 @@ static gchar *_make_uri(GisWms *wms, GisTile *tile)
                tile->edge.n);
 }
 
-static void _soup_chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _file)
+gchar *gis_wms_fetch(GisWms *self, GisTile *tile, GisCacheType mode,
+               GisChunkCallback callback, gpointer user_data)
 {
-       FILE *file = _file;
-       if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
-               g_warning("GisWms: soup_chunk_cb - soup failed with %d", message->status_code);
-               return;
-       }
-       goffset total = soup_message_headers_get_content_length(message->response_headers);
-       if (fwrite(chunk->data, chunk->length, 1, file) != 1)
-               g_warning("GisWms: soup_chunk_cb - eror writing data");
-}
-
-char *gis_wms_make_local(GisWms *self, GisTile *tile)
-{
-       /* Get file path */
-       gchar *tile_path = gis_tile_get_path(tile);
-       gchar *path = g_strdup_printf("%s/%s/%s%s%s",
-               g_get_user_cache_dir(), PACKAGE,
-               self->cache_prefix, tile_path, self->cache_ext);
-       g_free(tile_path);
-
-       /* Return if it already exists */
-       if (g_file_test(path, G_FILE_TEST_EXISTS))
-               return path;
-
-       /* Open temp file for writing */
-       gchar *tmp_path = g_strconcat(path, ".part", NULL);
-       gchar *dirname = g_path_get_dirname(tmp_path);
-       g_mkdir_with_parents(dirname, 0755);
-       g_free(dirname);
-       FILE *file = fopen(tmp_path, "a");
-
-       /* Download file */
-       gchar *uri = _make_uri(self, tile);
-       g_debug("GisWms: make_local - fetching %s", uri);
-       SoupMessage *message = soup_message_new("GET", uri);
-       g_signal_connect(message, "got-chunk", G_CALLBACK(_soup_chunk_cb), file);
-       soup_message_headers_set_range(message->request_headers, ftell(file), -1);
-       int status = soup_session_send_message(self->soup, message);
-       if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
-               g_warning("GisWms: make_local - soup failed with %d", message->status_code);
+       gchar *uri   = _make_uri(self, tile);
+       gchar *tilep = gis_tile_get_path(tile);
+       gchar *local = g_strdup_printf("%s%s", tilep, self->extension);
+       gchar *path  = gis_http_fetch(self->http, uri, local,
+                       mode, callback, user_data);
        g_free(uri);
-
-       /* Clean up */
-       fclose(file);
-       rename(tmp_path, path);
-       g_free(tmp_path);
+       g_free(tilep);
+       g_free(local);
        return path;
 }
 
 GisWms *gis_wms_new(
-       gchar *uri_prefix, gchar *uri_layer, gchar *uri_format,
-       gchar *cache_prefix, gchar *cache_ext,
-       gint width, gint height)
+       const gchar *uri_prefix, const gchar *uri_layer,
+       const gchar *uri_format, const gchar *prefix,
+       const gchar *extension, gint width, gint height)
 {
        GisWms *self = g_new0(GisWms, 1);
-       self->uri_prefix   = uri_prefix;
-       self->uri_layer    = uri_layer;
-       self->uri_format   = uri_format;
-       self->cache_prefix = cache_prefix;
-       self->cache_ext    = cache_ext;
+       self->http         = gis_http_new(prefix);
+       self->uri_prefix   = g_strdup(uri_prefix);
+       self->uri_layer    = g_strdup(uri_layer);
+       self->uri_format   = g_strdup(uri_format);
+       self->extension    = g_strdup(extension);
        self->width        = width;
        self->height       = height;
-       self->soup         = soup_session_sync_new();
        return self;
 }
 
 void gis_wms_free(GisWms *self)
 {
-       g_object_unref(self->soup);
+       gis_http_free(self->http);
+       g_free(self->uri_prefix);
+       g_free(self->uri_layer);
+       g_free(self->uri_format);
+       g_free(self->extension);
        g_free(self);
 }
index 07547e4d498f5b0c403f5780e25543f9349c753c..105734108b2ebc3e1868443140750f2aab6e0c8c 100644 (file)
 #define __GIS_WMS_H__
 
 #include <glib.h>
-#include <libsoup/soup.h>
 
+#include "data/gis-http.h"
 #include "objects/gis-tile.h"
 
 typedef struct _GisWms {
+       GisHttp *http;
        gchar *uri_prefix;
        gchar *uri_layer;
        gchar *uri_format;
-       gchar *cache_prefix;
-       gchar *cache_ext;
+       gchar *extension;
        gint   width;
        gint   height;
-       SoupSession  *soup;
 } GisWms;
 
 
 GisWms *gis_wms_new(
-       gchar *uri_prefix, gchar *uri_layer, gchar *uri_format,
-       gchar *cache_prefix, gchar *cache_ext,
-       gint width, gint height);
+       const gchar *uri_prefix, const gchar *uri_layer,
+       const gchar *uri_format, const gchar *prefix,
+       const gchar *extension, gint width, gint height);
 
-char *gis_wms_make_local(GisWms *wms, GisTile *tile);
+gchar *gis_wms_fetch(GisWms *wms, GisTile *tile, GisCacheType mode,
+               GisChunkCallback callback, gpointer user_data);
 
 void gis_wms_free(GisWms *self);
 
index 65f3b828bbd195e3c238cadbfdfe78f3d006f603..2a61cde9df10ed7074489a2f5b328230f92e6a50 100644 (file)
@@ -195,7 +195,7 @@ static void _load_tile(GisTile *tile, gpointer _self)
        GisPluginElev *self = _self;
 
        struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
-       load->path = gis_wms_make_local(self->wms, tile);
+       load->path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL);
        g_debug("GisPluginElev: _load_tile: %s", load->path);
        load->self = self;
        load->tile = tile;
index 6c0b57d9855d26ecf4ecec9552cf84bf39d277d4..27261f147d64cbec54ad3f587c34cf1854694ec7 100644 (file)
@@ -96,7 +96,7 @@ static void _load_tile(GisTile *tile, gpointer _self)
 {
        GisPluginMap *self = _self;
        g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
-       char *path = gis_wms_make_local(self->wms, tile);
+       char *path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL);
        struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
        data->self   = self;
        data->tile   = tile;
index 9ce831bc7343c4b8980d1c6b61ea795ed5f48682..5a5208b8ac6380d593b71bd662999019b1e9ceb9 100644 (file)
@@ -73,7 +73,7 @@ static void _load_tile(GisTile *tile, gpointer _self)
 {
        GisPluginSat *self = _self;
        g_debug("GisPluginSat: _load_tile start %p", g_thread_self());
-       char *path = gis_wms_make_local(self->wms, tile);
+       char *path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL);
        struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
        data->self   = self;
        data->tile   = tile;
index 2151f243058d7c2645545adaba6ddb91bc046157..66e105d1ed499f32bc5a35fbb0d78c662a1bebb5 100644 (file)
@@ -58,7 +58,7 @@ gpointer do_bmng_cache(gpointer _image)
        GisWms *bmng_wms = gis_wms_new(
                "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
                "bmng_test/", "jpg", 512, 256);
-       const char *path = gis_wms_make_local(bmng_wms, tile);
+       const char *path = gis_wms_fetch(bmng_wms, tile, GIS_ONCE, NULL, NULL);
 
        g_message("Loading bmng image: [%s]", path);
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
@@ -84,7 +84,7 @@ gpointer do_osm_cache(gpointer _image)
        GisWms *osm_wms = gis_wms_new(
                "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
                "osm_test/", "png", 512, 256);
-       const char *path = gis_wms_make_local(osm_wms, tile);
+       const char *path = gis_wms_fetch(osm_wms, tile, GIS_ONCE, NULL, NULL);
 
        g_message("Loading osm image: [%s]", path);
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);