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 3, TP_TYPE_CONNECTION, 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, conn,
96 conn_invalidated_cb (TpConnection *conn,
100 EmpathyConnectionAggregator *self)
102 self->priv->conns = g_list_remove (self->priv->conns, conn);
104 g_object_unref (conn);
108 check_connection (EmpathyConnectionAggregator *self,
113 if (g_list_find (self->priv->conns, conn) != NULL)
116 self->priv->conns = g_list_prepend (self->priv->conns,
117 g_object_ref (conn));
119 tp_g_signal_connect_object (conn, "contact-list-changed",
120 G_CALLBACK (contact_list_changed_cb), self, 0);
122 contacts = tp_connection_dup_contact_list (conn);
123 if (contacts != NULL)
127 empty = g_ptr_array_new ();
129 contact_list_changed_cb (conn, contacts, empty, self);
130 g_ptr_array_unref (empty);
133 tp_g_signal_connect_object (conn, "invalidated",
134 G_CALLBACK (conn_invalidated_cb), self, 0);
138 check_account (EmpathyConnectionAggregator *self,
143 conn = tp_account_get_connection (account);
145 check_connection (self, conn);
149 account_conn_changed_cb (TpAccount *account,
151 EmpathyConnectionAggregator *self)
153 check_account (self, account);
157 add_account (EmpathyConnectionAggregator *self,
160 check_account (self, account);
162 tp_g_signal_connect_object (account, "notify::connection",
163 G_CALLBACK (account_conn_changed_cb), self, 0);
167 account_validity_changed_cb (TpAccountManager *manager,
170 EmpathyConnectionAggregator *self)
173 add_account (self, account);
177 am_prepare_cb (GObject *source,
178 GAsyncResult *result,
181 EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
182 GError *error = NULL;
185 if (!tp_proxy_prepare_finish (source, result, &error))
187 DEBUG ("Failed to prepare account manager: %s", error->message);
188 g_error_free (error);
192 accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
193 for (l = accounts; l != NULL; l = g_list_next (l))
195 TpAccount *account = l->data;
197 add_account (self, account);
200 tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
201 G_CALLBACK (account_validity_changed_cb), self, 0);
203 g_list_free (accounts);
206 g_object_unref (self);
210 empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
212 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
213 EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
215 self->priv->mgr = tp_account_manager_dup ();
217 tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
218 g_object_ref (self));
221 EmpathyConnectionAggregator *
222 empathy_connection_aggregator_dup_singleton (void)
224 static EmpathyConnectionAggregator *aggregator = NULL;
226 if (G_LIKELY (aggregator != NULL))
227 return g_object_ref (aggregator);
229 aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
231 g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
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);