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>
27 #include <glib/gprintf.h>
28 #include <glib/gi18n.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
)
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
);
58 static gboolean
prompt_try_again(VikWindow
*vw
)
60 GtkWidget
*dialog
= NULL
;
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
)
73 gtk_widget_destroy(dialog
);
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();
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
);
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') {
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
)
116 gboolean found
= TRUE
;
117 gchar lat_buf
[32], lon_buf
[32];
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"));
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
) {
134 if ((pat
= g_strstr_len(text
, len
, GOOGLE_SEARCH_PATTERN_1
)) == NULL
) {
138 pat
+= strlen(GOOGLE_SEARCH_PATTERN_1
);
142 while ((s
< (lat_buf
+ sizeof(lat_buf
))) && (pat
< (text
+ len
)) &&
143 (g_ascii_isdigit(*pat
) || (*pat
== '.')))
146 if ((pat
>= (text
+ len
)) || (lat_buf
[0] == '\0')) {
151 if (strncmp(pat
, GOOGLE_SEARCH_PATTERN_2
, strlen(GOOGLE_SEARCH_PATTERN_2
))) {
156 pat
+= strlen(GOOGLE_SEARCH_PATTERN_2
);
161 while ((s
< (lon_buf
+ sizeof(lon_buf
))) && (pat
< (text
+ len
)) &&
162 (g_ascii_isdigit(*pat
) || (*pat
== '.')))
165 if ((pat
>= (text
+ len
)) || (lon_buf
[0] == '\0')) {
170 ll
->lat
= g_ascii_strtod(lat_buf
, NULL
);
171 ll
->lon
= g_ascii_strtod(lon_buf
, NULL
);
174 g_mapped_file_free(mf
);
179 gchar
*uri_escape(gchar
*str
)
181 gchar
*esc_str
= g_malloc(3*strlen(str
));
182 gchar
*dst
= esc_str
;
185 for (src
= str
; *src
; src
++) {
188 else if (g_ascii_isalnum(*src
))
191 g_sprintf(dst
, "%%%02X", *src
);
199 static int google_search_get_coord(VikWindow
*vw
, VikViewport
*vvp
, gchar
*srch_str
, VikCoord
*coord
)
205 gchar
*escaped_srch_str
;
206 int ret
= 0; /* OK */
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"));
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 */
228 if (!parse_file_for_latlon(tmpname
, &ll
)) {
233 vik_coord_load_from_latlon ( coord
, vik_viewport_get_coord_mode(vvp
), &ll
);
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
);
244 g_free(escaped_srch_str
);
251 void a_google_search(VikWindow
*vw
, VikLayersPanel
*vlp
, VikViewport
*vvp
)
255 gboolean more
= TRUE
;
258 s_str
= a_prompt_for_search_string(vw
);
259 if ((!s_str
) || (s_str
[0] == 0)) {
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
);
268 else if (!prompt_try_again(vw
))