]> git.0d.be Git - empathy.git/blob - libempathy/empathy-connection-aggregator.c
individual-menu: remove link-contacts-activated signal
[empathy.git] / libempathy / empathy-connection-aggregator.c
1 /*
2  * empathy-connection-aggregator.c - Source for EmpathyConnectionAggregator
3  * Copyright (C) 2010 Collabora Ltd.
4  * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <config.h>
22
23 #include "empathy-connection-aggregator.h"
24
25 #include <telepathy-glib/telepathy-glib.h>
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
28 #include "empathy-debug.h"
29 #include "empathy-utils.h"
30
31
32 #include "extensions/extensions.h"
33
34 G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
35     G_TYPE_OBJECT);
36
37 enum {
38   EVENT_CONTACT_LIST_CHANGED,
39   LAST_SIGNAL
40 };
41
42 static guint signals[LAST_SIGNAL];
43
44 struct _EmpathyConnectionAggregatorPriv {
45   TpAccountManager *mgr;
46
47   /* List of owned TpConnection */
48   GList *conns;
49 };
50
51 static void
52 empathy_connection_aggregator_dispose (GObject *object)
53 {
54   EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;
55
56   g_clear_object (&self->priv->mgr);
57
58   g_list_free_full (self->priv->conns, g_object_unref);
59   self->priv->conns = NULL;
60
61   G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
62 }
63
64 static void
65 empathy_connection_aggregator_class_init (
66     EmpathyConnectionAggregatorClass *klass)
67 {
68   GObjectClass *oclass = G_OBJECT_CLASS (klass);
69
70   oclass->dispose = empathy_connection_aggregator_dispose;
71
72   signals[EVENT_CONTACT_LIST_CHANGED] =
73     g_signal_new ("contact-list-changed",
74       G_TYPE_FROM_CLASS (klass),
75       G_SIGNAL_RUN_LAST,
76       0,
77       NULL, NULL,
78       g_cclosure_marshal_generic,
79       G_TYPE_NONE,
80       2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);
81
82   g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
83 }
84
85 static void
86 contact_list_changed_cb (TpConnection *conn,
87     GPtrArray *added,
88     GPtrArray *removed,
89     EmpathyConnectionAggregator *self)
90 {
91   g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
92 }
93
94 static void
95 conn_invalidated_cb (TpConnection *conn,
96     guint domain,
97     gint code,
98     gchar *message,
99     EmpathyConnectionAggregator *self)
100 {
101   self->priv->conns = g_list_remove (self->priv->conns, conn);
102
103   g_object_unref (conn);
104 }
105
106 static void
107 check_connection (EmpathyConnectionAggregator *self,
108     TpConnection *conn)
109 {
110   GPtrArray *contacts;
111
112   if (g_list_find (self->priv->conns, conn) != NULL)
113     return;
114
115   self->priv->conns = g_list_prepend (self->priv->conns,
116       g_object_ref (conn));
117
118   tp_g_signal_connect_object (conn, "contact-list-changed",
119       G_CALLBACK (contact_list_changed_cb), self, 0);
120
121   contacts = tp_connection_dup_contact_list (conn);
122   if (contacts != NULL)
123     {
124       GPtrArray *empty;
125
126       empty = g_ptr_array_new ();
127
128       contact_list_changed_cb (conn, contacts, empty, self);
129       g_ptr_array_unref (empty);
130     }
131
132   tp_g_signal_connect_object (conn, "invalidated",
133       G_CALLBACK (conn_invalidated_cb), self, 0);
134 }
135
136 static void
137 check_account (EmpathyConnectionAggregator *self,
138     TpAccount *account)
139 {
140   TpConnection *conn;
141
142   conn = tp_account_get_connection (account);
143   if (conn != NULL)
144     check_connection (self, conn);
145 }
146
147 static void
148 account_conn_changed_cb (TpAccount *account,
149     GParamSpec *spec,
150     EmpathyConnectionAggregator *self)
151 {
152   check_account (self, account);
153 }
154
155 static void
156 add_account (EmpathyConnectionAggregator *self,
157     TpAccount *account)
158 {
159   check_account (self, account);
160
161   tp_g_signal_connect_object (account, "notify::connection",
162       G_CALLBACK (account_conn_changed_cb), self, 0);
163 }
164
165 static void
166 account_validity_changed_cb (TpAccountManager *manager,
167     TpAccount *account,
168     gboolean valid,
169     EmpathyConnectionAggregator *self)
170 {
171   if (valid)
172     add_account (self, account);
173 }
174
175 static void
176 am_prepare_cb (GObject *source,
177     GAsyncResult *result,
178     gpointer user_data)
179 {
180   EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
181   GError *error = NULL;
182   GList *accounts, *l;
183
184   if (!tp_proxy_prepare_finish (source, result, &error))
185     {
186       DEBUG ("Failed to prepare account manager: %s", error->message);
187       g_error_free (error);
188       goto out;
189     }
190
191   accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
192   for (l = accounts; l != NULL; l = g_list_next (l))
193     {
194       TpAccount *account = l->data;
195
196       add_account (self, account);
197     }
198
199   tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
200       G_CALLBACK (account_validity_changed_cb), self, 0);
201
202   g_list_free (accounts);
203
204 out:
205   g_object_unref (self);
206 }
207
208 static void
209 empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
210 {
211   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
212       EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);
213
214   self->priv->mgr = tp_account_manager_dup ();
215
216   tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
217       g_object_ref (self));
218 }
219
220 EmpathyConnectionAggregator *
221 empathy_connection_aggregator_dup_singleton (void)
222 {
223   static EmpathyConnectionAggregator *aggregator = NULL;
224
225   if (G_LIKELY (aggregator != NULL))
226       return g_object_ref (aggregator);
227
228   aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);
229
230   g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
231   return aggregator;
232 }
233
234 /* (transfer container) */
235 GList *
236 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
237 {
238   GList *keys, *l;
239   GHashTable *set;
240
241   set = g_hash_table_new (g_str_hash, g_str_equal);
242
243   for (l = self->priv->conns; l != NULL; l = g_list_next (l))
244     {
245       TpConnection *conn = l->data;
246       const gchar * const *groups;
247       guint i;
248
249       groups = tp_connection_get_contact_groups (conn);
250       if (groups == NULL)
251         continue;
252
253       for (i = 0; groups[i] != NULL; i++)
254         g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
255     }
256
257   keys = g_hash_table_get_keys (set);
258   g_hash_table_unref (set);
259
260   return keys;
261 }
262
263 GPtrArray *
264 empathy_connection_aggregator_dup_all_contacts (
265     EmpathyConnectionAggregator *self)
266 {
267   GPtrArray *result;
268   GList *l;
269
270   result = g_ptr_array_new_with_free_func (g_object_unref);
271
272   for (l = self->priv->conns; l != NULL; l = g_list_next (l))
273     {
274       TpConnection *conn = l->data;
275       GPtrArray *contacts;
276
277       contacts = tp_connection_dup_contact_list (conn);
278       if (contacts == NULL)
279         continue;
280
281       tp_g_ptr_array_extend (result, contacts);
282
283       /* tp_g_ptr_array_extend() doesn't give us an extra ref */
284       g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
285
286       g_ptr_array_unref (contacts);
287     }
288
289   return result;
290 }