]> git.street.me.uk Git - andy/viking.git/blob - src/vikwebtoolbounds.c
Merge tag 'viking-1.2.2'
[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, 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 void webtool_bounds_class_init ( VikWebtoolBoundsClass *klass );
37 static void webtool_bounds_init ( VikWebtoolBounds *vwd );
38
39 static GObjectClass *parent_class;
40
41 static void webtool_bounds_finalize ( GObject *gob );
42
43 static gchar *webtool_bounds_get_url ( VikWebtool *vw, VikWindow *vwindow );
44
45 typedef struct _VikWebtoolBoundsPrivate VikWebtoolBoundsPrivate;
46
47 struct _VikWebtoolBoundsPrivate
48 {
49   gchar *url;
50 };
51
52 #define WEBTOOL_BOUNDS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53                                        VIK_WEBTOOL_BOUNDS_TYPE,          \
54                                        VikWebtoolBoundsPrivate))
55
56 GType vik_webtool_bounds_get_type()
57 {
58   static GType w_type = 0;
59
60   if (!w_type)
61   {
62     static const GTypeInfo w_info =
63     {
64       sizeof (VikWebtoolBoundsClass),
65       NULL, /* base_init */
66       NULL, /* base_finalize */
67       (GClassInitFunc) webtool_bounds_class_init,
68       NULL, /* class_finalize */
69       NULL, /* class_data */
70       sizeof (VikWebtoolBounds),
71       0,
72       (GInstanceInitFunc) webtool_bounds_init,
73     };
74     w_type = g_type_register_static ( VIK_WEBTOOL_TYPE, "VikWebtoolBounds", &w_info, 0 );
75   }
76
77   return w_type;
78 }
79
80 enum
81 {
82   PROP_0,
83
84   PROP_URL,
85 };
86
87 static void
88 webtool_bounds_set_property (GObject      *object,
89                              guint         property_id,
90                              const GValue *value,
91                              GParamSpec   *pspec)
92 {
93   VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
94   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
95
96   switch (property_id)
97     {
98     case PROP_URL:
99       g_free (priv->url);
100       priv->url = g_value_dup_string (value);
101       g_debug ("VikWebtoolBounds.url: %s", priv->url);
102       break;
103
104     default:
105       /* We don't have any other property... */
106       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107       break;
108     }
109 }
110
111 static void
112 webtool_bounds_get_property (GObject    *object,
113                              guint       property_id,
114                              GValue     *value,
115                              GParamSpec *pspec)
116 {
117   VikWebtoolBounds *self = VIK_WEBTOOL_BOUNDS (object);
118   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
119
120   switch (property_id)
121     {
122     case PROP_URL:
123       g_value_set_string (value, priv->url);
124       break;
125
126     default:
127       /* We don't have any other property... */
128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
129       break;
130     }
131 }
132
133 static void webtool_bounds_class_init ( VikWebtoolBoundsClass *klass )
134 {
135   GObjectClass *gobject_class;
136   VikWebtoolClass *base_class;
137   GParamSpec *pspec;
138
139   gobject_class = G_OBJECT_CLASS (klass);
140
141   gobject_class->finalize = webtool_bounds_finalize;
142   gobject_class->set_property = webtool_bounds_set_property;
143   gobject_class->get_property = webtool_bounds_get_property;
144
145   pspec = g_param_spec_string ("url",
146                                "Template Url",
147                                "Set the template url",
148                                VIKING_URL /* default value */,
149                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
150   g_object_class_install_property (gobject_class,
151                                    PROP_URL,
152                                    pspec);
153
154   parent_class = g_type_class_peek_parent (klass);
155
156   base_class = VIK_WEBTOOL_CLASS ( klass );
157   base_class->get_url = webtool_bounds_get_url;
158
159   //klass->mpp_to_zoom = webtool_bounds_mpp_to_zoom;
160
161   g_type_class_add_private (klass, sizeof (VikWebtoolBoundsPrivate));
162 }
163
164 VikWebtoolBounds *vik_webtool_bounds_new ()
165 {
166   return VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE, NULL ) );
167 }
168
169 VikWebtoolBounds *vik_webtool_bounds_new_with_members ( const gchar *label, const gchar *url )
170 {
171   VikWebtoolBounds *result = VIK_WEBTOOL_BOUNDS ( g_object_new ( VIK_WEBTOOL_BOUNDS_TYPE,
172                                                                  "label", label,
173                                                                  "url", url,
174                                                                  NULL ) );
175
176   return result;
177 }
178
179 static void webtool_bounds_init ( VikWebtoolBounds *self )
180 {
181   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
182   priv->url = NULL;
183 }
184
185 static void webtool_bounds_finalize ( GObject *gob )
186 {
187   VikWebtoolBoundsPrivate *priv = WEBTOOL_BOUNDS_GET_PRIVATE ( gob );
188   g_free ( priv->url ); priv->url = NULL;
189   G_OBJECT_CLASS(parent_class)->finalize(gob);
190 }
191
192 static gchar *webtool_bounds_get_url ( VikWebtool *self, VikWindow *vwindow )
193 {
194   VikWebtoolBoundsPrivate *priv = NULL;
195   VikViewport *viewport = NULL;
196
197   priv = WEBTOOL_BOUNDS_GET_PRIVATE (self);
198   viewport = vik_window_viewport ( vwindow );
199
200   // Get top left and bottom right lat/lon pairs from the viewport
201   gdouble min_lat, max_lat, min_lon, max_lon;
202   gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
203   gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
204   gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
205   gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
206   vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
207
208   // Cannot simply use g_strdup_printf and gdouble due to locale.
209   // As we compute an URL, we have to think in C locale.
210   g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
211   g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
212   g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
213   g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
214
215   return g_strdup_printf ( priv->url, sminlon, smaxlon, sminlat, smaxlat );
216 }