]> git.street.me.uk Git - andy/viking.git/blob - src/vikwebtoolcenter.c
SF Features#116: Add an Acquire From URL option.
[andy/viking.git] / src / vikwebtoolcenter.c
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"
35 #include "maputils.h"
36
37 static GObjectClass *parent_class;
38
39 static void webtool_center_finalize ( GObject *gob );
40
41 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
42 static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow );
43
44 typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate;
45
46 struct _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
55 G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
56
57 enum
58 {
59   PROP_0,
60
61   PROP_URL,
62 };
63
64 static void
65 webtool_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
88 static void
89 webtool_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
110 static void
111 vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
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
142 VikWebtoolCenter *vik_webtool_center_new ()
143 {
144   return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) );
145 }
146
147 VikWebtoolCenter *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
157 static void
158 vik_webtool_center_init ( VikWebtoolCenter *self )
159 {
160   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
161   priv->url = NULL;
162 }
163
164 static 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
171 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
172   return map_utils_mpp_to_zoom_level ( mpp );
173 }
174
175 static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
176 {
177   VikWebtoolCenterPrivate *priv = NULL;
178   VikViewport *viewport = NULL;
179   const VikCoord *coord = NULL;
180   guint8 zoom = 17;
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
191   // zoom - ideally x & y factors need to be the same otherwise use the default
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 ) );
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
200   return g_strdup_printf ( priv->url, strlat, strlon, zoom );
201 }
202
203 guint8 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 }