[Xfce4-commits] <xfce4-weather-plugin:master> GeoIP: Try to automatically determine system of measurement.

Harald Judt noreply at xfce.org
Wed Jul 18 17:06:01 CEST 2012


Updating branch refs/heads/master
         to a4657c4aa3077fe158413d03db68cc577935279d (commit)
       from 03ae60b9e13c8c17c59f885211799349e4d55583 (commit)

commit a4657c4aa3077fe158413d03db68cc577935279d
Author: Harald Judt <h.judt at gmx.at>
Date:   Wed Jul 18 17:00:48 2012 +0200

    GeoIP: Try to automatically determine system of measurement.
    
    Using the country code and a list of countries _officially_ using the
    imperial system to set the system of measurement automatically.
    Default to metric if no country code has been found or if the country
    is not in the list.

 panel-plugin/weather-config.c |    3 +-
 panel-plugin/weather-search.c |   45 +++++++++++++++++++++++++++++++++-------
 panel-plugin/weather-search.h |    5 ++-
 3 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/panel-plugin/weather-config.c b/panel-plugin/weather-config.c
index fdc576e..e196ebf 100644
--- a/panel-plugin/weather-config.c
+++ b/panel-plugin/weather-config.c
@@ -365,7 +365,7 @@ option_i (datas opt)
   return -1;
 }
 
-static void auto_locate_cb(const gchar *loc_name, const gchar *lat, const gchar *lon, gpointer user_data)
+static void auto_locate_cb(const gchar *loc_name, const gchar *lat, const gchar *lon, unit_systems unit_system, gpointer user_data)
 {
   xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
 
@@ -383,6 +383,7 @@ static void auto_locate_cb(const gchar *loc_name, const gchar *lat, const gchar
     gtk_entry_set_text (GTK_ENTRY (dialog->txt_loc_name), _("Unset"));
     gtk_widget_set_sensitive(dialog->txt_loc_name, FALSE);
   }
+  gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo_unit_system), unit_system);
 }
 
 static void start_auto_locate(xfceweather_dialog *dialog)
diff --git a/panel-plugin/weather-search.c b/panel-plugin/weather-search.c
index 83df627..ecf59d9 100644
--- a/panel-plugin/weather-search.c
+++ b/panel-plugin/weather-search.c
@@ -343,15 +343,38 @@ free_search_dialog (search_dialog * dialog)
   g_slice_free (search_dialog, dialog);
 }
 
+
+
 typedef struct {
   const gchar *proxy_host;
   gint proxy_port;
-  void (*cb)(const gchar *loc_name, const gchar *lat, const gchar *lon, gpointer user_data);
+  void (*cb)(const gchar *loc_name, const gchar *lat, const gchar *lon,
+             const unit_systems unit_system, gpointer user_data);
   gpointer user_data;
 }
 geolocation_data;
 
 
+
+static unit_systems
+get_preferred_unit_system (const gchar *country_code)
+{
+  /* List gathered from http://www.maxmind.com/app/iso3166 */
+  if (country_code == NULL)
+    return METRIC;
+  else if (!strcmp (country_code, "US"))  /* United States */
+    return IMPERIAL;
+  else if (!strcmp (country_code, "GB"))  /* United Kingdom */
+    return IMPERIAL;
+  else if (!strcmp (country_code, "LR"))  /* Liberia */
+    return IMPERIAL;
+  else if (!strcmp (country_code, "MM"))  /* Myanmar (Burma) */
+    return IMPERIAL;
+  return METRIC;
+}
+
+
+
 static void
 cb_geolocation (gboolean  succeed,
                 gchar    *received,
@@ -365,11 +388,12 @@ cb_geolocation (gboolean  succeed,
   gchar         *country_code = NULL, *region = NULL;
   gchar         *latitude = NULL, *longitude = NULL;
   gchar         *full_loc;
+  unit_systems   unit_system;
   gsize          length;
   gchar         *p;
 
   if (!succeed || received == NULL) {
-    data->cb(NULL, NULL, NULL, data->user_data);
+    data->cb(NULL, NULL, NULL, METRIC, data->user_data);
     g_free(data);
     return;
   }
@@ -390,7 +414,7 @@ cb_geolocation (gboolean  succeed,
   g_free (received);
 
   if (!doc) {
-    data->cb(NULL, NULL, NULL, data->user_data);
+    data->cb(NULL, NULL, NULL, METRIC, data->user_data);
     g_free(data);
     return;
   }
@@ -448,6 +472,8 @@ cb_geolocation (gboolean  succeed,
       full_loc = NULL;
     }
 
+  unit_system = get_preferred_unit_system(country_code);
+
   g_free (country_code);
   g_free (region);
   g_free (country);
@@ -455,7 +481,7 @@ cb_geolocation (gboolean  succeed,
 
   xmlFreeDoc (doc);
 
-  data->cb(full_loc, latitude, longitude, data->user_data);
+  data->cb(full_loc, latitude, longitude, unit_system, data->user_data);
   g_free (latitude);
   g_free (longitude);
   g_free (full_loc);
@@ -464,10 +490,13 @@ cb_geolocation (gboolean  succeed,
 
 
 
-void weather_search_by_ip(
-	const gchar *proxy_host, gint proxy_port,
-        void (*gui_cb)(const gchar *loc_name, const gchar *lat, const gchar *lon, gpointer user_data),
-	gpointer user_data)
+void weather_search_by_ip(const gchar *proxy_host, gint proxy_port,
+                          void (*gui_cb)(const gchar *loc_name,
+                                         const gchar *lat,
+                                         const gchar *lon,
+                                         const unit_systems unit_system,
+                                         gpointer user_data),
+                          gpointer user_data)
 {
   geolocation_data *data;
 
diff --git a/panel-plugin/weather-search.h b/panel-plugin/weather-search.h
index 83c40da..aba2e03 100644
--- a/panel-plugin/weather-search.h
+++ b/panel-plugin/weather-search.h
@@ -49,8 +49,9 @@ search_dialog *create_search_dialog (GtkWindow *, gchar *, gint);
 gboolean run_search_dialog (search_dialog * dialog);
 
 void weather_search_by_ip(const gchar *proxy_host, gint proxy_port,
-	void (*gui_cb)(const gchar *loc_name, const gchar *lat, const gchar *lon, gpointer user_data),
-	gpointer user_data);
+    void (*gui_cb)(const gchar *loc_name, const gchar *lat, const gchar *lon,
+                   const unit_systems unit_system, gpointer user_data),
+    gpointer user_data);
 
 void free_search_dialog (search_dialog * dialog);
 


More information about the Xfce4-commits mailing list