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