NEWS: document changes in 0.2
[azimuth] / src / azimuth.c
1 /*
2  * azimuth.c - Source for Azimith
3  * Copyright (C) 2010 Guillaume Desmottes
4  * @author Guillaume Desmottes <gdesmott@gnome.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <gconf/gconf-client.h>
26 #include <telepathy-glib/util.h>
27
28 #include <location/location-gpsd-control.h>
29
30 #include "azimuth.h"
31 #include "azimuth-gconf.h"
32 #include "position-publisher.h"
33
34 G_DEFINE_TYPE(Azimuth, azimuth, G_TYPE_OBJECT)
35
36 /* private structure */
37 typedef struct _AzimuthPrivate AzimuthPrivate;
38
39 struct _AzimuthPrivate
40 {
41   GMainLoop *loop;
42   PositionPublisher *publisher;
43   GConfClient *gconf;
44   LocationGPSDControl *gps_control;
45 };
46
47 #define AZIMUTH_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), AZIMUTH_TYPE, AzimuthPrivate))
48
49 static void
50 update_gps (Azimuth *self)
51 {
52   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
53   gboolean start_gps;
54
55   start_gps = gconf_client_get_bool (priv->gconf,
56       AZIMUTH_GCONF_KEY_START_GPS, NULL);
57
58   if (priv->publisher != NULL && start_gps)
59     {
60       g_print ("starting GPS\n");
61       location_gpsd_control_start (priv->gps_control);
62     }
63   else
64     {
65       g_print ("stopping GPS\n");
66       location_gpsd_control_stop (priv->gps_control);
67     }
68 }
69
70 static void
71 update_blur (Azimuth *self)
72 {
73   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
74   gboolean blur;
75
76   if (priv->publisher == NULL)
77     return;
78
79   blur = gconf_client_get_bool (priv->gconf,
80       AZIMUTH_GCONF_KEY_BLUR, NULL);
81
82   position_publisher_set_blur (priv->publisher, blur);
83 }
84
85 static void
86 enabled_changed (Azimuth *self,
87     gboolean enabled)
88 {
89   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
90
91   if (enabled)
92     {
93       if (priv->publisher != NULL)
94         return;
95
96       g_print ("enable publishing\n");
97       priv->publisher = position_publisher_new ();
98
99       update_blur (self);
100     }
101   else
102     {
103       g_print ("disable publishing\n");
104       if (priv->publisher == NULL)
105         return;
106
107       g_object_unref (priv->publisher);
108       priv->publisher = NULL;
109     }
110
111   update_gps (self);
112 }
113
114 static void
115 gconf_notification_cb (GConfClient *client,
116     guint cnxn_id,
117     GConfEntry *entry,
118     gpointer user_data)
119 {
120   Azimuth *self = user_data;
121   const gchar *key = gconf_entry_get_key (entry);
122   GConfValue *value = gconf_entry_get_value (entry);
123
124   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
125       value->type == GCONF_VALUE_BOOL)
126     {
127       gboolean enabled = gconf_value_get_bool (value);
128
129       enabled_changed (self, enabled);
130     }
131
132   else if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_START_GPS) &&
133       value->type == GCONF_VALUE_BOOL)
134     {
135       update_gps (self);
136     }
137
138   else if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_BLUR) &&
139       value->type == GCONF_VALUE_BOOL)
140     {
141       update_blur (self);
142     }
143 }
144
145 static void
146 azimuth_init (Azimuth *self)
147 {
148   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
149
150   priv->loop = g_main_loop_new (NULL, FALSE);
151   priv->publisher = NULL;
152
153   priv->gconf = gconf_client_get_default ();
154
155   gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
156       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
157
158   gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
159       gconf_notification_cb, self, NULL, NULL);
160
161   /* GPS controller */
162   priv->gps_control = location_gpsd_control_get_default();
163
164   g_object_set (G_OBJECT(priv->gps_control),
165     "preferred-method", LOCATION_METHOD_USER_SELECTED,
166     "preferred-interval", LOCATION_INTERVAL_120S,
167     NULL);
168 }
169
170 static void
171 azimuth_dispose (GObject *object)
172 {
173   Azimuth *self = AZIMUTH (object);
174   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
175
176   if (priv->publisher != NULL)
177     {
178       g_object_unref (priv->publisher);
179       priv->publisher = NULL;
180     }
181
182   if (priv->loop != NULL)
183     {
184       g_main_loop_unref (priv->loop);
185       priv->loop = NULL;
186     }
187
188   if (priv->gconf != NULL)
189     {
190       g_object_unref (priv->gconf);
191       priv->gconf = NULL;
192     }
193
194   if (priv->gps_control != NULL)
195     {
196       location_gpsd_control_stop (priv->gps_control);
197       g_object_unref (priv->gps_control);
198       priv->gps_control = NULL;
199     }
200
201   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
202     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
203 }
204
205 static void
206 azimuth_class_init (AzimuthClass *azimuth_class)
207 {
208   GObjectClass *object_class = G_OBJECT_CLASS (azimuth_class);
209
210   g_type_class_add_private (azimuth_class, sizeof (AzimuthPrivate));
211
212   object_class->dispose = azimuth_dispose;
213 }
214
215 Azimuth *
216 azimuth_new (void)
217 {
218   return g_object_new (AZIMUTH_TYPE,
219       NULL);
220 }
221
222 void
223 azimuth_run (Azimuth *self)
224 {
225   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
226   gboolean enabled;
227
228   enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
229       NULL);
230   if (enabled)
231     {
232       g_print ("publishing is enabled\n");
233       g_assert (priv->publisher == NULL);
234       priv->publisher = position_publisher_new ();
235
236       update_gps (self);
237       update_blur (self);
238     }
239   else
240     {
241       g_print ("publishing is disabled\n");
242     }
243
244   g_print ("azimuth running\n");
245   g_main_loop_run (priv->loop);
246 }