make use of the enabled gconf key
[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       g_print ("enable publishing\n");
55       if (priv->publisher != NULL)
56         return;
57
58       priv->publisher = position_publisher_new ();
59     }
60   else
61     {
62       g_print ("disable publishing\n");
63       if (priv->publisher == NULL)
64         return;
65
66       g_object_unref (priv->publisher);
67       priv->publisher = NULL;
68     }
69 }
70
71 static void
72 gconf_notification_cb (GConfClient *client,
73     guint cnxn_id,
74     GConfEntry *entry,
75     gpointer user_data)
76 {
77   Azimuth *self = user_data;
78   const gchar *key = gconf_entry_get_key (entry);
79   GConfValue *value = gconf_entry_get_value (entry);
80
81   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
82       value->type == GCONF_VALUE_BOOL)
83     {
84       gboolean enabled = gconf_value_get_bool (value);
85
86       enabled_changed (self, enabled);
87     }
88 }
89
90 static void
91 azimuth_init (Azimuth *self)
92 {
93   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
94
95   priv->loop = g_main_loop_new (NULL, FALSE);
96   priv->publisher = NULL;
97
98   priv->gconf = gconf_client_get_default ();
99
100   gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
101       GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
102
103   gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
104       gconf_notification_cb, self, NULL, NULL);
105 }
106
107 static void
108 azimuth_dispose (GObject *object)
109 {
110   Azimuth *self = AZIMUTH (object);
111   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
112
113   if (priv->publisher != NULL)
114     {
115       g_object_unref (priv->publisher);
116       priv->publisher = NULL;
117     }
118
119   if (priv->loop != NULL)
120     {
121       g_main_loop_unref (priv->loop);
122       priv->loop = NULL;
123     }
124
125   if (priv->gconf != NULL)
126     {
127       g_object_unref (priv->gconf);
128       priv->gconf = NULL;
129     }
130
131   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
132     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
133 }
134
135 static void
136 azimuth_class_init (AzimuthClass *azimuth_class)
137 {
138   GObjectClass *object_class = G_OBJECT_CLASS (azimuth_class);
139
140   g_type_class_add_private (azimuth_class, sizeof (AzimuthPrivate));
141
142   object_class->dispose = azimuth_dispose;
143 }
144
145 Azimuth *
146 azimuth_new (void)
147 {
148   return g_object_new (AZIMUTH_TYPE,
149       NULL);
150 }
151
152 void
153 azimuth_run (Azimuth *self)
154 {
155   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
156   gboolean enabled;
157
158   enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
159       NULL);
160   if (enabled)
161     {
162       g_print ("publishing is enabled\n");
163       g_assert (priv->publisher == NULL);
164       priv->publisher = position_publisher_new ();
165     }
166   else
167     {
168       g_print ("publishing is disabled\n");
169     }
170
171   g_print ("azimuth running\n");
172   g_main_loop_run (priv->loop);
173 }