]> git.street.me.uk Git - andy/viking.git/blame - src/vikwebtoolcenter.c
Add Waypoint List dialog.
[andy/viking.git] / src / vikwebtoolcenter.c
CommitLineData
92806042
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2008, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include "vikwebtoolcenter.h"
27
28#include <string.h>
29
30#include <glib.h>
31#include <glib/gi18n.h>
32
33#include "util.h"
34#include "globals.h"
e1dde2a6 35#include "maputils.h"
92806042 36
92806042
GB
37static GObjectClass *parent_class;
38
39static void webtool_center_finalize ( GObject *gob );
40
41static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
42static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow );
43
44typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate;
45
46struct _VikWebtoolCenterPrivate
47{
48 gchar *url;
49};
50
51#define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
52 VIK_WEBTOOL_CENTER_TYPE, \
53 VikWebtoolCenterPrivate))
54
15044d72 55G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
92806042
GB
56
57enum
58{
59 PROP_0,
60
61 PROP_URL,
62};
63
64static void
65webtool_center_set_property (GObject *object,
66 guint property_id,
67 const GValue *value,
68 GParamSpec *pspec)
69{
70 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
71 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
72
73 switch (property_id)
74 {
75 case PROP_URL:
76 g_free (priv->url);
77 priv->url = g_value_dup_string (value);
78 g_debug ("VikWebtoolCenter.url: %s", priv->url);
79 break;
80
81 default:
82 /* We don't have any other property... */
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84 break;
85 }
86}
87
88static void
89webtool_center_get_property (GObject *object,
90 guint property_id,
91 GValue *value,
92 GParamSpec *pspec)
93{
94 VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
95 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
96
97 switch (property_id)
98 {
99 case PROP_URL:
100 g_value_set_string (value, priv->url);
101 break;
102
103 default:
104 /* We don't have any other property... */
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106 break;
107 }
108}
109
15044d72
GB
110static void
111vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
92806042
GB
112{
113 GObjectClass *gobject_class;
114 VikWebtoolClass *base_class;
115 GParamSpec *pspec;
116
117 gobject_class = G_OBJECT_CLASS (klass);
118
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;
122
123 pspec = g_param_spec_string ("url",
124 "Template 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,
129 PROP_URL,
130 pspec);
131
132 parent_class = g_type_class_peek_parent (klass);
133
134 base_class = VIK_WEBTOOL_CLASS ( klass );
135 base_class->get_url = webtool_center_get_url;
136
137 klass->mpp_to_zoom = webtool_center_mpp_to_zoom;
138
139 g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate));
140}
141
142VikWebtoolCenter *vik_webtool_center_new ()
143{
144 return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) );
145}
146
147VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url )
148{
149 VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE,
150 "label", label,
151 "url", url,
152 NULL ) );
153
154 return result;
155}
156
15044d72
GB
157static void
158vik_webtool_center_init ( VikWebtoolCenter *self )
92806042
GB
159{
160 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
161 priv->url = NULL;
162}
163
164static void webtool_center_finalize ( GObject *gob )
165{
166 VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob );
167 g_free ( priv->url ); priv->url = NULL;
168 G_OBJECT_CLASS(parent_class)->finalize(gob);
169}
170
92806042 171static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
e1dde2a6 172 return map_utils_mpp_to_zoom_level ( mpp );
92806042
GB
173}
174
175static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
176{
177 VikWebtoolCenterPrivate *priv = NULL;
178 VikViewport *viewport = NULL;
179 const VikCoord *coord = NULL;
e1dde2a6 180 guint8 zoom = 17;
92806042
GB
181 struct LatLon ll;
182 gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE];
183
184 priv = WEBTOOL_CENTER_GET_PRIVATE (self);
185 viewport = vik_window_viewport ( vwindow );
186
187 // Coords
188 coord = vik_viewport_get_center ( viewport );
189 vik_coord_to_latlon ( coord, &ll );
190
4b4b5bc4 191 // zoom - ideally x & y factors need to be the same otherwise use the default
1d76b08b
RN
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 ) );
92806042
GB
194
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);
199
e1dde2a6 200 return g_strdup_printf ( priv->url, strlat, strlon, zoom );
92806042
GB
201}
202
203guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp)
204{
205 return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp );
206}