]> git.street.me.uk Git - andy/viking.git/blame - src/googlesearch.c
Cosmetic change
[andy/viking.git] / src / googlesearch.c
CommitLineData
369126f3
QT
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 * Created by Quy Tonthat <qtonthat@gmail.com>
21 */
4c77d5e0
GB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
369126f3 25#include <stdlib.h>
8c060406 26#include <stdio.h>
369126f3 27#include <string.h>
8c060406
MA
28#include <glib.h>
29#include <glib/gstdio.h>
369126f3 30#include <glib/gprintf.h>
4c77d5e0 31#include <glib/gi18n.h>
369126f3
QT
32
33#include "viking.h"
34#include "curl_download.h"
35
36#define GOOGLE_SEARCH_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
844fde6c
QT
37#define GOOGLE_SEARCH_PATTERN_1 "{center:{lat:"
38#define GOOGLE_SEARCH_PATTERN_2 ",lng:"
d1f48cc2 39#define GOOGLE_SEARCH_NOT_FOUND "around this map area did not match any locations"
369126f3
QT
40
41static gchar *last_search_str = NULL;
014128f6
QT
42static VikCoord *last_coord = NULL;
43static gchar *last_successful_search_str = NULL;
44
1a8143e6 45static DownloadOptions googlesearch_options = { "http://maps.google.com/", 0 };
a3188993 46
014128f6
QT
47gchar * a_googlesearch_get_search_string_for_this_place(VikWindow *vw)
48{
812909d1
QT
49 if (!last_coord)
50 return NULL;
51
014128f6 52 VikViewport *vvp = vik_window_viewport(vw);
494eb388 53 const VikCoord *cur_center = vik_viewport_get_center(vvp);
014128f6
QT
54 if (vik_coord_equals(cur_center, last_coord)) {
55 return(last_successful_search_str);
56 }
57 else
58 return NULL;
59}
369126f3
QT
60
61static gboolean prompt_try_again(VikWindow *vw)
62{
63 GtkWidget *dialog = NULL;
64 gboolean ret = TRUE;
65
66 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
4c77d5e0 67 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
369126f3 68
4c77d5e0 69 GtkWidget *search_label = gtk_label_new(_("I don't know that place. Do you want another search?"));
369126f3
QT
70 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
71 gtk_widget_show_all(dialog);
72
73 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
74 ret = FALSE;
75
76 gtk_widget_destroy(dialog);
77 return ret;
78}
79
80static gchar * a_prompt_for_search_string(VikWindow *vw)
81{
82 GtkWidget *dialog = NULL;
83
84 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
4c77d5e0 85 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
369126f3 86
4c77d5e0 87 GtkWidget *search_label = gtk_label_new(_("Enter address or place name:"));
369126f3
QT
88 GtkWidget *search_entry = gtk_entry_new();
89 if (last_search_str)
90 gtk_entry_set_text(GTK_ENTRY(search_entry), last_search_str);
91
92 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
93 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_entry, FALSE, FALSE, 5 );
94 gtk_widget_show_all(dialog);
95
96 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
97 gtk_widget_destroy(dialog);
98 return NULL;
99 }
100
101 gchar *search_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(search_entry) ) );
102
369126f3
QT
103 gtk_widget_destroy(dialog);
104
105 if (search_str[0] != '\0') {
106 if (last_search_str)
107 g_free(last_search_str);
108 last_search_str = g_strdup(search_str);
109 }
110
111 return(search_str); /* search_str needs to be freed by caller */
112}
113
114static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll)
115{
116 gchar *text, *pat;
117 GMappedFile *mf;
118 gsize len;
119 gboolean found = TRUE;
120 gchar lat_buf[32], lon_buf[32];
121 gchar *s;
122
369126f3
QT
123 lat_buf[0] = lon_buf[0] = '\0';
124
125 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
4258f4e2 126 g_critical(_("couldn't map temp file"));
369126f3
QT
127 exit(1);
128 }
129 len = g_mapped_file_get_length(mf);
130 text = g_mapped_file_get_contents(mf);
131
d1f48cc2
QT
132 if (g_strstr_len(text, len, GOOGLE_SEARCH_NOT_FOUND) != NULL) {
133 found = FALSE;
134 goto done;
135 }
136
369126f3 137 if ((pat = g_strstr_len(text, len, GOOGLE_SEARCH_PATTERN_1)) == NULL) {
369126f3
QT
138 found = FALSE;
139 goto done;
140 }
369126f3
QT
141 pat += strlen(GOOGLE_SEARCH_PATTERN_1);
142 s = lat_buf;
143 if (*pat == '-')
144 *s++ = *pat++;
145 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
146 (g_ascii_isdigit(*pat) || (*pat == '.')))
147 *s++ = *pat++;
148 *s = '\0';
149 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
150 found = FALSE;
151 goto done;
152 }
153
154 if (strncmp(pat, GOOGLE_SEARCH_PATTERN_2, strlen(GOOGLE_SEARCH_PATTERN_2))) {
155 found = FALSE;
156 goto done;
157 }
369126f3
QT
158
159 pat += strlen(GOOGLE_SEARCH_PATTERN_2);
160 s = lon_buf;
161
162 if (*pat == '-')
163 *s++ = *pat++;
164 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
165 (g_ascii_isdigit(*pat) || (*pat == '.')))
166 *s++ = *pat++;
167 *s = '\0';
168 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
169 found = FALSE;
170 goto done;
171 }
172
173 ll->lat = g_ascii_strtod(lat_buf, NULL);
174 ll->lon = g_ascii_strtod(lon_buf, NULL);
175
369126f3
QT
176done:
177 g_mapped_file_free(mf);
369126f3
QT
178 return (found);
179
180}
181
182gchar *uri_escape(gchar *str)
183{
184 gchar *esc_str = g_malloc(3*strlen(str));
185 gchar *dst = esc_str;
186 gchar *src;
187
188 for (src = str; *src; src++) {
189 if (*src == ' ')
190 *dst++ = '+';
191 else if (g_ascii_isalnum(*src))
192 *dst++ = *src;
193 else {
194 g_sprintf(dst, "%%%02X", *src);
195 dst += 3;
196 }
197 }
198
199 return(esc_str);
200}
201
202static int google_search_get_coord(VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord)
203{
204 FILE *tmp_file;
205 int tmp_fd;
206 gchar *tmpname;
207 gchar *uri;
208 gchar *escaped_srch_str;
209 int ret = 0; /* OK */
210 struct LatLon ll;
211
212 escaped_srch_str = uri_escape(srch_str);
213
d1f48cc2 214 if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
4258f4e2 215 g_critical(_("couldn't open temp file"));
369126f3
QT
216 exit(1);
217 }
218
219 tmp_file = fdopen(tmp_fd, "r+");
220 //uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, srch_str);
221 uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, escaped_srch_str);
222
369126f3 223 /* TODO: curl may not be available */
a3188993 224 if (curl_download_uri(uri, tmp_file, &googlesearch_options)) { /* error */
369126f3 225 fclose(tmp_file);
8c060406 226 tmp_file = NULL;
369126f3
QT
227 ret = -1;
228 goto done;
229 }
230
231 fclose(tmp_file);
8c060406 232 tmp_file = NULL;
369126f3
QT
233 if (!parse_file_for_latlon(tmpname, &ll)) {
234 ret = -1;
235 goto done;
236 }
237
238 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
239
014128f6
QT
240 if (last_coord)
241 g_free(last_coord);
242 last_coord = g_malloc(sizeof(VikCoord));
243 *last_coord = *coord;
244 if (last_successful_search_str)
245 g_free(last_successful_search_str);
246 last_successful_search_str = g_strdup(last_search_str);
247
369126f3
QT
248done:
249 g_free(escaped_srch_str);
250 g_free(uri);
8c060406 251 g_remove(tmpname);
369126f3
QT
252 g_free(tmpname);
253 return ret;
254}
255
256void a_google_search(VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp)
257{
258 VikCoord new_center;
259 gchar *s_str;
260 gboolean more = TRUE;
261
262 do {
263 s_str = a_prompt_for_search_string(vw);
264 if ((!s_str) || (s_str[0] == 0)) {
265 more = FALSE;
266 }
267
268 else if (!google_search_get_coord(vw, vvp, s_str, &new_center)) {
269 vik_viewport_set_center_coord(vvp, &new_center);
270 vik_layers_panel_emit_update(vlp);
271 more = FALSE;
272 }
273 else if (!prompt_try_again(vw))
274 more = FALSE;
275 g_free(s_str);
276 } while (more);
277}
278