]> git.street.me.uk Git - andy/viking.git/commitdiff
Factor download to a temporary file into a single reused function.
authorRob Norris <rw_norris@hotmail.com>
Mon, 26 Nov 2012 00:07:10 +0000 (00:07 +0000)
committerRob Norris <rw_norris@hotmail.com>
Mon, 7 Jan 2013 21:43:03 +0000 (21:43 +0000)
src/bingmapsource.c
src/download.c
src/download.h
src/geonamessearch.c
src/vikgototool.c

index 5a797d911cbc0d25d65a5613e4c24def51194ba7..cf72e21428c9e3c97c94b62e0a00dd366a1646a1 100644 (file)
@@ -46,7 +46,6 @@
 #include <glib/gi18n.h>
 #include <gdk-pixbuf/gdk-pixdata.h>
 #include "globals.h"
-#include "curl_download.h"
 #include "bingmapsource.h"
 #include "bbox.h"
 #include "background.h"
@@ -391,32 +390,12 @@ _parse_file_for_attributions(BingMapSource *self, gchar *filename)
 static int
 _load_attributions ( BingMapSource *self )
 {
-       FILE *tmp_file;
-       int tmp_fd;
-       gchar *tmpname;
-       gchar *uri;
        int ret = 0;  /* OK */
 
        BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
+       gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
 
-       if ((tmp_fd = g_file_open_tmp ("vik-bing.XXXXXX", &tmpname, NULL)) == -1) {
-               g_critical(_("couldn't open temp file"));
-               return -1;
-       }
-
-       tmp_file = fdopen(tmp_fd, "r+");
-       uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
-
-       /* TODO: curl may not be available */
-       if (curl_download_uri(uri, tmp_file, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)), 0, NULL)) {  /* error */
-               fclose(tmp_file);
-               tmp_file = NULL;
-               ret = -1;
-               goto done;
-       }
-
-       fclose(tmp_file);
-       tmp_file = NULL;
+       gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
 
        g_debug("%s: %s", __FUNCTION__, tmpname);
        if (!_parse_file_for_attributions(self, tmpname)) {
index 05a525a092338a448b7cb0ba64a6b9b7da4b0200..b881dc1eef8d7699b65b69deba3f3d94887758d3 100644 (file)
@@ -293,3 +293,37 @@ void a_download_handle_cleanup ( void *handle )
 {
   curl_download_handle_cleanup ( handle );
 }
+
+/**
+ * a_download_url_to_tmp_file:
+ * @uri:         The URI (Uniform Resource Identifier)
+ * @options:     Download options (maybe NULL)
+ *
+ * returns name of the temporary file created - NULL if unsuccessful
+ *  this string needs to be freed once used
+ *  the file needs to be removed once used
+ */
+gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options )
+{
+  FILE *tmp_file;
+  int tmp_fd;
+  gchar *tmpname;
+
+  if ( (tmp_fd = g_file_open_tmp ("viking-download.XXXXXX", &tmpname, NULL)) == -1 ) {
+    g_critical (_("couldn't open temp file"));
+    return NULL;
+  }
+
+  tmp_file = fdopen(tmp_fd, "r+");
+
+  if ( curl_download_uri ( uri, tmp_file, options, NULL, NULL ) ) {
+    // error
+    fclose ( tmp_file );
+    g_remove ( tmpname );
+    g_free ( tmpname );
+    return NULL;
+  }
+  fclose ( tmp_file );
+
+  return tmpname;
+}
index e9f69c237bd6eba271813879beda56aefbeea4de..7bbbfbb080cde73c261fbb513f337fe42438f943 100644 (file)
@@ -88,6 +88,8 @@ int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *
 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,
index 88f7df5eb6416cfa1cd1f551c0537b2550faa0d3..221fe2442477c8f2242222b72f81ced1a5970659 100644 (file)
@@ -31,7 +31,6 @@
 
 #include "viking.h"
 #include "util.h"
-#include "curl_download.h"
 #include "geonamessearch.h"
 
 /* Compatibility */
@@ -399,31 +398,6 @@ static GList *get_entries_from_file(gchar *file_name)
 }
 
 
-static gchar *download_url(gchar *uri)
-{
-  FILE *tmp_file;
-  int tmp_fd;
-  gchar *tmpname;
-
-  if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
-    g_critical(_("couldn't open temp file"));
-    return NULL;
-  }
-  tmp_file = fdopen(tmp_fd, "r+");
-
-  if (curl_download_uri(uri, tmp_file, NULL, 0, NULL)) {
-    // error
-    fclose(tmp_file);
-    tmp_file = NULL;
-    g_remove(tmpname);
-    g_free(tmpname);
-    return(NULL);
-  }
-  fclose(tmp_file);
-  tmp_file = NULL;
-  return(tmpname);
-}
-
 void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon maxmin[2] )
 {
   gchar *uri;
@@ -444,7 +418,7 @@ void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon m
   g_free(south); south = NULL;
   g_free(east);  east = NULL;
   g_free(west);  west = NULL;
-  tmpname = download_url(uri);
+  tmpname = a_download_uri_to_tmp_file ( uri, NULL );
   if (!tmpname) {
     none_found(vw);
     return;
index b6516c3f2a7c1edf08a9296481ce658b9c1c5267..f784228c6ceef8d5764fdb44867811c6555faaca 100644 (file)
@@ -25,7 +25,6 @@
 
 #include "vikgototool.h"
 #include "util.h"
-#include "curl_download.h"
 
 #include <string.h>
 
@@ -207,8 +206,6 @@ gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename
 
 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
 {
-  FILE *tmp_file;
-  int tmp_fd;
   gchar *tmpname;
   gchar *uri;
   gchar *escaped_srch_str;
@@ -221,24 +218,10 @@ int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp
 
   g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
 
-  if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) {
-    g_critical(_("couldn't open temp file"));
-    return -1;
-  }
-  
-  tmp_file = fdopen(tmp_fd, "r+");
   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, NULL)) {  /* error */
-    fclose(tmp_file);
-    tmp_file = NULL;
-    ret = -1;
-    goto done;
-  }
+  tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) );
 
-  fclose(tmp_file);
-  tmp_file = NULL;
   g_debug("%s: %s", __FILE__, tmpname);
   if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
     ret = -1;