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