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);
131 g_ptr_array_unref (contacts);
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_dup_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_full (accounts, g_object_unref);
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);
235 /* (transfer container) */
237 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
242 set = g_hash_table_new (g_str_hash, g_str_equal);
244 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
246 TpConnection *conn = l->data;
247 const gchar * const *groups;
250 groups = tp_connection_get_contact_groups (conn);
254 for (i = 0; groups[i] != NULL; i++)
255 g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
258 keys = g_hash_table_get_keys (set);
259 g_hash_table_unref (set);
265 empathy_connection_aggregator_dup_all_contacts (
266 EmpathyConnectionAggregator *self)
271 result = g_ptr_array_new_with_free_func (g_object_unref);
273 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
275 TpConnection *conn = l->data;
278 contacts = tp_connection_dup_contact_list (conn);
279 if (contacts == NULL)
282 tp_g_ptr_array_extend (result, contacts);
284 /* tp_g_ptr_array_extend() doesn't give us an extra ref */
285 g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
287 g_ptr_array_unref (contacts);
294 rename_group_cb (GObject *source,
295 GAsyncResult *result,
298 GError *error = NULL;
300 if (!tp_connection_rename_group_finish (TP_CONNECTION (source), result,
303 DEBUG ("Failed to rename group on %s: %s",
304 tp_proxy_get_object_path (source), error->message);
305 g_error_free (error);
310 empathy_connection_aggregator_rename_group (EmpathyConnectionAggregator *self,
311 const gchar *old_name,
312 const gchar *new_name)
316 for (l = self->priv->conns; l != NULL; l = g_list_next (l))
318 TpConnection *conn = l->data;
319 const gchar * const *groups;
321 groups = tp_connection_get_contact_groups (conn);
323 if (!tp_strv_contains (groups, old_name))
326 DEBUG ("Rename group '%s' to '%s' on %s", old_name, new_name,
327 tp_proxy_get_object_path (conn));
329 tp_connection_rename_group_async (conn, old_name, new_name,
330 rename_group_cb, NULL);