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) 2011, 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 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.
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.
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
27 #include "vikwebtoolbounds.h"
32 #include <glib/gi18n.h>
36 static void webtool_bounds_class_init ( VikWebtoolBoundsClass *klass );
37 static void webtool_bounds_init ( VikWebtoolBounds *vwd );
39 static GObjectClass *parent_class;
41 static void webtool_bounds_finalize ( GObject *gob );
43 static gchar *webtool_bounds_get_url ( VikWebtool *vw, VikWindow *vwindow );
45 typedef struct _VikWebtoolBoundsPrivate VikWebtoolBoundsPrivate;
47 struct _VikWebtoolBoundsPrivate
52 #define WEBTOOL_BOUNDS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53 VIK_WEBTOOL_BOUNDS_TYPE, \
54 VikWebtoolBoundsPrivate))
56 GType vik_webtool_bounds_get_type()
58 static GType w_type = 0;
62 static const GTypeInfo w_info =
64 sizeof (VikWebtoolBoundsClass),
66 NULL, /* base_finalize */
67 (GClassInitFunc) webtool_bounds_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 sizeof (VikWebtoolBounds),
72 (GInstanceInitFunc) webtool_bounds_init,
74 w_type = g_type_register_static ( VIK_WEBTOOL_TYPE, "VikWebtoolBounds", &w_info, 0 );
88 webtool_bounds_set_property (GObject *object,
93 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
94 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
100 priv->url = g_value_dup_string (value);
101 g_debug ("VikWebtoolBounds.url: %s", priv->url);
105 /* We don't have any other property... */
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 webtool_bounds_get_property (GObject *object,
117 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
118 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
123 g_value_set_string (value, priv->url);
127 /* We don't have any other property... */
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 static void webtool_bounds_class_init ( VikWebtoolBoundsClass *klass )
135 GObjectClass *gobject_class;
136 VikWebtoolClass *base_class;
139 gobject_class = G_OBJECT_CLASS (klass);
141 gobject_class->finalize = webtool_bounds_finalize;
142 gobject_class->set_property = webtool_bounds_set_property;
143 gobject_class->get_property = webtool_bounds_get_property;
145 pspec = g_param_spec_string ("url",
147 "Set the template url",
148 VIKING_URL /* default value */,
149 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
150 g_object_class_install_property (gobject_class,
154 parent_class = g_type_class_peek_parent (klass);
156 base_class = VIK_WEBTOOL_CLASS ( klass );
157 base_class->get_url = webtool_bounds_get_url;
159 //klass->mpp_to_zoom = webtool_bounds_mpp_to_zoom;
161 g_type_class_add_private (klass, sizeof (VikWebtoolBoundsPrivate));
164 VikWebtoolBounds *vik_webtool_bounds_new ()
166 return VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE, NULL ) );
169 VikWebtoolBounds *vik_webtool_bounds_new_with_members ( const gchar *label, const gchar *url )
171 VikWebtoolBounds *result = VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE,
179 static void webtool_bounds_init ( VikWebtoolBounds *self )
181 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
185 static void webtool_bounds_finalize ( GObject *gob )
187 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE ( gob );
188 g_free ( priv->url ); priv->url = NULL;
189 G_OBJECT_CLASS(parent_class)->finalize(gob);
192 static gchar *webtool_bounds_get_url ( VikWebtool *self, VikWindow *vwindow )
194 VikWebtoolBoundsPrivate *priv = NULL;
195 VikViewport *viewport = NULL;
197 priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
198 viewport = vik_window_viewport ( vwindow );
200 // Get top left and bottom right lat/lon pairs from the viewport
201 gdouble min_lat, max_lat, min_lon, max_lon;
202 gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
203 gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
204 gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
205 gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
206 vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
208 // Cannot simply use g_strdup_printf and gdouble due to locale.
209 // As we compute an URL, we have to think in C locale.
210 g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
211 g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
212 g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
213 g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
215 return g_strdup_printf ( priv->url, sminlon, smaxlon, sminlat, smaxlat );