No issues found
1 /*
2 * e-source-weather.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <webcal://www.gnu.org/licenses/>
16 *
17 */
18
19 #include "e-source-weather.h"
20
21 #define E_SOURCE_WEATHER_GET_PRIVATE(obj) \
22 (G_TYPE_INSTANCE_GET_PRIVATE \
23 ((obj), E_TYPE_SOURCE_WEATHER, ESourceWeatherPrivate))
24
25 struct _ESourceWeatherPrivate {
26 GMutex *property_lock;
27 ESourceWeatherUnits units;
28 gchar *location;
29 };
30
31 enum {
32 PROP_0,
33 PROP_LOCATION,
34 PROP_UNITS
35 };
36
37 static GType e_source_weather_units_type = G_TYPE_INVALID;
38
39 G_DEFINE_DYNAMIC_TYPE (
40 ESourceWeather,
41 e_source_weather,
42 E_TYPE_SOURCE_EXTENSION)
43
44 static void
45 source_weather_set_property (GObject *object,
46 guint property_id,
47 const GValue *value,
48 GParamSpec *pspec)
49 {
50 switch (property_id) {
51 case PROP_LOCATION:
52 e_source_weather_set_location (
53 E_SOURCE_WEATHER (object),
54 g_value_get_string (value));
55 return;
56
57 case PROP_UNITS:
58 e_source_weather_set_units (
59 E_SOURCE_WEATHER (object),
60 g_value_get_enum (value));
61 return;
62 }
63
64 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
65 }
66
67 static void
68 source_weather_get_property (GObject *object,
69 guint property_id,
70 GValue *value,
71 GParamSpec *pspec)
72 {
73 switch (property_id) {
74 case PROP_LOCATION:
75 g_value_take_string (
76 value,
77 e_source_weather_dup_location (
78 E_SOURCE_WEATHER (object)));
79 return;
80
81 case PROP_UNITS:
82 g_value_set_enum (
83 value,
84 e_source_weather_get_units (
85 E_SOURCE_WEATHER (object)));
86 return;
87 }
88
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90 }
91
92 static void
93 source_weather_finalize (GObject *object)
94 {
95 ESourceWeatherPrivate *priv;
96
97 priv = E_SOURCE_WEATHER_GET_PRIVATE (object);
98
99 g_mutex_free (priv->property_lock);
100
101 g_free (priv->location);
102
103 /* Chain up to parent's finalize() method. */
104 G_OBJECT_CLASS (e_source_weather_parent_class)->finalize (object);
105 }
106
107 static void
108 e_source_weather_class_init (ESourceWeatherClass *class)
109 {
110 GObjectClass *object_class;
111 ESourceExtensionClass *extension_class;
112
113 g_type_class_add_private (class, sizeof (ESourceWeatherPrivate));
114
115 object_class = G_OBJECT_CLASS (class);
116 object_class->set_property = source_weather_set_property;
117 object_class->get_property = source_weather_get_property;
118 object_class->finalize = source_weather_finalize;
119
120 extension_class = E_SOURCE_EXTENSION_CLASS (class);
121 extension_class->name = E_SOURCE_EXTENSION_WEATHER_BACKEND;
122
123 g_object_class_install_property (
124 object_class,
125 PROP_LOCATION,
126 g_param_spec_string (
127 "location",
128 "Location",
129 "Weather location code",
130 NULL,
131 G_PARAM_READWRITE |
132 G_PARAM_CONSTRUCT |
133 E_SOURCE_PARAM_SETTING));
134
135 g_object_class_install_property (
136 object_class,
137 PROP_UNITS,
138 g_param_spec_enum (
139 "units",
140 "Units",
141 "Metric or imperial units",
142 E_TYPE_SOURCE_WEATHER_UNITS,
143 E_SOURCE_WEATHER_UNITS_METRIC,
144 G_PARAM_READWRITE |
145 G_PARAM_CONSTRUCT |
146 E_SOURCE_PARAM_SETTING));
147 }
148
149 static void
150 e_source_weather_class_finalize (ESourceWeatherClass *class)
151 {
152 }
153
154 static void
155 e_source_weather_init (ESourceWeather *extension)
156 {
157 extension->priv = E_SOURCE_WEATHER_GET_PRIVATE (extension);
158 extension->priv->property_lock = g_mutex_new ();
159 }
160
161 void
162 e_source_weather_type_register (GTypeModule *type_module)
163 {
164 static const GEnumValue e_source_weather_units_values[] = {
165 { E_SOURCE_WEATHER_UNITS_METRIC,
166 "E_SOURCE_WEATHER_UNITS_METRIC",
167 "metric" },
168 { E_SOURCE_WEATHER_UNITS_IMPERIAL,
169 "E_SOURCE_WEATHER_UNITS_IMPERIAL",
170 "imperial" },
171 { 0, NULL, NULL }
172 };
173
174 e_source_weather_units_type =
175 g_type_module_register_enum (
176 type_module, "ESourceWeatherUnits",
177 e_source_weather_units_values);
178
179 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
180 * function, so we have to wrap it with a public function in
181 * order to register types from a separate compilation unit. */
182 e_source_weather_register_type (type_module);
183 }
184
185 const gchar *
186 e_source_weather_get_location (ESourceWeather *extension)
187 {
188 g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), NULL);
189
190 return extension->priv->location;
191 }
192
193 gchar *
194 e_source_weather_dup_location (ESourceWeather *extension)
195 {
196 const gchar *protected;
197 gchar *duplicate;
198
199 g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), NULL);
200
201 g_mutex_lock (extension->priv->property_lock);
202
203 protected = e_source_weather_get_location (extension);
204 duplicate = g_strdup (protected);
205
206 g_mutex_unlock (extension->priv->property_lock);
207
208 return duplicate;
209 }
210
211 void
212 e_source_weather_set_location (ESourceWeather *extension,
213 const gchar *location)
214 {
215 gchar *new_location;
216
217 g_return_if_fail (E_IS_SOURCE_WEATHER (extension));
218
219 g_mutex_lock (extension->priv->property_lock);
220
221 new_location = e_util_strdup_strip (location);
222 if (g_strcmp0 (extension->priv->location, new_location) == 0) {
223 g_mutex_unlock (extension->priv->property_lock);
224 g_free (new_location);
225 return;
226 }
227
228 g_free (extension->priv->location);
229 extension->priv->location = new_location;
230
231 g_mutex_unlock (extension->priv->property_lock);
232
233 g_object_notify (G_OBJECT (extension), "location");
234 }
235
236 ESourceWeatherUnits
237 e_source_weather_get_units (ESourceWeather *extension)
238 {
239 g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), 0);
240
241 return extension->priv->units;
242 }
243
244 void
245 e_source_weather_set_units (ESourceWeather *extension,
246 ESourceWeatherUnits units)
247 {
248 g_return_if_fail (E_IS_SOURCE_WEATHER (extension));
249
250 if (extension->priv->units == units)
251 return;
252
253 extension->priv->units = units;
254
255 g_object_notify (G_OBJECT (extension), "units");
256 }
257
258 GType
259 e_source_weather_units_get_type (void)
260 {
261 return e_source_weather_units_type;
262 }