2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Quy Tonthat <qtonthat@gmail.com>
5 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <glib/gstdio.h>
29 #include <glib/gprintf.h>
30 #include <glib/gi18n.h>
34 #include "curl_download.h"
36 #include "googlesearch.h"
38 #define GOOGLE_GOTO_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
39 #define GOOGLE_GOTO_PATTERN_1 "{center:{lat:"
40 #define GOOGLE_GOTO_PATTERN_2 ",lng:"
41 #define GOOGLE_GOTO_NOT_FOUND "not understand the location"
43 static DownloadMapOptions googlesearch_options = { FALSE, FALSE, "http://maps.google.com/", 0, a_check_map_file };
45 static void google_goto_tool_finalize ( GObject *gob );
47 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self );
48 static DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self );
49 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
51 G_DEFINE_TYPE (GoogleGotoTool, google_goto_tool, VIK_GOTO_TOOL_TYPE)
53 static void google_goto_tool_class_init ( GoogleGotoToolClass *klass )
55 GObjectClass *object_class;
56 VikGotoToolClass *parent_class;
58 object_class = G_OBJECT_CLASS (klass);
60 object_class->finalize = google_goto_tool_finalize;
62 parent_class = VIK_GOTO_TOOL_CLASS (klass);
64 parent_class->get_url_format = google_goto_tool_get_url_format;
65 parent_class->get_download_options = google_goto_tool_get_download_options;
66 parent_class->parse_file_for_latlon = google_goto_tool_parse_file_for_latlon;
69 GoogleGotoTool *google_goto_tool_new ()
71 return GOOGLE_GOTO_TOOL ( g_object_new ( GOOGLE_GOTO_TOOL_TYPE, "label", "Google", NULL ) );
74 static void google_goto_tool_init ( GoogleGotoTool *vlp )
78 static void google_goto_tool_finalize ( GObject *gob )
80 G_OBJECT_GET_CLASS(gob)->finalize(gob);
83 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll)
88 gboolean found = TRUE;
89 gchar lat_buf[32], lon_buf[32];
92 lat_buf[0] = lon_buf[0] = '\0';
94 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
95 g_critical(_("couldn't map temp file"));
98 len = g_mapped_file_get_length(mf);
99 text = g_mapped_file_get_contents(mf);
101 if (g_strstr_len(text, len, GOOGLE_GOTO_NOT_FOUND) != NULL) {
106 if ((pat = g_strstr_len(text, len, GOOGLE_GOTO_PATTERN_1)) == NULL) {
110 pat += strlen(GOOGLE_GOTO_PATTERN_1);
114 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
115 (g_ascii_isdigit(*pat) || (*pat == '.')))
118 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
123 if (strncmp(pat, GOOGLE_GOTO_PATTERN_2, strlen(GOOGLE_GOTO_PATTERN_2))) {
128 pat += strlen(GOOGLE_GOTO_PATTERN_2);
133 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
134 (g_ascii_isdigit(*pat) || (*pat == '.')))
137 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
142 ll->lat = g_ascii_strtod(lat_buf, NULL);
143 ll->lon = g_ascii_strtod(lon_buf, NULL);
146 g_mapped_file_free(mf);
151 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self )
153 return GOOGLE_GOTO_URL_FMT;
156 DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self )
158 return &googlesearch_options;