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