2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
20 * Created by Quy Tonthat <qtonthat@gmail.com>
29 #include <glib/gstdio.h>
30 #include <glib/gprintf.h>
31 #include <glib/gi18n.h>
35 #include "curl_download.h"
37 #include "googlesearch.h"
39 #define GOOGLE_GOTO_URL_FMT "http://maps.google.com/maps?q=%s&output=js"
40 #define GOOGLE_GOTO_PATTERN_1 "{center:{lat:"
41 #define GOOGLE_GOTO_PATTERN_2 ",lng:"
42 #define GOOGLE_GOTO_NOT_FOUND "not understand the location"
44 static DownloadOptions googlesearch_options = { "http://maps.google.com/", 0, a_check_map_file };
46 static void google_goto_tool_class_init ( GoogleGotoToolClass *klass );
47 static void google_goto_tool_init ( GoogleGotoTool *vwd );
49 static void google_goto_tool_finalize ( GObject *gob );
51 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self );
52 static DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self );
53 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
55 GType google_goto_tool_get_type()
57 static GType w_type = 0;
61 static const GTypeInfo w_info =
63 sizeof (GoogleGotoToolClass),
65 NULL, /* base_finalize */
66 (GClassInitFunc) google_goto_tool_class_init,
67 NULL, /* class_finalize */
68 NULL, /* class_data */
69 sizeof (GoogleGotoTool),
71 (GInstanceInitFunc) google_goto_tool_init,
73 w_type = g_type_register_static ( VIK_GOTO_TOOL_TYPE, "GoogleGotoTool", &w_info, 0 );
79 static void google_goto_tool_class_init ( GoogleGotoToolClass *klass )
81 GObjectClass *object_class;
82 VikGotoToolClass *parent_class;
84 object_class = G_OBJECT_CLASS (klass);
86 object_class->finalize = google_goto_tool_finalize;
88 parent_class = VIK_GOTO_TOOL_CLASS (klass);
90 parent_class->get_url_format = google_goto_tool_get_url_format;
91 parent_class->get_download_options = google_goto_tool_get_download_options;
92 parent_class->parse_file_for_latlon = google_goto_tool_parse_file_for_latlon;
95 GoogleGotoTool *google_goto_tool_new ()
97 return GOOGLE_GOTO_TOOL ( g_object_new ( GOOGLE_GOTO_TOOL_TYPE, "label", "Google", NULL ) );
100 static void google_goto_tool_init ( GoogleGotoTool *vlp )
104 static void google_goto_tool_finalize ( GObject *gob )
106 G_OBJECT_GET_CLASS(gob)->finalize(gob);
109 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll)
114 gboolean found = TRUE;
115 gchar lat_buf[32], lon_buf[32];
118 lat_buf[0] = lon_buf[0] = '\0';
120 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
121 g_critical(_("couldn't map temp file"));
124 len = g_mapped_file_get_length(mf);
125 text = g_mapped_file_get_contents(mf);
127 if (g_strstr_len(text, len, GOOGLE_GOTO_NOT_FOUND) != NULL) {
132 if ((pat = g_strstr_len(text, len, GOOGLE_GOTO_PATTERN_1)) == NULL) {
136 pat += strlen(GOOGLE_GOTO_PATTERN_1);
140 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
141 (g_ascii_isdigit(*pat) || (*pat == '.')))
144 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
149 if (strncmp(pat, GOOGLE_GOTO_PATTERN_2, strlen(GOOGLE_GOTO_PATTERN_2))) {
154 pat += strlen(GOOGLE_GOTO_PATTERN_2);
159 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
160 (g_ascii_isdigit(*pat) || (*pat == '.')))
163 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
168 ll->lat = g_ascii_strtod(lat_buf, NULL);
169 ll->lon = g_ascii_strtod(lon_buf, NULL);
172 g_mapped_file_free(mf);
177 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self )
179 return GOOGLE_GOTO_URL_FMT;
182 DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self )
184 return &googlesearch_options;