]> git.street.me.uk Git - andy/viking.git/commitdiff
Report download failures in the statusbar.
authorRob Norris <rw_norris@hotmail.com>
Sat, 22 Mar 2014 12:08:41 +0000 (12:08 +0000)
committerRob Norris <rw_norris@hotmail.com>
Sat, 22 Mar 2014 12:08:41 +0000 (12:08 +0000)
Tile and DEM download failures are now shown in the GUI,
 rather than just messages on the console.

Use enumerated failure states.

src/babel.c
src/curl_download.c
src/curl_download.h
src/download.c
src/download.h
src/expedia.c
src/vikdemlayer.c
src/vikmapslayer.c
src/vikmapsource.c
src/vikmapsourcedefault.c
src/vikmaptype.c

index 93ad52e32cbf00f7d0c73634a1bf66c6ca5ba898..27553c6d723d2a59bae604776edbd9ea346cb2ef 100644 (file)
@@ -374,7 +374,7 @@ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char
     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 );
index 8b171f8af0ae3ca9ca8c2e433f10c6b96a0ff090..be54741cabf143324f5315318c29f9851e8f7f56 100644 (file)
@@ -125,7 +125,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
 
   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 );
@@ -176,7 +176,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
     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;
@@ -184,15 +184,15 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
          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 );
index ef2cd62a2a84930d75e4a9519a2bb7095fbd37b4..84d8ed172907837c2b1e5e9b4986b8528f62ece6 100644 (file)
 
 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 );
index 3660fa189731fab58e3fa746a18a3cb75fe59b44..d640d2221f1cd37d0bbbdc62df8d3b7b1c8b3f17 100644 (file)
@@ -237,7 +237,7 @@ void a_try_decompress_file (gchar *name)
 #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;
@@ -251,7 +251,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     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;
@@ -261,7 +261,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     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) {
@@ -296,7 +296,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     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 ) {
@@ -304,20 +304,24 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     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 );
@@ -333,7 +337,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
       g_free ( file_options.etag );
       g_free ( file_options.new_etag );
     }
-    return -1;
+    return result;
   }
 
   if ( options->convert_file )
@@ -349,7 +353,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     }
   }
 
-  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 */
@@ -366,18 +370,19 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
     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 );
 }
index 424d28b71dbd6390c651b3da7f811bbdb01b12b6..43ecdd2c038e847b53dda2ebc2b43c256fddb2bb 100644 (file)
@@ -96,19 +96,22 @@ typedef struct {
 
 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
index 2443ceb7e5fddedb7b531325c7b623d3dd89fc53..abaec47024f2e683c8ec51a709ffba718ffa9e08 100644 (file)
@@ -169,12 +169,11 @@ static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
   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 );
 
@@ -187,7 +186,8 @@ static int expedia_download ( MapCoord *src, const gchar *dest_fn, void *handle
   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);
index cae46a4e87cf33b177e734dd661d726d6a15c792..99efc49c5c9f01591b719c653878616934a5578f 100644 (file)
@@ -953,7 +953,26 @@ static void srtm_dem_download_thread ( DEMDownloadParams *p, gpointer threaddata
                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 );
 }
 
index b8e38f01ce3cc07c228a0c1fb6c4421f2fea3e55..2718a11e37c47681dbe324aa76c35d4b88258568 100644 (file)
@@ -1371,8 +1371,27 @@ static int map_download_thread ( MapDownloadInfo *mdi, gpointer threaddata )
       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);
index bc7e558312699170a00d6f1a05fdd0e3ce607c04..7d4cc97cb7111dc8c9441a8ff0e89534d2db8201 100644 (file)
@@ -31,7 +31,7 @@
 #include "vikviewport.h"
 #include "vikcoord.h"
 #include "mapcoord.h"
-
+#include "download.h"
 #include "vikmapsource.h"
 
 static void vik_map_source_init (VikMapSource *object);
@@ -296,7 +296,16 @@ vik_map_source_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikC
        (*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;
index 9251588897c5b65ace32bb5a461f2498c1cdb73c..a4ac7b9e02b395473f21d1add060973e2c0f2ed3 100644 (file)
@@ -41,7 +41,7 @@ static guint16 map_source_get_tilesize_x (VikMapSource *self);
 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 );
 
@@ -409,14 +409,13 @@ map_source_get_drawmode (VikMapSource *self)
        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;
index 2ca7265657b4b58179bc1739048234ddf144eba6..fecd9d2ce01bb8ed7e1b3ab58bd3c08c6cb97678 100644 (file)
@@ -31,6 +31,7 @@
 
 #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);
@@ -39,7 +40,7 @@ static guint16 map_type_get_tilesize_y (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);
 
@@ -168,7 +169,7 @@ map_type_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikCoord *
        (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);
@@ -194,4 +195,3 @@ map_type_download_handle_cleanup (VikMapSource * self, void * handle)
 
        (priv->map_type.download_handle_cleanup)(handle);
 }
-