Get the connection for newly created accounts
[empathy.git] / libempathy / empathy-account-manager.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <libmissioncontrol/mc-account-monitor.h>
24
25 #include "empathy-account-manager.h"
26 #include "empathy-marshal.h"
27 #include "empathy-utils.h"
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
30 #include <libempathy/empathy-debug.h>
31
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountManager)
33
34 typedef struct {
35   McAccountMonitor *monitor;
36   MissionControl   *mc;
37
38   GHashTable       *accounts;
39   GHashTable       *connections;
40   int               connected;
41   int               connecting;
42   gboolean          dispose_run;
43 } EmpathyAccountManagerPriv;
44
45 typedef struct {
46   TpConnection *connection;
47   McPresence presence;
48   TpConnectionStatus status;
49   gboolean is_enabled;
50
51   guint source_id;
52 } AccountData;
53
54 enum {
55   ACCOUNT_CREATED,
56   ACCOUNT_DELETED,
57   ACCOUNT_ENABLED,
58   ACCOUNT_DISABLED,
59   ACCOUNT_CHANGED,
60   ACCOUNT_CONNECTION_CHANGED,
61   ACCOUNT_PRESENCE_CHANGED,
62   NEW_CONNECTION,
63   LAST_SIGNAL
64 };
65
66 static guint signals[LAST_SIGNAL];
67 static EmpathyAccountManager *manager_singleton = NULL;
68
69 G_DEFINE_TYPE (EmpathyAccountManager, empathy_account_manager, G_TYPE_OBJECT);
70
71 static AccountData *
72 account_data_new (McPresence presence,
73                   TpConnectionStatus status,
74                   gboolean is_enabled)
75 {
76   AccountData *retval;
77
78   retval = g_slice_new0 (AccountData);
79   retval->presence = presence;
80   retval->status = status;
81   retval->is_enabled = is_enabled;
82   retval->source_id = 0;
83
84   return retval;
85 }
86
87 static AccountData *
88 account_data_new_default (MissionControl *mc,
89                           McAccount *account)
90 {
91   McPresence actual_p;
92   TpConnectionStatus actual_c;
93   GError *err = NULL;
94
95   actual_p = mission_control_get_presence_actual (mc, &err);
96   if (err != NULL)
97     {
98       actual_p = MC_PRESENCE_UNSET;
99       g_clear_error (&err);
100     }
101
102   actual_c = mission_control_get_connection_status (mc, account, &err);
103
104   if (err != NULL)
105     {
106       actual_c = TP_CONNECTION_STATUS_DISCONNECTED;
107       g_error_free (err);
108     }
109
110   return account_data_new (actual_p, actual_c, mc_account_is_enabled (account));
111 }
112
113 static void
114 account_data_free (AccountData *data)
115 {
116   if (data->source_id > 0)
117     {
118       g_source_remove (data->source_id);
119       data->source_id = 0;
120     }
121   if (data->connection != NULL)
122     {
123       g_object_unref (data->connection);
124       data->connection = NULL;
125     }
126
127   g_slice_free (AccountData, data);
128 }
129
130 static void
131 connection_invalidated_cb (TpProxy *connection,
132                            guint    domain,
133                            gint     code,
134                            gchar   *message,
135                            EmpathyAccountManager *manager)
136 {
137   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
138   McAccount *account;
139   AccountData *data;
140
141   DEBUG ("Message: %s", message);
142   account = g_hash_table_lookup (priv->connections, connection);
143   data = g_hash_table_lookup (priv->accounts, account);
144   g_object_unref (data->connection);
145   data->connection = NULL;
146   g_hash_table_remove (priv->connections, connection);
147 }
148
149 static void
150 connection_ready_cb (TpConnection *connection,
151                      const GError *error,
152                      gpointer manager)
153 {
154   /* Errors will be handled in invalidated callback */
155   if (error != NULL)
156     return;
157
158   g_signal_emit (manager, signals[NEW_CONNECTION], 0, connection);
159 }
160
161 static void
162 account_manager_update_connection (EmpathyAccountManager *manager,
163                                    AccountData *data,
164                                    McAccount *account)
165 {
166   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
167
168   if (data->connection)
169     return;
170
171   data->connection = mission_control_get_tpconnection (priv->mc, account, NULL);
172   if (data->connection != NULL)
173     {
174       g_signal_connect (data->connection, "invalidated",
175           G_CALLBACK (connection_invalidated_cb), manager);
176       g_hash_table_insert (priv->connections, g_object_ref (data->connection),
177           g_object_ref (account));
178       tp_connection_call_when_ready (data->connection, connection_ready_cb,
179           manager);
180     }
181 }
182
183 static void
184 account_created_cb (McAccountMonitor *mon,
185                     gchar *account_name,
186                     EmpathyAccountManager *manager)
187 {
188   McAccount *account;
189   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
190   TpConnectionStatus initial_status;
191
192   account = mc_account_lookup (account_name);
193
194   if (account)
195     {
196       AccountData *data;
197
198       data = account_data_new_default (priv->mc, account);
199       g_hash_table_insert (priv->accounts, g_object_ref (account), data);
200
201       initial_status = mission_control_get_connection_status (priv->mc,
202                                                               account, NULL);
203
204       if (initial_status == TP_CONNECTION_STATUS_CONNECTED)
205         priv->connected++;
206       else if (initial_status == TP_CONNECTION_STATUS_CONNECTING)
207         priv->connecting++;
208
209       account_manager_update_connection (manager, data, account);
210
211       g_signal_emit (manager, signals[ACCOUNT_CREATED], 0, account);
212       g_object_unref (account);
213     }
214 }
215
216 static void
217 account_deleted_cb (McAccountMonitor *mon,
218                     gchar *account_name,
219                     EmpathyAccountManager *manager)
220 {
221   McAccount *account;
222   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
223
224   account = mc_account_lookup (account_name);
225
226   if (account)
227     {
228       g_hash_table_remove (priv->accounts, account);
229       g_signal_emit (manager, signals[ACCOUNT_DELETED], 0, account);
230       g_object_unref (account);
231     }
232 }
233
234 static void
235 account_changed_cb (McAccountMonitor *mon,
236                     gchar *account_name,
237                     EmpathyAccountManager *manager)
238 {
239   McAccount *account;
240
241   account = mc_account_lookup (account_name);
242
243   if (account)
244     {
245       g_signal_emit (manager, signals[ACCOUNT_CHANGED], 0, account);
246       g_object_unref (account);
247     }
248 }
249
250 static void
251 account_disabled_cb (McAccountMonitor *mon,
252                      gchar *account_name,
253                      EmpathyAccountManager *manager)
254 {
255   McAccount *account;
256   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
257   AccountData *data;
258
259   account = mc_account_lookup (account_name);
260
261   if (account)
262     {
263       data = g_hash_table_lookup (priv->accounts, account);
264       g_assert (data);
265       data->is_enabled = FALSE;
266
267       g_signal_emit (manager, signals[ACCOUNT_DISABLED], 0, account);
268       g_object_unref (account);
269     }
270 }
271
272 static void
273 account_enabled_cb (McAccountMonitor *mon,
274                     gchar *account_name,
275                     EmpathyAccountManager *manager)
276 {
277   McAccount *account;
278   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
279   AccountData *data;
280
281   account = mc_account_lookup (account_name);
282
283   if (account)
284     {
285       data = g_hash_table_lookup (priv->accounts, account);
286       g_assert (data);
287       data->is_enabled = TRUE;
288
289       g_signal_emit (manager, signals[ACCOUNT_ENABLED], 0, account);
290       g_object_unref (account);
291     }
292 }
293
294 static void
295 update_connection_numbers (EmpathyAccountManager *manager,
296                            TpConnectionStatus status,
297                            TpConnectionStatus old_s)
298 {
299   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
300
301   if (status == TP_CONNECTION_STATUS_CONNECTED)
302     {
303       priv->connected++;
304       if (old_s == TP_CONNECTION_STATUS_CONNECTING)
305         priv->connecting--;
306     }
307
308   if (status == TP_CONNECTION_STATUS_CONNECTING)
309     {
310       priv->connecting++;
311       if (old_s == TP_CONNECTION_STATUS_CONNECTED)
312         priv->connected--;
313     }
314
315   if (status == TP_CONNECTION_STATUS_DISCONNECTED)
316     {
317       if (old_s == TP_CONNECTION_STATUS_CONNECTED)
318         priv->connected--;
319
320       if (old_s == TP_CONNECTION_STATUS_CONNECTING)
321         priv->connecting--;
322     }
323 }
324
325 static gboolean
326 remove_data_timeout (gpointer _data)
327 {
328   AccountData *data = _data;
329
330   data->source_id = 0;
331
332   return FALSE;
333 }
334
335 typedef struct {
336   TpConnectionStatus status;
337   McPresence presence;
338   TpConnectionStatusReason reason;
339   gchar *unique_name;
340   EmpathyAccountManager *manager;
341 } ChangedSignalData;
342
343 static gboolean
344 account_status_changed_idle_cb (ChangedSignalData *signal_data)
345 {
346   McAccount *account;
347   AccountData *data;
348   McPresence presence, old_p;
349   TpConnectionStatus status, old_s;
350   gboolean emit_presence = FALSE, emit_connection = FALSE;
351   EmpathyAccountManager *manager = signal_data->manager;
352   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
353
354   presence = signal_data->presence;
355   status = signal_data->status;
356   account = mc_account_lookup (signal_data->unique_name);
357
358   if (account)
359     {
360       data = g_hash_table_lookup (priv->accounts, account);
361       g_assert (data);
362
363       old_p = data->presence;
364       old_s = data->status;
365
366       if (old_p != presence)
367         {
368           data->presence = presence;
369           emit_presence = TRUE;
370         }
371
372       if (old_s != status)
373         {
374           data->status = status;
375           update_connection_numbers (manager, status, old_s);
376
377           if (status == TP_CONNECTION_STATUS_CONNECTED)
378             {
379                 if (data->source_id > 0)
380                   {
381                     g_source_remove (data->source_id);
382                     data->source_id = 0;
383                   }
384
385                 data->source_id = g_timeout_add_seconds (10,
386                                                          remove_data_timeout,
387                                                          data);
388             }
389           emit_connection = TRUE;
390         }
391
392       account_manager_update_connection (manager, data, account);
393
394       if (emit_presence)
395         g_signal_emit (manager, signals[ACCOUNT_PRESENCE_CHANGED], 0,
396                        account, presence, old_p);
397
398       if (emit_connection)
399         g_signal_emit (manager, signals[ACCOUNT_CONNECTION_CHANGED], 0,
400                        account, signal_data->reason, status, old_s);
401
402       g_object_unref (account);
403     }
404
405   g_object_unref (signal_data->manager);
406   g_free (signal_data->unique_name);
407   g_slice_free (ChangedSignalData, signal_data);
408
409   return FALSE;
410 }
411
412 static void
413 account_status_changed_cb (MissionControl *mc,
414                            TpConnectionStatus status,
415                            McPresence presence,
416                            TpConnectionStatusReason reason,
417                            const gchar *unique_name,
418                            EmpathyAccountManager *manager)
419 {
420   ChangedSignalData *data;
421
422   data = g_slice_new0 (ChangedSignalData);
423   data->status = status;
424   data->presence = presence;
425   data->reason = reason;
426   data->unique_name = g_strdup (unique_name);
427   data->manager = g_object_ref (manager);
428
429   g_idle_add ((GSourceFunc) account_status_changed_idle_cb, data);
430 }
431
432 static void
433 empathy_account_manager_init (EmpathyAccountManager *manager)
434 {
435   EmpathyAccountManagerPriv *priv;
436   GList *mc_accounts, *l;
437
438   priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
439                                       EMPATHY_TYPE_ACCOUNT_MANAGER, EmpathyAccountManagerPriv);
440   manager->priv = priv;
441   priv->monitor = mc_account_monitor_new ();
442   priv->mc = empathy_mission_control_dup_singleton ();
443   priv->connected = priv->connecting = 0;
444   priv->dispose_run = FALSE;
445
446   priv->accounts = g_hash_table_new_full (empathy_account_hash,
447                                           empathy_account_equal,
448                                           g_object_unref, 
449                                           (GDestroyNotify) account_data_free);
450   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
451                                              g_object_unref, g_object_unref);
452
453   mc_accounts = mc_accounts_list ();
454
455   for (l = mc_accounts; l; l = l->next)
456     account_created_cb (priv->monitor, (char *) mc_account_get_unique_name (l->data), manager);
457
458   g_signal_connect (priv->monitor, "account-created",
459                     G_CALLBACK (account_created_cb), manager);
460   g_signal_connect (priv->monitor, "account-deleted",
461                     G_CALLBACK (account_deleted_cb), manager);
462   g_signal_connect (priv->monitor, "account-disabled",
463                     G_CALLBACK (account_disabled_cb), manager);
464   g_signal_connect (priv->monitor, "account-enabled",
465                     G_CALLBACK (account_enabled_cb), manager);
466   g_signal_connect (priv->monitor, "account-changed",
467                     G_CALLBACK (account_changed_cb), manager);
468
469   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
470                                G_CALLBACK (account_status_changed_cb),
471                                manager, NULL);
472
473   mc_accounts_list_free (mc_accounts);
474 }
475
476 static void
477 do_finalize (GObject *obj)
478 {
479   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
480   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
481
482   g_hash_table_unref (priv->accounts);
483   g_hash_table_unref (priv->connections);
484
485   G_OBJECT_CLASS (empathy_account_manager_parent_class)->finalize (obj);
486 }
487
488 static void
489 do_dispose (GObject *obj)
490 {
491   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
492   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
493
494   if (priv->dispose_run)
495     return;
496
497   priv->dispose_run = TRUE;
498
499   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
500                                   "AccountStatusChanged",
501                                   G_CALLBACK (account_status_changed_cb),
502                                   obj);
503
504   if (priv->monitor)
505     {
506       g_signal_handlers_disconnect_by_func (priv->monitor,
507                                             account_created_cb, obj);
508       g_signal_handlers_disconnect_by_func (priv->monitor,
509                                             account_deleted_cb, obj);
510       g_signal_handlers_disconnect_by_func (priv->monitor,
511                                             account_disabled_cb, obj);
512       g_signal_handlers_disconnect_by_func (priv->monitor,
513                                             account_enabled_cb, obj);
514       g_signal_handlers_disconnect_by_func (priv->monitor,
515                                             account_changed_cb, obj);
516       g_object_unref (priv->monitor);
517       priv->monitor = NULL;
518     }
519
520   if (priv->mc)
521     g_object_unref (priv->mc);
522
523   g_hash_table_remove_all (priv->accounts);
524
525   G_OBJECT_CLASS (empathy_account_manager_parent_class)->dispose (obj);
526 }
527
528 static GObject*
529 do_constructor (GType type,
530                 guint n_construct_params,
531                 GObjectConstructParam *construct_params)
532 {
533   GObject *retval;
534
535   if (!manager_singleton)
536     {
537       retval = G_OBJECT_CLASS (empathy_account_manager_parent_class)->constructor (type,
538                                                                                    n_construct_params,
539                                                                                    construct_params);
540       manager_singleton = EMPATHY_ACCOUNT_MANAGER (retval);
541       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
542     }
543   else
544     {
545       retval = g_object_ref (manager_singleton);
546     }
547
548   return retval;
549 }
550
551 static void
552 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
553 {
554   GObjectClass *oclass = G_OBJECT_CLASS (klass);
555
556   oclass->finalize = do_finalize;
557   oclass->dispose = do_dispose;
558   oclass->constructor = do_constructor;
559
560   signals[ACCOUNT_CREATED] =
561     g_signal_new ("account-created",
562                   G_TYPE_FROM_CLASS (klass),
563                   G_SIGNAL_RUN_LAST,
564                   0,
565                   NULL, NULL,
566                   g_cclosure_marshal_VOID__OBJECT,
567                   G_TYPE_NONE,
568                   1, MC_TYPE_ACCOUNT);
569
570   signals[ACCOUNT_DELETED] =
571     g_signal_new ("account-deleted",
572                   G_TYPE_FROM_CLASS (klass),
573                   G_SIGNAL_RUN_LAST,
574                   0,
575                   NULL, NULL,
576                   g_cclosure_marshal_VOID__OBJECT,
577                   G_TYPE_NONE,
578                   1, MC_TYPE_ACCOUNT);
579
580   signals[ACCOUNT_ENABLED] =
581     g_signal_new ("account-enabled",
582                   G_TYPE_FROM_CLASS (klass),
583                   G_SIGNAL_RUN_LAST,
584                   0,
585                   NULL, NULL,
586                   g_cclosure_marshal_VOID__OBJECT,
587                   G_TYPE_NONE,
588                   1, MC_TYPE_ACCOUNT);
589
590   signals[ACCOUNT_DISABLED] =
591     g_signal_new ("account-disabled",
592                   G_TYPE_FROM_CLASS (klass),
593                   G_SIGNAL_RUN_LAST,
594                   0,
595                   NULL, NULL,
596                   g_cclosure_marshal_VOID__OBJECT,
597                   G_TYPE_NONE,
598                   1, MC_TYPE_ACCOUNT);
599
600   signals[ACCOUNT_CHANGED] =
601     g_signal_new ("account-changed",
602                   G_TYPE_FROM_CLASS (klass),
603                   G_SIGNAL_RUN_LAST,
604                   0,
605                   NULL, NULL,
606                   g_cclosure_marshal_VOID__OBJECT,
607                   G_TYPE_NONE,
608                   1, MC_TYPE_ACCOUNT);
609
610   signals[ACCOUNT_CONNECTION_CHANGED] =
611     g_signal_new ("account-connection-changed",
612                   G_TYPE_FROM_CLASS (klass),
613                   G_SIGNAL_RUN_LAST,
614                   0,
615                   NULL, NULL,
616                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
617                   G_TYPE_NONE,
618                   4, MC_TYPE_ACCOUNT,
619                   G_TYPE_INT,   /* reason */
620                   G_TYPE_UINT,  /* actual connection */
621                   G_TYPE_UINT); /* previous connection */
622
623   signals[ACCOUNT_PRESENCE_CHANGED] =
624     g_signal_new ("account-presence-changed",
625                   G_TYPE_FROM_CLASS (klass),
626                   G_SIGNAL_RUN_LAST,
627                   0,
628                   NULL, NULL,
629                   _empathy_marshal_VOID__OBJECT_INT_INT,
630                   G_TYPE_NONE,
631                   3, MC_TYPE_ACCOUNT,
632                   G_TYPE_INT,  /* actual presence */
633                   G_TYPE_INT); /* previous presence */
634
635   signals[NEW_CONNECTION] =
636     g_signal_new ("new-connection",
637                   G_TYPE_FROM_CLASS (klass),
638                   G_SIGNAL_RUN_LAST,
639                   0,
640                   NULL, NULL,
641                   g_cclosure_marshal_VOID__OBJECT,
642                   G_TYPE_NONE,
643                   1, TP_TYPE_CONNECTION);
644   
645   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
646 }
647
648 /* public methods */
649
650 EmpathyAccountManager *
651 empathy_account_manager_dup_singleton (void)
652 {
653   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
654 }
655
656 int
657 empathy_account_manager_get_connected_accounts (EmpathyAccountManager *manager)
658 {
659   EmpathyAccountManagerPriv *priv;
660
661   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
662
663   priv = GET_PRIV (manager);
664
665   return priv->connected;
666 }
667
668 int
669 empathy_account_manager_get_connecting_accounts (EmpathyAccountManager *manager)
670 {
671   EmpathyAccountManagerPriv *priv;
672
673   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
674
675   priv = GET_PRIV (manager);
676
677   return priv->connecting;
678 }
679
680 gboolean
681 empathy_account_manager_is_account_just_connected (EmpathyAccountManager *manager,
682                                                    McAccount *account)
683 {
684   EmpathyAccountManagerPriv *priv;
685   AccountData *data;
686
687   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), FALSE);
688
689   priv = GET_PRIV (manager);
690   data = g_hash_table_lookup (priv->accounts, account);
691
692   g_assert (data);
693
694   return (data->source_id > 0);
695 }
696
697 /**
698  * empathy_account_manager_get_count:
699  * @manager: a #EmpathyAccountManager
700  *
701  * Get the number of accounts.
702  *
703  * Returns: the number of accounts.
704  **/
705 int
706 empathy_account_manager_get_count (EmpathyAccountManager *manager)
707 {
708   EmpathyAccountManagerPriv *priv;
709
710   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
711
712   priv = GET_PRIV (manager);
713
714   return g_hash_table_size (priv->accounts);
715 }
716
717 McAccount *
718 empathy_account_manager_get_account (EmpathyAccountManager *manager,
719                                      TpConnection          *connection)
720 {
721   EmpathyAccountManagerPriv *priv;
722
723   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
724
725   priv = GET_PRIV (manager);
726
727   return g_hash_table_lookup (priv->connections, connection);
728 }
729
730 GList *
731 empathy_account_manager_dup_accounts (EmpathyAccountManager *manager)
732 {
733   EmpathyAccountManagerPriv *priv;
734   GList *ret;
735
736   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
737
738   priv = GET_PRIV (manager);
739
740   ret = g_hash_table_get_keys (priv->accounts);
741   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
742
743   return ret;
744 }
745
746 /**
747  * empathy_account_manager_get_connection:
748  * @manager: a #EmpathyAccountManager
749  * @account: a #McAccount
750  *
751  * Get the connection of the accounts, or NULL if account is offline or the
752  * connection is not yet ready.
753  *
754  * Returns: the connection of the accounts.
755  **/
756 TpConnection *
757 empathy_account_manager_get_connection (EmpathyAccountManager *manager,
758                                         McAccount *account)
759 {
760   EmpathyAccountManagerPriv *priv;
761   AccountData *data;
762
763   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
764   g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
765
766   priv = GET_PRIV (manager);
767
768   data = g_hash_table_lookup (priv->accounts, account);
769   if (data && data->connection && tp_connection_is_ready (data->connection))
770     return data->connection;
771
772   return NULL;
773 }
774
775 GList *
776 empathy_account_manager_dup_connections (EmpathyAccountManager *manager)
777 {
778   EmpathyAccountManagerPriv *priv;
779   GHashTableIter iter;
780   gpointer connection;
781   GList *ret = NULL;
782
783   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
784
785   priv = GET_PRIV (manager);
786
787   g_hash_table_iter_init (&iter, priv->connections);
788   while (g_hash_table_iter_next (&iter, &connection, NULL)) 
789     if (connection != NULL && tp_connection_is_ready (connection))
790       ret = g_list_prepend (ret, g_object_ref (connection));
791
792   return ret;
793 }
794