1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
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.
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.
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
27 #include "vikwebtoolformat.h"
36 static GObjectClass *parent_class;
38 static void webtool_format_finalize ( GObject *gob );
40 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
41 static gchar *webtool_format_get_url ( VikWebtool *vw, VikWindow *vwindow );
42 static gchar *webtool_format_get_url_at_position ( VikWebtool *vw, VikWindow *vwindow, VikCoord *vc );
44 typedef struct _VikWebtoolFormatPrivate VikWebtoolFormatPrivate;
46 struct _VikWebtoolFormatPrivate
49 gchar *url_format_code;
52 #define WEBTOOL_FORMAT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53 VIK_WEBTOOL_FORMAT_TYPE, \
54 VikWebtoolFormatPrivate))
56 G_DEFINE_TYPE (VikWebtoolFormat, vik_webtool_format, VIK_WEBTOOL_TYPE)
67 webtool_format_set_property (GObject *object,
72 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
73 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
75 switch (property_id) {
78 priv->url = g_value_dup_string (value);
79 g_debug ("VikWebtoolFormat.url: %s", priv->url);
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 );
88 /* We don't have any other property... */
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95 webtool_format_get_property (GObject *object,
100 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
101 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
103 switch (property_id) {
105 g_value_set_string (value, priv->url);
108 case PROP_URL_FORMAT_CODE:
109 g_value_set_string ( value, priv->url_format_code );
113 /* We don't have any other property... */
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
120 vik_webtool_format_class_init ( VikWebtoolFormatClass *klass )
122 GObjectClass *gobject_class;
123 VikWebtoolClass *base_class;
126 gobject_class = G_OBJECT_CLASS (klass);
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;
132 pspec = g_param_spec_string ("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,
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,
150 parent_class = g_type_class_peek_parent (klass);
152 base_class = VIK_WEBTOOL_CLASS ( klass );
153 base_class->get_url = webtool_format_get_url;
154 base_class->get_url_at_position = webtool_format_get_url_at_position;
156 klass->mpp_to_zoom = webtool_format_mpp_to_zoom;
158 g_type_class_add_private (klass, sizeof (VikWebtoolFormatPrivate));
161 VikWebtoolFormat *vik_webtool_format_new ()
163 return VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE, NULL ) );
166 VikWebtoolFormat *vik_webtool_format_new_with_members ( const gchar *label,
168 const gchar *url_format_code )
170 VikWebtoolFormat *result = VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE,
173 "url_format_code", url_format_code,
180 vik_webtool_format_init ( VikWebtoolFormat *self )
182 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
184 priv->url_format_code = NULL;
187 static void webtool_format_finalize ( GObject *gob )
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);
195 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
196 return map_utils_mpp_to_zoom_level ( mpp );
199 #define MAX_NUMBER_CODES 9
201 static gchar *webtool_format_get_url_at_position ( VikWebtool *self, VikWindow *vw, VikCoord *vc )
203 VikWebtoolFormatPrivate *priv = NULL;
204 priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
206 VikViewport *viewport = vik_window_viewport ( vw );
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 );
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);
224 const VikCoord *coord = vik_viewport_get_center ( viewport );
226 vik_coord_to_latlon ( coord, &ll );
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);
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);
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 ) );
248 gchar szoom[G_ASCII_DTOSTR_BUF_SIZE];
249 g_snprintf ( szoom, G_ASCII_DTOSTR_BUF_SIZE, "%d", zoom );
252 if ( priv->url_format_code )
253 len = strlen ( priv->url_format_code );
254 if ( len > MAX_NUMBER_CODES )
255 len = MAX_NUMBER_CODES;
257 gchar* values[MAX_NUMBER_CODES];
259 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
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;
272 case 'P': values[i] = g_strdup ( spointlat ); break;
273 case 'N': values[i] = g_strdup ( spointlon ); break;
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] );
280 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
281 if ( values[i] != '\0' )
282 g_free ( values[i] );
285 g_debug ("%s %s", __FUNCTION__, url);
289 static gchar *webtool_format_get_url ( VikWebtool *self, VikWindow *vw )
291 return webtool_format_get_url_at_position ( self, vw, NULL );
294 guint8 vik_webtool_format_mpp_to_zoom (VikWebtool *self, gdouble mpp)
296 return VIK_WEBTOOL_FORMAT_GET_CLASS( self )->mpp_to_zoom( self, mpp );