]> git.street.me.uk Git - andy/viking.git/blob - src/vikwebtoolcenter.c
Coverity: Prevent deference after null checks
[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 static gchar *webtool_center_get_url_at_position ( VikWebtool *vw, VikWindow *vwindow, VikCoord *vc );
44
45 typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate;
46
47 struct _VikWebtoolCenterPrivate
48 {
49   gchar *url;
50 };
51
52 #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53                                        VIK_WEBTOOL_CENTER_TYPE,          \
54                                        VikWebtoolCenterPrivate))
55
56 G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
57
58 enum
59 {
60   PROP_0,
61
62   PROP_URL,
63 };
64
65 static void
66 webtool_center_set_property (GObject      *object,
67                              guint         property_id,
68                              const GValue *value,
69                              GParamSpec   *pspec)
70 {
71   VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
72   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
73
74   switch (property_id)
75     {
76     case PROP_URL:
77       g_free (priv->url);
78       priv->url = g_value_dup_string (value);
79       g_debug ("VikWebtoolCenter.url: %s", priv->url);
80       break;
81
82     default:
83       /* We don't have any other property... */
84       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85       break;
86     }
87 }
88
89 static void
90 webtool_center_get_property (GObject    *object,
91                              guint       property_id,
92                              GValue     *value,
93                              GParamSpec *pspec)
94 {
95   VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object);
96   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
97
98   switch (property_id)
99     {
100     case PROP_URL:
101       g_value_set_string (value, 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 vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
113 {
114   GObjectClass *gobject_class;
115   VikWebtoolClass *base_class;
116   GParamSpec *pspec;
117
118   gobject_class = G_OBJECT_CLASS (klass);
119
120   gobject_class->finalize = webtool_center_finalize;
121   gobject_class->set_property = webtool_center_set_property;
122   gobject_class->get_property = webtool_center_get_property;
123
124   pspec = g_param_spec_string ("url",
125                                "Template Url",
126                                "Set the template url",
127                                VIKING_URL /* default value */,
128                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
129   g_object_class_install_property (gobject_class,
130                                    PROP_URL,
131                                    pspec);
132
133   parent_class = g_type_class_peek_parent (klass);
134
135   base_class = VIK_WEBTOOL_CLASS ( klass );
136   base_class->get_url = webtool_center_get_url;
137   base_class->get_url_at_position = webtool_center_get_url_at_position;
138
139   klass->mpp_to_zoom = webtool_center_mpp_to_zoom;
140
141   g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate));
142 }
143
144 VikWebtoolCenter *vik_webtool_center_new ()
145 {
146   return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) );
147 }
148
149 VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url )
150 {
151   VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE,
152                                                                  "label", label,
153                                                                  "url", url,
154                                                                  NULL ) );
155
156   return result;
157 }
158
159 static void
160 vik_webtool_center_init ( VikWebtoolCenter *self )
161 {
162   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
163   priv->url = NULL;
164 }
165
166 static void webtool_center_finalize ( GObject *gob )
167 {
168   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob );
169   g_free ( priv->url ); priv->url = NULL;
170   G_OBJECT_CLASS(parent_class)->finalize(gob);
171 }
172
173 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
174   return map_utils_mpp_to_zoom_level ( mpp );
175 }
176
177 static gchar *webtool_center_get_url_at_position ( VikWebtool *self, VikWindow *vwindow, VikCoord *vc )
178 {
179   VikWebtoolCenterPrivate *priv = NULL;
180   VikViewport *viewport = NULL;
181   guint8 zoom = 17;
182   struct LatLon ll;
183   gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE];
184
185   priv = WEBTOOL_CENTER_GET_PRIVATE (self);
186   viewport = vik_window_viewport ( vwindow );
187   // Coords
188   // Use the provided position otherwise use center of the viewport
189   if ( vc )
190     vik_coord_to_latlon ( vc, &ll );
191   else {
192     const VikCoord *coord = NULL;
193     coord = vik_viewport_get_center ( viewport );
194     vik_coord_to_latlon ( coord, &ll );
195   }
196
197   // zoom - ideally x & y factors need to be the same otherwise use the default
198   if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
199     zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) );
200
201   // Cannot simply use g_strdup_printf and gdouble due to locale.
202   // As we compute an URL, we have to think in C locale.
203   g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
204   g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
205
206   return g_strdup_printf ( priv->url, strlat, strlon, zoom );
207 }
208
209 static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
210 {
211   return webtool_center_get_url_at_position ( self, vwindow, NULL );
212 }
213
214 guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp)
215 {
216   return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp );
217 }