From 0c6b26d31b0d192fe8715244b25b426658e67063 Mon Sep 17 00:00:00 2001 From: Guilhem Bonnefille Date: Thu, 17 Dec 2009 23:48:22 +0100 Subject: [PATCH] Refactoring: move download code to vikgototool --- src/googlesearch.c | 62 ++++++-------------------- src/vikgototool.c | 77 +++++++++++++++++++++++++++++++-- src/vikgototool.h | 8 +++- src/vikgotoxmltool.c | 101 +++++++++++++------------------------------ 4 files changed, 123 insertions(+), 125 deletions(-) diff --git a/src/googlesearch.c b/src/googlesearch.c index 00acb371..f9cb614c 100644 --- a/src/googlesearch.c +++ b/src/googlesearch.c @@ -48,7 +48,9 @@ static void google_goto_tool_init ( GoogleGotoTool *vwd ); static void google_goto_tool_finalize ( GObject *gob ); -static int google_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ); +static gchar *google_goto_tool_get_url_format ( VikGotoTool *self ); +static DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self ); +static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll); GType google_goto_tool_get_type() { @@ -85,7 +87,9 @@ static void google_goto_tool_class_init ( GoogleGotoToolClass *klass ) parent_class = VIK_GOTO_TOOL_CLASS (klass); - parent_class->get_coord = google_goto_tool_get_coord; + parent_class->get_url_format = google_goto_tool_get_url_format; + parent_class->get_download_options = google_goto_tool_get_download_options; + parent_class->parse_file_for_latlon = google_goto_tool_parse_file_for_latlon; } GoogleGotoTool *google_goto_tool_new () @@ -102,7 +106,7 @@ static void google_goto_tool_finalize ( GObject *gob ) G_OBJECT_GET_CLASS(gob)->finalize(gob); } -static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll) +static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll) { gchar *text, *pat; GMappedFile *mf; @@ -170,52 +174,12 @@ done: } -static int google_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) +static gchar *google_goto_tool_get_url_format ( VikGotoTool *self ) { - FILE *tmp_file; - int tmp_fd; - gchar *tmpname; - gchar *uri; - gchar *escaped_srch_str; - int ret = 0; /* OK */ - struct LatLon ll; - - g_debug("%s: raw search: %s", __FUNCTION__, srch_str); - - escaped_srch_str = uri_escape(srch_str); - - g_debug("%s: escaped search: %s", __FUNCTION__, escaped_srch_str); - - if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) { - g_critical(_("couldn't open temp file")); - exit(1); - } - - tmp_file = fdopen(tmp_fd, "r+"); - //uri = g_strdup_printf(GOOGLE_GOTO_URL_FMT, srch_str); - uri = g_strdup_printf(GOOGLE_GOTO_URL_FMT, escaped_srch_str); - - /* TODO: curl may not be available */ - if (curl_download_uri(uri, tmp_file, &googlesearch_options)) { /* error */ - fclose(tmp_file); - tmp_file = NULL; - ret = -1; - goto done; - } - - fclose(tmp_file); - tmp_file = NULL; - if (!parse_file_for_latlon(tmpname, &ll)) { - ret = -1; - goto done; - } - - vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); + return GOOGLE_GOTO_URL_FMT; +} -done: - g_free(escaped_srch_str); - g_free(uri); - g_remove(tmpname); - g_free(tmpname); - return ret; +DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self ) +{ + return &googlesearch_options; } diff --git a/src/vikgototool.c b/src/vikgototool.c index e5692e02..4ed2dfee 100644 --- a/src/vikgototool.c +++ b/src/vikgototool.c @@ -24,10 +24,14 @@ #endif #include "vikgototool.h" +#include "util.h" +#include "curl_download.h" #include +#include #include +#include static void goto_tool_class_init ( VikGotoToolClass *klass ); static void goto_tool_init ( VikGotoTool *vlp ); @@ -36,6 +40,7 @@ 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 ); typedef struct _VikGotoToolPrivate VikGotoToolPrivate; @@ -167,6 +172,7 @@ static void goto_tool_class_init ( VikGotoToolClass *klass ) pspec); klass->get_label = goto_tool_get_label; + klass->get_download_options = goto_tool_get_download_options; parent_class = g_type_class_peek_parent (klass); @@ -198,12 +204,77 @@ static gchar *goto_tool_get_label ( VikGotoTool *self ) return g_strdup ( priv->label ); } -gchar *vik_goto_tool_get_label ( VikGotoTool *w ) +static DownloadOptions *goto_tool_get_download_options ( VikGotoTool *self ) { - return VIK_GOTO_TOOL_GET_CLASS( w )->get_label( w ); + // Default: return NULL + return NULL; +} + +gchar *vik_goto_tool_get_label ( VikGotoTool *self ) +{ + return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self ); +} + +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 ) +{ + return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self ); +} + +gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll) +{ + return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll ); } int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) { - return VIK_GOTO_TOOL_GET_CLASS( self )->get_coord( self, vw, vvp, srch_str, coord ); + FILE *tmp_file; + int tmp_fd; + gchar *tmpname; + gchar *uri; + gchar *escaped_srch_str; + int ret = 0; /* OK */ + struct LatLon ll; + + g_debug("%s: raw goto: %s", __FUNCTION__, srch_str); + + escaped_srch_str = uri_escape(srch_str); + + 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; + } + + 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; + goto done; + } + vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); + +done: + g_free(escaped_srch_str); + g_free(uri); + g_remove(tmpname); + g_free(tmpname); + return ret; } diff --git a/src/vikgototool.h b/src/vikgototool.h index f8043c94..95f94bc5 100644 --- a/src/vikgototool.h +++ b/src/vikgototool.h @@ -24,6 +24,7 @@ #include #include "vikwindow.h" +#include "download.h" #define VIK_GOTO_TOOL_TYPE (vik_goto_tool_get_type ()) #define VIK_GOTO_TOOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIK_GOTO_TOOL_TYPE, VikGotoTool)) @@ -40,7 +41,9 @@ struct _VikGotoToolClass { GObjectClass object_class; gchar *(* get_label) (VikGotoTool *self); - int (* get_coord) (VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord); + gchar *(* get_url_format) (VikGotoTool *self); + DownloadOptions *(* get_download_options) (VikGotoTool *self); + gboolean (* parse_file_for_latlon) (VikGotoTool *self, gchar *filename, struct LatLon *ll); }; GType vik_goto_tool_get_type (); @@ -50,6 +53,9 @@ struct _VikGotoTool { }; gchar *vik_goto_tool_get_label ( VikGotoTool *self ); +gchar *vik_goto_tool_get_url_format ( VikGotoTool *self ); +DownloadOptions *vik_goto_tool_get_download_options ( VikGotoTool *self ); +gboolean vik_goto_tool_parse_file_for_latlon ( VikGotoTool *self, gchar *filename, struct LatLon *ll ); int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ); #endif diff --git a/src/vikgotoxmltool.c b/src/vikgotoxmltool.c index 06767f58..29de52c2 100644 --- a/src/vikgotoxmltool.c +++ b/src/vikgotoxmltool.c @@ -35,18 +35,17 @@ #include #include "viking.h" -#include "util.h" -#include "curl_download.h" #include "vikgotoxmltool.h" -static void vik_goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ); -static void vik_goto_xml_tool_init ( VikGotoXmlTool *vwd ); +static void _goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ); +static void _goto_xml_tool_init ( VikGotoXmlTool *self ); -static void vik_goto_xml_tool_finalize ( GObject *gob ); +static void _goto_xml_tool_finalize ( GObject *gob ); -static int vik_goto_xml_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ); +static gchar *_goto_xml_tool_get_url_format ( VikGotoTool *self ); +static gboolean _goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll); typedef struct _VikGotoXmlToolPrivate VikGotoXmlToolPrivate; @@ -74,12 +73,12 @@ GType vik_goto_xml_tool_get_type() sizeof (VikGotoXmlToolClass), NULL, /* base_init */ NULL, /* base_finalize */ - (GClassInitFunc) vik_goto_xml_tool_class_init, + (GClassInitFunc) _goto_xml_tool_class_init, NULL, /* class_finalize */ NULL, /* class_data */ sizeof (VikGotoXmlTool), 0, - (GInstanceInitFunc) vik_goto_xml_tool_init, + (GInstanceInitFunc) _goto_xml_tool_init, }; w_type = g_type_register_static ( VIK_GOTO_TOOL_TYPE, "VikGotoXmlTool", &w_info, 0 ); } @@ -97,10 +96,10 @@ enum }; static void -xml_goto_tool_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) +_goto_xml_tool_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) { VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object); VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); @@ -130,10 +129,10 @@ xml_goto_tool_set_property (GObject *object, } static void -xml_goto_tool_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) +_goto_xml_tool_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) { VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object); VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); @@ -159,7 +158,7 @@ xml_goto_tool_get_property (GObject *object, } } -static void vik_goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ) +static void _goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ) { GObjectClass *object_class; VikGotoToolClass *parent_class; @@ -167,9 +166,9 @@ static void vik_goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ) object_class = G_OBJECT_CLASS (klass); - object_class->finalize = vik_goto_xml_tool_finalize; - object_class->set_property = xml_goto_tool_set_property; - object_class->get_property = xml_goto_tool_get_property; + object_class->finalize = _goto_xml_tool_finalize; + object_class->set_property = _goto_xml_tool_set_property; + object_class->get_property = _goto_xml_tool_get_property; pspec = g_param_spec_string ("url-format", @@ -201,7 +200,8 @@ static void vik_goto_xml_tool_class_init ( VikGotoXmlToolClass *klass ) parent_class = VIK_GOTO_TOOL_CLASS (klass); - parent_class->get_coord = vik_goto_xml_tool_get_coord; + parent_class->get_url_format = _goto_xml_tool_get_url_format; + parent_class->parse_file_for_latlon = _goto_xml_tool_parse_file_for_latlon; g_type_class_add_private (klass, sizeof (VikGotoXmlToolPrivate)); } @@ -211,7 +211,7 @@ VikGotoXmlTool *vik_goto_xml_tool_new () return VIK_GOTO_XML_TOOL ( g_object_new ( VIK_GOTO_XML_TOOL_TYPE, "label", "Google", NULL ) ); } -static void vik_goto_xml_tool_init ( VikGotoXmlTool *self ) +static void _goto_xml_tool_init ( VikGotoXmlTool *self ) { VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); priv->url_format = NULL; @@ -222,7 +222,7 @@ static void vik_goto_xml_tool_init ( VikGotoXmlTool *self ) priv->ll.lon = NAN; } -static void vik_goto_xml_tool_finalize ( GObject *gob ) +static void _goto_xml_tool_finalize ( GObject *gob ) { G_OBJECT_GET_CLASS(gob)->finalize(gob); } @@ -282,12 +282,13 @@ _text (GMarkupParseContext *context, } static gboolean -parse_file_for_latlon(VikGotoXmlTool *self, gchar *filename, struct LatLon *ll) +_goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll) { GMarkupParser xml_parser; GMarkupParseContext *xml_context; GError *error; VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); + g_return_val_if_fail(priv != NULL, FALSE); FILE *file = g_fopen (filename, "r"); if (file == NULL) @@ -333,54 +334,10 @@ parse_file_for_latlon(VikGotoXmlTool *self, gchar *filename, struct LatLon *ll) return TRUE; } -static int vik_goto_xml_tool_get_coord ( VikGotoTool *object, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) +static gchar * +_goto_xml_tool_get_url_format ( VikGotoTool *self ) { - FILE *tmp_file; - int tmp_fd; - gchar *tmpname; - gchar *uri; - gchar *escaped_srch_str; - int ret = 0; /* OK */ - struct LatLon ll; - - g_debug("%s: raw goto: %s", __FUNCTION__, srch_str); - - escaped_srch_str = uri_escape(srch_str); - - g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str); - - if ((tmp_fd = g_file_open_tmp ("GOTO.XXXXXX", &tmpname, NULL)) == -1) { - g_critical(_("couldn't open temp file")); - exit(1); - } - - VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object); VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); - - tmp_file = fdopen(tmp_fd, "r+"); - uri = g_strdup_printf(priv->url_format, escaped_srch_str); - - /* TODO: curl may not be available */ - if (curl_download_uri(uri, tmp_file, NULL)) { /* error */ - fclose(tmp_file); - tmp_file = NULL; - ret = -1; - goto done; - } - - fclose(tmp_file); - tmp_file = NULL; - g_debug("%s: %s", __FILE__, tmpname); - if (!parse_file_for_latlon(self, tmpname, &ll)) { - ret = -1; - goto done; - } - vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); - -done: - g_free(escaped_srch_str); - g_free(uri); - g_remove(tmpname); - g_free(tmpname); - return ret; + g_return_val_if_fail(priv != NULL, NULL); + return priv->url_format; } -- 2.39.5