From 29dcc54ded4e0971ad244822abd1e31b887bf3d5 Mon Sep 17 00:00:00 2001 From: Jonas Danielsson Date: Tue, 3 Nov 2015 23:11:28 +0800 Subject: [PATCH] gclue-simple: Only notify on location change We only want to notify on the 'location' property if the location actually has changed. Otherwise the applications will perform their callback code to often. --- libgeoclue/gclue-simple.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/libgeoclue/gclue-simple.c b/libgeoclue/gclue-simple.c index 0915761..654fee9 100644 --- a/libgeoclue/gclue-simple.c +++ b/libgeoclue/gclue-simple.c @@ -43,6 +43,7 @@ */ #include +#include #include "gclue-simple.h" #include "gclue-helpers.h" @@ -221,6 +222,29 @@ gclue_simple_class_init (GClueSimpleClass *klass) gParamSpecs[PROP_LOCATION]); } +static gboolean +location_equals(GClueLocation *a, + GClueLocation *b) + +{ + gdouble epsilon = 0.0000001; + gdouble lat_a, lon_a, lat_b, lon_b; + + if (!a && !b) + return TRUE; + + if (!a || !b) + return FALSE; + + lat_a = gclue_location_get_latitude (a); + lon_a = gclue_location_get_longitude (a); + lat_b = gclue_location_get_latitude (b); + lon_b = gclue_location_get_longitude (b); + + return ((fabs (lat_a - lat_b) < epsilon) && + (fabs (lon_a - lon_b) < epsilon)); +} + static void on_location_proxy_ready (GObject *source_object, GAsyncResult *res, @@ -242,14 +266,16 @@ on_location_proxy_ready (GObject *source_object, return; } - g_clear_object (&priv->location); - priv->location = location; + + if (!priv->location || !location_equals (priv->location, location)) { + g_clear_object (&priv->location); + priv->location = location; + g_object_notify (G_OBJECT (user_data), "location"); + } if (priv->task != NULL) { g_task_return_boolean (priv->task, TRUE); g_clear_object (&priv->task); - } else { - g_object_notify (G_OBJECT (user_data), "location"); } } -- 2.5.0