make use of the enabled gconf key
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Sun, 2 May 2010 14:46:14 +0000 (16:46 +0200)
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Sun, 2 May 2010 18:19:20 +0000 (20:19 +0200)
src/azimuth.c

index 4176c5d..bc3445a 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <gconf/gconf-client.h>
+#include <telepathy-glib/util.h>
+
 #include "azimuth.h"
+#include "azimuth-gconf.h"
 #include "position-publisher.h"
 
 G_DEFINE_TYPE(Azimuth, azimuth, G_TYPE_OBJECT)
@@ -34,17 +38,70 @@ struct _AzimuthPrivate
 {
   GMainLoop *loop;
   PositionPublisher *publisher;
+  GConfClient *gconf;
 };
 
 #define AZIMUTH_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), AZIMUTH_TYPE, AzimuthPrivate))
 
 static void
-azimuth_init (Azimuth *obj)
+enabled_changed (Azimuth *self,
+    gboolean enabled)
+{
+  AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
+
+  if (enabled)
+    {
+      g_print ("enable publishing\n");
+      if (priv->publisher != NULL)
+        return;
+
+      priv->publisher = position_publisher_new ();
+    }
+  else
+    {
+      g_print ("disable publishing\n");
+      if (priv->publisher == NULL)
+        return;
+
+      g_object_unref (priv->publisher);
+      priv->publisher = NULL;
+    }
+}
+
+static void
+gconf_notification_cb (GConfClient *client,
+    guint cnxn_id,
+    GConfEntry *entry,
+    gpointer user_data)
 {
-  AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (obj);
+  Azimuth *self = user_data;
+  const gchar *key = gconf_entry_get_key (entry);
+  GConfValue *value = gconf_entry_get_value (entry);
+
+  if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_ENABLED) &&
+      value->type == GCONF_VALUE_BOOL)
+    {
+      gboolean enabled = gconf_value_get_bool (value);
+
+      enabled_changed (self, enabled);
+    }
+}
+
+static void
+azimuth_init (Azimuth *self)
+{
+  AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
 
   priv->loop = g_main_loop_new (NULL, FALSE);
   priv->publisher = NULL;
+
+  priv->gconf = gconf_client_get_default ();
+
+  gconf_client_add_dir (priv->gconf, AZIMUTH_GCONF_SECTION,
+      GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
+
+  gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
+      gconf_notification_cb, self, NULL, NULL);
 }
 
 static void
@@ -65,6 +122,12 @@ azimuth_dispose (GObject *object)
       priv->loop = NULL;
     }
 
+  if (priv->gconf != NULL)
+    {
+      g_object_unref (priv->gconf);
+      priv->gconf = NULL;
+    }
+
   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
 }
@@ -90,9 +153,20 @@ void
 azimuth_run (Azimuth *self)
 {
   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
+  gboolean enabled;
 
-  g_assert (priv->publisher == NULL);
-  priv->publisher = position_publisher_new ();
+  enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
+      NULL);
+  if (enabled)
+    {
+      g_print ("publishing is enabled\n");
+      g_assert (priv->publisher == NULL);
+      priv->publisher = position_publisher_new ();
+    }
+  else
+    {
+      g_print ("publishing is disabled\n");
+    }
 
   g_print ("azimuth running\n");
   g_main_loop_run (priv->loop);