No issues found
1 /*
2 * evolution-cal-config-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 <http://www.gnu.org/licenses/>
16 *
17 */
18
19 #include <config.h>
20 #include <glib/gi18n-lib.h>
21
22 #include <libebackend/libebackend.h>
23
24 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
25 #include <libgweather/location-entry.h>
26 #undef GWEATHER_I_KNOW_THIS_IS_UNSTABLE
27
28 #include <misc/e-cal-source-config.h>
29 #include <misc/e-source-config-backend.h>
30
31 #include "e-source-weather.h"
32
33 typedef ESourceConfigBackend ECalConfigWeather;
34 typedef ESourceConfigBackendClass ECalConfigWeatherClass;
35
36 /* Module Entry Points */
37 void e_module_load (GTypeModule *type_module);
38 void e_module_unload (GTypeModule *type_module);
39
40 typedef struct _Context Context;
41
42 struct _Context {
43 GtkWidget *location_entry;
44 };
45
46 /* Forward Declarations */
47 GType e_cal_config_weather_get_type (void);
48
49 G_DEFINE_DYNAMIC_TYPE (
50 ECalConfigWeather,
51 e_cal_config_weather,
52 E_TYPE_SOURCE_CONFIG_BACKEND)
53
54 static void
55 cal_config_weather_context_free (Context *context)
56 {
57 g_object_unref (context->location_entry);
58
59 g_slice_free (Context, context);
60 }
61
62 static gboolean
63 cal_config_weather_location_to_string (GBinding *binding,
64 const GValue *source_value,
65 GValue *target_value,
66 gpointer user_data)
67 {
68 GWeatherLocation *location;
69 gchar *string = NULL;
70
71 location = g_value_get_boxed (source_value);
72
73 if (location != NULL) {
74 const gchar *code;
75 gchar *city_name;
76
77 code = gweather_location_get_code (location);
78 city_name = gweather_location_get_city_name (location);
79 string = g_strdup_printf ("%s/%s", code, city_name);
80 g_free (city_name);
81 }
82
83 g_value_take_string (target_value, string);
84
85 return TRUE;
86 }
87
88 static gboolean
89 cal_config_weather_string_to_location (GBinding *binding,
90 const GValue *source_value,
91 GValue *target_value,
92 gpointer user_data)
93 {
94 GWeatherLocation *world;
95 GWeatherLocation *match, *start;
96 const gchar *string;
97 const gchar *city_name;
98 const gchar *code;
99 gchar **tokens;
100
101 world = user_data;
102
103 string = g_value_get_string (source_value);
104
105 if (string == NULL)
106 return FALSE;
107
108 /* String is: STATION-CODE '/' CITY-NAME */
109 tokens = g_strsplit (string, "/", 2);
110
111 if (g_strv_length (tokens) != 2) {
112 g_strfreev (tokens);
113 return FALSE;
114 }
115
116 code = tokens[0];
117 city_name = tokens[1];
118
119 match = start = gweather_location_find_by_station_code (world, code);
120 while (match) {
121 gchar *cmp_city_name;
122
123 /* Does the city name match? */
124 cmp_city_name = gweather_location_get_city_name (match);
125 if (g_strcmp0 (city_name, cmp_city_name) == 0) {
126 g_free (cmp_city_name);
127 break;
128 }
129 g_free (cmp_city_name);
130
131 /* No match, try parent */
132 match = gweather_location_get_parent (match);
133 }
134
135 if (match == NULL) {
136 /* No exact match, use start instead */
137 match = start;
138 }
139
140 g_value_set_boxed (target_value, match);
141
142 g_strfreev (tokens);
143
144 return TRUE;
145 }
146
147 static gboolean
148 cal_config_weather_allow_creation (ESourceConfigBackend *backend)
149 {
150 ESourceConfig *config;
151 ECalSourceConfig *cal_config;
152 ECalClientSourceType source_type;
153
154 /* No such thing as weather task lists or weather memo lists. */
155
156 config = e_source_config_backend_get_config (backend);
157
158 cal_config = E_CAL_SOURCE_CONFIG (config);
159 source_type = e_cal_source_config_get_source_type (cal_config);
160
161 return (source_type == E_CAL_CLIENT_SOURCE_TYPE_EVENTS);
162 }
163
164 static void
165 cal_config_weather_insert_widgets (ESourceConfigBackend *backend,
166 ESource *scratch_source)
167 {
168 ESourceConfig *config;
169 ESourceExtension *extension;
170 GWeatherLocation *world;
171 GtkWidget *widget;
172 Context *context;
173 const gchar *extension_name;
174 const gchar *uid;
175
176 context = g_slice_new (Context);
177 uid = e_source_get_uid (scratch_source);
178 config = e_source_config_backend_get_config (backend);
179
180 g_object_set_data_full (
181 G_OBJECT (backend), uid, context,
182 (GDestroyNotify) cal_config_weather_context_free);
183
184 world = gweather_location_new_world (TRUE);
185
186 e_cal_source_config_add_offline_toggle (
187 E_CAL_SOURCE_CONFIG (config), scratch_source);
188
189 widget = gweather_location_entry_new (world);
190 e_source_config_insert_widget (
191 config, scratch_source, _("Location:"), widget);
192 context->location_entry = g_object_ref (widget);
193 gtk_widget_show (widget);
194
195 e_source_config_add_refresh_interval (config, scratch_source);
196
197 extension_name = E_SOURCE_EXTENSION_WEATHER_BACKEND;
198 extension = e_source_get_extension (scratch_source, extension_name);
199
200 g_object_bind_property_full (
201 extension, "location",
202 context->location_entry, "location",
203 G_BINDING_BIDIRECTIONAL |
204 G_BINDING_SYNC_CREATE,
205 cal_config_weather_string_to_location,
206 cal_config_weather_location_to_string,
207 gweather_location_ref (world),
208 (GDestroyNotify) gweather_location_unref);
209
210 gweather_location_unref (world);
211 }
212
213 static gboolean
214 cal_config_weather_check_complete (ESourceConfigBackend *backend,
215 ESource *scratch_source)
216 {
217 ESourceWeather *extension;
218 const gchar *extension_name;
219 const gchar *location;
220
221 extension_name = E_SOURCE_EXTENSION_WEATHER_BACKEND;
222 extension = e_source_get_extension (scratch_source, extension_name);
223
224 location = e_source_weather_get_location (extension);
225
226 g_debug ("Location: [%s]", location);
227
228 return (location != NULL) && (*location != '\0');
229 }
230
231 static void
232 e_cal_config_weather_class_init (ESourceConfigBackendClass *class)
233 {
234 EExtensionClass *extension_class;
235
236 extension_class = E_EXTENSION_CLASS (class);
237 extension_class->extensible_type = E_TYPE_CAL_SOURCE_CONFIG;
238
239 class->parent_uid = "weather-stub";
240 class->backend_name = "weather";
241 class->allow_creation = cal_config_weather_allow_creation;
242 class->insert_widgets = cal_config_weather_insert_widgets;
243 class->check_complete = cal_config_weather_check_complete;
244 }
245
246 static void
247 e_cal_config_weather_class_finalize (ESourceConfigBackendClass *class)
248 {
249 }
250
251 static void
252 e_cal_config_weather_init (ESourceConfigBackend *backend)
253 {
254 }
255
256 G_MODULE_EXPORT void
257 e_module_load (GTypeModule *type_module)
258 {
259 e_source_weather_type_register (type_module);
260 e_cal_config_weather_register_type (type_module);
261 }
262
263 G_MODULE_EXPORT void
264 e_module_unload (GTypeModule *type_module)
265 {
266 }