Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com>
3 : : * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se>
4 : : *
5 : : * This library is free software; you can redistribute it and/or
6 : : * modify it under the terms of the GNU Lesser General Public
7 : : * License as published by the Free Software Foundation; either
8 : : * version 2.1 of the License, or (at your option) any later version.
9 : : *
10 : : * This library is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public
16 : : * License along with this library; if not, write to the Free Software
17 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 : : */
19 : :
20 : : /**
21 : : * ShumateCoordinate:
22 : : *
23 : : * A simple object implementing [iface@Location].
24 : : */
25 : :
26 : : #include "shumate-coordinate.h"
27 : :
28 : : #include "shumate-marshal.h"
29 : : #include "shumate-location.h"
30 : :
31 : : enum
32 : : {
33 : : PROP_LONGITUDE = 1,
34 : : PROP_LATITUDE,
35 : : };
36 : :
37 : :
38 : : static void set_location (ShumateLocation *location,
39 : : double latitude,
40 : : double longitude);
41 : : static double get_latitude (ShumateLocation *location);
42 : : static double get_longitude (ShumateLocation *location);
43 : :
44 : : static void location_interface_init (ShumateLocationInterface *iface);
45 : :
46 : : typedef struct
47 : : {
48 : : double longitude;
49 : : double latitude;
50 : : } ShumateCoordinatePrivate;
51 : :
52 [ + + + - ]: 177 : G_DEFINE_TYPE_WITH_CODE (ShumateCoordinate, shumate_coordinate, G_TYPE_INITIALLY_UNOWNED,
53 : : G_ADD_PRIVATE (ShumateCoordinate)
54 : : G_IMPLEMENT_INTERFACE (SHUMATE_TYPE_LOCATION, location_interface_init));
55 : :
56 : : static void
57 : 0 : shumate_coordinate_get_property (GObject *object,
58 : : guint prop_id,
59 : : GValue *value,
60 : : GParamSpec *pspec)
61 : : {
62 : 0 : ShumateCoordinate *coordinate = SHUMATE_COORDINATE (object);
63 : 0 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
64 : :
65 [ # # # ]: 0 : switch (prop_id)
66 : : {
67 : 0 : case PROP_LONGITUDE:
68 : 0 : g_value_set_double (value, priv->longitude);
69 : 0 : break;
70 : :
71 : 0 : case PROP_LATITUDE:
72 : 0 : g_value_set_double (value, priv->latitude);
73 : 0 : break;
74 : :
75 : 0 : default:
76 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 : : }
78 : 0 : }
79 : :
80 : :
81 : : static void
82 : 12 : shumate_coordinate_set_property (GObject *object,
83 : : guint prop_id,
84 : : const GValue *value,
85 : : GParamSpec *pspec)
86 : : {
87 : 12 : ShumateCoordinate *coordinate = SHUMATE_COORDINATE (object);
88 : 12 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
89 : :
90 [ + + - ]: 12 : switch (prop_id)
91 : : {
92 : 6 : case PROP_LONGITUDE:
93 : : {
94 : 6 : double longitude = g_value_get_double (value);
95 : 6 : set_location (SHUMATE_LOCATION (coordinate), priv->latitude, longitude);
96 : 6 : break;
97 : : }
98 : :
99 : 6 : case PROP_LATITUDE:
100 : : {
101 : 6 : double latitude = g_value_get_double (value);
102 : 6 : set_location (SHUMATE_LOCATION (coordinate), latitude, priv->longitude);
103 : 6 : break;
104 : : }
105 : :
106 : 0 : default:
107 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 : : }
109 : 12 : }
110 : :
111 : :
112 : : static void
113 : 24 : set_location (ShumateLocation *location,
114 : : double latitude,
115 : : double longitude)
116 : : {
117 : 24 : ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location);
118 : 24 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
119 : :
120 [ + - ]: 24 : g_return_if_fail (SHUMATE_IS_COORDINATE (location));
121 : :
122 [ + - + - ]: 24 : priv->longitude = CLAMP (longitude, SHUMATE_MIN_LONGITUDE, SHUMATE_MAX_LONGITUDE);
123 [ + - + - ]: 24 : priv->latitude = CLAMP (latitude, SHUMATE_MIN_LATITUDE, SHUMATE_MAX_LATITUDE);
124 : :
125 : 24 : g_object_notify (G_OBJECT (location), "latitude");
126 : 24 : g_object_notify (G_OBJECT (location), "longitude");
127 : : }
128 : :
129 : :
130 : : static double
131 : 24 : get_latitude (ShumateLocation *location)
132 : : {
133 : 24 : ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location);
134 : 24 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
135 : :
136 [ + - ]: 24 : g_return_val_if_fail (SHUMATE_IS_COORDINATE (location), 0.0);
137 : :
138 : 24 : return priv->latitude;
139 : : }
140 : :
141 : :
142 : : static double
143 : 24 : get_longitude (ShumateLocation *location)
144 : : {
145 : 24 : ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location);
146 : 24 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
147 : :
148 [ + - ]: 24 : g_return_val_if_fail (SHUMATE_IS_COORDINATE (location), 0.0);
149 : :
150 : 24 : return priv->longitude;
151 : : }
152 : :
153 : :
154 : : static void
155 : 4 : location_interface_init (ShumateLocationInterface *iface)
156 : : {
157 : 4 : iface->get_latitude = get_latitude;
158 : 4 : iface->get_longitude = get_longitude;
159 : 4 : iface->set_location = set_location;
160 : 4 : }
161 : :
162 : : static void
163 : 4 : shumate_coordinate_class_init (ShumateCoordinateClass *coordinate_class)
164 : : {
165 : 4 : GObjectClass *object_class = G_OBJECT_CLASS (coordinate_class);
166 : 4 : object_class->get_property = shumate_coordinate_get_property;
167 : 4 : object_class->set_property = shumate_coordinate_set_property;
168 : :
169 : 4 : g_object_class_override_property (object_class,
170 : : PROP_LONGITUDE,
171 : : "longitude");
172 : :
173 : 4 : g_object_class_override_property (object_class,
174 : : PROP_LATITUDE,
175 : : "latitude");
176 : 4 : }
177 : :
178 : :
179 : : static void
180 : 6 : shumate_coordinate_init (ShumateCoordinate *coordinate)
181 : : {
182 : 6 : ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate);
183 : :
184 : 6 : priv->latitude = 0.0;
185 : 6 : priv->longitude = 0.0;
186 : 6 : }
187 : :
188 : :
189 : : /**
190 : : * shumate_coordinate_new:
191 : : *
192 : : * Creates a new instance of #ShumateCoordinate.
193 : : *
194 : : * Returns: the created instance.
195 : : */
196 : : ShumateCoordinate *
197 : 0 : shumate_coordinate_new ()
198 : : {
199 : 0 : return SHUMATE_COORDINATE (g_object_new (SHUMATE_TYPE_COORDINATE, NULL));
200 : : }
201 : :
202 : :
203 : : /**
204 : : * shumate_coordinate_new_full:
205 : : * @latitude: the latitude coordinate
206 : : * @longitude: the longitude coordinate
207 : : *
208 : : * Creates a new instance of #ShumateCoordinate initialized with the given
209 : : * coordinates.
210 : : *
211 : : * Returns: the created instance.
212 : : */
213 : : ShumateCoordinate *
214 : 6 : shumate_coordinate_new_full (double latitude,
215 : : double longitude)
216 : : {
217 : 6 : return SHUMATE_COORDINATE (g_object_new (SHUMATE_TYPE_COORDINATE,
218 : : "latitude", latitude,
219 : : "longitude", longitude,
220 : : NULL));
221 : : }
|