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>
24 #include <glib/gprintf.h>
27 #include "curl_download.h"
29 #define GOOGLE_SEARCH_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
30 #define GOOGLE_SEARCH_PATTERN_1 "{center: {lat: "
31 #define GOOGLE_SEARCH_PATTERN_2 ",lng: "
33 static gchar *last_search_str = NULL;
35 static gboolean prompt_try_again(VikWindow *vw)
37 GtkWidget *dialog = NULL;
40 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
41 gtk_window_set_title(GTK_WINDOW(dialog), "Search");
43 GtkWidget *search_label = gtk_label_new("I don't know that place. Do you want another search?");
44 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
45 gtk_widget_show_all(dialog);
47 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
50 gtk_widget_destroy(dialog);
54 static gchar * a_prompt_for_search_string(VikWindow *vw)
56 GtkWidget *dialog = NULL;
58 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
59 gtk_window_set_title(GTK_WINDOW(dialog), "Search");
61 GtkWidget *search_label = gtk_label_new("Enter address or place name:");
62 GtkWidget *search_entry = gtk_entry_new();
64 gtk_entry_set_text(GTK_ENTRY(search_entry), last_search_str);
66 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
67 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_entry, FALSE, FALSE, 5 );
68 gtk_widget_show_all(dialog);
70 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
71 gtk_widget_destroy(dialog);
75 gchar *search_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(search_entry) ) );
77 gtk_widget_destroy(dialog);
79 if (search_str[0] != '\0') {
81 g_free(last_search_str);
82 last_search_str = g_strdup(search_str);
85 return(search_str); /* search_str needs to be freed by caller */
88 static gboolean parse_file_for_latlon(gchar *file_name, struct LatLon *ll)
93 gboolean found = TRUE;
94 gchar lat_buf[32], lon_buf[32];
97 lat_buf[0] = lon_buf[0] = '\0';
99 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
100 g_critical("couldn't map temp file\n");
103 len = g_mapped_file_get_length(mf);
104 text = g_mapped_file_get_contents(mf);
106 if ((pat = g_strstr_len(text, len, GOOGLE_SEARCH_PATTERN_1)) == NULL) {
110 pat += strlen(GOOGLE_SEARCH_PATTERN_1);
114 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
115 (g_ascii_isdigit(*pat) || (*pat == '.')))
118 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
123 if (strncmp(pat, GOOGLE_SEARCH_PATTERN_2, strlen(GOOGLE_SEARCH_PATTERN_2))) {
128 pat += strlen(GOOGLE_SEARCH_PATTERN_2);
133 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
134 (g_ascii_isdigit(*pat) || (*pat == '.')))
137 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
142 ll->lat = g_ascii_strtod(lat_buf, NULL);
143 ll->lon = g_ascii_strtod(lon_buf, NULL);
146 g_mapped_file_free(mf);
151 gchar *uri_escape(gchar *str)
153 gchar *esc_str = g_malloc(3*strlen(str));
154 gchar *dst = esc_str;
157 for (src = str; *src; src++) {
160 else if (g_ascii_isalnum(*src))
163 g_sprintf(dst, "%%%02X", *src);
171 static int google_search_get_coord(VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord)
177 gchar *escaped_srch_str;
178 int ret = 0; /* OK */
181 escaped_srch_str = uri_escape(srch_str);
183 if ((tmp_fd = g_file_open_tmp (NULL, &tmpname, NULL)) == -1) {
184 g_critical("couldn't open temp file\n");
188 tmp_file = fdopen(tmp_fd, "r+");
189 //uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, srch_str);
190 uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, escaped_srch_str);
192 /* TODO: curl may not be available */
193 if (curl_download_uri(uri, tmp_file)) { /* error */
194 fprintf(stderr, "DEBUG: %s() download error\n", __PRETTY_FUNCTION__);
201 if (!parse_file_for_latlon(tmpname, &ll)) {
206 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
209 g_free(escaped_srch_str);
216 void a_google_search(VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp)
220 gboolean more = TRUE;
223 s_str = a_prompt_for_search_string(vw);
224 if ((!s_str) || (s_str[0] == 0)) {
228 else if (!google_search_get_coord(vw, vvp, s_str, &new_center)) {
229 vik_viewport_set_center_coord(vvp, &new_center);
230 vik_layers_panel_emit_update(vlp);
233 else if (!prompt_try_again(vw))