[Xfce4-commits] <xfce4-weather-plugin:master> Fix auto location detection (bug #6208).
Nick Schermer
noreply at xfce.org
Wed Feb 2 21:26:02 CET 2011
Updating branch refs/heads/master
to 2c1769259226843dedd47e9f8b6696297111c005 (commit)
from 9dfefdc166d936c92e6c67fbcfaee1a27040e122 (commit)
commit 2c1769259226843dedd47e9f8b6696297111c005
Author: Nick Schermer <nick at xfce.org>
Date: Wed Feb 2 21:21:20 2011 +0100
Fix auto location detection (bug #6208).
Added our own geoip detection on http://archive.xfce.org/geolocation, since
the ipinfodb.com service stopped. Unfortunatly the server does not return
a content-length in the header (ssi scripting), so add a hack for that.
panel-plugin/weather-http.c | 60 ++++++++++++++++++++++++-----------------
panel-plugin/weather-search.c | 51 +++++++++++++++++++++++++---------
2 files changed, 72 insertions(+), 39 deletions(-)
diff --git a/panel-plugin/weather-http.c b/panel-plugin/weather-http.c
index 96758fa..1834343 100644
--- a/panel-plugin/weather-http.c
+++ b/panel-plugin/weather-http.c
@@ -149,7 +149,6 @@ weather_http_receive_data_idle (gpointer user_data)
gchar *request;
fd_set fds;
struct timeval select_timeout;
-
struct addrinfo h, *r, *a;
gchar *port = NULL;
gint err;
@@ -363,37 +362,48 @@ weather_http_receive_data_idle (gpointer user_data)
if (G_LIKELY (connection->received_len > 0))
{
/* get the pointer to the content-length */
+ int cts_len = -1;
p = strstr ((char *)connection->received, "Content-Length:");
-
if (G_LIKELY (p))
{
- int cts_len = 0;
/* advance the pointer */
p += strlen("Content-Length:");
cts_len = strtol (p, NULL, 10);
- if (G_UNLIKELY (cts_len < 0)) {
- g_warning(_("Negative content length"));
- /* set status */
- connection->status = STATUS_ERROR;
- } else {
- /* calculate the header length */
- n = connection->received_len - cts_len;
-
- if (G_LIKELY (n > 0))
- {
- /* erase the header from the reveiced string */
- void *tmp = g_malloc(cts_len+1);
- memcpy(tmp, connection->received+n, cts_len);
- ((gchar *)tmp)[cts_len] = 0;
- g_free(connection->received);
- connection->received = tmp;
- connection->received_len = cts_len;
- }
-
- /* everything went fine... */
- connection->status = STATUS_SUCCEED;
- }
+ if (G_UNLIKELY (cts_len < 0))
+ {
+ g_warning(_("Negative content length"));
+
+ /* set status */
+ connection->status = STATUS_ERROR;
+ }
+ }
+ else
+ {
+ /* hack for archive.xfce.org, which return no content-length */
+ p = strstr ((char *)connection->received, "<Response>");
+ if (G_LIKELY (p))
+ cts_len = connection->received_len - (p - connection->received);
+ }
+
+ if (cts_len > -1)
+ {
+ /* calculate the header length */
+ n = connection->received_len - cts_len;
+
+ if (G_LIKELY (n > 0))
+ {
+ /* erase the header from the reveiced string */
+ void *tmp = g_malloc(cts_len+1);
+ memcpy(tmp, connection->received+n, cts_len);
+ ((gchar *)tmp)[cts_len] = 0;
+ g_free(connection->received);
+ connection->received = tmp;
+ connection->received_len = cts_len;
+ }
+
+ /* everything went fine... */
+ connection->status = STATUS_SUCCEED;
}
else
{
diff --git a/panel-plugin/weather-search.c b/panel-plugin/weather-search.c
index d764116..04ea631 100644
--- a/panel-plugin/weather-search.c
+++ b/panel-plugin/weather-search.c
@@ -413,17 +413,28 @@ cb_geolocation (gboolean succeed,
xmlNode *cur_node;
gchar *city = NULL, *country = NULL;
gchar *country_code = NULL, *region = NULL;
+ gchar *full_loc;
+ gsize length;
+ gchar *p;
if (!succeed || received == NULL) {
data->cb(NULL, NULL, data->user_data);
g_free(data);
return;
}
+
+ /* hack for archive.xfce.org, which return no content-length */
+ p = strstr (received, "</Response>");
+ if (p != NULL)
+ length = p - received + strlen ("</Response>");
+ else
+ length = strlen (received);
+
if (g_utf8_validate(received, -1, NULL)) {
/* force parsing as UTF-8, the XML encoding header may lie */
- doc = xmlReadMemory (received, strlen (received), NULL, "UTF-8", 0);
+ doc = xmlReadMemory (received, length, NULL, "UTF-8", 0);
} else {
- doc = xmlParseMemory (received, strlen(received));
+ doc = xmlParseMemory (received, length);
}
g_free (received);
@@ -457,22 +468,32 @@ cb_geolocation (gboolean succeed,
}
}
}
+g_message ("%s %s %s %s", city, country, country_code, region);
+ if (country && city)
+ {
+ if (country_code && !strcmp (country_code, "US") && region)
+ full_loc = g_strdup_printf ("%s, %s", city, region);
+ else
+ full_loc = g_strdup_printf ("%s, %s", city, country);
+ }
+ else if (country)
+ {
+ full_loc = g_strdup (country);
+ }
+ else
+ {
+ full_loc = NULL;
+ }
- if (country_code && region && !strcmp(country_code, "US")) {
- g_free(country);
- country = region;
- region = NULL;
- }
- g_free(country_code);
- g_free(region);
+ g_free (country_code);
+ g_free (region);
+ g_free (country);
+ g_free (city);
xmlFreeDoc (doc);
- if (city && country) {
- gchar *full_loc = g_strdup_printf("%s, %s", city, country);
+ if (full_loc) {
gchar *url, *sane_str;
- g_free(city);
- g_free(country);
if ((sane_str = sanitize_str (full_loc)) == NULL) {
data->cb(NULL, NULL, data->user_data);
@@ -510,7 +531,9 @@ void weather_search_by_ip(
data->proxy_host = proxy_host;
data->proxy_port = proxy_port;
- weather_http_receive_data ("ipinfodb.com", "/ip_query.php",
+ /* archive.xfce.org is our download server that runs mod_geoip
+ * with the www.maxmind.com database */
+ weather_http_receive_data ("archive.xfce.org", "/geolocation",
proxy_host, proxy_port,
cb_geolocation, data);
return;
More information about the Xfce4-commits
mailing list