]> git.street.me.uk Git - andy/viking.git/blob - src/googlesearch.c
Add maps layer tooltip to show the map name(type).
[andy/viking.git] / src / googlesearch.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Quy Tonthat <qtonthat@gmail.com>
5  * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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
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 #include "googlesearch.h"
37
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"
42
43 static DownloadMapOptions googlesearch_options = { FALSE, FALSE, "http://maps.google.com/", 0, a_check_map_file };
44
45 static void google_goto_tool_class_init ( GoogleGotoToolClass *klass );
46 static void google_goto_tool_init ( GoogleGotoTool *vwd );
47
48 static void google_goto_tool_finalize ( GObject *gob );
49
50 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self );
51 static DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self );
52 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
53
54 GType google_goto_tool_get_type()
55 {
56   static GType w_type = 0;
57
58   if (!w_type)
59   {
60     static const GTypeInfo w_info = 
61     {
62       sizeof (GoogleGotoToolClass),
63       NULL, /* base_init */
64       NULL, /* base_finalize */
65       (GClassInitFunc) google_goto_tool_class_init,
66       NULL, /* class_finalize */
67       NULL, /* class_data */
68       sizeof (GoogleGotoTool),
69       0,
70       (GInstanceInitFunc) google_goto_tool_init,
71     };
72     w_type = g_type_register_static ( VIK_GOTO_TOOL_TYPE, "GoogleGotoTool", &w_info, 0 );
73   }
74
75   return w_type;
76 }
77
78 static void google_goto_tool_class_init ( GoogleGotoToolClass *klass )
79 {
80   GObjectClass *object_class;
81   VikGotoToolClass *parent_class;
82
83   object_class = G_OBJECT_CLASS (klass);
84
85   object_class->finalize = google_goto_tool_finalize;
86
87   parent_class = VIK_GOTO_TOOL_CLASS (klass);
88
89   parent_class->get_url_format = google_goto_tool_get_url_format;
90   parent_class->get_download_options = google_goto_tool_get_download_options;
91   parent_class->parse_file_for_latlon = google_goto_tool_parse_file_for_latlon;
92 }
93
94 GoogleGotoTool *google_goto_tool_new ()
95 {
96   return GOOGLE_GOTO_TOOL ( g_object_new ( GOOGLE_GOTO_TOOL_TYPE, "label", "Google", NULL ) );
97 }
98
99 static void google_goto_tool_init ( GoogleGotoTool *vlp )
100 {
101 }
102
103 static void google_goto_tool_finalize ( GObject *gob )
104 {
105   G_OBJECT_GET_CLASS(gob)->finalize(gob);
106 }
107
108 static gboolean google_goto_tool_parse_file_for_latlon(VikGotoTool *self, gchar *file_name, struct LatLon *ll)
109 {
110   gchar *text, *pat;
111   GMappedFile *mf;
112   gsize len;
113   gboolean found = TRUE;
114   gchar lat_buf[32], lon_buf[32];
115   gchar *s;
116
117   lat_buf[0] = lon_buf[0] = '\0';
118
119   if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
120     g_critical(_("couldn't map temp file"));
121     exit(1);
122   }
123   len = g_mapped_file_get_length(mf);
124   text = g_mapped_file_get_contents(mf);
125
126   if (g_strstr_len(text, len, GOOGLE_GOTO_NOT_FOUND) != NULL) {
127     found = FALSE;
128     goto done;
129   }
130
131   if ((pat = g_strstr_len(text, len, GOOGLE_GOTO_PATTERN_1)) == NULL) {
132     found = FALSE;
133     goto done;
134   }
135   pat += strlen(GOOGLE_GOTO_PATTERN_1);
136   s = lat_buf;
137   if (*pat == '-')
138     *s++ = *pat++;
139   while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
140           (g_ascii_isdigit(*pat) || (*pat == '.')))
141     *s++ = *pat++;
142   *s = '\0';
143   if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
144     found = FALSE;
145     goto done;
146   }
147
148   if (strncmp(pat, GOOGLE_GOTO_PATTERN_2, strlen(GOOGLE_GOTO_PATTERN_2))) {
149       found = FALSE;
150       goto done;
151   }
152
153   pat += strlen(GOOGLE_GOTO_PATTERN_2);
154   s = lon_buf;
155
156   if (*pat == '-')
157     *s++ = *pat++;
158   while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
159           (g_ascii_isdigit(*pat) || (*pat == '.')))
160     *s++ = *pat++;
161   *s = '\0';
162   if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
163     found = FALSE;
164     goto done;
165   }
166
167   ll->lat = g_ascii_strtod(lat_buf, NULL);
168   ll->lon = g_ascii_strtod(lon_buf, NULL);
169
170 done:
171   g_mapped_file_free(mf);
172   return (found);
173
174 }
175
176 static gchar *google_goto_tool_get_url_format ( VikGotoTool *self )
177 {
178   return GOOGLE_GOTO_URL_FMT;
179 }
180
181 DownloadMapOptions *google_goto_tool_get_download_options ( VikGotoTool *self )
182 {
183   return &googlesearch_options;
184 }