Marking translatable string in main.c
[viking.git] / src / googlesearch.c
blob3448200f15e7ba50450ca3e7ac34b8ac86e4677b
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 <string.h>
27 #include <glib/gprintf.h>
28 #include <glib/gi18n.h>
30 #include "viking.h"
31 #include "curl_download.h"
33 #define GOOGLE_SEARCH_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
34 #define GOOGLE_SEARCH_PATTERN_1 "{center:{lat:"
35 #define GOOGLE_SEARCH_PATTERN_2 ",lng:"
36 #define GOOGLE_SEARCH_NOT_FOUND "around this map area did not match any locations"
38 static gchar *last_search_str = NULL;
39 static VikCoord *last_coord = NULL;
40 static gchar *last_successful_search_str = NULL;
42 static DownloadOptions googlesearch_options = { "http://maps.google.com/", 0 };
44 gchar * a_googlesearch_get_search_string_for_this_place(VikWindow *vw)
46 if (!last_coord)
47 return NULL;
49 VikViewport *vvp = vik_window_viewport(vw);
50 const VikCoord *cur_center = vik_viewport_get_center(vvp);
51 if (vik_coord_equals(cur_center, last_coord)) {
52 return(last_successful_search_str);
54 else
55 return NULL;
58 static gboolean prompt_try_again(VikWindow *vw)
60 GtkWidget *dialog = NULL;
61 gboolean ret = TRUE;
63 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
64 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
66 GtkWidget *search_label = gtk_label_new(_("I don't know that place. Do you want another search?"));
67 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
68 gtk_widget_show_all(dialog);
70 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
71 ret = FALSE;
73 gtk_widget_destroy(dialog);
74 return ret;
77 static gchar * a_prompt_for_search_string(VikWindow *vw)
79 GtkWidget *dialog = NULL;
81 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
82 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
84 GtkWidget *search_label = gtk_label_new(_("Enter address or place name:"));
85 GtkWidget *search_entry = gtk_entry_new();
86 if (last_search_str)
87 gtk_entry_set_text(GTK_ENTRY(search_entry), last_search_str);
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);
93 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
94 gtk_widget_destroy(dialog);
95 return NULL;
98 gchar *search_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(search_entry) ) );
100 gtk_widget_destroy(dialog);
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);
108 return(search_str); /* search_str needs to be freed by caller */
111 static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll)
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;
120 lat_buf[0] = lon_buf[0] = '\0';
122 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
123 g_critical(_("couldn't map temp file\n"));
124 exit(1);
126 len = g_mapped_file_get_length(mf);
127 text = g_mapped_file_get_contents(mf);
129 if (g_strstr_len(text, len, GOOGLE_SEARCH_NOT_FOUND) != NULL) {
130 found = FALSE;
131 goto done;
134 if ((pat = g_strstr_len(text, len, GOOGLE_SEARCH_PATTERN_1)) == NULL) {
135 found = FALSE;
136 goto done;
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;
151 if (strncmp(pat, GOOGLE_SEARCH_PATTERN_2, strlen(GOOGLE_SEARCH_PATTERN_2))) {
152 found = FALSE;
153 goto done;
156 pat += strlen(GOOGLE_SEARCH_PATTERN_2);
157 s = lon_buf;
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;
170 ll->lat = g_ascii_strtod(lat_buf, NULL);
171 ll->lon = g_ascii_strtod(lon_buf, NULL);
173 done:
174 g_mapped_file_free(mf);
175 return (found);
179 gchar *uri_escape(gchar *str)
181 gchar *esc_str = g_malloc(3*strlen(str));
182 gchar *dst = esc_str;
183 gchar *src;
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;
196 return(esc_str);
199 static int google_search_get_coord(VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord)
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;
209 escaped_srch_str = uri_escape(srch_str);
211 if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
212 g_critical(_("couldn't open temp file\n"));
213 exit(1);
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);
220 /* TODO: curl may not be available */
221 if (curl_download_uri(uri, tmp_file, &googlesearch_options)) { /* error */
222 fclose(tmp_file);
223 ret = -1;
224 goto done;
227 fclose(tmp_file);
228 if (!parse_file_for_latlon(tmpname, &ll)) {
229 ret = -1;
230 goto done;
233 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
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);
243 done:
244 g_free(escaped_srch_str);
245 g_free(uri);
246 remove(tmpname);
247 g_free(tmpname);
248 return ret;
251 void a_google_search(VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp)
253 VikCoord new_center;
254 gchar *s_str;
255 gboolean more = TRUE;
257 do {
258 s_str = a_prompt_for_search_string(vw);
259 if ((!s_str) || (s_str[0] == 0)) {
260 more = FALSE;
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;
268 else if (!prompt_try_again(vw))
269 more = FALSE;
270 g_free(s_str);
271 } while (more);