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 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
26 #include "empathy-debug.h"
27 #include "empathy-utils.h"
30 #include "extensions/extensions.h"
32 G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
36 EVENT_CONTACT_LIST_CHANGED,
40 static guint signals[LAST_SIGNAL];
42 struct _EmpathyConnectionAggregatorPriv {
43 TpAccountManager *mgr;
45 /* List of owned TpConnection */
50 empathy_connection_aggregator_dispose (GObject *object)
52 EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;
54 g_clear_object (&self->priv->mgr);
56 g_list_free_full (self->priv->conns, g_object_unref);
57 self->priv->conns = NULL;
59 G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
63 empathy_connection_aggregator_class_init (
64 EmpathyConnectionAggregatorClass *klass)
66 GObjectClass *oclass = G_OBJECT_CLASS (klass);
68 oclass->dispose = empathy_connection_aggregator_dispose;
70 signals[EVENT_CONTACT_LIST_CHANGED] =
71 g_signal_new ("contact-list-changed",
72 G_TYPE_FROM_CLASS (klass),
76 g_cclosure_marshal_generic,
78 2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);
80 g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
84 contact_list_changed_cb (TpConnection *conn,
87 EmpathyConnectionAggregator *self)
89 g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
93 conn_invalidated_cb (TpConnection *conn,
97 EmpathyConnectionAggregator *self)
99 self->priv->conns = g_list_remove (self->priv->conns, conn);
101 g_object_unref (conn);
105 check_connection (EmpathyConnectionAggregator *self,
110 if (g_list_find (self->priv->conns, conn) != NULL)
113 self->priv->conns = g_list_prepend (self->priv->conns,
114 g_object_ref (conn));
116 tp_g_signal_connect_object (conn, "contact-list-changed",
117 G_CALLBACK (contact_list_changed_cb), self, 0);
119 contacts = tp_connection_dup_contact_list (conn);
120 if (contacts != NULL)
124 empty = g_ptr_array_new ();
126 contact_list_changed_cb (conn, contacts, empty, self);
127 g_ptr_array_unref (empty);
129 g_ptr_array_unref (contacts);
131 tp_g_signal_connect_object (conn, "invalidated",
132 G_CALLBACK (conn_invalidated_cb), self, 0);
136 check_account (EmpathyConnectionAggregator *self,
141 conn = tp_account_get_connection (account);
143 check_connection (self, conn);
147 account_conn_changed_cb (TpAccount *account,
149 EmpathyConnectionAggregator *self)
151 check_account (self, account);
155 add_account (EmpathyConnectionAggregator *self,
158 check_account (self, account);
160 tp_g_signal_connect_object (account, "notify::connection",
161 G_CALLBACK (account_conn_changed_cb), self, 0);
165 account_validity_changed_cb (TpAccountManager *manager,
168 EmpathyConnectionAggregator *self)
171 add_account (self, account);
175 am_prepare_cb (GObject *source,
176 GAsyncResult *result,
179 EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
180 GError *error = NULL;
183 if (!tp_proxy_prepare_finish (source, result, &error))
185 DEBUG ("Failed to prepare account manager: %s", error->message);
186 g_error_free (error);
190 accounts = tp_account_manager_dup_valid_accounts (self->priv->mgr);
191 for (l = accounts; l != NULL; l = g_list_next (l))
193 TpAccount *account = l->data;
195 add_account (self, account);
198 tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
199 G_CALLBACK (account_validity_changed_cb), self, 0);
201 g_list_free_full (accounts, g_object_unref);
204 g_object_unref (self);
208 empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
210 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
211 EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
213 self->priv->mgr = tp_account_manager_dup ();
215 tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
216 g_object_ref (self));
219 EmpathyConnectionAggregator *
220 empathy_connection_aggregator_dup_singleton (void)
222 static EmpathyConnectionAggregator *aggregator = NULL;
224 if (G_LIKELY (aggregator != NULL))
225 return g_object_ref (aggregator);
227 aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
229 g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
233 /* (transfer container) */
235 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
240 set = g_hash_table_new (g_str_hash, g_str_equal);
242 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
244 TpConnection *conn = l->data;
245 const gchar * const *groups;
248 groups = tp_connection_get_contact_groups (conn);
252 for (i = 0; groups[i] != NULL; i++)
253 g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
256 keys = g_hash_table_get_keys (set);
257 g_hash_table_unref (set);
263 empathy_connection_aggregator_dup_all_contacts (
264 EmpathyConnectionAggregator *self)
269 result = g_ptr_array_new_with_free_func (g_object_unref);
271 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
273 TpConnection *conn = l->data;
276 contacts = tp_connection_dup_contact_list (conn);
277 if (contacts == NULL)
280 tp_g_ptr_array_extend (result, contacts);
282 /* tp_g_ptr_array_extend() doesn't give us an extra ref */
283 g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
285 g_ptr_array_unref (contacts);
292 rename_group_cb (GObject *source,
293 GAsyncResult *result,
296 GError *error = NULL;
298 if (!tp_connection_rename_group_finish (TP_CONNECTION (source), result,
301 DEBUG ("Failed to rename group on %s: %s",
302 tp_proxy_get_object_path (source), error->message);
303 g_error_free (error);
308 empathy_connection_aggregator_rename_group (EmpathyConnectionAggregator *self,
309 const gchar *old_name,
310 const gchar *new_name)
314 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
316 TpConnection *conn = l->data;
317 const gchar * const *groups;
319 groups = tp_connection_get_contact_groups (conn);
321 if (!tp_strv_contains (groups, old_name))
324 DEBUG ("Rename group '%s' to '%s' on %s", old_name, new_name,
325 tp_proxy_get_object_path (conn));
327 tp_connection_rename_group_async (conn, old_name, new_name,
328 rename_group_cb, NULL);