X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/fb07e4d71c0315a0f7af12b3cf31194efa8c7542..686baff0fa2461a831bd1d0b614478a31c6a0a95:/src/vikgototool.c diff --git a/src/vikgototool.c b/src/vikgototool.c index 4ed2dfee..20cc7624 100644 --- a/src/vikgototool.c +++ b/src/vikgototool.c @@ -25,7 +25,6 @@ #include "vikgototool.h" #include "util.h" -#include "curl_download.h" #include @@ -33,14 +32,11 @@ #include #include -static void goto_tool_class_init ( VikGotoToolClass *klass ); -static void goto_tool_init ( VikGotoTool *vlp ); - static GObjectClass *parent_class; static void goto_tool_finalize ( GObject *gob ); static gchar *goto_tool_get_label ( VikGotoTool *vw ); -static DownloadOptions *goto_tool_get_download_options ( VikGotoTool *self ); +static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self ); typedef struct _VikGotoToolPrivate VikGotoToolPrivate; @@ -54,29 +50,7 @@ struct _VikGotoToolPrivate VIK_GOTO_TOOL_TYPE, \ VikGotoToolPrivate)) -GType vik_goto_tool_get_type() -{ - static GType w_type = 0; - - if (!w_type) - { - static const GTypeInfo w_info = - { - sizeof (VikGotoToolClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) goto_tool_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (VikGotoTool), - 0, - (GInstanceInitFunc) goto_tool_init, - }; - w_type = g_type_register_static ( G_TYPE_OBJECT, "VikGotoTool", &w_info, G_TYPE_FLAG_ABSTRACT ); - } - - return w_type; -} +G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT) enum { @@ -141,7 +115,7 @@ goto_tool_get_property (GObject *object, } } -static void goto_tool_class_init ( VikGotoToolClass *klass ) +static void vik_goto_tool_class_init ( VikGotoToolClass *klass ) { GObjectClass *gobject_class; GParamSpec *pspec; @@ -184,7 +158,7 @@ VikGotoTool *vik_goto_tool_new () return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) ); } -static void goto_tool_init ( VikGotoTool *self ) +static void vik_goto_tool_init ( VikGotoTool *self ) { VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self); priv->label = NULL; @@ -204,7 +178,7 @@ static gchar *goto_tool_get_label ( VikGotoTool *self ) return g_strdup ( priv->label ); } -static DownloadOptions *goto_tool_get_download_options ( VikGotoTool *self ) +static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self ) { // Default: return NULL return NULL; @@ -220,7 +194,7 @@ gchar *vik_goto_tool_get_url_format ( VikGotoTool *self ) return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self ); } -DownloadOptions *vik_goto_tool_get_download_options ( VikGotoTool *self ) +DownloadFileOptions *vik_goto_tool_get_download_options ( VikGotoTool *self ) { return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self ); } @@ -230,10 +204,22 @@ gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll ); } +/** + * vik_goto_tool_get_coord: + * + * @self: The #VikGotoTool + * @vvp: The #VikViewport + * @srch_str: The string to search with + * @coord: Returns the top match position for a successful search + * + * Returns: An integer value indicating: + * 0 = search found something + * -1 = search place not found by the #VikGotoTool + * 1 = search unavailable in the #VikGotoTool due to communication issue + * + */ 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; @@ -246,24 +232,16 @@ 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))) { /* 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) ); + + if ( !tmpname ) { + // Some kind of download error, so no tmp file + ret = 1; + goto done_no_file; } - 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; @@ -272,9 +250,10 @@ int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); done: + (void)util_remove(tmpname); +done_no_file: + g_free(tmpname); g_free(escaped_srch_str); g_free(uri); - g_remove(tmpname); - g_free(tmpname); return ret; }