[Goodies-commits] r7609 - in xfce4-weather-plugin/trunk: . panel-plugin po
Colin Leroy
colin at xfce.org
Thu Jun 18 19:29:09 CEST 2009
Author: colin
Date: 2009-06-18 17:29:09 +0000 (Thu, 18 Jun 2009)
New Revision: 7609
Modified:
xfce4-weather-plugin/trunk/ChangeLog
xfce4-weather-plugin/trunk/panel-plugin/weather-config.c
xfce4-weather-plugin/trunk/panel-plugin/weather-http.c
xfce4-weather-plugin/trunk/panel-plugin/weather-search.c
xfce4-weather-plugin/trunk/panel-plugin/weather-search.h
xfce4-weather-plugin/trunk/po/xfce4-weather-plugin.pot
Log:
2009-06-18 Colin Leroy <colin at colino.net>
* panel-plugin/*: Use ipinfodb.com to automatically set the location at
the first startup of the plugin instance (when no location is set). This
should provide a more or less relevant location (but it can't be worse
than no location at all :-)
Modified: xfce4-weather-plugin/trunk/ChangeLog
===================================================================
--- xfce4-weather-plugin/trunk/ChangeLog 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/ChangeLog 2009-06-18 17:29:09 UTC (rev 7609)
@@ -1,3 +1,10 @@
+2009-06-18 Colin Leroy <colin at colino.net>
+
+ * panel-plugin/*: Use ipinfodb.com to automatically set the location at
+ the first startup of the plugin instance (when no location is set). This
+ should provide a more or less relevant location (but it can't be worse
+ than no location at all :-)
+
2009-06-17 Colin Leroy <colin at colino.net>
* panel-plugin/*: Some fixes to the location handling: better search
Modified: xfce4-weather-plugin/trunk/panel-plugin/weather-config.c
===================================================================
--- xfce4-weather-plugin/trunk/panel-plugin/weather-config.c 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/panel-plugin/weather-config.c 2009-06-18 17:29:09 UTC (rev 7609)
@@ -273,7 +273,31 @@
return -1;
}
+static void auto_locate_cb(const gchar *loc_name, const gchar *loc_code, gpointer user_data)
+{
+ xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
+
+ if (loc_code && loc_name) {
+ gtk_entry_set_text (GTK_ENTRY (dialog->txt_loc_code), loc_code);
+ gtk_label_set_text (GTK_LABEL (dialog->txt_loc_name), loc_name);
+ gtk_widget_set_sensitive(dialog->txt_loc_name, TRUE);
+#if GTK_CHECK_VERSION(2,12,0)
+ gtk_widget_set_tooltip_text(dialog->txt_loc_name,loc_name);
+#endif
+ } else {
+ gtk_entry_set_text (GTK_ENTRY (dialog->txt_loc_code), "");
+ gtk_label_set_text (GTK_LABEL (dialog->txt_loc_name), _("Unset"));
+ gtk_widget_set_sensitive(dialog->txt_loc_name, TRUE);
+ }
+}
+static void start_auto_locate(xfceweather_dialog *dialog)
+{
+ gtk_widget_set_sensitive(dialog->txt_loc_name, FALSE);
+ gtk_label_set_text (GTK_LABEL (dialog->txt_loc_name), _("Detecting..."));
+ weather_search_by_ip(dialog->wd->proxy_host, dialog->wd->proxy_port,
+ auto_locate_cb, dialog);
+}
static gboolean
cb_findlocation (GtkButton *button,
@@ -289,6 +313,7 @@
if (run_search_dialog (sdialog)) {
gtk_entry_set_text (GTK_ENTRY (dialog->txt_loc_code), sdialog->result);
gtk_label_set_text (GTK_LABEL (dialog->txt_loc_name), sdialog->result_name);
+ gtk_widget_set_sensitive(dialog->txt_loc_name, TRUE);
#if GTK_CHECK_VERSION(2,12,0)
gtk_widget_set_tooltip_text(dialog->txt_loc_name,sdialog->result_name);
#endif
@@ -376,6 +401,9 @@
gtk_label_get_text(GTK_LABEL(dialog->txt_loc_name)));
#endif
+ if (dialog->wd->location_code == NULL) {
+ start_auto_locate(dialog);
+ }
gtk_size_group_add_widget (sg, label);
button = gtk_button_new_with_label(_("Change..."));
Modified: xfce4-weather-plugin/trunk/panel-plugin/weather-http.c
===================================================================
--- xfce4-weather-plugin/trunk/panel-plugin/weather-http.c 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/panel-plugin/weather-http.c 2009-06-18 17:29:09 UTC (rev 7609)
@@ -32,6 +32,8 @@
#include <stdlib.h>
#include <netinet/in.h>
#include <resolv.h>
+#include <signal.h>
+#include <setjmp.h>
#include <glib.h>
#include <gtk/gtk.h>
@@ -140,6 +142,15 @@
#endif /*G_OS_UNIX*/
}
+#ifdef G_OS_UNIX
+static sigjmp_buf jmpenv;
+
+static void timeout_handler(gint sig)
+{
+ siglongjmp(jmpenv, 1);
+}
+#endif /*G_OS_UNIX*/
+
static gboolean
weather_http_receive_data_idle (gpointer user_data)
{
@@ -151,7 +162,18 @@
struct sockaddr_in sockaddr;
const gchar *p;
GTimeVal timeout;
+#ifdef G_OS_UNIX
+ void (*prev_handler)(gint);
+ alarm(0);
+ prev_handler = signal(SIGALRM, timeout_handler);
+ if (sigsetjmp(jmpenv, 1)) {
+ alarm(0);
+ signal(SIGALRM, prev_handler);
+ connection->status = STATUS_TIMEOUT;
+ return FALSE;
+ }
+#endif
/* set the current time */
g_get_current_time (&timeout);
@@ -159,7 +181,15 @@
refresh_resolvers();
/* try to get the hostname */
+#ifdef G_OS_UNIX
+ alarm(WEATHER_MAX_CONN_TIMEOUT);
+#endif
host = gethostbyname (connection->proxy_host ? connection->proxy_host : connection->hostname);
+#ifdef G_OS_UNIX
+ alarm(0);
+ signal(SIGALRM, prev_handler);
+#endif
+
if (G_UNLIKELY (host == NULL))
{
/* display error */
@@ -198,7 +228,15 @@
memset(&(sockaddr.sin_zero), '\0', 8);
/* open a connection with the host */
+#ifdef G_OS_UNIX
+ signal(SIGALRM, timeout_handler);
+ alarm(WEATHER_MAX_CONN_TIMEOUT);
+#endif
m = connect (connection->fd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));
+#ifdef G_OS_UNIX
+ alarm(0);
+ signal(SIGALRM, prev_handler);
+#endif
if (G_UNLIKELY (m < 0))
{
/* display warning */
Modified: xfce4-weather-plugin/trunk/panel-plugin/weather-search.c
===================================================================
--- xfce4-weather-plugin/trunk/panel-plugin/weather-search.c 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/panel-plugin/weather-search.c 2009-06-18 17:29:09 UTC (rev 7609)
@@ -81,7 +81,7 @@
xmlDoc *doc;
xmlNode *cur_node;
gchar *id, *city;
- gboolean found = FALSE;
+ gint found = 0;
GtkTreeIter iter;
GtkTreeSelection *selection;
@@ -123,7 +123,7 @@
}
append_result (dialog->result_mdl, id, city);
- found = TRUE;
+ found++;
g_free (id);
g_free (city);
}
@@ -132,13 +132,15 @@
xmlFreeDoc (doc);
- if (found) {
+ if (found > 0) {
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL (dialog->result_mdl), &iter)) {
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->result_list));
gtk_tree_selection_select_iter(selection, &iter);
gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog->dialog), GTK_RESPONSE_ACCEPT, TRUE);
}
}
+
+gtk_tree_view_column_set_title(dialog->column, _("Results"));
return;
}
@@ -179,6 +181,7 @@
url = g_strdup_printf ("/search/search?where=%s", sane_str);
g_free (sane_str);
+ gtk_tree_view_column_set_title(dialog->column, _("Searching..."));
weather_http_receive_data ("xoap.weather.com", url,
dialog->proxy_host, dialog->proxy_port,
cb_searchdone, dialog);
@@ -205,9 +208,9 @@
gint proxy_port)
{
GtkWidget *vbox, *hbox, *scroll, *frame;
- GtkTreeViewColumn *column;
GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
search_dialog *dialog;
+ GtkWidget *dialog_vbox, *dialog_action;
dialog = panel_slice_new0 (search_dialog);
@@ -228,11 +231,19 @@
gtk_window_set_icon_name (GTK_WINDOW (dialog->dialog), GTK_STOCK_FIND);
+#if GTK_CHECK_VERSION(2,14,0)
+ dialog_vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog->dialog));
+ dialog_action = gtk_dialog_get_action_area(GTK_DIALOG(dialog->dialog));
+#else
+ dialog_vbox = GTK_DIALOG (dialog->dialog)->vbox;
+ dialog_action = GTK_DIALOG (dialog->dialog)->action_area;
+#endif
+
vbox = gtk_vbox_new (FALSE, BORDER);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
- gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog->dialog)->vbox), vbox,
+ gtk_box_pack_start (GTK_BOX (dialog_vbox), vbox,
TRUE, TRUE, 0);
-
+
xfce_titled_dialog_set_subtitle (XFCE_TITLED_DIALOG (dialog->dialog),
_("Enter a city name or zip code"));
@@ -241,6 +252,7 @@
dialog->search_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (hbox), dialog->search_entry, TRUE, TRUE, 0);
+
g_signal_connect (G_OBJECT (dialog->search_entry), "activate",
G_CALLBACK (search_cb), dialog);
@@ -260,8 +272,8 @@
dialog->result_mdl = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
dialog->result_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (dialog->result_mdl));
- column = gtk_tree_view_column_new_with_attributes (_("Results"), renderer, "text", 0, NULL);
- gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->result_list), column);
+ dialog->column = gtk_tree_view_column_new_with_attributes (_("Results"), renderer, "text", 0, NULL);
+ gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->result_list), dialog->column);
g_signal_connect (G_OBJECT (dialog->result_list), "row-activated",
G_CALLBACK (pass_search_results), dialog->dialog);
gtk_container_add (GTK_CONTAINER (scroll), dialog->result_list);
@@ -307,8 +319,6 @@
return FALSE;
}
-
-
void
free_search_dialog (search_dialog * dialog)
{
@@ -320,3 +330,192 @@
panel_slice_free (search_dialog, dialog);
}
+
+typedef struct {
+ const gchar *proxy_host;
+ gint proxy_port;
+ void (*cb)(const gchar *loc_name, const gchar *loc_code, gpointer user_data);
+ gpointer user_data;
+}
+geolocation_data;
+
+static void
+cb_geo_searchdone (gboolean succeed,
+ gchar *received,
+ gpointer user_data)
+{
+ geolocation_data *data = (geolocation_data *) user_data;
+ xmlDoc *doc;
+ xmlNode *cur_node;
+ gchar *id, *city;
+ gboolean found = FALSE;
+ GtkTreeIter iter;
+ GtkTreeSelection *selection;
+
+ if (!succeed || received == NULL) {
+ data->cb(NULL, NULL, data->user_data);
+ g_free(data);
+ return;
+ }
+
+ 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);
+ } else {
+ doc = xmlParseMemory (received, strlen(received));
+ }
+ g_free (received);
+
+ if (!doc) {
+ data->cb(NULL, NULL, data->user_data);
+ g_free(data);
+ return;
+ }
+
+ cur_node = xmlDocGetRootElement (doc);
+
+ if (cur_node)
+ {
+ for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next)
+ {
+ if (NODE_IS_TYPE (cur_node, "loc"))
+ {
+ id = (gchar *) xmlGetProp (cur_node, (const xmlChar *) "id");
+
+ if (!id)
+ continue;
+
+ city = DATA (cur_node);
+
+ if (!city)
+ {
+ g_free (id);
+ continue;
+ }
+
+ data->cb(city, id, data->user_data);
+ g_free (id);
+ g_free (city);
+ break;
+ }
+ }
+ }
+
+ g_free(data);
+ xmlFreeDoc (doc);
+}
+
+static void
+cb_geolocation (gboolean succeed,
+ gchar *received,
+ gpointer user_data)
+{
+ geolocation_data *data = (geolocation_data *) user_data;
+ xmlDoc *doc;
+ xmlNode *cur_node;
+ gchar *city = NULL, *country = NULL;
+ gchar *country_code = NULL, *region = NULL;
+ gboolean found = FALSE;
+
+ if (!succeed || received == NULL) {
+ data->cb(NULL, NULL, data->user_data);
+ g_free(data);
+ return;
+ }
+ 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);
+ } else {
+ doc = xmlParseMemory (received, strlen(received));
+ }
+ g_free (received);
+
+ if (!doc) {
+ data->cb(NULL, NULL, data->user_data);
+ g_free(data);
+ return;
+ }
+
+ cur_node = xmlDocGetRootElement (doc);
+
+ if (cur_node)
+ {
+ for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next)
+ {
+ if (NODE_IS_TYPE (cur_node, "City"))
+ {
+ city = DATA (cur_node);
+ }
+ if (NODE_IS_TYPE (cur_node, "CountryName"))
+ {
+ country = DATA (cur_node);
+ }
+ if (NODE_IS_TYPE (cur_node, "CountryCode"))
+ {
+ country_code = DATA (cur_node);
+ }
+ if (NODE_IS_TYPE (cur_node, "RegionName"))
+ {
+ region = DATA (cur_node);
+ }
+ }
+ }
+
+ if (country_code && region && !strcmp(country_code, "US")) {
+ g_free(country);
+ country = region;
+ region = NULL;
+ }
+ g_free(country_code);
+ g_free(region);
+
+ xmlFreeDoc (doc);
+
+ if (city && country) {
+ gchar *full_loc = g_strdup_printf("%s, %s", city, country);
+ 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);
+ g_free(data);
+ return;
+ }
+ g_free(full_loc);
+
+ url = g_strdup_printf ("/search/search?where=%s", sane_str);
+ g_free (sane_str);
+
+ weather_http_receive_data ("xoap.weather.com", url,
+ data->proxy_host, data->proxy_port,
+ cb_geo_searchdone, data);
+ g_free(url);
+ }
+}
+
+
+
+gboolean weather_search_by_ip(
+ const gchar *proxy_host, gint proxy_port,
+ void (*gui_cb)(const gchar *loc_name, const gchar *loc_code, gpointer user_data),
+ gpointer user_data)
+{
+ gchar *city = NULL, *country = NULL;
+ gchar *search = NULL;
+ geolocation_data *data;
+
+ if (!gui_cb)
+ return;
+
+ data = g_new0(geolocation_data, 1);
+ data->cb = gui_cb;
+ data->user_data = user_data;
+ data->proxy_host = proxy_host;
+ data->proxy_port = proxy_port;
+
+ weather_http_receive_data ("ipinfodb.com", "/ip_query.php",
+ proxy_host, proxy_port,
+ cb_geolocation, data);
+
+}
Modified: xfce4-weather-plugin/trunk/panel-plugin/weather-search.h
===================================================================
--- xfce4-weather-plugin/trunk/panel-plugin/weather-search.h 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/panel-plugin/weather-search.h 2009-06-18 17:29:09 UTC (rev 7609)
@@ -33,6 +33,7 @@
GtkWidget *result_list;
GtkWidget *find_button;
GtkListStore *result_mdl;
+ GtkTreeViewColumn *column;
gchar *result;
gchar *result_name;
@@ -48,6 +49,10 @@
gboolean run_search_dialog (search_dialog * dialog);
+gboolean weather_search_by_ip(const gchar *proxy_host, gint proxy_port,
+ void (*gui_cb)(const gchar *loc_name, const gchar *loc_code, gpointer user_data),
+ gpointer user_data);
+
void free_search_dialog (search_dialog * dialog);
G_END_DECLS
Modified: xfce4-weather-plugin/trunk/po/xfce4-weather-plugin.pot
===================================================================
--- xfce4-weather-plugin/trunk/po/xfce4-weather-plugin.pot 2009-06-18 16:12:28 UTC (rev 7608)
+++ xfce4-weather-plugin/trunk/po/xfce4-weather-plugin.pot 2009-06-18 17:29:09 UTC (rev 7609)
@@ -8,7 +8,7 @@
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2009-06-17 20:13+0200\n"
+"POT-Creation-Date: 2009-06-18 19:26+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL at ADDRESS>\n"
"Language-Team: LANGUAGE <LL at li.org>\n"
@@ -123,44 +123,52 @@
msgid "Please enter proxy settings"
msgstr ""
-#: ../panel-plugin/weather-config.c:325
+#: ../panel-plugin/weather-config.c:289
+msgid "Unset"
+msgstr ""
+
+#: ../panel-plugin/weather-config.c:297
+msgid "Detecting..."
+msgstr ""
+
+#: ../panel-plugin/weather-config.c:350
msgid "Measurement unit:"
msgstr ""
-#: ../panel-plugin/weather-config.c:331
+#: ../panel-plugin/weather-config.c:356
msgid "Imperial"
msgstr ""
-#: ../panel-plugin/weather-config.c:333
+#: ../panel-plugin/weather-config.c:358
msgid "Metric"
msgstr ""
-#: ../panel-plugin/weather-config.c:348
+#: ../panel-plugin/weather-config.c:373
msgid "Location:"
msgstr ""
-#: ../panel-plugin/weather-config.c:381
+#: ../panel-plugin/weather-config.c:409
msgid "Change..."
msgstr ""
#. proxy
-#: ../panel-plugin/weather-config.c:394
+#: ../panel-plugin/weather-config.c:422
msgid "Proxy server:"
msgstr ""
-#: ../panel-plugin/weather-config.c:397
+#: ../panel-plugin/weather-config.c:425
msgid "Use proxy server"
msgstr ""
-#: ../panel-plugin/weather-config.c:399
+#: ../panel-plugin/weather-config.c:427
msgid "Auto-detect from environment"
msgstr ""
-#: ../panel-plugin/weather-config.c:471
+#: ../panel-plugin/weather-config.c:499
msgid "Labels to display"
msgstr ""
-#: ../panel-plugin/weather-config.c:520
+#: ../panel-plugin/weather-config.c:548
msgid "Animate transitions between labels"
msgstr ""
@@ -189,56 +197,60 @@
msgstr ""
#. display error
-#: ../panel-plugin/weather-http.c:166
+#: ../panel-plugin/weather-http.c:196
#, c-format
msgid "Failed to get the hostname. Retry in %d seconds."
msgstr ""
#. display warning
-#: ../panel-plugin/weather-http.c:183
+#: ../panel-plugin/weather-http.c:213
#, c-format
msgid "Failed to open the socket (%s)."
msgstr ""
#. display warning
-#: ../panel-plugin/weather-http.c:205
+#: ../panel-plugin/weather-http.c:243
#, c-format
msgid "Failed to create a connection with the host (%s)."
msgstr ""
#. display warning
-#: ../panel-plugin/weather-http.c:242
+#: ../panel-plugin/weather-http.c:280
#, c-format
msgid "Failed to send the request (%s)."
msgstr ""
#. display warning
-#: ../panel-plugin/weather-http.c:279
+#: ../panel-plugin/weather-http.c:317
#, c-format
msgid "Failed to receive data (%s)"
msgstr ""
#. display warning
-#: ../panel-plugin/weather-http.c:320
+#: ../panel-plugin/weather-http.c:358
msgid "Unable to detect the content length."
msgstr ""
-#: ../panel-plugin/weather-http.c:328
+#: ../panel-plugin/weather-http.c:366
msgid "No content received."
msgstr ""
-#: ../panel-plugin/weather-search.c:221
+#: ../panel-plugin/weather-search.c:143 ../panel-plugin/weather-search.c:275
+msgid "Results"
+msgstr ""
+
+#: ../panel-plugin/weather-search.c:184
+msgid "Searching..."
+msgstr ""
+
+#: ../panel-plugin/weather-search.c:224
msgid "Search weather location code"
msgstr ""
-#: ../panel-plugin/weather-search.c:237
+#: ../panel-plugin/weather-search.c:248
msgid "Enter a city name or zip code"
msgstr ""
-#: ../panel-plugin/weather-search.c:263
-msgid "Results"
-msgstr ""
-
#. head
#: ../panel-plugin/weather-summary.c:212
#, c-format
More information about the Goodies-commits
mailing list