2 * empathy-connection-aggregator.c - Source for EmpathyConnectionAggregator
3 * Copyright (C) 2010 Collabora Ltd.
4 * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
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.
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.
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
23 #include "empathy-connection-aggregator.h"
25 #include <telepathy-glib/telepathy-glib.h>
27 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
28 #include "empathy-debug.h"
29 #include "empathy-utils.h"
32 #include "extensions/extensions.h"
34 G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
38 EVENT_CONTACT_LIST_CHANGED,
42 static guint signals[LAST_SIGNAL];
44 struct _EmpathyConnectionAggregatorPriv {
45 TpAccountManager *mgr;
47 /* List of owned TpConnection */
52 empathy_connection_aggregator_dispose (GObject *object)
54 EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;
56 g_clear_object (&self->priv->mgr);
58 g_list_free_full (self->priv->conns, g_object_unref);
59 self->priv->conns = NULL;
61 G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
65 empathy_connection_aggregator_class_init (
66 EmpathyConnectionAggregatorClass *klass)
68 GObjectClass *oclass = G_OBJECT_CLASS (klass);
70 oclass->dispose = empathy_connection_aggregator_dispose;
72 signals[EVENT_CONTACT_LIST_CHANGED] =
73 g_signal_new ("contact-list-changed",
74 G_TYPE_FROM_CLASS (klass),
78 g_cclosure_marshal_generic,
80 2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);
82 g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
86 contact_list_changed_cb (TpConnection *conn,
89 EmpathyConnectionAggregator *self)
91 g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
95 conn_invalidated_cb (TpConnection *conn,
99 EmpathyConnectionAggregator *self)
101 self->priv->conns = g_list_remove (self->priv->conns, conn);
103 g_object_unref (conn);
107 check_connection (EmpathyConnectionAggregator *self,
112 if (g_list_find (self->priv->conns, conn) != NULL)
115 self->priv->conns = g_list_prepend (self->priv->conns,
116 g_object_ref (conn));
118 tp_g_signal_connect_object (conn, "contact-list-changed",
119 G_CALLBACK (contact_list_changed_cb), self, 0);
121 contacts = tp_connection_dup_contact_list (conn);
122 if (contacts != NULL)
126 empty = g_ptr_array_new ();
128 contact_list_changed_cb (conn, contacts, empty, self);
129 g_ptr_array_unref (empty);
132 tp_g_signal_connect_object (conn, "invalidated",
133 G_CALLBACK (conn_invalidated_cb), self, 0);
137 check_account (EmpathyConnectionAggregator *self,
142 conn = tp_account_get_connection (account);
144 check_connection (self, conn);
148 account_conn_changed_cb (TpAccount *account,
150 EmpathyConnectionAggregator *self)
152 check_account (self, account);
156 add_account (EmpathyConnectionAggregator *self,
159 check_account (self, account);
161 tp_g_signal_connect_object (account, "notify::connection",
162 G_CALLBACK (account_conn_changed_cb), self, 0);
166 account_validity_changed_cb (TpAccountManager *manager,
169 EmpathyConnectionAggregator *self)
172 add_account (self, account);
176 am_prepare_cb (GObject *source,
177 GAsyncResult *result,
180 EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
181 GError *error = NULL;
184 if (!tp_proxy_prepare_finish (source, result, &error))
186 DEBUG ("Failed to prepare account manager: %s", error->message);
187 g_error_free (error);
191 accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
192 for (l = accounts; l != NULL; l = g_list_next (l))
194 TpAccount *account = l->data;
196 add_account (self, account);
199 tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
200 G_CALLBACK (account_validity_changed_cb), self, 0);
202 g_list_free (accounts);
205 g_object_unref (self);
209 empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
211 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
212 EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
214 self->priv->mgr = tp_account_manager_dup ();
216 tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
217 g_object_ref (self));
220 EmpathyConnectionAggregator *
221 empathy_connection_aggregator_dup_singleton (void)
223 static EmpathyConnectionAggregator *aggregator = NULL;
225 if (G_LIKELY (aggregator != NULL))
226 return g_object_ref (aggregator);
228 aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
230 g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
234 /* (transfer container) */
236 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
241 set = g_hash_table_new (g_str_hash, g_str_equal);
243 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
245 TpConnection *conn = l->data;
246 const gchar * const *groups;
249 groups = tp_connection_get_contact_groups (conn);
253 for (i = 0; groups[i] != NULL; i++)
254 g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
257 keys = g_hash_table_get_keys (set);
258 g_hash_table_unref (set);
264 empathy_connection_aggregator_dup_all_contacts (
265 EmpathyConnectionAggregator *self)
270 result = g_ptr_array_new_with_free_func (g_object_unref);
272 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
274 TpConnection *conn = l->data;
277 contacts = tp_connection_dup_contact_list (conn);
278 if (contacts == NULL)
281 tp_g_ptr_array_extend (result, contacts);
283 /* tp_g_ptr_array_extend() doesn't give us an extra ref */
284 g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
286 g_ptr_array_unref (contacts);