]> git.street.me.uk Git - andy/viking.git/blame - src/geonamessearch.c
Remove old code
[andy/viking.git] / src / geonamessearch.c
CommitLineData
93c47137
HR
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#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>
32
33#include "viking.h"
ba4a5e11 34#include "util.h"
93c47137
HR
35#include "curl_download.h"
36
165a4fa9 37#define GEONAMES_WIKIPEDIA_URL_FMT "http://ws.geonames.org/wikipediaBoundingBoxJSON?formatted=true&north=%f&south=%f&east=%f&west=%f"
93c47137 38#define GEONAMES_SEARCH_URL_FMT "http://ws.geonames.org/searchJSON?formatted=true&style=medium&maxRows=10&lang=en&q=%s"
09a77f0d
HR
39#define GEONAMES_COUNTRY_PATTERN "\"countryName\": \""
40#define GEONAMES_LONGITUDE_PATTERN "\"lng\": "
41#define GEONAMES_NAME_PATTERN "\"name\": \""
42#define GEONAMES_LATITUDE_PATTERN "\"lat\": "
165a4fa9
HR
43#define GEONAMES_TITLE_PATTERN "\"title\": \""
44#define GEONAMES_WIKIPEDIAURL_PATTERN "\"wikipediaUrl\": \""
45#define GEONAMES_THUMBNAILIMG_PATTERN "\"thumbnailImg\": \""
93c47137
HR
46#define GEONAMES_SEARCH_NOT_FOUND "not understand the location"
47
165a4fa9
HR
48/* found_geoname: Type to contain data returned from GeoNames.org */
49
09a77f0d
HR
50typedef struct {
51 gchar *name;
52 gchar *country;
53 struct LatLon ll;
165a4fa9 54 gchar *desc;
09a77f0d
HR
55} found_geoname;
56
165a4fa9
HR
57found_geoname *new_found_geoname()
58{
59 found_geoname *ret;
60
61 ret = (found_geoname *)g_malloc(sizeof(found_geoname));
62 ret->name = NULL;
63 ret->country = NULL;
64 ret->desc = NULL;
65 ret->ll.lat = 0.0;
66 ret->ll.lon = 0.0;
67 return(ret);
68}
69
09a77f0d
HR
70found_geoname *copy_found_geoname(found_geoname *src)
71{
165a4fa9 72 found_geoname *dest = new_found_geoname();
09a77f0d
HR
73 dest->name = g_strdup(src->name);
74 dest->country = g_strdup(src->country);
75 dest->ll.lat = src->ll.lat;
76 dest->ll.lon = src->ll.lon;
165a4fa9 77 dest->desc = g_strdup(src->desc);
09a77f0d
HR
78 return(dest);
79}
80
165a4fa9
HR
81static void free_list_geonames(found_geoname *geoname, gpointer userdata)
82{
83 g_free(geoname->name);
84 g_free(geoname->country);
85 g_free(geoname->desc);
86}
87
88void free_geoname_list(GList *found_places)
89{
90 g_list_foreach(found_places, (GFunc)free_list_geonames, NULL);
91 g_list_free(found_places);
92}
93
165a4fa9
HR
94static void none_found(VikWindow *vw)
95{
96 GtkWidget *dialog = NULL;
97
98 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
99 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
100
101 GtkWidget *search_label = gtk_label_new(_("No entries found!"));
102 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), search_label, FALSE, FALSE, 5 );
103 gtk_widget_show_all(dialog);
104
105 gtk_dialog_run ( GTK_DIALOG(dialog) );
106 gtk_widget_destroy(dialog);
107}
108
09a77f0d
HR
109void buttonToggled(GtkCellRendererToggle* renderer, gchar* pathStr, gpointer data)
110{
111 GtkTreeIter iter;
112 gboolean enabled;
113 GtkTreePath* path = gtk_tree_path_new_from_string(pathStr);
114 gtk_tree_model_get_iter(GTK_TREE_MODEL (data), &iter, path);
115 gtk_tree_model_get(GTK_TREE_MODEL (data), &iter, 0, &enabled, -1);
116 enabled = !enabled;
117 gtk_tree_store_set(GTK_TREE_STORE (data), &iter, 0, enabled, -1);
118}
119
120GList *a_select_geoname_from_list(GtkWindow *parent, GList *geonames, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg)
121{
122 GtkTreeIter iter;
123 GtkCellRenderer *renderer;
124 GtkCellRenderer *toggle_render;
125 GtkWidget *view;
126 found_geoname *geoname;
127 gchar *latlon_string;
128 int column_runner;
165a4fa9
HR
129 gboolean checked;
130 gboolean to_copy;
09a77f0d
HR
131
132 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
133 parent,
134 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
135 GTK_STOCK_CANCEL,
136 GTK_RESPONSE_REJECT,
137 GTK_STOCK_OK,
138 GTK_RESPONSE_ACCEPT,
139 NULL);
140 GtkWidget *label = gtk_label_new ( msg );
141 GtkTreeStore *store;
142 if (multiple_selection_allowed)
143 {
144 store = gtk_tree_store_new(4, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
145 }
146 else
147 {
148 store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
149 }
150 GList *geoname_runner = geonames;
151 while (geoname_runner)
152 {
153 geoname = (found_geoname *)geoname_runner->data;
154 latlon_string = g_strdup_printf("(%f,%f)", geoname->ll.lat, geoname->ll.lon);
155 gtk_tree_store_append(store, &iter, NULL);
156 if (multiple_selection_allowed)
157 {
158 gtk_tree_store_set(store, &iter, 0, FALSE, 1, geoname->name, 2, geoname->country, 3, latlon_string, -1);
159 }
160 else
161 {
162 gtk_tree_store_set(store, &iter, 0, geoname->name, 1, geoname->country, 2, latlon_string, -1);
163 }
164 geoname_runner = g_list_next(geoname_runner);
165 g_free(latlon_string);
166 }
167 view = gtk_tree_view_new();
168 renderer = gtk_cell_renderer_text_new();
169 column_runner = 0;
170 if (multiple_selection_allowed)
171 {
172 toggle_render = gtk_cell_renderer_toggle_new();
173 g_object_set(toggle_render, "activatable", TRUE, NULL);
174 g_signal_connect(toggle_render, "toggled", (GCallback) buttonToggled, GTK_TREE_MODEL(store));
175 gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW(view), -1, "Select", toggle_render, "active", column_runner, NULL);
176 column_runner++;
177 }
178 gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW(view), -1, "Name", renderer, "text", column_runner, NULL);
179 column_runner++;
180 gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW(view), -1, "Country", renderer, "text", column_runner, NULL);
181 column_runner++;
182 gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW(view), -1, "Lat/Lon", renderer, "text", column_runner, NULL);
183 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(view), TRUE);
184 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
185 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
186 multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
187 g_object_unref(store);
188
189 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), label, FALSE, FALSE, 0);
190 gtk_widget_show ( label );
191 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), view, FALSE, FALSE, 0);
192 gtk_widget_show ( view );
193 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
194 {
195 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
196 GList *selected_geonames = NULL;
197
198 gtk_tree_model_get_iter_first( GTK_TREE_MODEL(store), &iter);
199 geoname_runner = geonames;
200 while (geoname_runner)
201 {
165a4fa9 202 to_copy = FALSE;
09a77f0d
HR
203 if (multiple_selection_allowed)
204 {
165a4fa9
HR
205 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, &checked, -1);
206 if (checked) {
207 to_copy = TRUE;
208 }
09a77f0d
HR
209 }
210 else
211 {
212 if (gtk_tree_selection_iter_is_selected(selection, &iter))
213 {
165a4fa9 214 to_copy = TRUE;
09a77f0d
HR
215 }
216 }
165a4fa9
HR
217 if (to_copy) {
218 found_geoname *copied = copy_found_geoname(geoname_runner->data);
219 selected_geonames = g_list_prepend(selected_geonames, copied);
220 }
09a77f0d
HR
221 geoname_runner = g_list_next(geoname_runner);
222 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
223 }
224 if (selected_geonames)
225 {
226 gtk_widget_destroy ( dialog );
227 return (selected_geonames);
228 }
229 a_dialog_error_msg(parent, _("Nothing was selected"));
230 }
231 gtk_widget_destroy ( dialog );
232 return NULL;
233}
234
165a4fa9 235GList *get_entries_from_file(gchar *file_name)
93c47137
HR
236{
237 gchar *text, *pat;
238 GMappedFile *mf;
239 gsize len;
09a77f0d 240 gboolean more = TRUE;
93c47137
HR
241 gchar lat_buf[32], lon_buf[32];
242 gchar *s;
09a77f0d
HR
243 gint fragment_len;
244 GList *found_places = NULL;
245 found_geoname *geoname = NULL;
246 gchar **found_entries;
247 gchar *entry;
248 int entry_runner;
165a4fa9
HR
249 gchar *wikipedia_url = NULL;
250 gchar *thumbnail_url = NULL;
93c47137
HR
251
252 lat_buf[0] = lon_buf[0] = '\0';
253
254 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
255 g_critical(_("couldn't map temp file"));
256 exit(1);
257 }
258 len = g_mapped_file_get_length(mf);
259 text = g_mapped_file_get_contents(mf);
260
261 if (g_strstr_len(text, len, GEONAMES_SEARCH_NOT_FOUND) != NULL) {
09a77f0d 262 more = FALSE;
93c47137 263 }
09a77f0d
HR
264 found_entries = g_strsplit(text, "},", 0);
265 entry_runner = 0;
266 entry = found_entries[entry_runner];
267 while (entry)
268 {
269 more = TRUE;
165a4fa9
HR
270 geoname = new_found_geoname();
271 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_COUNTRY_PATTERN))) {
09a77f0d
HR
272 pat += strlen(GEONAMES_COUNTRY_PATTERN);
273 fragment_len = 0;
274 s = pat;
275 while (*pat != '"') {
276 fragment_len++;
277 pat++;
278 }
279 geoname -> country = g_strndup(s, fragment_len);
280 }
281 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LONGITUDE_PATTERN)) == NULL) {
282 more = FALSE;
283 }
284 else {
285 pat += strlen(GEONAMES_LONGITUDE_PATTERN);
286 s = lon_buf;
287 if (*pat == '-')
288 *s++ = *pat++;
289 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
290 (g_ascii_isdigit(*pat) || (*pat == '.')))
291 *s++ = *pat++;
292 *s = '\0';
293 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
294 more = FALSE;
295 }
296 geoname->ll.lon = g_ascii_strtod(lon_buf, NULL);
297 }
165a4fa9 298 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_NAME_PATTERN))) {
09a77f0d
HR
299 pat += strlen(GEONAMES_NAME_PATTERN);
300 fragment_len = 0;
301 s = pat;
302 while (*pat != '"') {
303 fragment_len++;
304 pat++;
305 }
306 geoname -> name = g_strndup(s, fragment_len);
307 }
165a4fa9
HR
308 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_TITLE_PATTERN))) {
309 pat += strlen(GEONAMES_TITLE_PATTERN);
310 fragment_len = 0;
311 s = pat;
312 while (*pat != '"') {
313 fragment_len++;
314 pat++;
315 }
316 geoname -> name = g_strndup(s, fragment_len);
317 }
318 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_WIKIPEDIAURL_PATTERN))) {
319 pat += strlen(GEONAMES_WIKIPEDIAURL_PATTERN);
320 fragment_len = 0;
321 s = pat;
322 while (*pat != '"') {
323 fragment_len++;
324 pat++;
325 }
326 wikipedia_url = g_strndup(s, fragment_len);
327 }
328 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_THUMBNAILIMG_PATTERN))) {
329 pat += strlen(GEONAMES_THUMBNAILIMG_PATTERN);
330 fragment_len = 0;
331 s = pat;
332 while (*pat != '"') {
333 fragment_len++;
334 pat++;
335 }
336 thumbnail_url = g_strndup(s, fragment_len);
337 }
09a77f0d
HR
338 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LATITUDE_PATTERN)) == NULL) {
339 more = FALSE;
340 }
341 else {
342 pat += strlen(GEONAMES_LATITUDE_PATTERN);
343 s = lat_buf;
344 if (*pat == '-')
345 *s++ = *pat++;
346 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
347 (g_ascii_isdigit(*pat) || (*pat == '.')))
348 *s++ = *pat++;
349 *s = '\0';
350 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
351 more = FALSE;
352 }
353 geoname->ll.lat = g_ascii_strtod(lat_buf, NULL);
354 }
355 if (!more) {
356 if (geoname) {
357 g_free(geoname);
358 }
359 }
360 else {
165a4fa9
HR
361 if (wikipedia_url) {
362 if (thumbnail_url) {
363 geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\"><img src=\"%s\" border=\"0\"/></a>", wikipedia_url, thumbnail_url);
364 }
365 else {
366 geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\">%s</a>", wikipedia_url, geoname->name);
367 }
368 }
369 if (wikipedia_url) {
370 g_free(wikipedia_url);
371 wikipedia_url = NULL;
372 }
373 if (thumbnail_url) {
374 g_free(thumbnail_url);
375 thumbnail_url = NULL;
376 }
09a77f0d
HR
377 found_places = g_list_prepend(found_places, geoname);
378 }
379 entry_runner++;
380 entry = found_entries[entry_runner];
93c47137 381 }
09a77f0d 382 g_strfreev(found_entries);
165a4fa9
HR
383 found_places = g_list_reverse(found_places);
384 g_mapped_file_free(mf);
385 return(found_places);
386}
387
388
165a4fa9 389gchar *download_url(gchar *uri)
93c47137
HR
390{
391 FILE *tmp_file;
392 int tmp_fd;
393 gchar *tmpname;
93c47137
HR
394
395 if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) {
396 g_critical(_("couldn't open temp file"));
397 exit(1);
398 }
93c47137 399 tmp_file = fdopen(tmp_fd, "r+");
93c47137 400
09a77f0d
HR
401 // TODO: curl may not be available
402 if (curl_download_uri(uri, tmp_file, NULL)) { // error
93c47137
HR
403 fclose(tmp_file);
404 tmp_file = NULL;
165a4fa9
HR
405 g_remove(tmpname);
406 g_free(tmpname);
407 return(NULL);
93c47137 408 }
93c47137
HR
409 fclose(tmp_file);
410 tmp_file = NULL;
165a4fa9
HR
411 return(tmpname);
412}
413
165a4fa9
HR
414void a_geonames_wikipedia_box(VikWindow *vw, VikTrwLayer *vtl, VikLayersPanel *vlp, struct LatLon maxmin[2])
415{
416 gchar *uri;
417 gchar *tmpname;
418 GList *wiki_places;
419 GList *selected;
420 GList *wp_runner;
421 VikWaypoint *wiki_wp;
422 found_geoname *wiki_geoname;
423
424 uri = g_strdup_printf(GEONAMES_WIKIPEDIA_URL_FMT, maxmin[0].lat, maxmin[1].lat, maxmin[0].lon, maxmin[1].lon);
425 tmpname = download_url(uri);
426 if (!tmpname) {
427 none_found(vw);
428 return;
429 }
430 wiki_places = get_entries_from_file(tmpname);
431 if (g_list_length(wiki_places) == 0) {
432 none_found(vw);
433 return;
434 }
435 selected = a_select_geoname_from_list(VIK_GTK_WINDOW_FROM_WIDGET(vw), wiki_places, TRUE, "Select articles", "Select the articles you want to add.");
436 wp_runner = selected;
437 while (wp_runner) {
438 wiki_geoname = (found_geoname *)wp_runner->data;
439 wiki_wp = vik_waypoint_new();
165a4fa9
HR
440 wiki_wp->visible = TRUE;
441 vik_coord_load_from_latlon(&(wiki_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &(wiki_geoname->ll));
442 vik_waypoint_set_comment(wiki_wp, wiki_geoname->desc);
443 vik_trw_layer_filein_add_waypoint ( vtl, wiki_geoname->name, wiki_wp );
444 wp_runner = g_list_next(wp_runner);
445 }
446 free_geoname_list(wiki_places);
447 free_geoname_list(selected);
448 g_free(uri);
449 if (tmpname) {
450 g_free(tmpname);
451 }
452 vik_layers_panel_emit_update(vlp);
453}