2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2008, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "vikwebtoolcenter.h"
31 #include <glib/gi18n.h>
37 static GObjectClass *parent_class;
39 static void webtool_center_finalize ( GObject *gob );
41 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
42 static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow );
43 static gchar *webtool_center_get_url_at_position ( VikWebtool *vw, VikWindow *vwindow, VikCoord *vc );
45 typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate;
47 struct _VikWebtoolCenterPrivate
52 #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53 VIK_WEBTOOL_CENTER_TYPE, \
54 VikWebtoolCenterPrivate))
56 G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
66 webtool_center_set_property (GObject *object,
71 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
72 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
78 priv->url = g_value_dup_string (value);
79 g_debug ("VikWebtoolCenter.url: %s", priv->url);
83 /* We don't have any other property... */
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90 webtool_center_get_property (GObject *object,
95 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
96 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
101 g_value_set_string (value, priv->url);
105 /* We don't have any other property... */
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
114 GObjectClass *gobject_class;
115 VikWebtoolClass *base_class;
118 gobject_class = G_OBJECT_CLASS (klass);
120 gobject_class->finalize = webtool_center_finalize;
121 gobject_class->set_property = webtool_center_set_property;
122 gobject_class->get_property = webtool_center_get_property;
124 pspec = g_param_spec_string ("url",
126 "Set the template url",
127 VIKING_URL /* default value */,
128 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
129 g_object_class_install_property (gobject_class,
133 parent_class = g_type_class_peek_parent (klass);
135 base_class = VIK_WEBTOOL_CLASS ( klass );
136 base_class->get_url = webtool_center_get_url;
137 base_class->get_url_at_position = webtool_center_get_url_at_position;
139 klass->mpp_to_zoom = webtool_center_mpp_to_zoom;
141 g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate));
144 VikWebtoolCenter *vik_webtool_center_new ()
146 return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) );
149 VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url )
151 VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE,
160 vik_webtool_center_init ( VikWebtoolCenter *self )
162 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
166 static void webtool_center_finalize ( GObject *gob )
168 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob );
169 g_free ( priv->url ); priv->url = NULL;
170 G_OBJECT_CLASS(parent_class)->finalize(gob);
173 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
174 return map_utils_mpp_to_zoom_level ( mpp );
177 static gchar *webtool_center_get_url_at_position ( VikWebtool *self, VikWindow *vwindow, VikCoord *vc )
179 VikWebtoolCenterPrivate *priv = NULL;
180 VikViewport *viewport = NULL;
183 gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE];
185 priv = WEBTOOL_CENTER_GET_PRIVATE (self);
186 viewport = vik_window_viewport ( vwindow );
188 // Use the provided position otherwise use center of the viewport
190 vik_coord_to_latlon ( vc, &ll );
192 const VikCoord *coord = NULL;
193 coord = vik_viewport_get_center ( viewport );
194 vik_coord_to_latlon ( coord, &ll );
197 // zoom - ideally x & y factors need to be the same otherwise use the default
198 if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
199 zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) );
201 // Cannot simply use g_strdup_printf and gdouble due to locale.
202 // As we compute an URL, we have to think in C locale.
203 g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
204 g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
206 return g_strdup_printf ( priv->url, strlat, strlon, zoom );
209 static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
211 return webtool_center_get_url_at_position ( self, vwindow, NULL );
214 guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp)
216 return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp );