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 );
44 typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate;
46 struct _VikWebtoolCenterPrivate
51 #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
52 VIK_WEBTOOL_CENTER_TYPE, \
53 VikWebtoolCenterPrivate))
55 G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
65 webtool_center_set_property (GObject *object,
70 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
71 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
77 priv->url = g_value_dup_string (value);
78 g_debug ("VikWebtoolCenter.url: %s", priv->url);
82 /* We don't have any other property... */
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89 webtool_center_get_property (GObject *object,
94 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
95 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
100 g_value_set_string (value, priv->url);
104 /* We don't have any other property... */
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111 vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
113 GObjectClass *gobject_class;
114 VikWebtoolClass *base_class;
117 gobject_class = G_OBJECT_CLASS (klass);
119 gobject_class->finalize = webtool_center_finalize;
120 gobject_class->set_property = webtool_center_set_property;
121 gobject_class->get_property = webtool_center_get_property;
123 pspec = g_param_spec_string ("url",
125 "Set the template url",
126 VIKING_URL /* default value */,
127 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
128 g_object_class_install_property (gobject_class,
132 parent_class = g_type_class_peek_parent (klass);
134 base_class = VIK_WEBTOOL_CLASS ( klass );
135 base_class->get_url = webtool_center_get_url;
137 klass->mpp_to_zoom = webtool_center_mpp_to_zoom;
139 g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate));
142 VikWebtoolCenter *vik_webtool_center_new ()
144 return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) );
147 VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url )
149 VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE,
158 vik_webtool_center_init ( VikWebtoolCenter *self )
160 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
164 static void webtool_center_finalize ( GObject *gob )
166 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob );
167 g_free ( priv->url ); priv->url = NULL;
168 G_OBJECT_CLASS(parent_class)->finalize(gob);
171 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
172 return map_utils_mpp_to_zoom_level ( mpp );
175 static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
177 VikWebtoolCenterPrivate *priv = NULL;
178 VikViewport *viewport = NULL;
179 const VikCoord *coord = NULL;
182 gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE];
184 priv = WEBTOOL_CENTER_GET_PRIVATE (self);
185 viewport = vik_window_viewport ( vwindow );
188 coord = vik_viewport_get_center ( viewport );
189 vik_coord_to_latlon ( coord, &ll );
191 // zoom - ideally x & y factors need to be the same otherwise use the default
192 if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
193 zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) );
195 // Cannot simply use g_strdup_printf and gdouble due to locale.
196 // As we compute an URL, we have to think in C locale.
197 g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
198 g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
200 return g_strdup_printf ( priv->url, strlat, strlon, zoom );
203 guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp)
205 return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp );