Fix URL formatting issue
[viking/guyou.git] / src / googlesearch.c
bloba46430898fabf1c051fcd8326a6cf988311f7fd0
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Created by Quy Tonthat <qtonthat@gmail.com>
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <glib/gprintf.h>
31 #include <glib/gi18n.h>
33 #include "viking.h"
34 #include "util.h"
35 #include "curl_download.h"
37 #include "googlesearch.h"
39 #define GOOGLE_SEARCH_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
40 #define GOOGLE_SEARCH_PATTERN_1 "{center:{lat:"
41 #define GOOGLE_SEARCH_PATTERN_2 ",lng:"
42 #define GOOGLE_SEARCH_NOT_FOUND "not understand the location"
44 static DownloadOptions googlesearch_options = { "http://maps.google.com/", 0, a_check_map_file };
46 static void google_search_tool_class_init ( GoogleSearchToolClass *klass );
47 static void google_search_tool_init ( GoogleSearchTool *vwd );
49 static void google_search_tool_finalize ( GObject *gob );
51 static int google_search_tool_get_coord ( VikSearchTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord );
53 GType google_search_tool_get_type()
55 static GType w_type = 0;
57 if (!w_type)
59 static const GTypeInfo w_info =
61 sizeof (GoogleSearchToolClass),
62 NULL, /* base_init */
63 NULL, /* base_finalize */
64 (GClassInitFunc) google_search_tool_class_init,
65 NULL, /* class_finalize */
66 NULL, /* class_data */
67 sizeof (GoogleSearchTool),
69 (GInstanceInitFunc) google_search_tool_init,
71 w_type = g_type_register_static ( VIK_SEARCH_TOOL_TYPE, "GoogleSearchTool", &w_info, 0 );
74 return w_type;
77 static void google_search_tool_class_init ( GoogleSearchToolClass *klass )
79 GObjectClass *object_class;
80 VikSearchToolClass *parent_class;
82 object_class = G_OBJECT_CLASS (klass);
84 object_class->finalize = google_search_tool_finalize;
86 parent_class = VIK_SEARCH_TOOL_CLASS (klass);
88 parent_class->get_coord = google_search_tool_get_coord;
91 GoogleSearchTool *google_search_tool_new ()
93 return GOOGLE_SEARCH_TOOL ( g_object_new ( GOOGLE_SEARCH_TOOL_TYPE, "label", "Google", NULL ) );
96 static void google_search_tool_init ( GoogleSearchTool *vlp )
100 static void google_search_tool_finalize ( GObject *gob )
102 G_OBJECT_GET_CLASS(gob)->finalize(gob);
105 static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll)
107 gchar *text, *pat;
108 GMappedFile *mf;
109 gsize len;
110 gboolean found = TRUE;
111 gchar lat_buf[32], lon_buf[32];
112 gchar *s;
114 lat_buf[0] = lon_buf[0] = '\0';
116 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
117 g_critical(_("couldn't map temp file"));
118 exit(1);
120 len = g_mapped_file_get_length(mf);
121 text = g_mapped_file_get_contents(mf);
123 if (g_strstr_len(text, len, GOOGLE_SEARCH_NOT_FOUND) != NULL) {
124 found = FALSE;
125 goto done;
128 if ((pat = g_strstr_len(text, len, GOOGLE_SEARCH_PATTERN_1)) == NULL) {
129 found = FALSE;
130 goto done;
132 pat += strlen(GOOGLE_SEARCH_PATTERN_1);
133 s = lat_buf;
134 if (*pat == '-')
135 *s++ = *pat++;
136 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
137 (g_ascii_isdigit(*pat) || (*pat == '.')))
138 *s++ = *pat++;
139 *s = '\0';
140 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
141 found = FALSE;
142 goto done;
145 if (strncmp(pat, GOOGLE_SEARCH_PATTERN_2, strlen(GOOGLE_SEARCH_PATTERN_2))) {
146 found = FALSE;
147 goto done;
150 pat += strlen(GOOGLE_SEARCH_PATTERN_2);
151 s = lon_buf;
153 if (*pat == '-')
154 *s++ = *pat++;
155 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
156 (g_ascii_isdigit(*pat) || (*pat == '.')))
157 *s++ = *pat++;
158 *s = '\0';
159 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
160 found = FALSE;
161 goto done;
164 ll->lat = g_ascii_strtod(lat_buf, NULL);
165 ll->lon = g_ascii_strtod(lon_buf, NULL);
167 done:
168 g_mapped_file_free(mf);
169 return (found);
173 static int google_search_tool_get_coord ( VikSearchTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
175 FILE *tmp_file;
176 int tmp_fd;
177 gchar *tmpname;
178 gchar *uri;
179 gchar *escaped_srch_str;
180 int ret = 0; /* OK */
181 struct LatLon ll;
183 g_debug("%s: raw search: %s", __FUNCTION__, srch_str);
185 escaped_srch_str = uri_escape(srch_str);
187 g_debug("%s: escaped search: %s", __FUNCTION__, escaped_srch_str);
189 if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
190 g_critical(_("couldn't open temp file"));
191 exit(1);
194 tmp_file = fdopen(tmp_fd, "r+");
195 //uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, srch_str);
196 uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, escaped_srch_str);
198 /* TODO: curl may not be available */
199 if (curl_download_uri(uri, tmp_file, &googlesearch_options)) { /* error */
200 fclose(tmp_file);
201 tmp_file = NULL;
202 ret = -1;
203 goto done;
206 fclose(tmp_file);
207 tmp_file = NULL;
208 if (!parse_file_for_latlon(tmpname, &ll)) {
209 ret = -1;
210 goto done;
213 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
215 done:
216 g_free(escaped_srch_str);
217 g_free(uri);
218 g_remove(tmpname);
219 g_free(tmpname);
220 return ret;