]> git.0d.be Git - empathy.git/commitdiff
empathy-connectivity: add signals, properties and hook in NM
authorJonny Lamb <jonny.lamb@collabora.co.uk>
Tue, 18 Aug 2009 17:23:54 +0000 (18:23 +0100)
committerJonny Lamb <jonny.lamb@collabora.co.uk>
Tue, 18 Aug 2009 17:23:54 +0000 (18:23 +0100)
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
libempathy/empathy-connectivity.c
libempathy/empathy-connectivity.h

index 264b58d068f98af87504cfeb2fcb0f786f3e97e8..47d02989e6dbbde17c8e7ee17d62543dfcf88202 100644 (file)
  */
 
 #include "config.h"
+#include "empathy-connectivity.h"
 
-#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
+#ifdef HAVE_NM
+#include <nm-client.h>
+#endif
+
+#include "empathy-utils.h"
+#include "empathy-marshal.h"
+
+#define DEBUG_FLAG EMPATHY_DEBUG_CONNECTIVITY
+#include "empathy-debug.h"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConnectivity)
 
 typedef struct {
+#ifdef HAVE_NM
+  NMClient *nm_client;
+#endif
+
+  gboolean connected;
+  gboolean use_conn;
   gboolean dispose_run;
 } EmpathyConnectivityPriv;
 
@@ -35,11 +49,47 @@ enum {
   LAST_SIGNAL
 };
 
+enum {
+  PROP_0,
+  PROP_USE_CONN,
+};
+
 static guint signals[LAST_SIGNAL];
 static EmpathyConnectivity *connectivity_singleton = NULL;
 
 G_DEFINE_TYPE (EmpathyConnectivity, empathy_connectivity, G_TYPE_OBJECT);
 
+#ifdef HAVE_NM
+static void
+connectivity_nm_state_change_cb (NMClient *client,
+    const GParamSpec *pspec,
+    EmpathyConnectivity *connectivity)
+{
+  EmpathyConnectivityPriv *priv;
+  gboolean old_nm_connected;
+  gboolean new_nm_connected;
+  NMState state;
+
+  priv = GET_PRIV (connectivity);
+
+  if (!priv->use_conn)
+    return;
+
+  state = nm_client_get_state (priv->nm_client);
+  old_nm_connected = priv->connected;
+  new_nm_connected = state == NM_STATE_CONNECTED;
+  new_nm_connected = !(state == NM_STATE_CONNECTING
+      || state == NM_STATE_DISCONNECTED);
+
+  DEBUG ("New NetworkManager network state %d", state);
+
+  priv->connected = new_nm_connected;
+
+  g_signal_emit (connectivity, signals[STATE_CHANGE], 0,
+      old_nm_connected, new_nm_connected);
+}
+#endif
+
 static void
 empathy_connectivity_init (EmpathyConnectivity *connectivity)
 {
@@ -50,21 +100,40 @@ empathy_connectivity_init (EmpathyConnectivity *connectivity)
 
   connectivity->priv = priv;
   priv->dispose_run = FALSE;
+
+#ifdef HAVE_NM
+  priv->nm_client = nm_client_new ();
+  if (priv->nm_client != NULL)
+    {
+      g_signal_connect (priv->nm_client, "notify::" NM_CLIENT_STATE,
+          G_CALLBACK (connectivity_nm_state_change_cb), connectivity);
+    }
+  else
+    {
+      DEBUG ("Failed to get NetworkManager proxy");
+    }
+#endif
 }
 
 static void
 connectivity_finalize (GObject *object)
 {
-  EmpathyConnectivity *manager = EMPATHY_CONNECTIVITY (obj);
+  EmpathyConnectivity *manager = EMPATHY_CONNECTIVITY (object);
   EmpathyConnectivityPriv *priv = GET_PRIV (manager);
 
-  G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (obj);
+  if (priv->nm_client != NULL)
+    {
+      g_object_unref (priv->nm_client);
+      priv->nm_client = NULL;
+    }
+
+  G_OBJECT_CLASS (empathy_connectivity_parent_class)->finalize (object);
 }
 
 static void
 connectivity_dispose (GObject *object)
 {
-  EmpathyConnectivity *manager = EMPATHY_CONNECTIVITY (obj);
+  EmpathyConnectivity *manager = EMPATHY_CONNECTIVITY (object);
   EmpathyConnectivityPriv *priv = GET_PRIV (manager);
 
   if (priv->dispose_run)
@@ -72,7 +141,7 @@ connectivity_dispose (GObject *object)
 
   priv->dispose_run = TRUE;
 
-  G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (obj);
+  G_OBJECT_CLASS (empathy_connectivity_parent_class)->dispose (object);
 }
 
 static GObject *
@@ -82,22 +151,62 @@ connectivity_constructor (GType type,
 {
   GObject *retval;
 
-  if (!manager_singleton)
+  if (!connectivity_singleton)
     {
       retval = G_OBJECT_CLASS (empathy_connectivity_parent_class)->constructor
         (type, n_construct_params, construct_params);
 
-      manager_singleton = EMPATHY_CONNECTIVITY (retval);
-      g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
+      connectivity_singleton = EMPATHY_CONNECTIVITY (retval);
+      g_object_add_weak_pointer (retval, (gpointer) &connectivity_singleton);
     }
   else
     {
-      retval = g_object_ref (manager_singleton);
+      retval = g_object_ref (connectivity_singleton);
     }
 
   return retval;
 }
 
+static void
+connectivity_get_property (GObject *object,
+    guint param_id,
+    GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
+
+  switch (param_id)
+    {
+    case PROP_USE_CONN:
+      g_value_set_boolean (value, empathy_connectivity_get_use_conn (
+              connectivity));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+      break;
+    };
+}
+
+static void
+connectivity_set_property (GObject *object,
+    guint param_id,
+    const GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyConnectivity *connectivity = EMPATHY_CONNECTIVITY (object);
+
+  switch (param_id)
+    {
+    case PROP_USE_CONN:
+      empathy_connectivity_set_use_conn (connectivity,
+          g_value_get_boolean (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+      break;
+    };
+}
+
 static void
 empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
 {
@@ -106,6 +215,8 @@ empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
   oclass->finalize = connectivity_finalize;
   oclass->dispose = connectivity_dispose;
   oclass->constructor = connectivity_constructor;
+  oclass->get_property = connectivity_get_property;
+  oclass->set_property = connectivity_set_property;
 
   signals[STATE_CHANGE] =
     g_signal_new ("state-change",
@@ -113,9 +224,17 @@ empathy_connectivity_class_init (EmpathyConnectivityClass *klass)
         G_SIGNAL_RUN_LAST,
         0,
         NULL, NULL,
-        g_cclosure_marshal_VOID__OBJECT, /* TODO */
+        _empathy_marshal_VOID__BOOLEAN_BOOLEAN,
         G_TYPE_NONE,
-        0, NULL);
+        2, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, NULL);
+
+  g_object_class_install_property (oclass,
+      PROP_USE_CONN,
+      g_param_spec_boolean ("use-conn",
+          "Use connectivity managers",
+          "Set presence according to connectivity managers",
+          TRUE,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
 
   g_type_class_add_private (oclass, sizeof (EmpathyConnectivityPriv));
 }
@@ -127,3 +246,52 @@ empathy_connectivity_dup_singleton (void)
 {
   return g_object_new (EMPATHY_TYPE_CONNECTIVITY, NULL);
 }
+
+gboolean
+empathy_connectivity_is_online (EmpathyConnectivity *connectivity)
+{
+  EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
+
+  if (priv->use_conn)
+    {
+#ifdef HAVE_NM
+      return priv->connected;
+#else
+      return TRUE;
+#endif
+    }
+  else
+    {
+      return TRUE;
+    }
+}
+
+gboolean
+empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity)
+{
+  EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
+
+  return priv->use_conn;
+}
+
+void
+empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
+    gboolean use_conn)
+{
+  EmpathyConnectivityPriv *priv = GET_PRIV (connectivity);
+
+  if (use_conn == priv->use_conn)
+    return;
+
+  DEBUG ("use_conn gconf key changed; new value = %s",
+      use_conn ? "true" : "false");
+
+  priv->use_conn = use_conn;
+
+#ifdef HAVE_NM
+  if (use_conn)
+    connectivity_nm_state_change_cb (priv->nm_client, NULL, connectivity);
+#endif
+
+  g_object_notify (G_OBJECT (connectivity), "use-conn");
+}
index 391c6597fb637d8470acf3effe76e7ad77499cb4..ca507e910742f4f2183535675888cfb6f139fef4 100644 (file)
@@ -59,6 +59,12 @@ GType empathy_connectivity_get_type (void);
 
 EmpathyConnectivity * empathy_connectivity_dup_singleton (void);
 
+gboolean empathy_connectivity_is_online (EmpathyConnectivity *connectivity);
+
+gboolean empathy_connectivity_get_use_conn (EmpathyConnectivity *connectivity);
+void empathy_connectivity_set_use_conn (EmpathyConnectivity *connectivity,
+    gboolean use_conn);
+
 G_END_DECLS
 
 #endif /* __EMPATHY_CONNECTIVITY_H__ */