]> git.street.me.uk Git - andy/viking.git/blame - src/vikwebtoolbounds.c
QA: factorize GObject cast operations (datasource_file.c)
[andy/viking.git] / src / vikwebtoolbounds.c
CommitLineData
40341856
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2011, Rob Norris <rw_norris@hotmail.com>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "vikwebtoolbounds.h"
28
29#include <string.h>
30
31#include <glib.h>
32#include <glib/gi18n.h>
33
34#include "globals.h"
35
40341856
RN
36static GObjectClass *parent_class;
37
38static void webtool_bounds_finalize ( GObject *gob );
39
40static gchar *webtool_bounds_get_url ( VikWebtool *vw, VikWindow *vwindow );
41
42typedef struct _VikWebtoolBoundsPrivate VikWebtoolBoundsPrivate;
43
44struct _VikWebtoolBoundsPrivate
45{
46 gchar *url;
47};
48
49#define WEBTOOL_BOUNDS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
50 VIK_WEBTOOL_BOUNDS_TYPE, \
51 VikWebtoolBoundsPrivate))
52
1a6fcb98 53G_DEFINE_TYPE (VikWebtoolBounds, vik_webtool_bounds, VIK_WEBTOOL_TYPE)
40341856
RN
54
55enum
56{
57 PROP_0,
58
59 PROP_URL,
60};
61
62static void
63webtool_bounds_set_property (GObject *object,
64 guint property_id,
65 const GValue *value,
66 GParamSpec *pspec)
67{
68 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
69 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
70
71 switch (property_id)
72 {
73 case PROP_URL:
74 g_free (priv->url);
75 priv->url = g_value_dup_string (value);
76 g_debug ("VikWebtoolBounds.url: %s", priv->url);
77 break;
78
79 default:
80 /* We don't have any other property... */
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82 break;
83 }
84}
85
86static void
87webtool_bounds_get_property (GObject *object,
88 guint property_id,
89 GValue *value,
90 GParamSpec *pspec)
91{
92 VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
93 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
94
95 switch (property_id)
96 {
97 case PROP_URL:
98 g_value_set_string (value, priv->url);
99 break;
100
101 default:
102 /* We don't have any other property... */
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104 break;
105 }
106}
107
1a6fcb98
GB
108static void
109vik_webtool_bounds_class_init ( VikWebtoolBoundsClass *klass )
40341856
RN
110{
111 GObjectClass *gobject_class;
112 VikWebtoolClass *base_class;
113 GParamSpec *pspec;
114
115 gobject_class = G_OBJECT_CLASS (klass);
116
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;
120
121 pspec = g_param_spec_string ("url",
122 "Template 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,
127 PROP_URL,
128 pspec);
129
130 parent_class = g_type_class_peek_parent (klass);
131
132 base_class = VIK_WEBTOOL_CLASS ( klass );
133 base_class->get_url = webtool_bounds_get_url;
134
135 //klass->mpp_to_zoom = webtool_bounds_mpp_to_zoom;
136
137 g_type_class_add_private (klass, sizeof (VikWebtoolBoundsPrivate));
138}
139
140VikWebtoolBounds *vik_webtool_bounds_new ()
141{
142 return VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE, NULL ) );
143}
144
145VikWebtoolBounds *vik_webtool_bounds_new_with_members ( const gchar *label, const gchar *url )
146{
147 VikWebtoolBounds *result = VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE,
148 "label", label,
149 "url", url,
150 NULL ) );
151
152 return result;
153}
154
1a6fcb98
GB
155static void
156vik_webtool_bounds_init ( VikWebtoolBounds *self )
40341856
RN
157{
158 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
159 priv->url = NULL;
160}
161
162static void webtool_bounds_finalize ( GObject *gob )
163{
164 VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE ( gob );
165 g_free ( priv->url ); priv->url = NULL;
166 G_OBJECT_CLASS(parent_class)->finalize(gob);
167}
168
169static gchar *webtool_bounds_get_url ( VikWebtool *self, VikWindow *vwindow )
170{
171 VikWebtoolBoundsPrivate *priv = NULL;
172 VikViewport *viewport = NULL;
173
174 priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
175 viewport = vik_window_viewport ( vwindow );
176
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 );
184
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);
191
192 return g_strdup_printf ( priv->url, sminlon, smaxlon, sminlat, smaxlat );
193}