g_remove(name_src);
fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL);
- if (fetch_ret == 0) {
+ if (fetch_ret == DOWNLOAD_SUCCESS) {
if (input_type != NULL) {
babelargs = g_strdup_printf(" -i %s", input_type);
ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL );
curl = handle ? handle : curl_easy_init ();
if ( !curl ) {
- return DOWNLOAD_ERROR;
+ return CURL_DOWNLOAD_ERROR;
}
if (vik_verbose)
curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
glong response;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
if (response == 304) { // 304 = Not Modified
- res = DOWNLOAD_NO_NEWER_FILE;
+ res = CURL_DOWNLOAD_NO_NEWER_FILE;
} else if (response == 200 || // http: 200 = Ok
response == 226) { // ftp: 226 = sucess
gdouble size;
when the server has a (incorrect) time earlier than the time on the file we already have */
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
if (size == 0)
- res = DOWNLOAD_ERROR;
+ res = CURL_DOWNLOAD_ERROR;
else
- res = DOWNLOAD_NO_ERROR;
+ res = CURL_DOWNLOAD_NO_ERROR;
} else {
g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
- res = DOWNLOAD_ERROR;
+ res = CURL_DOWNLOAD_ERROR;
}
} else {
- res = DOWNLOAD_ERROR;
+ res = CURL_DOWNLOAD_ERROR;
}
if (!handle)
curl_easy_cleanup ( curl );
G_BEGIN_DECLS
+/* Error messages returned by download functions */
+enum { CURL_DOWNLOAD_NO_ERROR = 0,
+ CURL_DOWNLOAD_NO_NEWER_FILE,
+ CURL_DOWNLOAD_ERROR };
+
void curl_download_init ();
void curl_download_uninit ();
int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle );
#endif
}
-static int download( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *options, gboolean ftp, void *handle)
+static DownloadResult_t download( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *options, gboolean ftp, void *handle)
{
FILE *f;
int ret;
if (options == NULL || (!options->check_file_server_time &&
!options->use_etag)) {
/* Nothing to do as file already exists and we don't want to check server */
- return -3;
+ return DOWNLOAD_NOT_REQUIRED;
}
time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u;
time_t file_time = buf.st_mtime;
if ( (time(NULL) - file_time) < tile_age ) {
/* File cache is too recent, so return */
- return -3;
+ return DOWNLOAD_NOT_REQUIRED;
}
if (options->check_file_server_time) {
g_free ( tmpfilename );
if (options->use_etag)
g_free ( file_options.etag );
- return -4;
+ return DOWNLOAD_FILE_WRITE_ERROR;
}
f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */
if ( ! f ) {
g_free ( tmpfilename );
if (options->use_etag)
g_free ( file_options.etag );
- return -4;
+ return DOWNLOAD_FILE_WRITE_ERROR;
}
/* Call the backend function */
ret = curl_download_get_url ( hostname, uri, f, options, ftp, &file_options, handle );
- if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
+ DownloadResult_t result = DOWNLOAD_SUCCESS;
+
+ if (ret != CURL_DOWNLOAD_NO_ERROR && ret != CURL_DOWNLOAD_NO_NEWER_FILE) {
g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
failure = TRUE;
+ result = DOWNLOAD_HTTP_ERROR;
}
if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
g_debug("%s: file content checking failed", __FUNCTION__);
failure = TRUE;
+ result = DOWNLOAD_CONTENT_ERROR;
}
fclose ( f );
g_free ( file_options.etag );
g_free ( file_options.new_etag );
}
- return -1;
+ return result;
}
if ( options->convert_file )
}
}
- if (ret == DOWNLOAD_NO_NEWER_FILE) {
+ if (ret == CURL_DOWNLOAD_NO_NEWER_FILE) {
g_remove ( tmpfilename );
#if GLIB_CHECK_VERSION(2,18,0)
g_utime ( fn, NULL ); /* update mtime of local copy */
g_free ( file_options.etag );
g_free ( file_options.new_etag );
}
- return 0;
+ return DOWNLOAD_SUCCESS;
}
-/* 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, DownloadMapOptions *opt, void *handle )
+/**
+ * uri: like "/uri.html?whatever"
+ * only reason for the "wrapper" is so we can do redirects.
+ */
+DownloadResult_t a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
{
return download ( hostname, uri, fn, opt, FALSE, handle );
}
-int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
+DownloadResult_t a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
{
return download ( hostname, uri, fn, opt, TRUE, handle );
}
void a_download_init(void);
+typedef enum {
+ DOWNLOAD_FILE_WRITE_ERROR = -4, // Can't write downloaded file :(
+ DOWNLOAD_HTTP_ERROR = -2,
+ DOWNLOAD_CONTENT_ERROR = -1,
+ DOWNLOAD_SUCCESS = 0,
+ DOWNLOAD_NOT_REQUIRED = 1, // Also 'successful'. e.g. Because file already exists and no time checks used
+} DownloadResult_t;
+
/* TODO: convert to Glib */
-int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle );
-int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle );
+DownloadResult_t a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle );
+DownloadResult_t a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle );
void *a_download_handle_init ();
void a_download_handle_cleanup ( void *handle );
gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options );
-/* Error messages returned by download functions */
-enum { DOWNLOAD_NO_ERROR = 0,
- DOWNLOAD_NO_NEWER_FILE,
- DOWNLOAD_ERROR };
-
G_END_DECLS
#endif
dest->north_south = (((gdouble)src->y) / expedia_altis_freq(src->scale)) - 90;
}
-static int expedia_download ( MapCoord *src, const gchar *dest_fn, void *handle )
+static DownloadResult_t expedia_download ( MapCoord *src, const gchar *dest_fn, void *handle )
{
gint height, width;
struct LatLon ll;
gchar *uri;
- int res = -1;
expedia_xy_to_latlon_middle ( src->scale, src->x, src->y, &ll );
uri = g_strdup_printf ( "/pub/agent.dll?qscr=mrdt&ID=3XNsF.&CenP=%lf,%lf&Lang=%s&Alti=%d&Size=%d,%d&Offs=0.000000,0.000000&BCheck&tpid=1",
ll.lat, ll.lon, (ll.lon > -30) ? "EUR0809" : "USA0409", src->scale, width, height );
- if ((res = a_http_download_get_url ( EXPEDIA_SITE, uri, dest_fn, &expedia_options, NULL )) == 0) /* All OK */
+ DownloadResult_t res = a_http_download_get_url ( EXPEDIA_SITE, uri, dest_fn, &expedia_options, NULL );
+ if (res == DOWNLOAD_SUCCESS)
expedia_snip ( dest_fn );
g_free(uri);
return(res);
ABS(intlon) );
static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_map_file, NULL, NULL };
- a_http_download_get_url ( SRTM_HTTP_SITE, src_fn, p->dest, &options, NULL );
+ DownloadResult_t result = a_http_download_get_url ( SRTM_HTTP_SITE, src_fn, p->dest, &options, NULL );
+ switch ( result ) {
+ case DOWNLOAD_CONTENT_ERROR:
+ case DOWNLOAD_HTTP_ERROR: {
+ gchar *msg = g_strdup_printf ( _("DEM download failure for %f, %f"), p->lat, p->lon );
+ vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(p->vdl), msg, VIK_STATUSBAR_INFO );
+ g_free ( msg );
+ break;
+ }
+ case DOWNLOAD_FILE_WRITE_ERROR: {
+ gchar *msg = g_strdup_printf ( _("DEM write failure for %s"), p->dest );
+ vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(p->vdl), msg, VIK_STATUSBAR_INFO );
+ g_free ( msg );
+ break;
+ }
+ case DOWNLOAD_SUCCESS:
+ case DOWNLOAD_NOT_REQUIRED:
+ default:
+ break;
+ }
g_free ( src_fn );
}
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, handle))
- continue;
+ DownloadResult_t dr = vik_map_source_download( MAPS_LAYER_NTH_TYPE(mdi->maptype), &(mdi->mapcoord), mdi->filename_buf, handle);
+ switch ( dr ) {
+ case DOWNLOAD_HTTP_ERROR:
+ case DOWNLOAD_CONTENT_ERROR: {
+ // TODO: ?? count up the number of download errors somehow...
+ gchar* msg = g_strdup_printf ( "%s: %s", vik_maps_layer_get_map_label (mdi->vml), _("Failed to download tile") );
+ vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(mdi->vml), msg, VIK_STATUSBAR_INFO );
+ g_free (msg);
+ break;
+ }
+ case DOWNLOAD_FILE_WRITE_ERROR: {
+ gchar* msg = g_strdup_printf ( "%s: %s", vik_maps_layer_get_map_label (mdi->vml), _("Unable to save tile") );
+ vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(mdi->vml), msg, VIK_STATUSBAR_INFO );
+ g_free (msg);
+ break;
+ }
+ case DOWNLOAD_SUCCESS:
+ case DOWNLOAD_NOT_REQUIRED:
+ default:
+ break;
+ }
}
g_mutex_lock(mdi->mutex);
#include "vikviewport.h"
#include "vikcoord.h"
#include "mapcoord.h"
-
+#include "download.h"
#include "vikmapsource.h"
static void vik_map_source_init (VikMapSource *object);
(*klass->mapcoord_to_center_coord)(self, src, dest);
}
-int
+/**
+ * vik_map_source_download:
+ * @self: The VikMapSource of interest.
+ * @src: The map location to download
+ * @dest_fn: The filename to save the result in
+ * @handle: Potential reusable Curl Handle (may be NULL)
+ *
+ * Returns: How successful the download was as per the type #DownloadResult_t
+ */
+DownloadResult_t
vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void *handle)
{
VikMapSourceClass *klass;
static guint16 map_source_get_tilesize_y (VikMapSource *self);
static VikViewportDrawMode map_source_get_drawmode (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 );
return priv->drawmode;
}
-static int
+static DownloadResult_t
_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));
- 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;
#include "vikmaptype.h"
#include "vikmapslayer_compat.h"
+#include "download.h"
static guint16 map_type_get_uniq_id (VikMapSource *self);
static const gchar *map_type_get_label (VikMapSource *self);
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, void * handle);
+static DownloadResult_t 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);
(priv->map_type.mapcoord_to_center_coord)(src, dest);
}
-static int
+static DownloadResult_t
map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void * handle)
{
VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
(priv->map_type.download_handle_cleanup)(handle);
}
-