Line 0
Link Here
|
|
|
1 |
--- panel-plugin/weather.c.orig 2018-09-16 22:12:20 UTC |
2 |
+++ panel-plugin/weather.c |
3 |
@@ -26,6 +26,9 @@ |
4 |
#include <libxfce4util/libxfce4util.h> |
5 |
#include <libxfce4ui/libxfce4ui.h> |
6 |
|
7 |
+#include <libxml/parser.h> |
8 |
+#include <libxml/tree.h> |
9 |
+ |
10 |
#include "weather-parsers.h" |
11 |
#include "weather-data.h" |
12 |
#include "weather.h" |
13 |
@@ -47,12 +50,6 @@ |
14 |
#define CONN_RETRY_INTERVAL_SMALL (10) |
15 |
#define CONN_RETRY_INTERVAL_LARGE (10 * 60) |
16 |
|
17 |
-/* met.no sunrise API returns data for up to 30 days in the future and |
18 |
- will return an error page if too many days are requested. Let's |
19 |
- play it safe and request fewer than that, since we can only get a |
20 |
- 10 days forecast too. */ |
21 |
-#define ASTRODATA_MAX_DAYS 25 |
22 |
- |
23 |
/* power saving update interval in seconds used as a precaution to |
24 |
deal with suspend/resume events etc., when nothing needs to be |
25 |
updated earlier: */ |
26 |
@@ -81,6 +78,7 @@ |
27 |
g_free(locname); \ |
28 |
g_free(lat); \ |
29 |
g_free(lon); \ |
30 |
+ g_free(offset); \ |
31 |
if (keyfile) \ |
32 |
g_key_file_free(keyfile); |
33 |
|
34 |
@@ -270,6 +268,19 @@ update_timezone(plugin_data *data) |
35 |
|
36 |
|
37 |
void |
38 |
+update_offset(plugin_data *data) |
39 |
+{ |
40 |
+ GDateTime *dt; |
41 |
+ |
42 |
+ dt = g_date_time_new_now_local(); |
43 |
+ if (G_LIKELY(data->offset)) |
44 |
+ g_free(data->offset); |
45 |
+ |
46 |
+ data->offset = g_date_time_format(dt, "%:z"); |
47 |
+} |
48 |
+ |
49 |
+ |
50 |
+void |
51 |
update_icon(plugin_data *data) |
52 |
{ |
53 |
GdkPixbuf *icon; |
54 |
@@ -481,7 +492,7 @@ cb_astro_update(SoupSession *session, |
55 |
{ |
56 |
plugin_data *data = user_data; |
57 |
xmlDoc *doc; |
58 |
- xmlNode *root_node; |
59 |
+ xmlNode *root_node, *child_node; |
60 |
time_t now_t; |
61 |
gboolean parsing_error = TRUE; |
62 |
|
63 |
@@ -492,13 +503,19 @@ cb_astro_update(SoupSession *session, |
64 |
doc = get_xml_document(msg); |
65 |
if (G_LIKELY(doc)) { |
66 |
root_node = xmlDocGetRootElement(doc); |
67 |
- if (G_LIKELY(root_node)) |
68 |
- if (parse_astrodata(root_node, data->astrodata)) { |
69 |
- /* schedule next update */ |
70 |
- data->astro_update->attempt = 0; |
71 |
- data->astro_update->last = now_t; |
72 |
- parsing_error = FALSE; |
73 |
+ if (G_LIKELY(root_node)) { |
74 |
+ for (child_node = root_node->children; child_node; |
75 |
+ child_node = child_node->next) { |
76 |
+ if (child_node->type == XML_ELEMENT_NODE) { |
77 |
+ if (parse_astrodata(child_node, data->astrodata)) { |
78 |
+ /* schedule next update */ |
79 |
+ data->astro_update->attempt = 0; |
80 |
+ data->astro_update->last = now_t; |
81 |
+ parsing_error = FALSE; |
82 |
+ } |
83 |
+ } |
84 |
} |
85 |
+ } |
86 |
xmlFreeDoc(doc); |
87 |
} |
88 |
if (parsing_error) |
89 |
@@ -580,8 +597,8 @@ update_handler(plugin_data *data) |
90 |
{ |
91 |
gchar *url; |
92 |
gboolean night_time; |
93 |
- time_t now_t, end_t; |
94 |
- struct tm now_tm, end_tm; |
95 |
+ time_t now_t; |
96 |
+ struct tm now_tm; |
97 |
|
98 |
g_assert(data != NULL); |
99 |
if (G_UNLIKELY(data == NULL)) |
100 |
@@ -616,26 +633,22 @@ update_handler(plugin_data *data) |
101 |
data->astro_update->next = time_calc_hour(now_tm, 1); |
102 |
data->astro_update->started = TRUE; |
103 |
|
104 |
- /* calculate date range for request */ |
105 |
- end_t = time_calc_day(now_tm, ASTRODATA_MAX_DAYS); |
106 |
- end_tm = *localtime(&end_t); |
107 |
- |
108 |
/* build url */ |
109 |
- url = g_strdup_printf("https://api.met.no/weatherapi/sunrise/1.1/?" |
110 |
- "lat=%s;lon=%s;" |
111 |
- "from=%04d-%02d-%02d;" |
112 |
- "to=%04d-%02d-%02d", |
113 |
+ url = g_strdup_printf("https://api.met.no/weatherapi" |
114 |
+ "/sunrise/2.0/?lat=%s&lon=%s&" |
115 |
+ "date=%04d-%02d-%02d&" |
116 |
+ "offset=%s&days=%u", |
117 |
data->lat, data->lon, |
118 |
now_tm.tm_year + 1900, |
119 |
now_tm.tm_mon + 1, |
120 |
now_tm.tm_mday, |
121 |
- end_tm.tm_year + 1900, |
122 |
- end_tm.tm_mon + 1, |
123 |
- end_tm.tm_mday); |
124 |
+ data->offset, |
125 |
+ data->forecast_days); |
126 |
|
127 |
/* start receive thread */ |
128 |
g_message(_("getting %s"), url); |
129 |
- weather_http_queue_request(data->session, url, cb_astro_update, data); |
130 |
+ weather_http_queue_request(data->session, url, |
131 |
+ cb_astro_update, data); |
132 |
g_free(url); |
133 |
} |
134 |
|
135 |
@@ -647,10 +660,10 @@ update_handler(plugin_data *data) |
136 |
data->weather_update->started = TRUE; |
137 |
|
138 |
/* build url */ |
139 |
- url = |
140 |
- g_strdup_printf("https://api.met.no/weatherapi" |
141 |
- "/locationforecastlts/1.3/?lat=%s;lon=%s;msl=%d", |
142 |
- data->lat, data->lon, data->msl); |
143 |
+ url = g_strdup_printf("https://api.met.no/weatherapi" |
144 |
+ "/locationforecastlts/1.3/?lat=%s&lon=%s&" |
145 |
+ "msl=%d", |
146 |
+ data->lat, data->lon, data->msl); |
147 |
|
148 |
/* start receive thread */ |
149 |
g_message(_("getting %s"), url); |
150 |
@@ -707,7 +720,7 @@ schedule_next_wakeup(plugin_data *data) |
151 |
|
152 |
next_day_t = day_at_midnight(now_t, 1); |
153 |
diff = difftime(next_day_t, now_t); |
154 |
- data->next_wakeup_reason = "current astro data update"; |
155 |
+ data->next_wakeup_reason = "current astro data update"; |
156 |
SCHEDULE_WAKEUP_COMPARE(data->astro_update->next, |
157 |
"astro data download"); |
158 |
SCHEDULE_WAKEUP_COMPARE(data->weather_update->next, |
159 |
@@ -853,6 +866,12 @@ xfceweather_read_config(XfcePanelPlugin *plugin, |
160 |
data->timezone = g_strdup(value); |
161 |
} |
162 |
|
163 |
+ value = xfce_rc_read_entry(rc, "offset", NULL); |
164 |
+ if (value) { |
165 |
+ g_free(data->offset); |
166 |
+ data->offset = g_strdup(value); |
167 |
+ } |
168 |
+ |
169 |
value = xfce_rc_read_entry(rc, "geonames_username", NULL); |
170 |
if (value) { |
171 |
g_free(data->geonames_username); |
172 |
@@ -975,6 +994,8 @@ xfceweather_write_config(XfcePanelPlugin *plugin, |
173 |
|
174 |
xfce_rc_write_entry(rc, "timezone", data->timezone); |
175 |
|
176 |
+ xfce_rc_write_entry(rc, "offset", data->offset); |
177 |
+ |
178 |
if (data->geonames_username) |
179 |
xfce_rc_write_entry(rc, "geonames_username", data->geonames_username); |
180 |
|
181 |
@@ -1076,6 +1097,7 @@ write_cache_file(plugin_data *data) |
182 |
CACHE_APPEND("location_name=%s\n", data->location_name); |
183 |
CACHE_APPEND("lat=%s\n", data->lat); |
184 |
CACHE_APPEND("lon=%s\n", data->lon); |
185 |
+ CACHE_APPEND("offset=%s\n", data->offset); |
186 |
g_string_append_printf(out, "msl=%d\n", data->msl); |
187 |
g_string_append_printf(out, "timeslices=%d\n", wd->timeslices->len); |
188 |
if (G_LIKELY(data->weather_update)) { |
189 |
@@ -1190,7 +1212,7 @@ read_cache_file(plugin_data *data) |
190 |
xml_location *loc = NULL; |
191 |
xml_astro *astro = NULL; |
192 |
time_t now_t = time(NULL), cache_date_t; |
193 |
- gchar *file, *locname = NULL, *lat = NULL, *lon = NULL, *group = NULL; |
194 |
+ gchar *file, *locname = NULL, *lat = NULL, *lon = NULL, *group = NULL, *offset = NULL; |
195 |
gchar *timestring; |
196 |
gint msl, num_timeslices = 0, i, j; |
197 |
|
198 |
@@ -1225,7 +1247,8 @@ read_cache_file(plugin_data *data) |
199 |
locname = g_key_file_get_string(keyfile, group, "location_name", NULL); |
200 |
lat = g_key_file_get_string(keyfile, group, "lat", NULL); |
201 |
lon = g_key_file_get_string(keyfile, group, "lon", NULL); |
202 |
- if (locname == NULL || lat == NULL || lon == NULL) { |
203 |
+ offset = g_key_file_get_string(keyfile, group, "offset", NULL); |
204 |
+ if (locname == NULL || lat == NULL || lon == NULL || offset == NULL) { |
205 |
CACHE_FREE_VARS(); |
206 |
weather_debug("Required values are missing in the cache file, " |
207 |
"reading cache file aborted."); |
208 |
@@ -1236,7 +1259,8 @@ read_cache_file(plugin_data *data) |
209 |
num_timeslices = g_key_file_get_integer(keyfile, group, |
210 |
"timeslices", &err); |
211 |
if (err || strcmp(lat, data->lat) || strcmp(lon, data->lon) || |
212 |
- msl != data->msl || num_timeslices < 1) { |
213 |
+ strcmp(offset, data->offset) || msl != data->msl || |
214 |
+ num_timeslices < 1) { |
215 |
CACHE_FREE_VARS(); |
216 |
weather_debug("The required values are not present in the cache file " |
217 |
"or do not match the current plugin data. Reading " |
218 |
@@ -1404,6 +1428,9 @@ update_weatherdata_with_reset(plugin_data *data) |
219 |
/* set location timezone */ |
220 |
update_timezone(data); |
221 |
|
222 |
+ /* set the offset of timezone */ |
223 |
+ update_offset(data); |
224 |
+ |
225 |
/* clear update times */ |
226 |
init_update_infos(data); |
227 |
|
228 |
@@ -1709,9 +1736,9 @@ weather_get_tooltip_text(const plugin_data *data) |
229 |
sunval = g_strdup(_("The sun never sets today.")); |
230 |
} else { |
231 |
sunrise = format_date(data->current_astro->sunrise, |
232 |
- "%H:%M:%S", TRUE); |
233 |
+ "%H:%M:%S", FALSE); |
234 |
sunset = format_date(data->current_astro->sunset, |
235 |
- "%H:%M:%S", TRUE); |
236 |
+ "%H:%M:%S", FALSE); |
237 |
sunval = |
238 |
g_strdup_printf(_("The sun rises at %s and sets at %s."), |
239 |
sunrise, sunset); |
240 |
@@ -1999,6 +2026,7 @@ xfceweather_free(XfcePanelPlugin *plugin, |
241 |
g_free(data->location_name); |
242 |
g_free(data->scrollbox_font); |
243 |
g_free(data->timezone); |
244 |
+ g_free(data->offset); |
245 |
g_free(data->timezone_initial); |
246 |
g_free(data->geonames_username); |
247 |
|
248 |
@@ -2170,6 +2198,7 @@ weather_construct(XfcePanelPlugin *plugin) |
249 |
|
250 |
xfceweather_read_config(plugin, data); |
251 |
update_timezone(data); |
252 |
+ update_offset(data); |
253 |
read_cache_file(data); |
254 |
update_current_conditions(data, TRUE); |
255 |
scrollbox_set_visible(data); |