]> git.street.me.uk Git - andy/viking.git/blame - src/googlesearch.c
Refactor goto feature to allow multiple search engines
[andy/viking.git] / src / googlesearch.c
CommitLineData
369126f3
QT
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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 * Created by Quy Tonthat <qtonthat@gmail.com>
21 */
4c77d5e0
GB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
369126f3 25#include <stdlib.h>
8c060406 26#include <stdio.h>
369126f3 27#include <string.h>
8c060406
MA
28#include <glib.h>
29#include <glib/gstdio.h>
369126f3 30#include <glib/gprintf.h>
4c77d5e0 31#include <glib/gi18n.h>
369126f3
QT
32
33#include "viking.h"
ba4a5e11 34#include "util.h"
369126f3
QT
35#include "curl_download.h"
36
7a5b9f80
GB
37#include "googlesearch.h"
38
34e71b99
GB
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"
369126f3 43
1ac37c09 44static DownloadOptions googlesearch_options = { "http://maps.google.com/", 0, a_check_map_file };
a3188993 45
34e71b99
GB
46static void google_goto_tool_class_init ( GoogleGotoToolClass *klass );
47static void google_goto_tool_init ( GoogleGotoTool *vwd );
369126f3 48
34e71b99 49static void google_goto_tool_finalize ( GObject *gob );
369126f3 50
0c6b26d3
GB
51static gchar *google_goto_tool_get_url_format ( VikGotoTool *self );
52static DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self );
53static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
369126f3 54
34e71b99 55GType google_goto_tool_get_type()
7a5b9f80
GB
56{
57 static GType w_type = 0;
58
59 if (!w_type)
60 {
61 static const GTypeInfo w_info =
62 {
34e71b99 63 sizeof (GoogleGotoToolClass),
7a5b9f80
GB
64 NULL, /* base_init */
65 NULL, /* base_finalize */
34e71b99 66 (GClassInitFunc) google_goto_tool_class_init,
7a5b9f80
GB
67 NULL, /* class_finalize */
68 NULL, /* class_data */
34e71b99 69 sizeof (GoogleGotoTool),
7a5b9f80 70 0,
34e71b99 71 (GInstanceInitFunc) google_goto_tool_init,
7a5b9f80 72 };
34e71b99 73 w_type = g_type_register_static ( VIK_GOTO_TOOL_TYPE, "GoogleGotoTool", &w_info, 0 );
7a5b9f80 74 }
369126f3 75
7a5b9f80 76 return w_type;
369126f3
QT
77}
78
34e71b99 79static void google_goto_tool_class_init ( GoogleGotoToolClass *klass )
369126f3 80{
7a5b9f80 81 GObjectClass *object_class;
34e71b99 82 VikGotoToolClass *parent_class;
369126f3 83
7a5b9f80 84 object_class = G_OBJECT_CLASS (klass);
369126f3 85
34e71b99 86 object_class->finalize = google_goto_tool_finalize;
369126f3 87
34e71b99 88 parent_class = VIK_GOTO_TOOL_CLASS (klass);
369126f3 89
0c6b26d3
GB
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;
7a5b9f80 93}
369126f3 94
34e71b99 95GoogleGotoTool *google_goto_tool_new ()
7a5b9f80 96{
34e71b99 97 return GOOGLE_GOTO_TOOL ( g_object_new ( GOOGLE_GOTO_TOOL_TYPE, "label", "Google", NULL ) );
7a5b9f80 98}
369126f3 99
34e71b99 100static void google_goto_tool_init ( GoogleGotoTool *vlp )
7a5b9f80
GB
101{
102}
369126f3 103
34e71b99 104static void google_goto_tool_finalize ( GObject *gob )
7a5b9f80
GB
105{
106 G_OBJECT_GET_CLASS(gob)->finalize(gob);
369126f3
QT
107}
108
0c6b26d3 109static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll)
369126f3
QT
110{
111 gchar *text, *pat;
112 GMappedFile *mf;
113 gsize len;
114 gboolean found = TRUE;
115 gchar lat_buf[32], lon_buf[32];
116 gchar *s;
117
369126f3
QT
118 lat_buf[0] = lon_buf[0] = '\0';
119
120 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
4258f4e2 121 g_critical(_("couldn't map temp file"));
369126f3
QT
122 exit(1);
123 }
124 len = g_mapped_file_get_length(mf);
125 text = g_mapped_file_get_contents(mf);
126
34e71b99 127 if (g_strstr_len(text, len, GOOGLE_GOTO_NOT_FOUND) != NULL) {
d1f48cc2
QT
128 found = FALSE;
129 goto done;
130 }
131
34e71b99 132 if ((pat = g_strstr_len(text, len, GOOGLE_GOTO_PATTERN_1)) == NULL) {
369126f3
QT
133 found = FALSE;
134 goto done;
135 }
34e71b99 136 pat += strlen(GOOGLE_GOTO_PATTERN_1);
369126f3
QT
137 s = lat_buf;
138 if (*pat == '-')
139 *s++ = *pat++;
140 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
141 (g_ascii_isdigit(*pat) || (*pat == '.')))
142 *s++ = *pat++;
143 *s = '\0';
144 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
145 found = FALSE;
146 goto done;
147 }
148
34e71b99 149 if (strncmp(pat, GOOGLE_GOTO_PATTERN_2, strlen(GOOGLE_GOTO_PATTERN_2))) {
369126f3
QT
150 found = FALSE;
151 goto done;
152 }
369126f3 153
34e71b99 154 pat += strlen(GOOGLE_GOTO_PATTERN_2);
369126f3
QT
155 s = lon_buf;
156
157 if (*pat == '-')
158 *s++ = *pat++;
159 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
160 (g_ascii_isdigit(*pat) || (*pat == '.')))
161 *s++ = *pat++;
162 *s = '\0';
163 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
164 found = FALSE;
165 goto done;
166 }
167
168 ll->lat = g_ascii_strtod(lat_buf, NULL);
169 ll->lon = g_ascii_strtod(lon_buf, NULL);
170
369126f3
QT
171done:
172 g_mapped_file_free(mf);
369126f3
QT
173 return (found);
174
175}
176
0c6b26d3 177static gchar *google_goto_tool_get_url_format ( VikGotoTool *self )
369126f3 178{
0c6b26d3
GB
179 return GOOGLE_GOTO_URL_FMT;
180}
369126f3 181
0c6b26d3
GB
182DownloadOptions *google_goto_tool_get_download_options ( VikGotoTool *self )
183{
184 return &googlesearch_options;
369126f3 185}