[empathy] add EmpathyConnectionAggregator



commit 057fc319fd536f33353bf307e8124dc8fbdd9040
Author: Guillaume Desmottes <guillaume desmottes collabora co uk>
Date:   Tue Nov 29 12:57:54 2011 +0100

    add EmpathyConnectionAggregator
    
    It won't do much atm but will be used as a helper dealing with all
    connections.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665121

 libempathy/Makefile.am                     |    2 +
 libempathy/empathy-connection-aggregator.c |  190 ++++++++++++++++++++++++++++
 libempathy/empathy-connection-aggregator.h |   66 ++++++++++
 3 files changed, 258 insertions(+), 0 deletions(-)
---
diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am
index 1f4bdbc..b2c42f9 100644
--- a/libempathy/Makefile.am
+++ b/libempathy/Makefile.am
@@ -36,6 +36,7 @@ libempathy_headers =				\
 	empathy-client-factory.h \
 	empathy-connection-managers.h		\
 	empathy-connectivity.h			\
+	empathy-connection-aggregator.h		\
 	empathy-contact-groups.h		\
 	empathy-contact-list.h			\
 	empathy-contact-manager.h		\
@@ -79,6 +80,7 @@ libempathy_handwritten_source =				\
 	empathy-client-factory.c \
 	empathy-connection-managers.c			\
 	empathy-connectivity.c				\
+	empathy-connection-aggregator.c		\
 	empathy-contact-groups.c			\
 	empathy-contact-list.c				\
 	empathy-contact-manager.c			\
diff --git a/libempathy/empathy-connection-aggregator.c b/libempathy/empathy-connection-aggregator.c
new file mode 100644
index 0000000..0a85603
--- /dev/null
+++ b/libempathy/empathy-connection-aggregator.c
@@ -0,0 +1,190 @@
+/*
+ * empathy-connection-aggregator.c - Source for EmpathyConnectionAggregator
+ * Copyright (C) 2010 Collabora Ltd.
+ * @author Cosimo Cecchi <cosimo cecchi collabora co uk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <config.h>
+
+#include "empathy-connection-aggregator.h"
+
+#include <telepathy-glib/telepathy-glib.h>
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
+#include "empathy-utils.h"
+
+
+#include "extensions/extensions.h"
+
+G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator, 
+    G_TYPE_OBJECT);
+
+struct _EmpathyConnectionAggregatorPriv {
+  TpAccountManager *mgr;
+
+  /* List of owned TpConnection */
+  GList *conns;
+};
+
+static void
+empathy_connection_aggregator_dispose (GObject *object)
+{
+  EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;
+
+  g_clear_object (&self->priv->mgr);
+
+  g_list_free_full (self->priv->conns, g_object_unref);
+  self->priv->conns = NULL;
+
+  G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
+}
+
+static void
+empathy_connection_aggregator_class_init (
+    EmpathyConnectionAggregatorClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+
+  oclass->dispose = empathy_connection_aggregator_dispose;
+
+  g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
+}
+
+static void
+conn_invalidated_cb (TpConnection *conn,
+    guint domain,
+    gint code,
+    gchar *message,
+    EmpathyConnectionAggregator *self)
+{
+  self->priv->conns = g_list_remove (self->priv->conns, conn);
+
+  g_object_unref (conn);
+}
+
+static void
+check_connection (EmpathyConnectionAggregator *self,
+    TpConnection *conn)
+{
+  if (g_list_find (self->priv->conns, conn) != NULL)
+    return;
+
+  self->priv->conns = g_list_prepend (self->priv->conns,
+      g_object_ref (conn));
+
+  tp_g_signal_connect_object (conn, "invalidated",
+      G_CALLBACK (conn_invalidated_cb), self, 0);
+}
+
+static void
+check_account (EmpathyConnectionAggregator *self,
+    TpAccount *account)
+{
+  TpConnection *conn;
+
+  conn = tp_account_get_connection (account);
+  if (conn != NULL)
+    check_connection (self, conn);
+}
+
+static void
+account_conn_changed_cb (TpAccount *account,
+    GParamSpec *spec,
+    EmpathyConnectionAggregator *self)
+{
+  check_account (self, account);
+}
+
+static void
+add_account (EmpathyConnectionAggregator *self,
+    TpAccount *account)
+{
+  check_account (self, account);
+
+  tp_g_signal_connect_object (account, "notify::connection",
+      G_CALLBACK (account_conn_changed_cb), self, 0);
+}
+
+static void
+account_validity_changed_cb (TpAccountManager *manager,
+    TpAccount *account,
+    gboolean valid,
+    EmpathyConnectionAggregator *self)
+{
+  if (valid)
+    add_account (self, account);
+}
+
+static void
+am_prepare_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
+  GError *error = NULL;
+  GList *accounts, *l;
+
+  if (!tp_proxy_prepare_finish (source, result, &error))
+    {
+      DEBUG ("Failed to prepare account manager: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
+  accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
+  for (l = accounts; l != NULL; l = g_list_next (l))
+    {
+      TpAccount *account = l->data;
+
+      add_account (self, account);
+    }
+
+  tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
+      G_CALLBACK (account_validity_changed_cb), self, 0);
+
+  g_list_free (accounts);
+
+out:
+  g_object_unref (self);
+}
+
+static void
+empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
+
+  self->priv->mgr = tp_account_manager_dup ();
+
+  tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
+      g_object_ref (self));
+}
+
+EmpathyConnectionAggregator *
+empathy_connection_aggregator_dup_singleton (void)
+{
+  static EmpathyConnectionAggregator *aggregator = NULL;
+
+  if (G_LIKELY (aggregator != NULL))
+      return g_object_ref (aggregator);
+
+  aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
+
+  g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
+  return aggregator;
+}
diff --git a/libempathy/empathy-connection-aggregator.h b/libempathy/empathy-connection-aggregator.h
new file mode 100644
index 0000000..a6a2726
--- /dev/null
+++ b/libempathy/empathy-connection-aggregator.h
@@ -0,0 +1,66 @@
+/*
+ * empathy-connection-aggregator.h - Header for EmpathyConnectionAggregator
+ * Copyright (C) 2010 Collabora Ltd.
+ * @author Cosimo Cecchi <cosimo cecchi collabora co uk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __EMPATHY_CONNECTION_AGGREGATOR_H__
+#define __EMPATHY_CONNECTION_AGGREGATOR_H__
+
+#include <glib-object.h>
+
+#include <telepathy-glib/base-client.h>
+
+G_BEGIN_DECLS
+
+typedef struct _EmpathyConnectionAggregator EmpathyConnectionAggregator;
+typedef struct _EmpathyConnectionAggregatorClass EmpathyConnectionAggregatorClass;
+typedef struct _EmpathyConnectionAggregatorPriv EmpathyConnectionAggregatorPriv;
+
+struct _EmpathyConnectionAggregatorClass {
+    GObjectClass parent_class;
+};
+
+struct _EmpathyConnectionAggregator {
+    GObject parent;
+    EmpathyConnectionAggregatorPriv *priv;
+};
+
+GType empathy_connection_aggregator_get_type (void);
+
+/* TYPE MACROS */
+#define EMPATHY_TYPE_CONNECTION_AGGREGATOR \
+  (empathy_connection_aggregator_get_type ())
+#define EMPATHY_CONNECTION_AGGREGATOR(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), EMPATHY_TYPE_CONNECTION_AGGREGATOR, \
+    EmpathyConnectionAggregator))
+#define EMPATHY_CONNECTION_AGGREGATOR_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), EMPATHY_TYPE_CONNECTION_AGGREGATOR, \
+    EmpathyConnectionAggregatorClass))
+#define EMPATHY_IS_CONNECTION_AGGREGATOR(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj), EMPATHY_TYPE_CONNECTION_AGGREGATOR))
+#define EMPATHY_IS_CONNECTION_AGGREGATOR_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass), EMPATHY_TYPE_CONNECTION_AGGREGATOR))
+#define EMPATHY_CONNECTION_AGGREGATOR_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), EMPATHY_TYPE_CONNECTION_AGGREGATOR, \
+    EmpathyConnectionAggregatorClass))
+
+EmpathyConnectionAggregator * empathy_connection_aggregator_dup_singleton (void);
+
+G_END_DECLS
+
+#endif /* #ifndef __EMPATHY_CONNECTION_AGGREGATOR_H__*/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]