2 * azimuth.c - Source for Azimith
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 <gconf/gconf-client.h>
26 #include <telepathy-glib/util.h>
28 #include <location/location-gpsd-control.h>
31 #include "azimuth-gconf.h"
32 #include "position-publisher.h"
34 G_DEFINE_TYPE(Azimuth, azimuth, G_TYPE_OBJECT)
36 /* private structure */
37 typedef struct _AzimuthPrivate AzimuthPrivate;
39 struct _AzimuthPrivate
42 PositionPublisher *publisher;
44 LocationGPSDControl *gps_control;
47 #define AZIMUTH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), AZIMUTH_TYPE, AzimuthPrivate))
50 update_gps (Azimuth *self)
52 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
55 start_gps = gconf_client_get_bool (priv->gconf,
56 AZIMUTH_GCONF_KEY_START_GPS, NULL);
58 if (priv->publisher != NULL &&
60 position_publisher_has_connections (priv->publisher))
62 g_print ("starting GPS\n");
63 location_gpsd_control_start (priv->gps_control);
67 g_print ("stopping GPS\n");
68 location_gpsd_control_stop (priv->gps_control);
73 update_blur (Azimuth *self)
75 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
78 if (priv->publisher == NULL)
81 blur = gconf_client_get_bool (priv->gconf,
82 AZIMUTH_GCONF_KEY_BLUR, NULL);
84 position_publisher_set_blur (priv->publisher, blur);
88 has_connections_changed_cb (PositionPublisher *publisher,
89 gboolean has_connections,
96 create_publisher (Azimuth *self)
98 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
100 priv->publisher = position_publisher_new ();
102 g_signal_connect (priv->publisher, "has-connections-changed",
103 G_CALLBACK (has_connections_changed_cb), self);
107 enabled_changed (Azimuth *self,
110 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
114 if (priv->publisher != NULL)
117 g_print ("enable publishing\n");
118 create_publisher (self);
124 g_print ("disable publishing\n");
125 if (priv->publisher == NULL)
128 g_object_unref (priv->publisher);
129 priv->publisher = NULL;
136 gconf_notification_cb (GConfClient *client,
141 Azimuth *self = user_data;
142 const gchar *key = gconf_entry_get_key (entry);
143 GConfValue *value = gconf_entry_get_value (entry);
145 if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
146 value->type == GCONF_VALUE_BOOL)
148 gboolean enabled = gconf_value_get_bool (value);
150 enabled_changed (self, enabled);
153 else if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_START_GPS) &&
154 value->type == GCONF_VALUE_BOOL)
159 else if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_BLUR) &&
160 value->type == GCONF_VALUE_BOOL)
167 azimuth_init (Azimuth *self)
169 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
171 priv->loop = g_main_loop_new (NULL, FALSE);
172 priv->publisher = NULL;
174 priv->gconf = gconf_client_get_default ();
176 gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
177 GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
179 gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
180 gconf_notification_cb, self, NULL, NULL);
183 priv->gps_control = location_gpsd_control_get_default();
185 g_object_set (G_OBJECT(priv->gps_control),
186 "preferred-method", LOCATION_METHOD_USER_SELECTED,
187 "preferred-interval", LOCATION_INTERVAL_120S,
192 azimuth_dispose (GObject *object)
194 Azimuth *self = AZIMUTH (object);
195 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
197 if (priv->publisher != NULL)
199 g_object_unref (priv->publisher);
200 priv->publisher = NULL;
203 if (priv->loop != NULL)
205 g_main_loop_unref (priv->loop);
209 if (priv->gconf != NULL)
211 g_object_unref (priv->gconf);
215 if (priv->gps_control != NULL)
217 location_gpsd_control_stop (priv->gps_control);
218 g_object_unref (priv->gps_control);
219 priv->gps_control = NULL;
222 if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
223 G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
227 azimuth_class_init (AzimuthClass *azimuth_class)
229 GObjectClass *object_class = G_OBJECT_CLASS (azimuth_class);
231 g_type_class_add_private (azimuth_class, sizeof (AzimuthPrivate));
233 object_class->dispose = azimuth_dispose;
239 return g_object_new (AZIMUTH_TYPE,
244 azimuth_run (Azimuth *self)
246 AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
249 enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
253 g_print ("publishing is enabled\n");
254 g_assert (priv->publisher == NULL);
255 create_publisher (self);
262 g_print ("publishing is disabled\n");
265 g_print ("azimuth running\n");
266 g_main_loop_run (priv->loop);