]> git.street.me.uk Git - andy/viking.git/blame - src/vikwebtoolformat.c
Threading rewrite to remove use of unsupported cross platform threading update of...
[andy/viking.git] / src / vikwebtoolformat.c
CommitLineData
a3dcc49e
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Format 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 Format Public License for more details.
16 *
17 * You should have received a copy of the GNU Format 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 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "vikwebtoolformat.h"
28
29#include <string.h>
30#include <glib.h>
31
32#include "util.h"
33#include "globals.h"
34#include "maputils.h"
35
36static GObjectClass *parent_class;
37
38static void webtool_format_finalize ( GObject *gob );
39
40static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
41static gchar *webtool_format_get_url ( VikWebtool *vw, VikWindow *vwindow );
027ff770 42static gchar *webtool_format_get_url_at_position ( VikWebtool *vw, VikWindow *vwindow, VikCoord *vc );
a3dcc49e
RN
43
44typedef struct _VikWebtoolFormatPrivate VikWebtoolFormatPrivate;
45
46struct _VikWebtoolFormatPrivate
47{
48 gchar *url;
49 gchar *url_format_code;
50};
51
52#define WEBTOOL_FORMAT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53 VIK_WEBTOOL_FORMAT_TYPE, \
54 VikWebtoolFormatPrivate))
55
56G_DEFINE_TYPE (VikWebtoolFormat, vik_webtool_format, VIK_WEBTOOL_TYPE)
57
58enum
59{
60 PROP_0,
61
62 PROP_URL,
63 PROP_URL_FORMAT_CODE,
64};
65
66static void
67webtool_format_set_property (GObject *object,
68 guint property_id,
69 const GValue *value,
70 GParamSpec *pspec)
71{
72 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
73 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
74
75 switch (property_id) {
76 case PROP_URL:
77 g_free (priv->url);
78 priv->url = g_value_dup_string (value);
79 g_debug ("VikWebtoolFormat.url: %s", priv->url);
80 break;
81 case PROP_URL_FORMAT_CODE:
82 g_free ( priv->url_format_code );
83 priv->url_format_code = g_value_dup_string ( value );
84 g_debug ( "VikWebtoolFormat.url_format_code: %s", priv->url_format_code );
85 break;
86
87 default:
88 /* We don't have any other property... */
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90 break;
91 }
92}
93
94static void
95webtool_format_get_property (GObject *object,
96 guint property_id,
97 GValue *value,
98 GParamSpec *pspec)
99{
100 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
101 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
102
103 switch (property_id) {
104 case PROP_URL:
105 g_value_set_string (value, priv->url);
106 break;
107
108 case PROP_URL_FORMAT_CODE:
109 g_value_set_string ( value, priv->url_format_code );
110 break;
111
112 default:
113 /* We don't have any other property... */
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 break;
116 }
117}
118
119static void
120vik_webtool_format_class_init ( VikWebtoolFormatClass *klass )
121{
122 GObjectClass *gobject_class;
123 VikWebtoolClass *base_class;
124 GParamSpec *pspec;
125
126 gobject_class = G_OBJECT_CLASS (klass);
127
128 gobject_class->finalize = webtool_format_finalize;
129 gobject_class->set_property = webtool_format_set_property;
130 gobject_class->get_property = webtool_format_get_property;
131
132 pspec = g_param_spec_string ("url",
133 "Template Url",
134 "Set the template url",
135 VIKING_URL /* default value */,
136 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
137 g_object_class_install_property (gobject_class,
138 PROP_URL,
139 pspec);
140
141 pspec = g_param_spec_string ("url_format_code",
142 "Template URL Format Code",
143 "Set the template URL format code",
144 "AOZ", // default value Lat, Long, Zoom
145 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
146 g_object_class_install_property (gobject_class,
147 PROP_URL_FORMAT_CODE,
148 pspec);
149
150 parent_class = g_type_class_peek_parent (klass);
151
152 base_class = VIK_WEBTOOL_CLASS ( klass );
153 base_class->get_url = webtool_format_get_url;
027ff770 154 base_class->get_url_at_position = webtool_format_get_url_at_position;
a3dcc49e
RN
155
156 klass->mpp_to_zoom = webtool_format_mpp_to_zoom;
157
158 g_type_class_add_private (klass, sizeof (VikWebtoolFormatPrivate));
159}
160
161VikWebtoolFormat *vik_webtool_format_new ()
162{
163 return VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE, NULL ) );
164}
165
166VikWebtoolFormat *vik_webtool_format_new_with_members ( const gchar *label,
167 const gchar *url,
168 const gchar *url_format_code )
169{
170 VikWebtoolFormat *result = VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE,
171 "label", label,
172 "url", url,
173 "url_format_code", url_format_code,
174 NULL ) );
175
176 return result;
177}
178
179static void
180vik_webtool_format_init ( VikWebtoolFormat *self )
181{
182 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
183 priv->url = NULL;
184 priv->url_format_code = NULL;
185}
186
187static void webtool_format_finalize ( GObject *gob )
188{
189 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE ( gob );
190 g_free ( priv->url ); priv->url = NULL;
191 g_free ( priv->url_format_code ); priv->url_format_code = NULL;
192 G_OBJECT_CLASS(parent_class)->finalize(gob);
193}
194
195static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
196 return map_utils_mpp_to_zoom_level ( mpp );
197}
198
027ff770 199#define MAX_NUMBER_CODES 9
a3dcc49e 200
027ff770 201static gchar *webtool_format_get_url_at_position ( VikWebtool *self, VikWindow *vw, VikCoord *vc )
a3dcc49e
RN
202{
203 VikWebtoolFormatPrivate *priv = NULL;
204 priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
205
206 VikViewport *viewport = vik_window_viewport ( vw );
207
208 // Get top left and bottom right lat/lon pairs from the viewport
209 gdouble min_lat, max_lat, min_lon, max_lon;
210 gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
211 gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
212 gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
213 gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
214 vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
215
216 // Cannot simply use g_strdup_printf and gdouble due to locale.
217 // As we compute an URL, we have to think in C locale.
218 g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
219 g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
220 g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
221 g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
222
223 // Center values
224 const VikCoord *coord = vik_viewport_get_center ( viewport );
225 struct LatLon ll;
226 vik_coord_to_latlon ( coord, &ll );
227
228 gchar scenterlat[G_ASCII_DTOSTR_BUF_SIZE];
229 gchar scenterlon[G_ASCII_DTOSTR_BUF_SIZE];
230 g_ascii_dtostr (scenterlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
231 g_ascii_dtostr (scenterlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
232
027ff770
RN
233 struct LatLon llpt;
234 llpt.lat = 0.0;
235 llpt.lon = 0.0;
236 if ( vc )
237 vik_coord_to_latlon ( vc, &ll );
238 gchar spointlat[G_ASCII_DTOSTR_BUF_SIZE];
239 gchar spointlon[G_ASCII_DTOSTR_BUF_SIZE];
240 g_ascii_dtostr (spointlat, G_ASCII_DTOSTR_BUF_SIZE, llpt.lat);
241 g_ascii_dtostr (spointlon, G_ASCII_DTOSTR_BUF_SIZE, llpt.lon);
242
a3dcc49e
RN
243 guint8 zoom = 17; // A zoomed in default
244 // zoom - ideally x & y factors need to be the same otherwise use the default
245 if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
246 zoom = map_utils_mpp_to_zoom_level ( vik_viewport_get_zoom ( viewport ) );
247
248 gchar szoom[G_ASCII_DTOSTR_BUF_SIZE];
249 g_snprintf ( szoom, G_ASCII_DTOSTR_BUF_SIZE, "%d", zoom );
250
251 gint len = 0;
252 if ( priv->url_format_code )
253 len = strlen ( priv->url_format_code );
254 if ( len > MAX_NUMBER_CODES )
255 len = MAX_NUMBER_CODES;
256
257 gchar* values[MAX_NUMBER_CODES];
258 int i;
259 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
260 values[i] = '\0';
261 }
262
263 for ( i = 0; i < len; i++ ) {
264 switch ( g_ascii_toupper ( priv->url_format_code[i] ) ) {
265 case 'L': values[i] = g_strdup ( sminlon ); break;
266 case 'R': values[i] = g_strdup ( smaxlon ); break;
267 case 'B': values[i] = g_strdup ( sminlat ); break;
268 case 'T': values[i] = g_strdup ( smaxlat ); break;
269 case 'A': values[i] = g_strdup ( scenterlat ); break;
270 case 'O': values[i] = g_strdup ( scenterlon ); break;
271 case 'Z': values[i] = g_strdup ( szoom ); break;
027ff770
RN
272 case 'P': values[i] = g_strdup ( spointlat ); break;
273 case 'N': values[i] = g_strdup ( spointlon ); break;
a3dcc49e
RN
274 default: break;
275 }
276 }
277
027ff770 278 gchar *url = g_strdup_printf ( priv->url, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8] );
a3dcc49e
RN
279
280 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
281 if ( values[i] != '\0' )
282 g_free ( values[i] );
283 }
284
285 g_debug ("%s %s", __FUNCTION__, url);
286 return url;
287}
288
027ff770
RN
289static gchar *webtool_format_get_url ( VikWebtool *self, VikWindow *vw )
290{
291 return webtool_format_get_url_at_position ( self, vw, NULL );
292}
293
a3dcc49e
RN
294guint8 vik_webtool_format_mpp_to_zoom (VikWebtool *self, gdouble mpp)
295{
296 return VIK_WEBTOOL_FORMAT_GET_CLASS( self )->mpp_to_zoom( self, mpp );
297}