57d3a76d2581ceaff4dd713ff0af31c064378e9e
[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 "azimuth.h"
29 #include "azimuth-gconf.h"
30 #include "position-publisher.h"
31
32 G_DEFINE_TYPE(Azimuth, azimuth, G_TYPE_OBJECT)
33
34 /* private structure */
35 typedef struct _AzimuthPrivate AzimuthPrivate;
36
37 struct _AzimuthPrivate
38 {
39   GMainLoop *loop;
40   PositionPublisher *publisher;
41   GConfClient *gconf;
42 };
43
44 #define AZIMUTH_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), AZIMUTH_TYPE, AzimuthPrivate))
45
46 static void
47 enabled_changed (Azimuth *self,
48     gboolean enabled)
49 {
50   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
51
52   if (enabled)
53     {
54       gboolean start_gps;
55       if (priv->publisher != NULL)
56         return;
57
58       start_gps = gconf_client_get_bool (priv->gconf,
59           AZIMUTH_GCONF_KEY_START_GPS, NULL);
60       g_print ("enable publishing (start gps: %s)\n",
61           start_gps ? "yes" : "no");
62       priv->publisher = position_publisher_new (TRUE, start_gps);
63     }
64   else
65     {
66       g_print ("disable publishing\n");
67       if (priv->publisher == NULL)
68         return;
69
70       g_object_unref (priv->publisher);
71       priv->publisher = NULL;
72     }
73 }
74
75 static void
76 start_gps_changed (Azimuth *self,
77     gboolean start_gps)
78 {
79   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
80
81   if (priv->publisher)
82     {
83       g_print ("%s GPS\n", start_gps ? "Start" : "Stop");
84       g_object_set (priv->publisher, "start-gps", start_gps, NULL);
85     }
86 }
87
88 static void
89 gconf_notification_cb (GConfClient *client,
90     guint cnxn_id,
91     GConfEntry *entry,
92     gpointer user_data)
93 {
94   Azimuth *self = user_data;
95   const gchar *key = gconf_entry_get_key (entry);
96   GConfValue *value = gconf_entry_get_value (entry);
97
98   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
99       value->type == GCONF_VALUE_BOOL)
100     {
101       gboolean enabled = gconf_value_get_bool (value);
102
103       enabled_changed (self, enabled);
104     }
105
106   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_START_GPS) &&
107       value->type == GCONF_VALUE_BOOL)
108     {
109       gboolean start_gps = gconf_value_get_bool (value);
110
111       start_gps_changed (self, start_gps);
112     }
113 }
114
115 static void
116 azimuth_init (Azimuth *self)
117 {
118   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
119
120   priv->loop = g_main_loop_new (NULL, FALSE);
121   priv->publisher = NULL;
122
123   priv->gconf = gconf_client_get_default ();
124
125   gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
126       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
127
128   gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
129       gconf_notification_cb, self, NULL, NULL);
130 }
131
132 static void
133 azimuth_dispose (GObject *object)
134 {
135   Azimuth *self = AZIMUTH (object);
136   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
137
138   if (priv->publisher != NULL)
139     {
140       g_object_unref (priv->publisher);
141       priv->publisher = NULL;
142     }
143
144   if (priv->loop != NULL)
145     {
146       g_main_loop_unref (priv->loop);
147       priv->loop = NULL;
148     }
149
150   if (priv->gconf != NULL)
151     {
152       g_object_unref (priv->gconf);
153       priv->gconf = NULL;
154     }
155
156   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
157     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
158 }
159
160 static void
161 azimuth_class_init (AzimuthClass *azimuth_class)
162 {
163   GObjectClass *object_class = G_OBJECT_CLASS (azimuth_class);
164
165   g_type_class_add_private (azimuth_class, sizeof (AzimuthPrivate));
166
167   object_class->dispose = azimuth_dispose;
168 }
169
170 Azimuth *
171 azimuth_new (void)
172 {
173   return g_object_new (AZIMUTH_TYPE,
174       NULL);
175 }
176
177 void
178 azimuth_run (Azimuth *self)
179 {
180   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
181   gboolean enabled;
182   gboolean start_gps;
183
184   enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
185       NULL);
186   start_gps = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_START_GPS,
187       NULL);
188   if (enabled)
189     {
190       g_print ("publishing is enabled (start gps: %s)\n",
191           start_gps ? "yes" : "no");
192       g_assert (priv->publisher == NULL);
193       priv->publisher = position_publisher_new (TRUE, start_gps);
194     }
195   else
196     {
197       g_print ("publishing is disabled\n");
198     }
199
200   g_print ("azimuth running\n");
201   g_main_loop_run (priv->loop);
202 }