diff options
Diffstat (limited to 'applets/clock/clock-location.c')
-rw-r--r-- | applets/clock/clock-location.c | 765 |
1 files changed, 310 insertions, 455 deletions
diff --git a/applets/clock/clock-location.c b/applets/clock/clock-location.c index 22ef32be..ce323235 100644 --- a/applets/clock/clock-location.c +++ b/applets/clock/clock-location.c @@ -22,123 +22,226 @@ #include "set-timezone.h" #include "system-timezone.h" +enum { + PROP_0, + PROP_LOCATION, + NUM_PROPERTIES +}; + +enum { + WEATHER_UPDATED, + SET_CURRENT, + LAST_SIGNAL +}; + typedef struct { - gchar *name; - gchar *city; + SystemTimezone *systz; + GWeatherLocation *glocation; + GWeatherInfo *gweather_info; + gulong network_id; + gulong weather_id; +} ClockLocationPrivate; - SystemTimezone *systz; +static ClockLocation *current_location = NULL; - gchar *timezone; +static guint location_signals[LAST_SIGNAL]; - gchar *tzname; +G_DEFINE_TYPE_WITH_PRIVATE (ClockLocation, clock_location, G_TYPE_OBJECT) - gfloat latitude; - gfloat longitude; +static void clock_location_dispose (GObject *object); +static void clock_location_finalize (GObject *); +static void clock_location_set_tz (ClockLocation *this); +static void clock_location_unset_tz (ClockLocation *this); +static void weather_info_updated (GWeatherInfo *info, gpointer data); - gchar *weather_code; - WeatherInfo *weather_info; - guint weather_timeout; - guint weather_retry_time; +static void clock_location_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static void clock_location_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); - TempUnit temperature_unit; - SpeedUnit speed_unit; -} ClockLocationPrivate; +static gint clock_location_compare (ClockLocation *one, ClockLocation *two) +{ + g_return_val_if_fail (CLOCK_IS_LOCATION(one), FALSE); + g_return_val_if_fail (CLOCK_IS_LOCATION(two), FALSE); + ClockLocationPrivate *priv_one; + ClockLocationPrivate *priv_two; -G_DEFINE_TYPE_WITH_PRIVATE (ClockLocation, clock_location, G_TYPE_OBJECT) + priv_one = clock_location_get_instance_private (one); + priv_two = clock_location_get_instance_private (two); -#define WEATHER_TIMEOUT_BASE 30 -#define WEATHER_TIMEOUT_MAX 1800 -#define WEATHER_EMPTY_CODE "-" + if (gweather_location_equal (priv_one->glocation, priv_two->glocation)) { + return 0; + } + return -1; +} -enum { - WEATHER_UPDATED, - SET_CURRENT, - LAST_SIGNAL -}; +gboolean clock_location_equal (ClockLocation *one, ClockLocation *two) +{ + if (clock_location_compare (one, two) == 0) + return TRUE; + else + return FALSE; +} -static guint location_signals[LAST_SIGNAL] = { 0 }; +gboolean clock_locations_has_location (GList *list, ClockLocation *loc) +{ + GList *found = NULL; + found = g_list_find_custom (list, loc, clock_location_compare); + if (found) { + return TRUE; + } else { + return FALSE; + } +} -static void clock_location_finalize (GObject *); -static void clock_location_set_tz (ClockLocation *this); -static void clock_location_unset_tz (ClockLocation *this); -static gboolean update_weather_info (gpointer data); -static void setup_weather_updates (ClockLocation *loc); - -static gchar *clock_location_get_valid_weather_code (const gchar *code); - -ClockLocation * -clock_location_find_and_ref (GList *locations, - const gchar *name, - const gchar *city, - const gchar *timezone, - gfloat latitude, - gfloat longitude, - const gchar *code) -{ - GList *l; - ClockLocationPrivate *priv; - - for (l = locations; l != NULL; l = l->next) { - priv = clock_location_get_instance_private (l->data); - - if (priv->latitude == latitude && - priv->longitude == longitude && - g_strcmp0 (priv->weather_code, code) == 0 && - g_strcmp0 (priv->timezone, timezone) == 0 && - g_strcmp0 (priv->city, city) == 0 && - g_strcmp0 (priv->name, name) == 0) - break; +GList* clock_locations_append (GList *locations, ClockLocation *loc) +{ + if (!clock_locations_has_location (locations, loc)) { + locations = g_list_append(locations, loc); + } + return locations; +} + +GList* clock_locations_merge (GList *old, GList *new) +{ + GList *l, *result = NULL; + + for (l = old; l != NULL; l = l->next) { + if (clock_locations_has_location (new, l->data)) { + result = g_list_append (result, g_object_ref (l->data)); } + } + g_list_free_full (old, g_object_unref); - if (l != NULL) - return g_object_ref (CLOCK_LOCATION (l->data)); - else - return NULL; + for (l = new; l != NULL; l = l->next) { + result = clock_locations_append (result, l->data); + } + return result; +} + +ClockLocation *clock_location_new (GWeatherLocation *gloc) +{ + return g_object_new (CLOCK_TYPE_LOCATION, "location", gloc, NULL); +} + +GVariant* clock_location_serialize (ClockLocation *loc) +{ + g_return_val_if_fail (CLOCK_IS_LOCATION(loc), NULL); + GVariant *value; + ClockLocationPrivate *priv; + + priv = clock_location_get_instance_private (loc); + + if (clock_location_is_current(loc)) { + value = g_variant_new ("(bv)", TRUE, gweather_location_serialize (priv->glocation)); + } else { + if (priv->glocation != NULL) { + value = g_variant_new ("(bv)", FALSE, gweather_location_serialize (priv->glocation)); + } + } + + return value; } -ClockLocation * -clock_location_new (const gchar *name, const gchar *city, - const gchar *timezone, - gfloat latitude, gfloat longitude, - const gchar *code, WeatherPrefs *prefs) +ClockLocation* clock_location_deserialize (GVariant *serialized) { - ClockLocation *this; - ClockLocationPrivate *priv; + gboolean current; + GVariant *value; + GWeatherLocation *world, *gloc; + ClockLocation *location; - this = g_object_new (CLOCK_LOCATION_TYPE, NULL); - priv = clock_location_get_instance_private (this); + g_variant_get (serialized, "(bv)", ¤t, &value); - priv->name = g_strdup (name); - priv->city = g_strdup (city); - priv->timezone = g_strdup (timezone); + world = gweather_location_get_world (); + gloc = gweather_location_deserialize (world, value); - /* initialize priv->tzname */ - clock_location_set_tz (this); - clock_location_unset_tz (this); + location = clock_location_new(gloc); + return location; +} - priv->latitude = latitude; - priv->longitude = longitude; +static void clock_location_set_gweather (ClockLocation *self, GWeatherLocation *loc) +{ + ClockLocationPrivate *priv; - priv->weather_code = clock_location_get_valid_weather_code (code); + priv = clock_location_get_instance_private (self); - if (prefs) { - priv->temperature_unit = prefs->temperature_unit; - priv->speed_unit = prefs->speed_unit; - } + if (priv->glocation != NULL && gweather_location_equal (priv->glocation, loc)) + return; + + if (priv->weather_id != 0) { + g_signal_handler_disconnect (priv->gweather_info, priv->weather_id); + priv->weather_id = 0; + } - setup_weather_updates (this); + if (priv->glocation != NULL) { + gweather_location_unref (priv->glocation); + priv->glocation = NULL; + } + if (priv->gweather_info != NULL) { + gweather_info_abort (priv->gweather_info); + g_object_unref (priv->gweather_info); + priv->gweather_info = NULL; + } - return this; + priv->glocation = gweather_location_ref (loc); + GWeatherProvider providers = GWEATHER_PROVIDER_METAR + | GWEATHER_PROVIDER_YAHOO + | GWEATHER_PROVIDER_YR_NO; + priv->gweather_info = g_object_new (GWEATHER_TYPE_INFO, "location", priv->glocation, "enabled-providers", providers, NULL); + priv->weather_id = g_signal_connect (priv->gweather_info, "updated", G_CALLBACK (weather_info_updated), self); } -static ClockLocation *current_location = NULL; +static void clock_location_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + ClockLocation *self; + + self = CLOCK_LOCATION (object); + + switch (prop_id) + { + case PROP_LOCATION: + clock_location_set_gweather (self, g_value_get_boxed (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void clock_location_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + ClockLocation *location; + ClockLocationPrivate *priv; + + location = CLOCK_LOCATION (object); + priv = clock_location_get_instance_private (location); + + switch (prop_id) + { + case PROP_LOCATION: + g_value_set_object (value, priv->glocation); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} static void clock_location_class_init (ClockLocationClass *this_class) { GObjectClass *g_obj_class = G_OBJECT_CLASS (this_class); + g_obj_class->dispose = clock_location_dispose; g_obj_class->finalize = clock_location_finalize; + g_obj_class->set_property = clock_location_set_property; + g_obj_class->get_property = clock_location_get_property; + + g_object_class_install_property (g_obj_class, + PROP_LOCATION, + g_param_spec_boxed ("location", + NULL, + NULL, + GWEATHER_TYPE_LOCATION, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); location_signals[WEATHER_UPDATED] = g_signal_new ("weather-updated", @@ -146,8 +249,8 @@ clock_location_class_init (ClockLocationClass *this_class) G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (ClockLocationClass, weather_updated), NULL, NULL, - g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER); + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, G_TYPE_OBJECT); location_signals[SET_CURRENT] = g_signal_new ("set-current", @@ -159,6 +262,14 @@ clock_location_class_init (ClockLocationClass *this_class) G_TYPE_NONE, 0); } +void clock_location_update_weather (ClockLocation *loc) +{ + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + + if (priv->gweather_info) + gweather_info_update (priv->gweather_info); +} + static void network_changed (GNetworkMonitor *monitor, gboolean available, @@ -167,35 +278,53 @@ network_changed (GNetworkMonitor *monitor, ClockLocationPrivate *priv = clock_location_get_instance_private (loc); if (available) { - priv->weather_retry_time = WEATHER_TIMEOUT_BASE; - update_weather_info (loc); + gweather_info_update (priv->gweather_info); } } static void clock_location_init (ClockLocation *this) { - ClockLocationPrivate *priv = clock_location_get_instance_private (this); - GNetworkMonitor *monitor; + ClockLocationPrivate *priv = clock_location_get_instance_private (this); + GNetworkMonitor *monitor; - priv->name = NULL; - priv->city = NULL; + priv->systz = system_timezone_new (); + priv->glocation = NULL; + priv->gweather_info = NULL; + priv->weather_id = 0; - priv->systz = system_timezone_new (); + monitor = g_network_monitor_get_default(); + priv->network_id = g_signal_connect (monitor, "network-changed", + G_CALLBACK (network_changed), this); +} - priv->timezone = NULL; +static void clock_location_dispose (GObject *object) +{ + ClockLocation *loc; + ClockLocationPrivate *priv; - priv->tzname = NULL; + loc = CLOCK_LOCATION (object); + priv = clock_location_get_instance_private (loc); - priv->latitude = 0; - priv->longitude = 0; + if (priv->weather_id != 0) { + g_signal_handler_disconnect (priv->gweather_info, priv->weather_id); + priv->weather_id = 0; + } - monitor = g_network_monitor_get_default(); - g_signal_connect (monitor, "network-changed", - G_CALLBACK (network_changed), this); + if (priv->systz != NULL) { + g_object_unref (priv->systz); + priv->systz = NULL; + } + if (priv->glocation != NULL) { + gweather_location_unref (priv->glocation); + priv->glocation = NULL; + } + if (priv->gweather_info != NULL) { + g_object_unref (priv->gweather_info); + priv->gweather_info = NULL; + } - priv->temperature_unit = TEMP_UNIT_CENTIGRADE; - priv->speed_unit = SPEED_UNIT_MS; + G_OBJECT_CLASS (clock_location_parent_class)->dispose (object); } static void @@ -209,46 +338,6 @@ clock_location_finalize (GObject *g_obj) G_CALLBACK (network_changed), CLOCK_LOCATION (g_obj)); - if (priv->name) { - g_free (priv->name); - priv->name = NULL; - } - - if (priv->city) { - g_free (priv->city); - priv->city = NULL; - } - - if (priv->systz) { - g_object_unref (priv->systz); - priv->systz = NULL; - } - - if (priv->timezone) { - g_free (priv->timezone); - priv->timezone = NULL; - } - - if (priv->tzname) { - g_free (priv->tzname); - priv->tzname = NULL; - } - - if (priv->weather_code) { - g_free (priv->weather_code); - priv->weather_code = NULL; - } - - if (priv->weather_info) { - weather_info_free (priv->weather_info); - priv->weather_info = NULL; - } - - if (priv->weather_timeout) { - g_source_remove (priv->weather_timeout); - priv->weather_timeout = 0; - } - G_OBJECT_CLASS (clock_location_parent_class)->finalize (g_obj); } @@ -256,32 +345,20 @@ const gchar * clock_location_get_display_name (ClockLocation *loc) { ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - if (priv->name && priv->name[0]) - return priv->name; - else - return priv->city; + return gweather_location_get_name (priv->glocation); } const gchar * clock_location_get_name (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - return priv->name; + return clock_location_get_display_name (loc); } -void -clock_location_set_name (ClockLocation *loc, const gchar *name) +const gchar * +clock_location_get_sort_name (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - if (priv->name) { - g_free (priv->name); - priv->name = NULL; - } - - priv->name = g_strdup (name); + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + return gweather_location_get_sort_name (priv->glocation); } const gchar * @@ -289,114 +366,65 @@ clock_location_get_city (ClockLocation *loc) { ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - return priv->city; -} - -void -clock_location_set_city (ClockLocation *loc, const gchar *city) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - if (priv->city) { - g_free (priv->city); - priv->city = NULL; - } - - priv->city = g_strdup (city); -} - -gchar * -clock_location_get_timezone (ClockLocation *loc) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - return priv->timezone; + return gweather_location_get_city_name (priv->glocation); } -void -clock_location_set_timezone (ClockLocation *loc, const gchar *timezone) +const gchar * +clock_location_get_tzname (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + ClockLocationPrivate *priv; + GWeatherTimezone *zone; + const gchar *name; - if (priv->timezone) { - g_free (priv->timezone); - priv->timezone = NULL; - } - - priv->timezone = g_strdup (timezone); + priv = clock_location_get_instance_private (loc); + zone = gweather_location_get_timezone (priv->glocation); + name = gweather_timezone_get_name (zone); + if (name != NULL) { + return name; + } else { + return gweather_timezone_get_tzid (zone); + } } -gchar * -clock_location_get_tzname (ClockLocation *loc) +const gchar * +clock_location_get_tzid (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + ClockLocationPrivate *priv; + GWeatherTimezone *zone; - return priv->tzname; + priv = clock_location_get_instance_private (loc); + zone = gweather_location_get_timezone (priv->glocation); + return gweather_timezone_get_tzid (zone); } void clock_location_get_coords (ClockLocation *loc, gfloat *latitude, gfloat *longitude) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + double lat, lon; - *latitude = priv->latitude; - *longitude = priv->longitude; -} + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + gweather_location_get_coords (priv->glocation, &lat, &lon); -void -clock_location_set_coords (ClockLocation *loc, gfloat latitude, - gfloat longitude) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - priv->latitude = latitude; - priv->longitude = longitude; -} - -static void -clock_location_set_tzname (ClockLocation *this, const char *tzname) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (CLOCK_LOCATION(this)); - - if (priv->tzname) { - if (strcmp (priv->tzname, tzname) == 0) { - return; - } - - g_free (priv->tzname); - priv->tzname = NULL; - } - - if (tzname) { - priv->tzname = g_strdup (tzname); - } else { - priv->tzname = NULL; - } + *latitude = (gfloat)lat; + *longitude = (gfloat)lon; + return; } static void clock_location_set_tz (ClockLocation *this) { - ClockLocationPrivate *priv = clock_location_get_instance_private (this); - + g_return_if_fail (CLOCK_IS_LOCATION(this)); + const gchar* zone; time_t now_t; struct tm now; - if (priv->timezone == NULL) { - return; - } - - setenv ("TZ", priv->timezone, 1); - tzset(); - - now_t = time (NULL); - localtime_r (&now_t, &now); - - if (now.tm_isdst > 0) { - clock_location_set_tzname (this, tzname[1]); - } else { - clock_location_set_tzname (this, tzname[0]); + zone = clock_location_get_tzid (this); + if (zone != NULL) { + setenv ("TZ", zone, 1); + tzset(); + now_t = time (NULL); + localtime_r (&now_t, &now); } } @@ -406,10 +434,6 @@ clock_location_unset_tz (ClockLocation *this) ClockLocationPrivate *priv = clock_location_get_instance_private (this); const char *env_timezone; - if (priv->timezone == NULL) { - return; - } - env_timezone = system_timezone_get_env (priv->systz); if (env_timezone) { @@ -436,24 +460,28 @@ clock_location_localtime (ClockLocation *loc, struct tm *tm) gboolean clock_location_is_current_timezone (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - const char *zone; + g_return_val_if_fail (CLOCK_IS_LOCATION(loc), FALSE); + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + const char *zone, *tzid; - zone = system_timezone_get (priv->systz); + zone = system_timezone_get (priv->systz); + tzid = clock_location_get_tzid (loc); - if (zone) - return strcmp (zone, priv->timezone) == 0; - else - return clock_location_get_offset (loc) == 0; + if (zone && tzid != NULL) { + return strcmp (zone, tzid) == 0; + } else { + return clock_location_get_offset (loc) == 0; + } } gboolean clock_location_is_current (ClockLocation *loc) { - if (current_location == loc) + if (current_location == loc) { return TRUE; - else if (current_location != NULL) + } else if (current_location != NULL) { return FALSE; + } if (clock_location_is_current_timezone (loc)) { /* Note that some code in clock.c depends on the fact that @@ -471,39 +499,18 @@ clock_location_is_current (ClockLocation *loc) return FALSE; } - -glong +gint clock_location_get_offset (ClockLocation *loc) { ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - glong sys_timezone, local_timezone; - glong offset; - time_t t; - struct tm *tm; - - t = time (NULL); + GWeatherTimezone *zone; - unsetenv ("TZ"); - tm = localtime (&t); - sys_timezone = timezone; - - if (tm->tm_isdst > 0) { - sys_timezone -= 3600; - } - - setenv ("TZ", priv->timezone, 1); - tm = localtime (&t); - local_timezone = timezone; - - if (tm->tm_isdst > 0) { - local_timezone -= 3600; + zone = gweather_location_get_timezone (priv->glocation); + if (zone != NULL) { + return gweather_timezone_get_offset (zone); + } else { + return 0; } - - offset = local_timezone - sys_timezone; - - clock_location_unset_tz (loc); - - return offset; } typedef struct { @@ -553,7 +560,6 @@ clock_location_make_current (ClockLocation *loc, gpointer data, GDestroyNotify destroy) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); gchar *filename; MakeCurrentData *mcdata; @@ -586,7 +592,7 @@ clock_location_make_current (ClockLocation *loc, mcdata->data = data; mcdata->destroy = destroy; - filename = g_build_filename (SYSTEM_ZONEINFODIR, priv->timezone, NULL); + filename = g_build_filename (SYSTEM_ZONEINFODIR, clock_location_get_tzid (loc), NULL); set_system_timezone_async (filename, (GFunc)make_current_cb, mcdata, @@ -594,186 +600,35 @@ clock_location_make_current (ClockLocation *loc, g_free (filename); } -static gchar * -clock_location_get_valid_weather_code (const gchar *code) -{ - if (!code || code[0] == '\0') - return g_strdup (WEATHER_EMPTY_CODE); - else - return g_strdup (code); -} - -const gchar * -clock_location_get_weather_code (ClockLocation *loc) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - return priv->weather_code; -} - -void -clock_location_set_weather_code (ClockLocation *loc, const gchar *code) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - g_free (priv->weather_code); - priv->weather_code = clock_location_get_valid_weather_code (code); - - setup_weather_updates (loc); -} - -WeatherInfo * +GWeatherInfo * clock_location_get_weather_info (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + g_return_val_if_fail (CLOCK_IS_LOCATION(loc), NULL); + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - return priv->weather_info; + return g_object_ref (priv->gweather_info); } -static void -set_weather_update_timeout (ClockLocation *loc) +GWeatherLocation* clock_location_get_glocation (ClockLocation *loc) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - guint timeout; - - if (!weather_info_network_error (priv->weather_info)) { - /* The last update succeeded; set the next update to - * happen in half an hour, and reset the retry timer. - */ - timeout = WEATHER_TIMEOUT_MAX; - priv->weather_retry_time = WEATHER_TIMEOUT_BASE; - } else { - /* The last update failed; set the next update - * according to the retry timer, and exponentially - * back off the retry timer. - */ - timeout = priv->weather_retry_time; - priv->weather_retry_time *= 2; - if (priv->weather_retry_time > WEATHER_TIMEOUT_MAX) - priv->weather_retry_time = WEATHER_TIMEOUT_MAX; - } + g_return_val_if_fail (CLOCK_IS_LOCATION(loc), NULL); - if (priv->weather_timeout) - g_source_remove (priv->weather_timeout); - priv->weather_timeout = - g_timeout_add_seconds (timeout, update_weather_info, loc); -} - -static void -weather_info_updated (WeatherInfo *info, gpointer data) -{ - ClockLocation *loc = data; - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - set_weather_update_timeout (loc); - g_signal_emit (loc, location_signals[WEATHER_UPDATED], - 0, priv->weather_info); -} - -static gboolean -update_weather_info (gpointer data) -{ - ClockLocation *loc = (ClockLocation *) data; - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - WeatherPrefs prefs = { - FORECAST_STATE, - FALSE, - NULL, - TEMP_UNIT_CENTIGRADE, - SPEED_UNIT_MS, - PRESSURE_UNIT_MB, - DISTANCE_UNIT_KM - }; - - // set temperature and speed units only if different from - // invalid/default - if (priv->temperature_unit > TEMP_UNIT_DEFAULT) - prefs.temperature_unit = priv->temperature_unit; - if (priv->speed_unit > SPEED_UNIT_DEFAULT) - prefs.speed_unit = priv->speed_unit; - - weather_info_abort (priv->weather_info); - weather_info_update (priv->weather_info, - &prefs, weather_info_updated, loc); - - return TRUE; -} - -static gchar * -rad2dms (gfloat lat, gfloat lon) -{ - gchar h, h2; - gfloat d, deg, min, d2, deg2, min2; - - h = lat > 0 ? 'N' : 'S'; - d = fabs (lat); - deg = floor (d); - min = floor (60 * (d - deg)); - h2 = lon > 0 ? 'E' : 'W'; - d2 = fabs (lon); - deg2 = floor (d2); - min2 = floor (60 * (d2 - deg2)); - return g_strdup_printf ("%02d-%02d%c %02d-%02d%c", - (int)deg, (int)min, h, - (int)deg2, (int)min2, h2); + return gweather_location_ref(priv->glocation); } static void -setup_weather_updates (ClockLocation *loc) -{ - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - WeatherLocation *wl; - WeatherPrefs prefs = { - FORECAST_STATE, - FALSE, - NULL, - TEMP_UNIT_CENTIGRADE, - SPEED_UNIT_MS, - PRESSURE_UNIT_MB, - DISTANCE_UNIT_KM - }; - - gchar *dms; - - prefs.temperature_unit = priv->temperature_unit; - prefs.speed_unit = priv->speed_unit; - - if (priv->weather_info) { - weather_info_free (priv->weather_info); - priv->weather_info = NULL; - } - - if (priv->weather_timeout) { - g_source_remove (priv->weather_timeout); - priv->weather_timeout = 0; - } - - if (!priv->weather_code || - strcmp (priv->weather_code, WEATHER_EMPTY_CODE) == 0) - return; - - dms = rad2dms (priv->latitude, priv->longitude); - wl = weather_location_new (priv->city, priv->weather_code, - NULL, NULL, dms, NULL, NULL); - - priv->weather_info = - weather_info_new (wl, &prefs, weather_info_updated, loc); - - set_weather_update_timeout (loc); - - weather_location_free (wl); - g_free (dms); -} - -void -clock_location_set_weather_prefs (ClockLocation *loc, - WeatherPrefs *prefs) +weather_info_updated (GWeatherInfo *info, gpointer data) { - ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - - priv->temperature_unit = prefs->temperature_unit; - priv->speed_unit = prefs->speed_unit; + g_return_if_fail (GWEATHER_IS_INFO(info)); + ClockLocation *loc = data; + g_return_if_fail (CLOCK_IS_LOCATION(loc)); + ClockLocationPrivate *priv = clock_location_get_instance_private (loc); - update_weather_info (loc); + if (gweather_info_network_error (priv->gweather_info)) + return; + if (priv->gweather_info) { + g_signal_emit (loc, location_signals[WEATHER_UPDATED], 0, priv->gweather_info); + } } - |