From 4a032d74c12061db9201a2d3db3c1902a5468dc3 Mon Sep 17 00:00:00 2001 From: Kalev Lember Date: Thu, 12 Sep 2013 01:09:33 +0200 Subject: [PATCH 4/4] locator: Keep track of last notified location ... and only emit the LocationUpdated signal when we cross the distance threshold compared to the previously notified location. https://bugs.freedesktop.org/show_bug.cgi?id=69105 --- src/gclue-locator.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/gclue-locator.c b/src/gclue-locator.c index 35d8dc2..afd9590 100644 --- a/src/gclue-locator.c +++ b/src/gclue-locator.c @@ -34,6 +34,7 @@ struct _GClueLocatorPrivate GClueIpclient *ipclient; GClueLocationInfo *location; + GClueLocationInfo *last_notified_location; guint threshold; GCancellable *cancellable; @@ -64,6 +65,7 @@ gclue_locator_finalize (GObject *object) gclue_locator_stop_sync (GCLUE_LOCATOR (object)); g_clear_object (&priv->ipclient); g_clear_object (&priv->location); + g_clear_object (&priv->last_notified_location); G_OBJECT_CLASS (gclue_locator_parent_class)->finalize (object); } @@ -158,29 +160,40 @@ gclue_locator_new (void) static void gclue_locator_update_location (GClueLocator *locator, - GClueLocationInfo *location) + GClueLocationInfo *new_location) { GClueLocatorPrivate *priv = locator->priv; + gboolean notify_location = TRUE; gdouble distance; gdouble threshold_km; + g_debug ("Updating location"); + if (priv->location == NULL) - goto update; - - threshold_km = priv->threshold / 1000.0; - distance = gclue_location_info_get_distance_from (priv->location, - location); - if (gclue_location_info_equal (priv->location, location) && - distance < threshold_km) { - g_debug ("Location remains unchanged"); - return; + priv->location = g_object_new (GCLUE_TYPE_LOCATION_INFO, NULL); + + 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); + + if (priv->last_notified_location != NULL) { + threshold_km = priv->threshold / 1000.0; + distance = gclue_location_info_get_distance_from (priv->last_notified_location, + new_location); + if (distance < threshold_km) + notify_location = FALSE; } - g_object_unref (priv->location); -update: - g_debug ("Updating location"); - priv->location = g_object_ref (location); - g_object_notify (G_OBJECT (locator), "location"); + if (notify_location) { + g_clear_object (&priv->last_notified_location); + priv->last_notified_location = g_object_ref (new_location); + + g_debug ("Notifying location update"); + g_object_notify (G_OBJECT (locator), "location"); + } } static void -- 1.8.3.1