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