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 GObjectClass *parent_class;
38 static void webtool_bounds_finalize ( GObject *gob );
40 static gchar *webtool_bounds_get_url ( VikWebtool *vw, VikWindow *vwindow );
42 typedef struct _VikWebtoolBoundsPrivate VikWebtoolBoundsPrivate;
44 struct _VikWebtoolBoundsPrivate
49 #define WEBTOOL_BOUNDS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
50 VIK_WEBTOOL_BOUNDS_TYPE, \
51 VikWebtoolBoundsPrivate))
53 G_DEFINE_TYPE (VikWebtoolBounds, vik_webtool_bounds, VIK_WEBTOOL_TYPE)
63 webtool_bounds_set_property (GObject *object,
68 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
69 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
75 priv->url = g_value_dup_string (value);
76 g_debug ("VikWebtoolBounds.url: %s", priv->url);
80 /* We don't have any other property... */
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
87 webtool_bounds_get_property (GObject *object,
92 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
93 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
98 g_value_set_string (value, priv->url);
102 /* We don't have any other property... */
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 vik_webtool_bounds_class_init ( VikWebtoolBoundsClass *klass )
111 GObjectClass *gobject_class;
112 VikWebtoolClass *base_class;
115 gobject_class = G_OBJECT_CLASS (klass);
117 gobject_class->finalize = webtool_bounds_finalize;
118 gobject_class->set_property = webtool_bounds_set_property;
119 gobject_class->get_property = webtool_bounds_get_property;
121 pspec = g_param_spec_string ("url",
123 "Set the template url",
124 VIKING_URL /* default value */,
125 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
126 g_object_class_install_property (gobject_class,
130 parent_class = g_type_class_peek_parent (klass);
132 base_class = VIK_WEBTOOL_CLASS ( klass );
133 base_class->get_url = webtool_bounds_get_url;
135 //klass->mpp_to_zoom = webtool_bounds_mpp_to_zoom;
137 g_type_class_add_private (klass, sizeof (VikWebtoolBoundsPrivate));
140 VikWebtoolBounds *vik_webtool_bounds_new ()
142 return VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE, NULL ) );
145 VikWebtoolBounds *vik_webtool_bounds_new_with_members ( const gchar *label, const gchar *url )
147 VikWebtoolBounds *result = VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE,
156 vik_webtool_bounds_init ( VikWebtoolBounds *self )
158 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
162 static void webtool_bounds_finalize ( GObject *gob )
164 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE ( gob );
165 g_free ( priv->url ); priv->url = NULL;
166 G_OBJECT_CLASS(parent_class)->finalize(gob);
169 static gchar *webtool_bounds_get_url ( VikWebtool *self, VikWindow *vwindow )
171 VikWebtoolBoundsPrivate *priv = NULL;
172 VikViewport *viewport = NULL;
174 priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
175 viewport = vik_window_viewport ( vwindow );
177 // Get top left and bottom right lat/lon pairs from the viewport
178 gdouble min_lat, max_lat, min_lon, max_lon;
179 gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
180 gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
181 gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
182 gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
183 vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
185 // Cannot simply use g_strdup_printf and gdouble due to locale.
186 // As we compute an URL, we have to think in C locale.
187 g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
188 g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
189 g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
190 g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
192 return g_strdup_printf ( priv->url, sminlon, smaxlon, sminlat, smaxlat );