From d38f2843104db1b448af64e5266d71f8c77799cf Mon Sep 17 00:00:00 2001 From: Kalev Lember Date: Sun, 15 Sep 2013 15:02:38 +0200 Subject: [PATCH 3/3] locator: Keep track of the distance travelled Previously, we would emit the LocationUpdated signal for any location property change, disregarding the DistanceThreshold value. A change in the "description" property would trigger the LocationUpdated signal, even if we were well below the threshold. This commit changes it to emit LocationUpdated only when we cross the threshold. We keep track of the distance travelled and while it's below the distance threshold, we update the location properties, but don't emit the signal. https://bugs.freedesktop.org/show_bug.cgi?id=69105 --- src/gclue-locator.c | 59 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/gclue-locator.c b/src/gclue-locator.c index 4a05682..705d5c4 100644 --- a/src/gclue-locator.c +++ b/src/gclue-locator.c @@ -34,6 +34,7 @@ struct _GClueLocatorPrivate GClueIpclient *ipclient; GClueLocationInfo *location; + gdouble distance_travelled; guint threshold; GCancellable *cancellable; @@ -156,37 +157,55 @@ gclue_locator_new (void) return g_object_new (GCLUE_TYPE_LOCATOR, NULL); } -static void -gclue_locator_update_location (GClueLocator *locator, - GClueLocationInfo *location) +static gboolean +below_threshold (GClueLocator *locator, + GClueLocationInfo *new_location) { GClueLocatorPrivate *priv = locator->priv; - gdouble accuracy, new_accuracy, distance; gdouble threshold_km; - const char *desc, *new_desc; - - if (priv->location == NULL) - goto update; - accuracy = gclue_location_info_get_accuracy (priv->location); - new_accuracy = gclue_location_info_get_accuracy (location); - desc = gclue_location_info_get_description (priv->location); - new_desc = gclue_location_info_get_description (location); + /* Return immediately if the threshold isn't set */ + if (priv->threshold == 0) + return FALSE; + priv->distance_travelled += gclue_location_info_get_distance_from (priv->location, + new_location); threshold_km = priv->threshold / 1000.0; - distance = gclue_location_info_get_distance_from (priv->location, - location); - if (accuracy == new_accuracy && - g_strcmp0 (desc, new_desc) == 0 && - distance < threshold_km) { + if (priv->distance_travelled > threshold_km) { + priv->distance_travelled = 0; + return FALSE; + } + + return TRUE; +} + +static void +gclue_locator_update_location (GClueLocator *locator, + GClueLocationInfo *new_location) +{ + GClueLocatorPrivate *priv = locator->priv; + + if (priv->location != NULL && gclue_location_info_equal (priv->location, + new_location)) { g_debug ("Location remain unchanged"); return; } - g_object_unref (priv->location); -update: + if (priv->location != NULL && below_threshold (locator, new_location)) { + g_debug ("Updating location, below threshold"); + g_object_set (priv->location, + "latitude", gclue_location_info_get_latitude (new_location), + "longitude", gclue_location_info_get_longitude (new_location), + "accuracy", gclue_location_info_get_accuracy (new_location), + "description", gclue_location_info_get_description (new_location), + NULL); + return; + } + + g_clear_object (&priv->location); + priv->location = g_object_ref (new_location); + g_debug ("Updating location"); - priv->location = g_object_ref (location); g_object_notify (G_OBJECT (locator), "location"); } -- 1.8.3.1