Move GPS code to Azimuth
[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 enabled_changed (Azimuth *self,
72     gboolean enabled)
73 {
74   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
75
76   if (enabled)
77     {
78       if (priv->publisher != NULL)
79         return;
80
81       g_print ("enable publishing\n");
82       priv->publisher = position_publisher_new (TRUE);
83     }
84   else
85     {
86       g_print ("disable publishing\n");
87       if (priv->publisher == NULL)
88         return;
89
90       g_object_unref (priv->publisher);
91       priv->publisher = NULL;
92     }
93
94   update_gps (self);
95 }
96
97 static void
98 gconf_notification_cb (GConfClient *client,
99     guint cnxn_id,
100     GConfEntry *entry,
101     gpointer user_data)
102 {
103   Azimuth *self = user_data;
104   const gchar *key = gconf_entry_get_key (entry);
105   GConfValue *value = gconf_entry_get_value (entry);
106
107   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
108       value->type == GCONF_VALUE_BOOL)
109     {
110       gboolean enabled = gconf_value_get_bool (value);
111
112       enabled_changed (self, enabled);
113     }
114
115   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_START_GPS) &&
116       value->type == GCONF_VALUE_BOOL)
117     {
118       update_gps (self);
119     }
120 }
121
122 static void
123 azimuth_init (Azimuth *self)
124 {
125   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
126
127   priv->loop = g_main_loop_new (NULL, FALSE);
128   priv->publisher = NULL;
129
130   priv->gconf = gconf_client_get_default ();
131
132   gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
133       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
134
135   gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
136       gconf_notification_cb, self, NULL, NULL);
137
138   /* GPS controller */
139   priv->gps_control = location_gpsd_control_get_default();
140
141   g_object_set (G_OBJECT(priv->gps_control),
142     "preferred-method", LOCATION_METHOD_USER_SELECTED,
143     "preferred-interval", LOCATION_INTERVAL_120S,
144     NULL);
145 }
146
147 static void
148 azimuth_dispose (GObject *object)
149 {
150   Azimuth *self = AZIMUTH (object);
151   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
152
153   if (priv->publisher != NULL)
154     {
155       g_object_unref (priv->publisher);
156       priv->publisher = NULL;
157     }
158
159   if (priv->loop != NULL)
160     {
161       g_main_loop_unref (priv->loop);
162       priv->loop = NULL;
163     }
164
165   if (priv->gconf != NULL)
166     {
167       g_object_unref (priv->gconf);
168       priv->gconf = NULL;
169     }
170
171   if (priv->gps_control != NULL)
172     {
173       location_gpsd_control_stop (priv->gps_control);
174       g_object_unref (priv->gps_control);
175       priv->gps_control = NULL;
176     }
177
178   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
179     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
180 }
181
182 static void
183 azimuth_class_init (AzimuthClass *azimuth_class)
184 {
185   GObjectClass *object_class = G_OBJECT_CLASS (azimuth_class);
186
187   g_type_class_add_private (azimuth_class, sizeof (AzimuthPrivate));
188
189   object_class->dispose = azimuth_dispose;
190 }
191
192 Azimuth *
193 azimuth_new (void)
194 {
195   return g_object_new (AZIMUTH_TYPE,
196       NULL);
197 }
198
199 void
200 azimuth_run (Azimuth *self)
201 {
202   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
203   gboolean enabled;
204
205   enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
206       NULL);
207   if (enabled)
208     {
209       g_print ("publishing is enabled\n");
210       g_assert (priv->publisher == NULL);
211       priv->publisher = position_publisher_new (TRUE);
212
213       update_gps (self);
214     }
215   else
216     {
217       g_print ("publishing is disabled\n");
218     }
219
220   g_print ("azimuth running\n");
221   g_main_loop_run (priv->loop);
222 }