]>
Commit | Line | Data |
---|---|---|
369126f3 QT |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
45ce7a38 GB |
4 | * Copyright (C) 2003-2005, Quy Tonthat <qtonthat@gmail.com> |
5 | * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> | |
369126f3 QT |
6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
369126f3 | 20 | */ |
4c77d5e0 GB |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
369126f3 | 24 | #include <stdlib.h> |
8c060406 | 25 | #include <stdio.h> |
369126f3 | 26 | #include <string.h> |
8c060406 MA |
27 | #include <glib.h> |
28 | #include <glib/gstdio.h> | |
369126f3 | 29 | #include <glib/gprintf.h> |
4c77d5e0 | 30 | #include <glib/gi18n.h> |
369126f3 QT |
31 | |
32 | #include "viking.h" | |
ba4a5e11 | 33 | #include "util.h" |
369126f3 QT |
34 | #include "curl_download.h" |
35 | ||
7a5b9f80 GB |
36 | #include "googlesearch.h" |
37 | ||
316945d8 GB |
38 | /* Compatibility */ |
39 | #if ! GLIB_CHECK_VERSION(2,22,0) | |
40 | #define g_mapped_file_unref g_mapped_file_free | |
41 | #endif | |
42 | ||
34e71b99 GB |
43 | #define GOOGLE_GOTO_URL_FMT "http://maps.google.com/maps?q=%s&output=js" |
44 | #define GOOGLE_GOTO_PATTERN_1 "{center:{lat:" | |
45 | #define GOOGLE_GOTO_PATTERN_2 ",lng:" | |
46 | #define GOOGLE_GOTO_NOT_FOUND "not understand the location" | |
369126f3 | 47 | |
a3697549 | 48 | static DownloadMapOptions googlesearch_options = { FALSE, FALSE, "http://maps.google.com/", 2, a_check_map_file, NULL }; |
a3188993 | 49 | |
34e71b99 | 50 | static void google_goto_tool_finalize ( GObject *gob ); |
369126f3 | 51 | |
0c6b26d3 | 52 | static gchar *google_goto_tool_get_url_format ( VikGotoTool *self ); |
b529dc9c | 53 | static DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self ); |
0c6b26d3 | 54 | static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll); |
369126f3 | 55 | |
475f0cee | 56 | G_DEFINE_TYPE (GoogleGotoTool, google_goto_tool, VIK_GOTO_TOOL_TYPE) |
369126f3 | 57 | |
34e71b99 | 58 | static void google_goto_tool_class_init ( GoogleGotoToolClass *klass ) |
369126f3 | 59 | { |
7a5b9f80 | 60 | GObjectClass *object_class; |
34e71b99 | 61 | VikGotoToolClass *parent_class; |
369126f3 | 62 | |
7a5b9f80 | 63 | object_class = G_OBJECT_CLASS (klass); |
369126f3 | 64 | |
34e71b99 | 65 | object_class->finalize = google_goto_tool_finalize; |
369126f3 | 66 | |
34e71b99 | 67 | parent_class = VIK_GOTO_TOOL_CLASS (klass); |
369126f3 | 68 | |
0c6b26d3 GB |
69 | parent_class->get_url_format = google_goto_tool_get_url_format; |
70 | parent_class->get_download_options = google_goto_tool_get_download_options; | |
71 | parent_class->parse_file_for_latlon = google_goto_tool_parse_file_for_latlon; | |
7a5b9f80 | 72 | } |
369126f3 | 73 | |
34e71b99 | 74 | GoogleGotoTool *google_goto_tool_new () |
7a5b9f80 | 75 | { |
34e71b99 | 76 | return GOOGLE_GOTO_TOOL ( g_object_new ( GOOGLE_GOTO_TOOL_TYPE, "label", "Google", NULL ) ); |
7a5b9f80 | 77 | } |
369126f3 | 78 | |
34e71b99 | 79 | static void google_goto_tool_init ( GoogleGotoTool *vlp ) |
7a5b9f80 GB |
80 | { |
81 | } | |
369126f3 | 82 | |
34e71b99 | 83 | static void google_goto_tool_finalize ( GObject *gob ) |
7a5b9f80 GB |
84 | { |
85 | G_OBJECT_GET_CLASS(gob)->finalize(gob); | |
369126f3 QT |
86 | } |
87 | ||
0c6b26d3 | 88 | static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll) |
369126f3 QT |
89 | { |
90 | gchar *text, *pat; | |
91 | GMappedFile *mf; | |
92 | gsize len; | |
93 | gboolean found = TRUE; | |
94 | gchar lat_buf[32], lon_buf[32]; | |
95 | gchar *s; | |
96 | ||
369126f3 QT |
97 | lat_buf[0] = lon_buf[0] = '\0'; |
98 | ||
99 | if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) { | |
4258f4e2 | 100 | g_critical(_("couldn't map temp file")); |
d84ade77 | 101 | return FALSE; |
369126f3 QT |
102 | } |
103 | len = g_mapped_file_get_length(mf); | |
104 | text = g_mapped_file_get_contents(mf); | |
105 | ||
34e71b99 | 106 | if (g_strstr_len(text, len, GOOGLE_GOTO_NOT_FOUND) != NULL) { |
d1f48cc2 QT |
107 | found = FALSE; |
108 | goto done; | |
109 | } | |
110 | ||
34e71b99 | 111 | if ((pat = g_strstr_len(text, len, GOOGLE_GOTO_PATTERN_1)) == NULL) { |
369126f3 QT |
112 | found = FALSE; |
113 | goto done; | |
114 | } | |
34e71b99 | 115 | pat += strlen(GOOGLE_GOTO_PATTERN_1); |
369126f3 QT |
116 | s = lat_buf; |
117 | if (*pat == '-') | |
118 | *s++ = *pat++; | |
119 | while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) && | |
120 | (g_ascii_isdigit(*pat) || (*pat == '.'))) | |
121 | *s++ = *pat++; | |
122 | *s = '\0'; | |
123 | if ((pat >= (text + len)) || (lat_buf[0] == '\0')) { | |
124 | found = FALSE; | |
125 | goto done; | |
126 | } | |
127 | ||
34e71b99 | 128 | if (strncmp(pat, GOOGLE_GOTO_PATTERN_2, strlen(GOOGLE_GOTO_PATTERN_2))) { |
369126f3 QT |
129 | found = FALSE; |
130 | goto done; | |
131 | } | |
369126f3 | 132 | |
34e71b99 | 133 | pat += strlen(GOOGLE_GOTO_PATTERN_2); |
369126f3 QT |
134 | s = lon_buf; |
135 | ||
136 | if (*pat == '-') | |
137 | *s++ = *pat++; | |
138 | while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) && | |
139 | (g_ascii_isdigit(*pat) || (*pat == '.'))) | |
140 | *s++ = *pat++; | |
141 | *s = '\0'; | |
142 | if ((pat >= (text + len)) || (lon_buf[0] == '\0')) { | |
143 | found = FALSE; | |
144 | goto done; | |
145 | } | |
146 | ||
147 | ll->lat = g_ascii_strtod(lat_buf, NULL); | |
148 | ll->lon = g_ascii_strtod(lon_buf, NULL); | |
149 | ||
369126f3 | 150 | done: |
316945d8 | 151 | g_mapped_file_unref(mf); |
369126f3 QT |
152 | return (found); |
153 | ||
154 | } | |
155 | ||
0c6b26d3 | 156 | static gchar *google_goto_tool_get_url_format ( VikGotoTool *self ) |
369126f3 | 157 | { |
0c6b26d3 GB |
158 | return GOOGLE_GOTO_URL_FMT; |
159 | } | |
369126f3 | 160 | |
b529dc9c | 161 | DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self ) |
0c6b26d3 GB |
162 | { |
163 | return &googlesearch_options; | |
369126f3 | 164 | } |