1 /*
2  * e-source-weather.c
3  *
4  * This library is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-data-server-config.h"
19 
20 #include "e-source-enumtypes.h"
21 #include "e-source-weather.h"
22 
23 struct _ESourceWeatherPrivate {
24 	ESourceWeatherUnits units;
25 	gchar *location;
26 };
27 
28 enum {
29 	PROP_0,
30 	PROP_LOCATION,
31 	PROP_UNITS
32 };
33 
G_DEFINE_TYPE_WITH_PRIVATE(ESourceWeather,e_source_weather,E_TYPE_SOURCE_EXTENSION)34 G_DEFINE_TYPE_WITH_PRIVATE (
35 	ESourceWeather,
36 	e_source_weather,
37 	E_TYPE_SOURCE_EXTENSION)
38 
39 static void
40 source_weather_set_property (GObject *object,
41                              guint property_id,
42                              const GValue *value,
43                              GParamSpec *pspec)
44 {
45 	switch (property_id) {
46 		case PROP_LOCATION:
47 			e_source_weather_set_location (
48 				E_SOURCE_WEATHER (object),
49 				g_value_get_string (value));
50 			return;
51 
52 		case PROP_UNITS:
53 			e_source_weather_set_units (
54 				E_SOURCE_WEATHER (object),
55 				g_value_get_enum (value));
56 			return;
57 	}
58 
59 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
60 }
61 
62 static void
source_weather_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)63 source_weather_get_property (GObject *object,
64                              guint property_id,
65                              GValue *value,
66                              GParamSpec *pspec)
67 {
68 	switch (property_id) {
69 		case PROP_LOCATION:
70 			g_value_take_string (
71 				value,
72 				e_source_weather_dup_location (
73 				E_SOURCE_WEATHER (object)));
74 			return;
75 
76 		case PROP_UNITS:
77 			g_value_set_enum (
78 				value,
79 				e_source_weather_get_units (
80 				E_SOURCE_WEATHER (object)));
81 			return;
82 	}
83 
84 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85 }
86 
87 static void
source_weather_finalize(GObject * object)88 source_weather_finalize (GObject *object)
89 {
90 	ESourceWeatherPrivate *priv;
91 
92 	priv = E_SOURCE_WEATHER (object)->priv;
93 
94 	g_free (priv->location);
95 
96 	/* Chain up to parent's finalize() method. */
97 	G_OBJECT_CLASS (e_source_weather_parent_class)->finalize (object);
98 }
99 
100 static void
e_source_weather_class_init(ESourceWeatherClass * class)101 e_source_weather_class_init (ESourceWeatherClass *class)
102 {
103 	GObjectClass *object_class;
104 	ESourceExtensionClass *extension_class;
105 
106 	object_class = G_OBJECT_CLASS (class);
107 	object_class->set_property = source_weather_set_property;
108 	object_class->get_property = source_weather_get_property;
109 	object_class->finalize = source_weather_finalize;
110 
111 	extension_class = E_SOURCE_EXTENSION_CLASS (class);
112 	extension_class->name = E_SOURCE_EXTENSION_WEATHER_BACKEND;
113 
114 	g_object_class_install_property (
115 		object_class,
116 		PROP_LOCATION,
117 		g_param_spec_string (
118 			"location",
119 			"Location",
120 			"Weather location code",
121 			NULL,
122 			G_PARAM_READWRITE |
123 			G_PARAM_CONSTRUCT |
124 			G_PARAM_EXPLICIT_NOTIFY |
125 			E_SOURCE_PARAM_SETTING));
126 
127 	g_object_class_install_property (
128 		object_class,
129 		PROP_UNITS,
130 		g_param_spec_enum (
131 			"units",
132 			"Units",
133 			"Fahrenheit, Centigrade or Kelvin units",
134 			E_TYPE_SOURCE_WEATHER_UNITS,
135 			E_SOURCE_WEATHER_UNITS_CENTIGRADE,
136 			G_PARAM_READWRITE |
137 			G_PARAM_CONSTRUCT |
138 			G_PARAM_EXPLICIT_NOTIFY |
139 			E_SOURCE_PARAM_SETTING));
140 }
141 
142 static void
e_source_weather_init(ESourceWeather * extension)143 e_source_weather_init (ESourceWeather *extension)
144 {
145 	extension->priv = e_source_weather_get_instance_private (extension);
146 }
147 
148 const gchar *
e_source_weather_get_location(ESourceWeather * extension)149 e_source_weather_get_location (ESourceWeather *extension)
150 {
151 	g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), NULL);
152 
153 	return extension->priv->location;
154 }
155 
156 gchar *
e_source_weather_dup_location(ESourceWeather * extension)157 e_source_weather_dup_location (ESourceWeather *extension)
158 {
159 	const gchar *protected;
160 	gchar *duplicate;
161 
162 	g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), NULL);
163 
164 	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));
165 
166 	protected = e_source_weather_get_location (extension);
167 	duplicate = g_strdup (protected);
168 
169 	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
170 
171 	return duplicate;
172 }
173 
174 void
e_source_weather_set_location(ESourceWeather * extension,const gchar * location)175 e_source_weather_set_location (ESourceWeather *extension,
176                                const gchar *location)
177 {
178 	g_return_if_fail (E_IS_SOURCE_WEATHER (extension));
179 
180 	e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));
181 
182 	if (e_util_strcmp0 (extension->priv->location, location) == 0) {
183 		e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
184 		return;
185 	}
186 
187 	g_free (extension->priv->location);
188 	extension->priv->location = e_util_strdup_strip (location);
189 
190 	e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
191 
192 	g_object_notify (G_OBJECT (extension), "location");
193 }
194 
195 ESourceWeatherUnits
e_source_weather_get_units(ESourceWeather * extension)196 e_source_weather_get_units (ESourceWeather *extension)
197 {
198 	g_return_val_if_fail (E_IS_SOURCE_WEATHER (extension), 0);
199 
200 	return extension->priv->units;
201 }
202 
203 void
e_source_weather_set_units(ESourceWeather * extension,ESourceWeatherUnits units)204 e_source_weather_set_units (ESourceWeather *extension,
205                             ESourceWeatherUnits units)
206 {
207 	g_return_if_fail (E_IS_SOURCE_WEATHER (extension));
208 
209 	if (extension->priv->units == units)
210 		return;
211 
212 	extension->priv->units = units;
213 
214 	g_object_notify (G_OBJECT (extension), "units");
215 }
216