]> git.street.me.uk Git - andy/viking.git/blob - src/vikwebtoolbounds.c
Use the correct definition.
[andy/viking.git] / src / vikwebtoolbounds.c
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-2015, 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
36 static GObjectClass *parent_class;
37
38 static void webtool_bounds_finalize ( GObject *gob );
39 static gchar *webtool_bounds_get_url ( VikWebtool *vw, VikWindow *vwindow );
40 static gchar *webtool_bounds_get_url_at_position ( VikWebtool *vw, VikWindow *vwindow, VikCoord *vc );
41
42 typedef struct _VikWebtoolBoundsPrivate VikWebtoolBoundsPrivate;
43
44 struct _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
53 G_DEFINE_TYPE (VikWebtoolBounds, vik_webtool_bounds, VIK_WEBTOOL_TYPE)
54
55 enum
56 {
57   PROP_0,
58
59   PROP_URL,
60 };
61
62 static void
63 webtool_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
86 static void
87 webtool_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
108 static void
109 vik_webtool_bounds_class_init ( VikWebtoolBoundsClass *klass )
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   base_class->get_url_at_position = webtool_bounds_get_url_at_position;
135
136   g_type_class_add_private (klass, sizeof (VikWebtoolBoundsPrivate));
137 }
138
139 VikWebtoolBounds *vik_webtool_bounds_new ()
140 {
141   return VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE, NULL ) );
142 }
143
144 VikWebtoolBounds *vik_webtool_bounds_new_with_members ( const gchar *label, const gchar *url )
145 {
146   VikWebtoolBounds *result = VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE,
147                                                                  "label", label,
148                                                                  "url", url,
149                                                                  NULL ) );
150
151   return result;
152 }
153
154 static void
155 vik_webtool_bounds_init ( VikWebtoolBounds *self )
156 {
157   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
158   priv->url = NULL;
159 }
160
161 static void webtool_bounds_finalize ( GObject *gob )
162 {
163   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE ( gob );
164   g_free ( priv->url ); priv->url = NULL;
165   G_OBJECT_CLASS(parent_class)->finalize(gob);
166 }
167
168 static gchar *webtool_bounds_get_url ( VikWebtool *self, VikWindow *vwindow )
169 {
170   VikWebtoolBoundsPrivate *priv = NULL;
171   VikViewport *viewport = NULL;
172
173   priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
174   viewport = vik_window_viewport ( vwindow );
175
176   // Get top left and bottom right lat/lon pairs from the viewport
177   gdouble min_lat, max_lat, min_lon, max_lon;
178   gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
179   gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
180   gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
181   gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
182   vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
183
184   // Cannot simply use g_strdup_printf and gdouble due to locale.
185   // As we compute an URL, we have to think in C locale.
186   g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
187   g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
188   g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
189   g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
190
191   return g_strdup_printf ( priv->url, sminlon, smaxlon, sminlat, smaxlat );
192 }
193
194 static gchar *webtool_bounds_get_url_at_position ( VikWebtool *self, VikWindow *vwindow, VikCoord *vc )
195 {
196   // TODO: could use zoom level to generate an offset from center lat/lon to get the bounds
197   // For now simply use the existing function to use bounds from the viewport
198   return webtool_bounds_get_url ( self, vwindow );
199 }