babelargs = g_strdup_printf(" -i %s", input_type);
- fetch_ret = a_http_download_get_url(url, "", name_src, &options);
+ fetch_ret = a_http_download_get_url(url, "", name_src, &options, NULL);
if (fetch_ret == 0)
ret = a_babel_convert_from( vt, babelargs, NULL, name_src, NULL);
curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
}
-int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition )
+int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition, void *handle )
{
CURL *curl;
CURLcode res = CURLE_FAILED_INIT;
g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
- curl = curl_easy_init ();
+ curl = handle ? handle : curl_easy_init ();
if ( !curl ) {
return DOWNLOAD_ERROR;
}
} else {
res = DOWNLOAD_ERROR;
}
- curl_easy_cleanup ( curl );
+ if (!handle)
+ curl_easy_cleanup ( curl );
return res;
}
-int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition )
+int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition, void *handle )
{
int ret;
gchar *full = NULL;
/* Compose the full url */
full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
- ret = curl_download_uri ( full, f, options, time_condition );
+ ret = curl_download_uri ( full, f, options, time_condition, handle );
g_free ( full );
full = NULL;
return ret;
}
+
+void * curl_download_handle_init ()
+{
+ return curl_easy_init();
+}
+
+void curl_download_handle_cleanup ( void *handle )
+{
+ curl_easy_cleanup(handle);
+}
#include "download.h"
void curl_download_init ();
-int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition );
-int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition );
+int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition, void *handle );
+int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition, void *handle );
+void * curl_download_handle_init ();
+void curl_download_handle_cleanup ( void * handle );
#endif
g_mutex_unlock(file_list_mutex);
}
-static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
+static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp, void *handle)
{
FILE *f;
int ret;
}
/* Call the backend function */
- ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition );
+ ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition, handle );
if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
/* uri: like "/uri.html?whatever" */
/* only reason for the "wrapper" is so we can do redirects. */
-int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
+int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle )
{
- return download ( hostname, uri, fn, opt, FALSE );
+ return download ( hostname, uri, fn, opt, FALSE, handle );
}
-int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
+int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle )
{
- return download ( hostname, uri, fn, opt, TRUE );
+ return download ( hostname, uri, fn, opt, TRUE, handle );
+}
+
+void * a_download_handle_init ()
+{
+ return curl_download_handle_init ();
+}
+
+void a_download_handle_cleanup ( void *handle )
+{
+ curl_download_handle_cleanup ( handle );
}
void a_download_init(void);
/* TODO: convert to Glib */
-int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt );
-int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt );
+int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle );
+int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle );
+void *a_download_handle_init ();
+void a_download_handle_cleanup ( void *handle );
/* Error messages returned by download functions */
enum { DOWNLOAD_NO_ERROR = 0,
tmp_file = fdopen(tmp_fd, "r+");
// TODO: curl may not be available
- if (curl_download_uri(uri, tmp_file, NULL, 0)) { // error
+ if (curl_download_uri(uri, tmp_file, NULL, 0, NULL)) { // error
fclose(tmp_file);
tmp_file = NULL;
g_remove(tmpname);
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 );
/* FIXME Huge gruik */
static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
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;
pspec = g_param_spec_uint ("type",
"Type",
}
static int
-_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
+_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
{
g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
int type = priv->type;
gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
src->scale, src->x, src->y, src->z );
- res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
+ res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options, handle );
g_free ( uri );
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 );
+}
+
TerraserverMapSource *
terraserver_map_source_new_with_id (guint8 id, const char *label, int type)
{
return g_object_new(TERRASERVER_TYPE_MAP_SOURCE, "id", id, "label", label, "type", type, NULL);
-}
\ No newline at end of file
+}
ABS(intlon) );
static DownloadOptions options = { 0, NULL, 0, a_check_map_file };
- a_http_download_get_url ( SRTM_HTTP_SITE, src_fn, p->dest, &options );
+ a_http_download_get_url ( SRTM_HTTP_SITE, src_fn, p->dest, &options, NULL );
g_free ( src_fn );
}
uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
/* TODO: curl may not be available */
- if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0)) { /* error */
+ if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */
fclose(tmp_file);
tmp_file = NULL;
ret = -1;
static int map_download_thread ( MapDownloadInfo *mdi, gpointer threaddata )
{
+ void *handle = vik_map_source_download_handle_init(MAPS_LAYER_NTH_TYPE(mdi->maptype));
guint donemaps = 0;
gint x, y;
for ( x = mdi->x0; x <= mdi->xf; x++ )
donemaps++;
int res = a_background_thread_progress ( threaddata, ((gdouble)donemaps) / mdi->mapstoget ); /* this also calls testcancel */
- if (res != 0)
+ if (res != 0) {
+ vik_map_source_download_handle_cleanup(MAPS_LAYER_NTH_TYPE(mdi->maptype), handle);
return -1;
+ }
if ( mdi->redownload == REDOWNLOAD_ALL)
g_remove ( mdi->filename_buf );
mdi->mapcoord.x = x; mdi->mapcoord.y = y;
if (need_download) {
- if ( vik_map_source_download( MAPS_LAYER_NTH_TYPE(mdi->maptype), &(mdi->mapcoord), mdi->filename_buf ))
+ if ( vik_map_source_download( MAPS_LAYER_NTH_TYPE(mdi->maptype), &(mdi->mapcoord), mdi->filename_buf, handle))
continue;
}
}
}
+ vik_map_source_download_handle_cleanup(MAPS_LAYER_NTH_TYPE(mdi->maptype), handle);
g_mutex_lock(mdi->mutex);
if (mdi->map_layer_alive)
g_object_weak_unref(G_OBJECT(mdi->vml), weak_ref_cb, mdi);
guint drawmode;
gboolean (*coord_to_mapcoord) ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
void (*mapcoord_to_center_coord) ( MapCoord *src, VikCoord *dest );
- int (*download) ( MapCoord *src, const gchar *dest_fn );
+ int (*download) ( MapCoord *src, const gchar *dest_fn, void *handle );
+ void *(*download_handle_init) ( );
+ void (*download_handle_cleanup) ( void *handle );
/* TODO: constant size (yay!) */
} VikMapsLayer_MapType;
klass->coord_to_mapcoord = NULL;
klass->mapcoord_to_center_coord = NULL;
klass->download = NULL;
+ klass->download_handle_init = NULL;
+ klass->download_handle_cleanup = NULL;
object_class->finalize = vik_map_source_finalize;
}
}
int
-vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn)
+vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void *handle)
{
VikMapSourceClass *klass;
g_return_val_if_fail (self != NULL, 0);
g_return_val_if_fail (klass->download != NULL, 0);
- return (*klass->download)(self, src, dest_fn);
+ return (*klass->download)(self, src, dest_fn, handle);
+}
+
+void *
+vik_map_source_download_handle_init (VikMapSource *self)
+{
+ VikMapSourceClass *klass;
+ g_return_val_if_fail (self != NULL, 0);
+ g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
+ klass = VIK_MAP_SOURCE_GET_CLASS(self);
+
+ g_return_val_if_fail (klass->download_handle_init != NULL, 0);
+
+ return (*klass->download_handle_init)(self);
+}
+
+void
+vik_map_source_download_handle_cleanup (VikMapSource * self, void * handle)
+{
+ VikMapSourceClass *klass;
+ g_return_val_if_fail (self != NULL, 0);
+ g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
+ klass = VIK_MAP_SOURCE_GET_CLASS(self);
+
+ g_return_val_if_fail (klass->download_handle_cleanup != NULL, 0);
+
+ return (*klass->download_handle_cleanup)(self, handle);
}
gboolean (* supports_if_modified_since) (VikMapSource * self);
gboolean (* coord_to_mapcoord) (VikMapSource * self, const VikCoord * src, gdouble xzoom, gdouble yzoom, MapCoord * dest);
void (* mapcoord_to_center_coord) (VikMapSource * self, MapCoord * src, VikCoord * dest);
- int (* download) (VikMapSource * self, MapCoord * src, const gchar * dest_fn);
+ int (* download) (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void * handle);
+ void * (* download_handle_init) (VikMapSource * self);
+ void (* download_handle_cleanup) (VikMapSource * self, void * handle);
};
struct _VikMapSource
gboolean vik_map_source_supports_if_modified_since (VikMapSource * self);
gboolean vik_map_source_coord_to_mapcoord (VikMapSource * self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
void vik_map_source_mapcoord_to_center_coord (VikMapSource * self, MapCoord *src, VikCoord *dest);
-int vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn);
+int vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void * handle);
+void * vik_map_source_download_handle_init (VikMapSource * self);
+void vik_map_source_download_handle_cleanup (VikMapSource * self, void * handle);
G_END_DECLS
static VikViewportDrawMode map_type_get_drawmode (VikMapSource *self);
static gboolean map_type_coord_to_mapcoord (VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
static void map_type_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikCoord *dest);
-static int map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn);
+static int map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void * handle);
+static void * map_type_download_handle_init (VikMapSource * self);
+static void map_type_download_handle_cleanup (VikMapSource * self, void * handle);
typedef struct _VikMapTypePrivate VikMapTypePrivate;
struct _VikMapTypePrivate
parent_class->coord_to_mapcoord = map_type_coord_to_mapcoord;
parent_class->mapcoord_to_center_coord = map_type_mapcoord_to_center_coord;
parent_class->download = map_type_download;
+ parent_class->download_handle_init = map_type_download_handle_init;
+ parent_class->download_handle_cleanup = map_type_download_handle_cleanup;
g_type_class_add_private (klass, sizeof (VikMapTypePrivate));
}
static int
-map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn)
+map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void * handle)
{
VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
g_return_val_if_fail (priv != NULL, 0);
- return (priv->map_type.download)(src, dest_fn);
+ return (priv->map_type.download)(src, dest_fn, handle);
+}
+
+static void *
+map_type_download_handle_init (VikMapSource * self)
+{
+ VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
+ g_return_val_if_fail (priv != NULL, 0);
+
+ return (priv->map_type.download_handle_init)();
+}
+
+static void
+map_type_download_handle_cleanup (VikMapSource * self, void * handle)
+{
+ VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
+ g_return_val_if_fail (priv != NULL, 0);
+
+ return (priv->map_type.download_handle_cleanup)(handle);
}
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 );
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 */
}
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 );
+ 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 )
{