]> git.street.me.uk Git - andy/viking.git/blob - src/vikwebtoolformat.c
Fix stdout/stderr variable usage.
[andy/viking.git] / src / vikwebtoolformat.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) 2014, 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 Format 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 Format Public License for more details.
16  *
17  * You should have received a copy of the GNU Format 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 "vikwebtoolformat.h"
28
29 #include <string.h>
30 #include <glib.h>
31
32 #include "util.h"
33 #include "globals.h"
34 #include "maputils.h"
35
36 static GObjectClass *parent_class;
37
38 static void webtool_format_finalize ( GObject *gob );
39
40 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
41 static gchar *webtool_format_get_url ( VikWebtool *vw, VikWindow *vwindow );
42
43 typedef struct _VikWebtoolFormatPrivate VikWebtoolFormatPrivate;
44
45 struct _VikWebtoolFormatPrivate
46 {
47         gchar *url;
48         gchar *url_format_code;
49 };
50
51 #define WEBTOOL_FORMAT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
52                                        VIK_WEBTOOL_FORMAT_TYPE,          \
53                                        VikWebtoolFormatPrivate))
54
55 G_DEFINE_TYPE (VikWebtoolFormat, vik_webtool_format, VIK_WEBTOOL_TYPE)
56
57 enum
58 {
59         PROP_0,
60
61         PROP_URL,
62         PROP_URL_FORMAT_CODE,
63 };
64
65 static void
66 webtool_format_set_property (GObject      *object,
67                              guint         property_id,
68                              const GValue *value,
69                              GParamSpec   *pspec)
70 {
71         VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
72         VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
73
74         switch (property_id) {
75         case PROP_URL:
76                 g_free (priv->url);
77                 priv->url = g_value_dup_string (value);
78                 g_debug ("VikWebtoolFormat.url: %s", priv->url);
79                 break;
80         case PROP_URL_FORMAT_CODE:
81                 g_free ( priv->url_format_code );
82                 priv->url_format_code = g_value_dup_string ( value );
83                 g_debug ( "VikWebtoolFormat.url_format_code: %s", priv->url_format_code );
84                 break;
85
86         default:
87                 /* We don't have any other property... */
88                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89                 break;
90         }
91 }
92
93 static void
94 webtool_format_get_property (GObject    *object,
95                              guint       property_id,
96                              GValue     *value,
97                              GParamSpec *pspec)
98 {
99         VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
100         VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
101
102         switch (property_id) {
103         case PROP_URL:
104                 g_value_set_string (value, priv->url);
105                 break;
106
107         case PROP_URL_FORMAT_CODE:
108                 g_value_set_string ( value, priv->url_format_code );
109                 break;
110
111         default:
112                 /* We don't have any other property... */
113                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114                 break;
115         }
116 }
117
118 static void
119 vik_webtool_format_class_init ( VikWebtoolFormatClass *klass )
120 {
121         GObjectClass *gobject_class;
122         VikWebtoolClass *base_class;
123         GParamSpec *pspec;
124
125         gobject_class = G_OBJECT_CLASS (klass);
126
127         gobject_class->finalize = webtool_format_finalize;
128         gobject_class->set_property = webtool_format_set_property;
129         gobject_class->get_property = webtool_format_get_property;
130
131         pspec = g_param_spec_string ("url",
132                                      "Template Url",
133                                      "Set the template url",
134                                      VIKING_URL /* default value */,
135                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
136         g_object_class_install_property (gobject_class,
137                                          PROP_URL,
138                                          pspec);
139
140         pspec = g_param_spec_string ("url_format_code",
141                                      "Template URL Format Code",
142                                      "Set the template URL format code",
143                                      "AOZ", // default value Lat, Long, Zoom
144                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
145         g_object_class_install_property (gobject_class,
146                                          PROP_URL_FORMAT_CODE,
147                                          pspec);
148
149         parent_class = g_type_class_peek_parent (klass);
150
151         base_class = VIK_WEBTOOL_CLASS ( klass );
152         base_class->get_url = webtool_format_get_url;
153
154         klass->mpp_to_zoom = webtool_format_mpp_to_zoom;
155
156         g_type_class_add_private (klass, sizeof (VikWebtoolFormatPrivate));
157 }
158
159 VikWebtoolFormat *vik_webtool_format_new ()
160 {
161         return VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE, NULL ) );
162 }
163
164 VikWebtoolFormat *vik_webtool_format_new_with_members ( const gchar *label,
165                                                         const gchar *url,
166                                                         const gchar *url_format_code )
167 {
168         VikWebtoolFormat *result = VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE,
169                                                                        "label", label,
170                                                                        "url", url,
171                                                                        "url_format_code", url_format_code,
172                                                                        NULL ) );
173
174         return result;
175 }
176
177 static void
178 vik_webtool_format_init ( VikWebtoolFormat *self )
179 {
180         VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
181         priv->url = NULL;
182         priv->url_format_code = NULL;
183 }
184
185 static void webtool_format_finalize ( GObject *gob )
186 {
187         VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE ( gob );
188         g_free ( priv->url ); priv->url = NULL;
189         g_free ( priv->url_format_code ); priv->url_format_code = NULL;
190         G_OBJECT_CLASS(parent_class)->finalize(gob);
191 }
192
193 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
194         return map_utils_mpp_to_zoom_level ( mpp );
195 }
196
197 #define MAX_NUMBER_CODES 7
198
199 static gchar *webtool_format_get_url ( VikWebtool *self, VikWindow *vw )
200 {
201         VikWebtoolFormatPrivate *priv = NULL;
202         priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
203
204         VikViewport *viewport = vik_window_viewport ( vw );
205
206         // Get top left and bottom right lat/lon pairs from the viewport
207         gdouble min_lat, max_lat, min_lon, max_lon;
208         gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
209         gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
210         gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
211         gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
212         vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
213
214         // Cannot simply use g_strdup_printf and gdouble due to locale.
215         // As we compute an URL, we have to think in C locale.
216         g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
217         g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
218         g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
219         g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
220
221         // Center values
222         const VikCoord *coord = vik_viewport_get_center ( viewport );
223         struct LatLon ll;
224         vik_coord_to_latlon ( coord, &ll );
225
226         gchar scenterlat[G_ASCII_DTOSTR_BUF_SIZE];
227         gchar scenterlon[G_ASCII_DTOSTR_BUF_SIZE];
228         g_ascii_dtostr (scenterlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
229         g_ascii_dtostr (scenterlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
230
231         guint8 zoom = 17; // A zoomed in default
232         // zoom - ideally x & y factors need to be the same otherwise use the default
233         if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
234                 zoom = map_utils_mpp_to_zoom_level ( vik_viewport_get_zoom ( viewport ) );
235
236         gchar szoom[G_ASCII_DTOSTR_BUF_SIZE];
237         g_snprintf ( szoom, G_ASCII_DTOSTR_BUF_SIZE, "%d", zoom );
238
239         gint len = 0;
240         if ( priv->url_format_code )
241                 len = strlen ( priv->url_format_code );
242         if ( len > MAX_NUMBER_CODES )
243                 len = MAX_NUMBER_CODES;
244
245         gchar* values[MAX_NUMBER_CODES];
246         int i;
247         for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
248                 values[i] = '\0';
249         }
250
251         for ( i = 0; i < len; i++ ) {
252                 switch ( g_ascii_toupper ( priv->url_format_code[i] ) ) {
253                 case 'L': values[i] = g_strdup ( sminlon ); break;
254                 case 'R': values[i] = g_strdup ( smaxlon ); break;
255                 case 'B': values[i] = g_strdup ( sminlat ); break;
256                 case 'T': values[i] = g_strdup ( smaxlat ); break;
257                 case 'A': values[i] = g_strdup ( scenterlat ); break;
258                 case 'O': values[i] = g_strdup ( scenterlon ); break;
259                 case 'Z': values[i] = g_strdup ( szoom ); break;
260                 default: break;
261                 }
262         }
263
264         gchar *url = g_strdup_printf ( priv->url, values[0], values[1], values[2], values[3], values[4], values[5], values[6] );
265
266         for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
267                 if ( values[i] != '\0' )
268                         g_free ( values[i] );
269         }
270
271         g_debug ("%s %s", __FUNCTION__, url);
272         return url;
273 }
274
275 guint8 vik_webtool_format_mpp_to_zoom (VikWebtool *self, gdouble mpp)
276 {
277         return VIK_WEBTOOL_FORMAT_GET_CLASS( self )->mpp_to_zoom( self, mpp );
278 }