Tiles must be older than a specified age before checking server
[viking/guyou.git] / src / googlesearch.c
blobf7dd793d87f284b0cd26892ac44e07e1601c5379
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 #define GOOGLE_SEARCH_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
38 #define GOOGLE_SEARCH_PATTERN_1 "{center:{lat:"
39 #define GOOGLE_SEARCH_PATTERN_2 ",lng:"
40 #define GOOGLE_SEARCH_NOT_FOUND "not understand the location"
42 static gchar *last_search_str = NULL;
43 static VikCoord *last_coord = NULL;
44 static gchar *last_successful_search_str = NULL;
46 static DownloadOptions googlesearch_options = { 0, "http://maps.google.com/", 0, a_check_map_file };
48 gchar * a_googlesearch_get_search_string_for_this_place(VikWindow *vw)
50 if (!last_coord)
51 return NULL;
53 VikViewport *vvp = vik_window_viewport(vw);
54 const VikCoord *cur_center = vik_viewport_get_center(vvp);
55 if (vik_coord_equals(cur_center, last_coord)) {
56 return(last_successful_search_str);
58 else
59 return NULL;
62 static gboolean prompt_try_again(VikWindow *vw)
64 GtkWidget *dialog = NULL;
65 gboolean ret = TRUE;
67 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
68 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
70 GtkWidget *search_label = gtk_label_new(_("I don't know that place. Do you want another search?"));
71 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
72 gtk_widget_show_all(dialog);
74 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
75 ret = FALSE;
77 gtk_widget_destroy(dialog);
78 return ret;
81 static gchar * a_prompt_for_search_string(VikWindow *vw)
83 GtkWidget *dialog = NULL;
85 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
86 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
88 GtkWidget *search_label = gtk_label_new(_("Enter address or place name:"));
89 GtkWidget *search_entry = gtk_entry_new();
90 if (last_search_str)
91 gtk_entry_set_text(GTK_ENTRY(search_entry), last_search_str);
93 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
94 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_entry, FALSE, FALSE, 5 );
95 gtk_widget_show_all(dialog);
97 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
98 gtk_widget_destroy(dialog);
99 return NULL;
102 gchar *search_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(search_entry) ) );
104 gtk_widget_destroy(dialog);
106 if (search_str[0] != '\0') {
107 if (last_search_str)
108 g_free(last_search_str);
109 last_search_str = g_strdup(search_str);
112 return(search_str); /* search_str needs to be freed by caller */
115 static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll)
117 gchar *text, *pat;
118 GMappedFile *mf;
119 gsize len;
120 gboolean found = TRUE;
121 gchar lat_buf[32], lon_buf[32];
122 gchar *s;
124 lat_buf[0] = lon_buf[0] = '\0';
126 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
127 g_critical(_("couldn't map temp file"));
128 exit(1);
130 len = g_mapped_file_get_length(mf);
131 text = g_mapped_file_get_contents(mf);
133 if (g_strstr_len(text, len, GOOGLE_SEARCH_NOT_FOUND) != NULL) {
134 found = FALSE;
135 goto done;
138 if ((pat = g_strstr_len(text, len, GOOGLE_SEARCH_PATTERN_1)) == NULL) {
139 found = FALSE;
140 goto done;
142 pat += strlen(GOOGLE_SEARCH_PATTERN_1);
143 s = lat_buf;
144 if (*pat == '-')
145 *s++ = *pat++;
146 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
147 (g_ascii_isdigit(*pat) || (*pat == '.')))
148 *s++ = *pat++;
149 *s = '\0';
150 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
151 found = FALSE;
152 goto done;
155 if (strncmp(pat, GOOGLE_SEARCH_PATTERN_2, strlen(GOOGLE_SEARCH_PATTERN_2))) {
156 found = FALSE;
157 goto done;
160 pat += strlen(GOOGLE_SEARCH_PATTERN_2);
161 s = lon_buf;
163 if (*pat == '-')
164 *s++ = *pat++;
165 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
166 (g_ascii_isdigit(*pat) || (*pat == '.')))
167 *s++ = *pat++;
168 *s = '\0';
169 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
170 found = FALSE;
171 goto done;
174 ll->lat = g_ascii_strtod(lat_buf, NULL);
175 ll->lon = g_ascii_strtod(lon_buf, NULL);
177 done:
178 g_mapped_file_free(mf);
179 return (found);
183 static int google_search_get_coord(VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord)
185 FILE *tmp_file;
186 int tmp_fd;
187 gchar *tmpname;
188 gchar *uri;
189 gchar *escaped_srch_str;
190 int ret = 0; /* OK */
191 struct LatLon ll;
193 g_debug("%s: raw search: %s", __FUNCTION__, srch_str);
195 escaped_srch_str = uri_escape(srch_str);
197 g_debug("%s: escaped search: %s", __FUNCTION__, escaped_srch_str);
199 if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
200 g_critical(_("couldn't open temp file"));
201 exit(1);
204 tmp_file = fdopen(tmp_fd, "r+");
205 //uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, srch_str);
206 uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, escaped_srch_str);
208 /* TODO: curl may not be available */
209 if (curl_download_uri(uri, tmp_file, &googlesearch_options, 0)) { /* error */
210 fclose(tmp_file);
211 tmp_file = NULL;
212 ret = -1;
213 goto done;
216 fclose(tmp_file);
217 tmp_file = NULL;
218 if (!parse_file_for_latlon(tmpname, &ll)) {
219 ret = -1;
220 goto done;
223 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
225 if (last_coord)
226 g_free(last_coord);
227 last_coord = g_malloc(sizeof(VikCoord));
228 *last_coord = *coord;
229 if (last_successful_search_str)
230 g_free(last_successful_search_str);
231 last_successful_search_str = g_strdup(last_search_str);
233 done:
234 g_free(escaped_srch_str);
235 g_free(uri);
236 g_remove(tmpname);
237 g_free(tmpname);
238 return ret;
241 void a_google_search(VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp)
243 VikCoord new_center;
244 gchar *s_str;
245 gboolean more = TRUE;
247 do {
248 s_str = a_prompt_for_search_string(vw);
249 if ((!s_str) || (s_str[0] == 0)) {
250 more = FALSE;
253 else if (!google_search_get_coord(vw, vvp, s_str, &new_center)) {
254 vik_viewport_set_center_coord(vvp, &new_center);
255 vik_layers_panel_emit_update(vlp);
256 more = FALSE;
258 else if (!prompt_try_again(vw))
259 more = FALSE;
260 g_free(s_str);
261 } while (more);