Index: applets/clock/ChangeLog
===================================================================
--- applets/clock/ChangeLog	(revision 10969)
+++ applets/clock/ChangeLog	(working copy)
@@ -1,3 +1,15 @@
+2008-03-31  Dan Winship  <danw@gnome.org>
+
+	* gweather-xml.c (gweather_xml_parse_node): handle the new
+	iso-code tag and put country codes into the tree model.
+	(find_weather_code): only return weather stations in the same
+	country as the timezone
+
+	* clock.c (find_timezone): if the weather station has a country
+	code associated with it, only return timezones that are also
+	located in that country. Helps fix some of the more egregious
+	parts of #519823.
+
 2008-03-28  Matthias Clasen  <mclasen@redhat.com>
 
 	* clock.c: Fix issues with adding/removing locations, pointed
Index: applets/clock/gweather-xml.c
===================================================================
--- applets/clock/gweather-xml.c	(revision 10969)
+++ applets/clock/gweather-xml.c	(working copy)
@@ -31,6 +31,7 @@
  *  <name xml:lang="zz">Another Translated Name</name>
  *  <country>
  *   <name>Name of the country</name>
+ *   <iso-code>2-letter ISO 3166 code for the country</iso-code>
  *   <location>
  *    <name>Name of the location</name>
  *    <code>IWIN code</code>
@@ -191,9 +192,9 @@
 gweather_xml_parse_node (GtkTreeStore *store, GtkTreeIter *parent,
                          xmlTextReaderPtr xml, 
                          const char *dflt_radar, const char *dflt_zone,
-                         const char *cityname)
+                         const char *dflt_iso_code, const char *cityname)
 {
-  char *name, *code, *zone, *radar, *coordinates;
+  char *name, *iso_code, *code, *zone, *radar, *coordinates;
   char **city, *nocity = NULL;
   GtkTreeIter iter, *self;
   gboolean is_location;
@@ -227,6 +228,7 @@
   radar = NULL;
   zone = NULL;
   code = NULL;
+  iso_code = dflt_iso_code ? (char *) xmlStrdup( (xmlChar *) dflt_iso_code ) : NULL;
   name = NULL;
 
   /* absorb the start tag */
@@ -255,7 +257,7 @@
     {
       /* recursively handle sub-sections */
       if( gweather_xml_parse_node( store, self, xml, 
-                                   radar, zone, *city ) )
+                                   radar, zone, dflt_iso_code, *city ) )
         goto error_out;
     }
     else if ( !strcmp( tagname, "name" ) )
@@ -264,6 +266,12 @@
       if( (name = gweather_xml_parse_name( xml )) == NULL )
         goto error_out;
     }
+    else if ( !strcmp( tagname, "iso-code" ) )
+    {
+      xmlFree( iso_code );
+      if( (iso_code = gweather_xml_get_value( xml )) == NULL )
+        goto error_out;
+    }
     else if ( !strcmp( tagname, "code" ) )
     {
       xmlFree( code );
@@ -299,7 +307,12 @@
   }
 
   if( self )
-    gtk_tree_store_set( store, self, GWEATHER_XML_COL_LOC, name, -1 );
+  {
+    gtk_tree_store_set( store, self,
+			GWEATHER_XML_COL_LOC, name, 
+			GWEATHER_XML_COL_ISO_CODE, iso_code, 
+			-1 );
+  }
 
   /* absorb the end tag.  in the case of processing a <gweather> then 'self'
      is NULL.  In this case, we let this fail since we might be at EOF */
@@ -392,7 +405,7 @@
   g_object_ref (store);
   gtk_tree_view_set_model (tree, NULL);
 
-  ret = gweather_xml_parse_node( store, NULL, xml, NULL, NULL, NULL );
+  ret = gweather_xml_parse_node( store, NULL, xml, NULL, NULL, NULL, NULL );
 
   gtk_tree_view_set_model (tree, GTK_TREE_MODEL (store));
   g_object_unref (store);
@@ -416,6 +429,7 @@
 
 typedef struct {
   const gchar *name;
+  const gchar *iso_code;
   gdouble latitude;
   gdouble longitude;
   gdouble distance;
@@ -440,12 +454,19 @@
 {
   SearchData *data = user_data;
   WeatherLocation *loc;
+  const gchar *iso_code;
   gdouble d;
    
-  gtk_tree_model_get (model, iter, GWEATHER_XML_COL_POINTER, &loc, -1);
+  gtk_tree_model_get (model, iter,
+		      GWEATHER_XML_COL_POINTER, &loc,
+		      GWEATHER_XML_COL_ISO_CODE, &iso_code,
+		      -1);
 
   if (!loc)
     return FALSE;
+  if (iso_code && data->iso_code &&
+      g_ascii_strcasecmp (iso_code, data->iso_code) != 0)
+    return FALSE;
 
   d = distance (data->latitude, data->longitude, loc->latitude, loc->longitude);
 
@@ -460,6 +481,7 @@
 gchar *
 find_weather_code (GtkTreeModel *model,
 		   const gchar  *name,
+		   const gchar  *iso_code,
                    gdouble       lat, 
                    gdouble       lon)
 {
@@ -467,6 +489,7 @@
   gchar *code;
 
   data.name = name;
+  data.iso_code = iso_code;
   data.latitude = lat;
   data.longitude = lon;
   data.distance = 1e6;
@@ -474,7 +497,7 @@
 
   gtk_tree_model_foreach (GTK_TREE_MODEL (model), compare_location, &data);
 
-  if (data.distance < 50)
+  if (data.location)
     code = g_strdup (data.location->code);
   else
     code = g_strdup ("-");
Index: applets/clock/gweather-xml.h
===================================================================
--- applets/clock/gweather-xml.h	(revision 10969)
+++ applets/clock/gweather-xml.h	(working copy)
@@ -26,12 +26,13 @@
 enum
 {
     GWEATHER_XML_COL_LOC = 0,
+    GWEATHER_XML_COL_ISO_CODE,
     GWEATHER_XML_COL_POINTER,
     GWEATHER_XML_NUM_COLUMNS
 }; 
 
 int gweather_xml_load_locations( GtkTreeView *tree);
-gchar *find_weather_code (GtkTreeModel *model, const gchar *name, gdouble lat, gdouble lon);
+gchar *find_weather_code (GtkTreeModel *model, const gchar *name, const gchar *iso_code, gdouble lat, gdouble lon);
 
 
 #endif /* __GWEATHER_XML_H__ */
Index: applets/clock/clock.c
===================================================================
--- applets/clock/clock.c	(revision 10969)
+++ applets/clock/clock.c	(working copy)
@@ -3188,19 +3188,28 @@
 }
 
 static gchar *
-find_timezone (ClockData *cd, const char *name, gfloat lat, gfloat lon)
+find_timezone (ClockData *cd, const char *name, const char *iso_code,
+               gfloat lat, gfloat lon)
 {
         ClockZoneTable *zonetab = cd->zones;
 	GList *zones, *l;
 	double dist, d;
 	gfloat zlat, zlon;
 	ClockZoneInfo *best;
+        const char *country;
 
 	dist = 1e6;
 	best = NULL;
 	zones = clock_zonetable_get_zones (zonetab);
 	for (l = zones; l; l = l->next) {
 		ClockZoneInfo *info = l->data;
+
+                if (iso_code) {
+                        country = clock_zoneinfo_get_country (info);
+                        if (!country || g_ascii_strcasecmp (iso_code, country) != 0)
+                                continue;
+                }
+
 		clock_zoneinfo_get_coords (info, &zlat, &zlon);
 
 		d = distance (lat, lon, zlat*M_PI/180.0, zlon*M_PI/180.0);
@@ -3211,13 +3220,15 @@
 		}
 	}
 
-	clock_zoneinfo_get_coords (best, &zlat, &zlon);
-
-	return g_strdup (clock_zoneinfo_get_name (best));
+        if (best)
+                return g_strdup (clock_zoneinfo_get_name (best));
+        else
+                return NULL;
 }
 
 static void
-update_timezone (ClockData *cd, const char *name, gfloat lat, gfloat lon)
+update_timezone (ClockData *cd, const char *name, const char *iso_code,
+                 gfloat lat, gfloat lon)
 {
         GtkWidget *zone_combo = glade_xml_get_widget (cd->glade_xml, "edit-location-timezone-combo");
 	gchar *timezone;
@@ -3227,7 +3238,10 @@
 	prefs.temperature_unit = cd->temperature_unit;
 	prefs.speed_unit = cd->speed_unit;
 
-	timezone = find_timezone (cd, name, lat, lon);
+	timezone = find_timezone (cd, name, iso_code, lat, lon);
+        if (!timezone)
+                return;
+
 	loc = clock_location_new (name, timezone, lat*180.0/M_PI, lon*180.0/M_PI, NULL, &prefs);
 
 	g_signal_handler_block (zone_combo, cd->zone_combo_changed);
@@ -3247,6 +3261,7 @@
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	WeatherLocation *loc = NULL;
+        const gchar *iso_code = NULL;
         GtkWidget *name_entry;
         GtkWidget *edit_window;
 
@@ -3254,7 +3269,10 @@
 	if (!gtk_tree_selection_get_selected (selection, &model, &iter))
 		return;
 
-	gtk_tree_model_get (model, &iter, GWEATHER_XML_COL_POINTER, &loc, -1);
+	gtk_tree_model_get (model, &iter,
+                            GWEATHER_XML_COL_POINTER, &loc,
+                            GWEATHER_XML_COL_ISO_CODE, &iso_code,
+                            -1);
 
 	if (!loc)
 		return;
@@ -3267,7 +3285,7 @@
 
 	update_coords (cd, loc->latlon_valid, loc->latitude*180.0/M_PI, loc->longitude*180.0/M_PI);
 
-	update_timezone (cd, loc->name, loc->latitude, loc->longitude);
+	update_timezone (cd, loc->name, iso_code, loc->latitude, loc->longitude);
 
 	find_hide (NULL, cd);
 }
@@ -3294,7 +3312,7 @@
 	if (gtk_tree_view_get_model (tree) != NULL)
 		return;
 
-	model = (GtkTreeModel*) gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
+	model = (GtkTreeModel*) gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
 	gtk_tree_view_set_model (tree, model);
 
 	cell = gtk_cell_renderer_text_new ();
@@ -3604,7 +3622,8 @@
 
 	fill_location_tree (cd);
 	model = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->location_tree));
-	weather_code = find_weather_code (model, city, lat * M_PI/180.0, lon * M_PI/180.0);
+	weather_code = find_weather_code (model, city, clock_zoneinfo_get_country (info),
+                                          lat * M_PI/180.0, lon * M_PI/180.0);
 	g_object_set_data_full (G_OBJECT (edit_window), "weather-code", weather_code, g_free);
 }
 
