2 * position-publisher.c - Source for PositionPublisher
3 * Copyright (C) 2010 Guillaume Desmottes
4 * @author Guillaume Desmottes <gdesmott@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <telepathy-glib/dbus.h>
26 #include <telepathy-glib/interfaces.h>
28 #include <location/location-gps-device.h>
30 #include "connection-watcher.h"
31 #include "position-publisher.h"
33 G_DEFINE_TYPE(PositionPublisher, position_publisher, G_TYPE_OBJECT)
42 /* Minimum time before 2 publishing (in seconds) */
43 #define PUBLISH_THROTTLE 10
45 /* private structure */
46 typedef struct _PositionPublisherPrivate PositionPublisherPrivate;
48 struct _PositionPublisherPrivate
50 ConnectionWatcher *watcher;
51 LocationGPSDevice *gps_device;
52 /* List of (TpConnection *) supporting location publishing */
55 /* If not 0, we are waiting before publishing again */
56 guint throttle_timeout;
57 /* TRUE if location has been modified while we were waiting */
61 gboolean dispose_has_run;
64 #define POSITION_PUBLISHER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), POSITION_PUBLISHER_TYPE, PositionPublisherPrivate))
66 static void conn_invalidated_cb (TpProxy *conn,
70 PositionPublisher *self);
72 static void publish_to_all (PositionPublisher *self);
75 remove_connection (PositionPublisher *self,
78 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
80 g_signal_handlers_disconnect_by_func (conn, G_CALLBACK (conn_invalidated_cb),
82 priv->connections = g_slist_remove (priv->connections, conn);
83 g_object_unref (conn);
87 set_location_cb (TpConnection *conn,
92 PositionPublisher *self = POSITION_PUBLISHER (weak_object);
96 g_print ("SetLocation failed (%s): %s\n", tp_proxy_get_object_path (conn),
99 if (error->code == TP_ERROR_NOT_IMPLEMENTED)
101 g_print ("remove connection\n");
102 remove_connection (self, conn);
108 g_print ("SetLocation succeed (%s)\n", tp_proxy_get_object_path (conn));
112 publish_throttle_timeout_cb (gpointer data)
114 PositionPublisher *self = data;
115 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
117 priv->throttle_timeout = 0;
121 publish_to_all (self);
122 priv->modified = FALSE;
129 publish_to_conn (PositionPublisher *self,
132 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
134 if (priv->location == NULL)
137 tp_cli_connection_interface_location_call_set_location (conn, -1,
138 priv->location, set_location_cb, NULL, NULL, G_OBJECT (self));
142 publish_to_all (PositionPublisher *self)
144 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
147 if (priv->throttle_timeout != 0)
151 for (l = priv->connections; l != NULL; l = g_slist_next (l))
153 TpConnection *conn = l->data;
155 publish_to_conn (self, conn);
158 /* We won't publish during the next PUBLISH_THROTTLE seconds */
159 priv->throttle_timeout = g_timeout_add_seconds (PUBLISH_THROTTLE,
160 publish_throttle_timeout_cb, self);
164 update_position (PositionPublisher *self,
170 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
174 /* Truncate at 1 decimal place */
175 lon = ((int) (lon * 10)) / 10.0;
176 lat = ((int) (lat * 10)) / 10.0;
178 /* FIXME: change accuracy (not that easy as accuracy is in meters) */
181 g_print ("update position: lat: %f lon: %f alt: %f accuracy: %f\n",
182 lat, lon, alt, accuracy);
184 if (priv->location != NULL)
185 g_hash_table_unref (priv->location);
187 priv->location = tp_asv_new (
188 "timestamp", G_TYPE_INT64, (gint64) time (NULL),
189 "lat", G_TYPE_DOUBLE, lat,
190 "lon", G_TYPE_DOUBLE, lon,
191 "alt", G_TYPE_DOUBLE, alt,
192 "accuracy", G_TYPE_DOUBLE, accuracy,
195 priv->modified = TRUE;
197 publish_to_all (self);
201 location_changed_cb (LocationGPSDevice *device,
202 PositionPublisher *self)
207 if (device->fix == NULL)
210 if (!(device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET))
213 update_position (self, device->fix->latitude, device->fix->longitude,
214 device->fix->altitude, device->fix->eph / 100.0);
218 conn_invalidated_cb (TpProxy *conn,
222 PositionPublisher *self)
224 g_print ("connection %s invalidated; removing\n", tp_proxy_get_object_path (
227 remove_connection (self, TP_CONNECTION (conn));
231 connection_added_cb (ConnectionWatcher *watcher,
233 PositionPublisher *self)
235 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
237 if (g_slist_find (priv->connections, conn) != NULL)
240 if (!tp_proxy_has_interface_by_id (conn,
241 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION))
244 priv->connections = g_slist_prepend (priv->connections, g_object_ref (conn));
246 g_signal_connect (conn, "invalidated",
247 G_CALLBACK (conn_invalidated_cb), self);
249 publish_to_conn (self, conn);
253 position_publisher_init (PositionPublisher *obj)
255 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (obj);
257 priv->watcher = connection_watcher_new ();
259 g_signal_connect (priv->watcher, "connection-added",
260 G_CALLBACK (connection_added_cb), obj);
262 priv->gps_device = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
264 g_signal_connect (priv->gps_device, "changed",
265 G_CALLBACK (location_changed_cb), obj);
267 priv->connections = NULL;
271 position_publisher_constructed (GObject *object)
273 PositionPublisher *self = POSITION_PUBLISHER (object);
274 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
276 connection_watcher_start (priv->watcher);
278 if (G_OBJECT_CLASS (position_publisher_parent_class)->constructed)
279 G_OBJECT_CLASS (position_publisher_parent_class)->constructed (object);
282 static void position_publisher_dispose (GObject *object);
283 static void position_publisher_finalize (GObject *object);
284 static void position_publisher_get_property (GObject *object,
285 guint property_id, GValue *value, GParamSpec *pspec);
286 static void position_publisher_set_property (GObject *object,
287 guint property_id, const GValue *value, GParamSpec *pspec);
290 position_publisher_class_init (PositionPublisherClass *position_publisher_class)
292 GObjectClass *object_class = G_OBJECT_CLASS (position_publisher_class);
293 GParamSpec *param_spec;
295 g_type_class_add_private (position_publisher_class, sizeof (PositionPublisherPrivate));
297 object_class->dispose = position_publisher_dispose;
298 object_class->finalize = position_publisher_finalize;
299 object_class->get_property = position_publisher_get_property;
300 object_class->set_property = position_publisher_set_property;
302 object_class->constructed = position_publisher_constructed;
304 param_spec = g_param_spec_boolean ("blur", "Blur?",
305 "Whether the real GPS position is truncated for privacy concerns.", TRUE,
306 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE
307 | G_PARAM_STATIC_STRINGS);
308 g_object_class_install_property (object_class, PROP_BLUR, param_spec);
312 position_publisher_dispose (GObject *object)
314 PositionPublisher *self = POSITION_PUBLISHER (object);
315 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
318 if (priv->dispose_has_run)
321 priv->dispose_has_run = TRUE;
323 g_object_unref (priv->watcher);
324 g_object_unref (priv->gps_device);
326 for (l = priv->connections; l != NULL; l = g_slist_next (l))
328 g_object_unref (l->data);
331 g_hash_table_unref (priv->location);
333 if (priv->throttle_timeout != 0)
334 g_source_remove (priv->throttle_timeout);
336 if (G_OBJECT_CLASS (position_publisher_parent_class)->dispose)
337 G_OBJECT_CLASS (position_publisher_parent_class)->dispose (object);
341 position_publisher_finalize (GObject *object)
343 PositionPublisher *self = POSITION_PUBLISHER (object);
344 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
346 g_slist_free (priv->connections);
348 G_OBJECT_CLASS (position_publisher_parent_class)->finalize (object);
352 position_publisher_get_property (GObject *object,
357 PositionPublisher *self = POSITION_PUBLISHER (object);
358 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
360 switch (property_id) {
362 g_value_set_boolean (value, priv->blur);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
371 position_publisher_set_property (GObject *object,
376 PositionPublisher *self = POSITION_PUBLISHER (object);
377 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
379 switch (property_id) {
381 priv->blur = g_value_get_boolean (value);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
392 position_publisher_new (void)
394 return g_object_new (POSITION_PUBLISHER_TYPE,
399 position_publisher_set_blur (PositionPublisher *self,
402 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
404 g_print ("%s blurring location\n", blur ? "Start": "Stop");
406 g_object_notify (G_OBJECT (self), "blur");
410 position_publisher_has_connections (PositionPublisher *self)
412 PositionPublisherPrivate *priv = POSITION_PUBLISHER_GET_PRIVATE (self);
414 return g_slist_length (priv->connections) > 0;