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"
28 G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
32 EVENT_CONTACT_LIST_CHANGED,
36 static guint signals[LAST_SIGNAL];
38 struct _EmpathyConnectionAggregatorPriv {
39 TpAccountManager *mgr;
41 /* List of owned TpConnection */
46 empathy_connection_aggregator_dispose (GObject *object)
48 EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;
50 g_clear_object (&self->priv->mgr);
52 g_list_free_full (self->priv->conns, g_object_unref);
53 self->priv->conns = NULL;
55 G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
59 empathy_connection_aggregator_class_init (
60 EmpathyConnectionAggregatorClass *klass)
62 GObjectClass *oclass = G_OBJECT_CLASS (klass);
64 oclass->dispose = empathy_connection_aggregator_dispose;
66 signals[EVENT_CONTACT_LIST_CHANGED] =
67 g_signal_new ("contact-list-changed",
68 G_TYPE_FROM_CLASS (klass),
72 g_cclosure_marshal_generic,
74 2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);
76 g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
80 contact_list_changed_cb (TpConnection *conn,
83 EmpathyConnectionAggregator *self)
85 g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
89 conn_invalidated_cb (TpConnection *conn,
93 EmpathyConnectionAggregator *self)
95 self->priv->conns = g_list_remove (self->priv->conns, conn);
97 g_object_unref (conn);
101 check_connection (EmpathyConnectionAggregator *self,
106 if (g_list_find (self->priv->conns, conn) != NULL)
109 self->priv->conns = g_list_prepend (self->priv->conns,
110 g_object_ref (conn));
112 tp_g_signal_connect_object (conn, "contact-list-changed",
113 G_CALLBACK (contact_list_changed_cb), self, 0);
115 contacts = tp_connection_dup_contact_list (conn);
116 if (contacts != NULL)
120 empty = g_ptr_array_new ();
122 contact_list_changed_cb (conn, contacts, empty, self);
123 g_ptr_array_unref (empty);
125 g_ptr_array_unref (contacts);
127 tp_g_signal_connect_object (conn, "invalidated",
128 G_CALLBACK (conn_invalidated_cb), self, 0);
132 check_account (EmpathyConnectionAggregator *self,
137 conn = tp_account_get_connection (account);
139 check_connection (self, conn);
143 account_conn_changed_cb (TpAccount *account,
145 EmpathyConnectionAggregator *self)
147 check_account (self, account);
151 add_account (EmpathyConnectionAggregator *self,
154 check_account (self, account);
156 tp_g_signal_connect_object (account, "notify::connection",
157 G_CALLBACK (account_conn_changed_cb), self, 0);
161 account_validity_changed_cb (TpAccountManager *manager,
164 EmpathyConnectionAggregator *self)
167 add_account (self, account);
171 am_prepare_cb (GObject *source,
172 GAsyncResult *result,
175 EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
176 GError *error = NULL;
179 if (!tp_proxy_prepare_finish (source, result, &error))
181 DEBUG ("Failed to prepare account manager: %s", error->message);
182 g_error_free (error);
186 accounts = tp_account_manager_dup_valid_accounts (self->priv->mgr);
187 for (l = accounts; l != NULL; l = g_list_next (l))
189 TpAccount *account = l->data;
191 add_account (self, account);
194 tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
195 G_CALLBACK (account_validity_changed_cb), self, 0);
197 g_list_free_full (accounts, g_object_unref);
200 g_object_unref (self);
204 empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
206 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
207 EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
209 self->priv->mgr = tp_account_manager_dup ();
211 tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
212 g_object_ref (self));
215 EmpathyConnectionAggregator *
216 empathy_connection_aggregator_dup_singleton (void)
218 static EmpathyConnectionAggregator *aggregator = NULL;
220 if (G_LIKELY (aggregator != NULL))
221 return g_object_ref (aggregator);
223 aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
225 g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
229 /* (transfer container) */
231 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
236 set = g_hash_table_new (g_str_hash, g_str_equal);
238 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
240 TpConnection *conn = l->data;
241 const gchar * const *groups;
244 groups = tp_connection_get_contact_groups (conn);
248 for (i = 0; groups[i] != NULL; i++)
249 g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
252 keys = g_hash_table_get_keys (set);
253 g_hash_table_unref (set);
259 empathy_connection_aggregator_dup_all_contacts (
260 EmpathyConnectionAggregator *self)
265 result = g_ptr_array_new_with_free_func (g_object_unref);
267 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
269 TpConnection *conn = l->data;
272 contacts = tp_connection_dup_contact_list (conn);
273 if (contacts == NULL)
276 tp_g_ptr_array_extend (result, contacts);
278 /* tp_g_ptr_array_extend() doesn't give us an extra ref */
279 g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
281 g_ptr_array_unref (contacts);
288 rename_group_cb (GObject *source,
289 GAsyncResult *result,
292 GError *error = NULL;
294 if (!tp_connection_rename_group_finish (TP_CONNECTION (source), result,
297 DEBUG ("Failed to rename group on %s: %s",
298 tp_proxy_get_object_path (source), error->message);
299 g_error_free (error);
304 empathy_connection_aggregator_rename_group (EmpathyConnectionAggregator *self,
305 const gchar *old_name,
306 const gchar *new_name)
310 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
312 TpConnection *conn = l->data;
313 const gchar * const *groups;
315 groups = tp_connection_get_contact_groups (conn);
317 if (!tp_strv_contains (groups, old_name))
320 DEBUG ("Rename group '%s' to '%s' on %s", old_name, new_name,
321 tp_proxy_get_object_path (conn));
323 tp_connection_rename_group_async (conn, old_name, new_name,
324 rename_group_cb, NULL);