Add asserts in connection_invalidated_cb
[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; /* McAccount -> AccountData */
39   GHashTable       *connections; /* TpConnection -> McAccount */
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
143   account = g_hash_table_lookup (priv->connections, connection);
144   g_assert (account != NULL);
145
146   data = g_hash_table_lookup (priv->accounts, account);
147   g_assert (data != NULL);
148
149   g_object_unref (data->connection);
150   data->connection = NULL;
151
152   g_hash_table_remove (priv->connections, connection);
153 }
154
155 static void
156 connection_ready_cb (TpConnection *connection,
157                      const GError *error,
158                      gpointer manager)
159 {
160   /* Errors will be handled in invalidated callback */
161   if (error != NULL)
162     return;
163
164   g_signal_emit (manager, signals[NEW_CONNECTION], 0, connection);
165 }
166
167 static void
168 account_manager_update_connection (EmpathyAccountManager *manager,
169                                    AccountData *data,
170                                    McAccount *account)
171 {
172   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
173
174   if (data->connection)
175     return;
176
177   data->connection = mission_control_get_tpconnection (priv->mc, account, NULL);
178   if (data->connection != NULL)
179     {
180       g_signal_connect (data->connection, "invalidated",
181           G_CALLBACK (connection_invalidated_cb), manager);
182       g_hash_table_insert (priv->connections, g_object_ref (data->connection),
183           g_object_ref (account));
184       tp_connection_call_when_ready (data->connection, connection_ready_cb,
185           manager);
186     }
187 }
188
189 static void
190 account_created_cb (McAccountMonitor *mon,
191                     gchar *account_name,
192                     EmpathyAccountManager *manager)
193 {
194   McAccount *account;
195   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
196   TpConnectionStatus initial_status;
197
198   account = mc_account_lookup (account_name);
199
200   if (account)
201     {
202       AccountData *data;
203
204       data = account_data_new_default (priv->mc, account);
205       g_hash_table_insert (priv->accounts, g_object_ref (account), data);
206
207       initial_status = mission_control_get_connection_status (priv->mc,
208                                                               account, NULL);
209
210       if (initial_status == TP_CONNECTION_STATUS_CONNECTED)
211         priv->connected++;
212       else if (initial_status == TP_CONNECTION_STATUS_CONNECTING)
213         priv->connecting++;
214
215       account_manager_update_connection (manager, data, account);
216
217       g_signal_emit (manager, signals[ACCOUNT_CREATED], 0, account);
218       g_object_unref (account);
219     }
220 }
221
222 static void
223 account_deleted_cb (McAccountMonitor *mon,
224                     gchar *account_name,
225                     EmpathyAccountManager *manager)
226 {
227   McAccount *account;
228   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
229
230   account = mc_account_lookup (account_name);
231
232   if (account)
233     {
234       g_hash_table_remove (priv->accounts, account);
235       g_signal_emit (manager, signals[ACCOUNT_DELETED], 0, account);
236       g_object_unref (account);
237     }
238 }
239
240 static void
241 account_changed_cb (McAccountMonitor *mon,
242                     gchar *account_name,
243                     EmpathyAccountManager *manager)
244 {
245   McAccount *account;
246
247   account = mc_account_lookup (account_name);
248
249   if (account)
250     {
251       g_signal_emit (manager, signals[ACCOUNT_CHANGED], 0, account);
252       g_object_unref (account);
253     }
254 }
255
256 static void
257 account_disabled_cb (McAccountMonitor *mon,
258                      gchar *account_name,
259                      EmpathyAccountManager *manager)
260 {
261   McAccount *account;
262   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
263   AccountData *data;
264
265   account = mc_account_lookup (account_name);
266
267   if (account)
268     {
269       data = g_hash_table_lookup (priv->accounts, account);
270       g_assert (data);
271       data->is_enabled = FALSE;
272
273       g_signal_emit (manager, signals[ACCOUNT_DISABLED], 0, account);
274       g_object_unref (account);
275     }
276 }
277
278 static void
279 account_enabled_cb (McAccountMonitor *mon,
280                     gchar *account_name,
281                     EmpathyAccountManager *manager)
282 {
283   McAccount *account;
284   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
285   AccountData *data;
286
287   account = mc_account_lookup (account_name);
288
289   if (account)
290     {
291       data = g_hash_table_lookup (priv->accounts, account);
292       g_assert (data);
293       data->is_enabled = TRUE;
294
295       g_signal_emit (manager, signals[ACCOUNT_ENABLED], 0, account);
296       g_object_unref (account);
297     }
298 }
299
300 static void
301 update_connection_numbers (EmpathyAccountManager *manager,
302                            TpConnectionStatus status,
303                            TpConnectionStatus old_s)
304 {
305   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
306
307   if (status == TP_CONNECTION_STATUS_CONNECTED)
308     {
309       priv->connected++;
310       if (old_s == TP_CONNECTION_STATUS_CONNECTING)
311         priv->connecting--;
312     }
313
314   if (status == TP_CONNECTION_STATUS_CONNECTING)
315     {
316       priv->connecting++;
317       if (old_s == TP_CONNECTION_STATUS_CONNECTED)
318         priv->connected--;
319     }
320
321   if (status == TP_CONNECTION_STATUS_DISCONNECTED)
322     {
323       if (old_s == TP_CONNECTION_STATUS_CONNECTED)
324         priv->connected--;
325
326       if (old_s == TP_CONNECTION_STATUS_CONNECTING)
327         priv->connecting--;
328     }
329 }
330
331 static gboolean
332 remove_data_timeout (gpointer _data)
333 {
334   AccountData *data = _data;
335
336   data->source_id = 0;
337
338   return FALSE;
339 }
340
341 typedef struct {
342   TpConnectionStatus status;
343   McPresence presence;
344   TpConnectionStatusReason reason;
345   gchar *unique_name;
346   EmpathyAccountManager *manager;
347 } ChangedSignalData;
348
349 static gboolean
350 account_status_changed_idle_cb (ChangedSignalData *signal_data)
351 {
352   McAccount *account;
353   AccountData *data;
354   McPresence presence, old_p;
355   TpConnectionStatus status, old_s;
356   gboolean emit_presence = FALSE, emit_connection = FALSE;
357   EmpathyAccountManager *manager = signal_data->manager;
358   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
359
360   presence = signal_data->presence;
361   status = signal_data->status;
362   account = mc_account_lookup (signal_data->unique_name);
363
364   if (account)
365     {
366       data = g_hash_table_lookup (priv->accounts, account);
367       g_assert (data);
368
369       old_p = data->presence;
370       old_s = data->status;
371
372       if (old_p != presence)
373         {
374           data->presence = presence;
375           emit_presence = TRUE;
376         }
377
378       if (old_s != status)
379         {
380           data->status = status;
381           update_connection_numbers (manager, status, old_s);
382
383           if (status == TP_CONNECTION_STATUS_CONNECTED)
384             {
385                 if (data->source_id > 0)
386                   {
387                     g_source_remove (data->source_id);
388                     data->source_id = 0;
389                   }
390
391                 data->source_id = g_timeout_add_seconds (10,
392                                                          remove_data_timeout,
393                                                          data);
394             }
395           emit_connection = TRUE;
396         }
397
398       account_manager_update_connection (manager, data, account);
399
400       if (emit_presence)
401         g_signal_emit (manager, signals[ACCOUNT_PRESENCE_CHANGED], 0,
402                        account, presence, old_p);
403
404       if (emit_connection)
405         g_signal_emit (manager, signals[ACCOUNT_CONNECTION_CHANGED], 0,
406                        account, signal_data->reason, status, old_s);
407
408       g_object_unref (account);
409     }
410
411   g_object_unref (signal_data->manager);
412   g_free (signal_data->unique_name);
413   g_slice_free (ChangedSignalData, signal_data);
414
415   return FALSE;
416 }
417
418 static void
419 account_status_changed_cb (MissionControl *mc,
420                            TpConnectionStatus status,
421                            McPresence presence,
422                            TpConnectionStatusReason reason,
423                            const gchar *unique_name,
424                            EmpathyAccountManager *manager)
425 {
426   ChangedSignalData *data;
427
428   data = g_slice_new0 (ChangedSignalData);
429   data->status = status;
430   data->presence = presence;
431   data->reason = reason;
432   data->unique_name = g_strdup (unique_name);
433   data->manager = g_object_ref (manager);
434
435   g_idle_add ((GSourceFunc) account_status_changed_idle_cb, data);
436 }
437
438 static void
439 empathy_account_manager_init (EmpathyAccountManager *manager)
440 {
441   EmpathyAccountManagerPriv *priv;
442   GList *mc_accounts, *l;
443
444   priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
445                                       EMPATHY_TYPE_ACCOUNT_MANAGER, EmpathyAccountManagerPriv);
446   manager->priv = priv;
447   priv->monitor = mc_account_monitor_new ();
448   priv->mc = empathy_mission_control_dup_singleton ();
449   priv->connected = priv->connecting = 0;
450   priv->dispose_run = FALSE;
451
452   priv->accounts = g_hash_table_new_full (empathy_account_hash,
453                                           empathy_account_equal,
454                                           g_object_unref, 
455                                           (GDestroyNotify) account_data_free);
456   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
457                                              g_object_unref, g_object_unref);
458
459   mc_accounts = mc_accounts_list ();
460
461   for (l = mc_accounts; l; l = l->next)
462     account_created_cb (priv->monitor, (char *) mc_account_get_unique_name (l->data), manager);
463
464   g_signal_connect (priv->monitor, "account-created",
465                     G_CALLBACK (account_created_cb), manager);
466   g_signal_connect (priv->monitor, "account-deleted",
467                     G_CALLBACK (account_deleted_cb), manager);
468   g_signal_connect (priv->monitor, "account-disabled",
469                     G_CALLBACK (account_disabled_cb), manager);
470   g_signal_connect (priv->monitor, "account-enabled",
471                     G_CALLBACK (account_enabled_cb), manager);
472   g_signal_connect (priv->monitor, "account-changed",
473                     G_CALLBACK (account_changed_cb), manager);
474
475   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
476                                G_CALLBACK (account_status_changed_cb),
477                                manager, NULL);
478
479   mc_accounts_list_free (mc_accounts);
480 }
481
482 static void
483 do_finalize (GObject *obj)
484 {
485   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
486   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
487
488   g_hash_table_unref (priv->accounts);
489   g_hash_table_unref (priv->connections);
490
491   G_OBJECT_CLASS (empathy_account_manager_parent_class)->finalize (obj);
492 }
493
494 static void
495 do_dispose (GObject *obj)
496 {
497   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
498   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
499
500   if (priv->dispose_run)
501     return;
502
503   priv->dispose_run = TRUE;
504
505   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
506                                   "AccountStatusChanged",
507                                   G_CALLBACK (account_status_changed_cb),
508                                   obj);
509
510   if (priv->monitor)
511     {
512       g_signal_handlers_disconnect_by_func (priv->monitor,
513                                             account_created_cb, obj);
514       g_signal_handlers_disconnect_by_func (priv->monitor,
515                                             account_deleted_cb, obj);
516       g_signal_handlers_disconnect_by_func (priv->monitor,
517                                             account_disabled_cb, obj);
518       g_signal_handlers_disconnect_by_func (priv->monitor,
519                                             account_enabled_cb, obj);
520       g_signal_handlers_disconnect_by_func (priv->monitor,
521                                             account_changed_cb, obj);
522       g_object_unref (priv->monitor);
523       priv->monitor = NULL;
524     }
525
526   if (priv->mc)
527     g_object_unref (priv->mc);
528
529   g_hash_table_remove_all (priv->accounts);
530
531   G_OBJECT_CLASS (empathy_account_manager_parent_class)->dispose (obj);
532 }
533
534 static GObject*
535 do_constructor (GType type,
536                 guint n_construct_params,
537                 GObjectConstructParam *construct_params)
538 {
539   GObject *retval;
540
541   if (!manager_singleton)
542     {
543       retval = G_OBJECT_CLASS (empathy_account_manager_parent_class)->constructor (type,
544                                                                                    n_construct_params,
545                                                                                    construct_params);
546       manager_singleton = EMPATHY_ACCOUNT_MANAGER (retval);
547       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
548     }
549   else
550     {
551       retval = g_object_ref (manager_singleton);
552     }
553
554   return retval;
555 }
556
557 static void
558 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
559 {
560   GObjectClass *oclass = G_OBJECT_CLASS (klass);
561
562   oclass->finalize = do_finalize;
563   oclass->dispose = do_dispose;
564   oclass->constructor = do_constructor;
565
566   signals[ACCOUNT_CREATED] =
567     g_signal_new ("account-created",
568                   G_TYPE_FROM_CLASS (klass),
569                   G_SIGNAL_RUN_LAST,
570                   0,
571                   NULL, NULL,
572                   g_cclosure_marshal_VOID__OBJECT,
573                   G_TYPE_NONE,
574                   1, MC_TYPE_ACCOUNT);
575
576   signals[ACCOUNT_DELETED] =
577     g_signal_new ("account-deleted",
578                   G_TYPE_FROM_CLASS (klass),
579                   G_SIGNAL_RUN_LAST,
580                   0,
581                   NULL, NULL,
582                   g_cclosure_marshal_VOID__OBJECT,
583                   G_TYPE_NONE,
584                   1, MC_TYPE_ACCOUNT);
585
586   signals[ACCOUNT_ENABLED] =
587     g_signal_new ("account-enabled",
588                   G_TYPE_FROM_CLASS (klass),
589                   G_SIGNAL_RUN_LAST,
590                   0,
591                   NULL, NULL,
592                   g_cclosure_marshal_VOID__OBJECT,
593                   G_TYPE_NONE,
594                   1, MC_TYPE_ACCOUNT);
595
596   signals[ACCOUNT_DISABLED] =
597     g_signal_new ("account-disabled",
598                   G_TYPE_FROM_CLASS (klass),
599                   G_SIGNAL_RUN_LAST,
600                   0,
601                   NULL, NULL,
602                   g_cclosure_marshal_VOID__OBJECT,
603                   G_TYPE_NONE,
604                   1, MC_TYPE_ACCOUNT);
605
606   signals[ACCOUNT_CHANGED] =
607     g_signal_new ("account-changed",
608                   G_TYPE_FROM_CLASS (klass),
609                   G_SIGNAL_RUN_LAST,
610                   0,
611                   NULL, NULL,
612                   g_cclosure_marshal_VOID__OBJECT,
613                   G_TYPE_NONE,
614                   1, MC_TYPE_ACCOUNT);
615
616   signals[ACCOUNT_CONNECTION_CHANGED] =
617     g_signal_new ("account-connection-changed",
618                   G_TYPE_FROM_CLASS (klass),
619                   G_SIGNAL_RUN_LAST,
620                   0,
621                   NULL, NULL,
622                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
623                   G_TYPE_NONE,
624                   4, MC_TYPE_ACCOUNT,
625                   G_TYPE_INT,   /* reason */
626                   G_TYPE_UINT,  /* actual connection */
627                   G_TYPE_UINT); /* previous connection */
628
629   signals[ACCOUNT_PRESENCE_CHANGED] =
630     g_signal_new ("account-presence-changed",
631                   G_TYPE_FROM_CLASS (klass),
632                   G_SIGNAL_RUN_LAST,
633                   0,
634                   NULL, NULL,
635                   _empathy_marshal_VOID__OBJECT_INT_INT,
636                   G_TYPE_NONE,
637                   3, MC_TYPE_ACCOUNT,
638                   G_TYPE_INT,  /* actual presence */
639                   G_TYPE_INT); /* previous presence */
640
641   signals[NEW_CONNECTION] =
642     g_signal_new ("new-connection",
643                   G_TYPE_FROM_CLASS (klass),
644                   G_SIGNAL_RUN_LAST,
645                   0,
646                   NULL, NULL,
647                   g_cclosure_marshal_VOID__OBJECT,
648                   G_TYPE_NONE,
649                   1, TP_TYPE_CONNECTION);
650   
651   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
652 }
653
654 /* public methods */
655
656 EmpathyAccountManager *
657 empathy_account_manager_dup_singleton (void)
658 {
659   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
660 }
661
662 int
663 empathy_account_manager_get_connected_accounts (EmpathyAccountManager *manager)
664 {
665   EmpathyAccountManagerPriv *priv;
666
667   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
668
669   priv = GET_PRIV (manager);
670
671   return priv->connected;
672 }
673
674 int
675 empathy_account_manager_get_connecting_accounts (EmpathyAccountManager *manager)
676 {
677   EmpathyAccountManagerPriv *priv;
678
679   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
680
681   priv = GET_PRIV (manager);
682
683   return priv->connecting;
684 }
685
686 gboolean
687 empathy_account_manager_is_account_just_connected (EmpathyAccountManager *manager,
688                                                    McAccount *account)
689 {
690   EmpathyAccountManagerPriv *priv;
691   AccountData *data;
692
693   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), FALSE);
694
695   priv = GET_PRIV (manager);
696   data = g_hash_table_lookup (priv->accounts, account);
697
698   g_assert (data);
699
700   return (data->source_id > 0);
701 }
702
703 /**
704  * empathy_account_manager_get_count:
705  * @manager: a #EmpathyAccountManager
706  *
707  * Get the number of accounts.
708  *
709  * Returns: the number of accounts.
710  **/
711 int
712 empathy_account_manager_get_count (EmpathyAccountManager *manager)
713 {
714   EmpathyAccountManagerPriv *priv;
715
716   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
717
718   priv = GET_PRIV (manager);
719
720   return g_hash_table_size (priv->accounts);
721 }
722
723 McAccount *
724 empathy_account_manager_get_account (EmpathyAccountManager *manager,
725                                      TpConnection          *connection)
726 {
727   EmpathyAccountManagerPriv *priv;
728
729   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
730
731   priv = GET_PRIV (manager);
732
733   return g_hash_table_lookup (priv->connections, connection);
734 }
735
736 GList *
737 empathy_account_manager_dup_accounts (EmpathyAccountManager *manager)
738 {
739   EmpathyAccountManagerPriv *priv;
740   GList *ret;
741
742   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
743
744   priv = GET_PRIV (manager);
745
746   ret = g_hash_table_get_keys (priv->accounts);
747   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
748
749   return ret;
750 }
751
752 /**
753  * empathy_account_manager_get_connection:
754  * @manager: a #EmpathyAccountManager
755  * @account: a #McAccount
756  *
757  * Get the connection of the accounts, or NULL if account is offline or the
758  * connection is not yet ready.
759  *
760  * Returns: the connection of the accounts.
761  **/
762 TpConnection *
763 empathy_account_manager_get_connection (EmpathyAccountManager *manager,
764                                         McAccount *account)
765 {
766   EmpathyAccountManagerPriv *priv;
767   AccountData *data;
768
769   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
770   g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
771
772   priv = GET_PRIV (manager);
773
774   data = g_hash_table_lookup (priv->accounts, account);
775   if (data && data->connection && tp_connection_is_ready (data->connection))
776     return data->connection;
777
778   return NULL;
779 }
780
781 GList *
782 empathy_account_manager_dup_connections (EmpathyAccountManager *manager)
783 {
784   EmpathyAccountManagerPriv *priv;
785   GHashTableIter iter;
786   gpointer connection;
787   GList *ret = NULL;
788
789   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
790
791   priv = GET_PRIV (manager);
792
793   g_hash_table_iter_init (&iter, priv->connections);
794   while (g_hash_table_iter_next (&iter, &connection, NULL)) 
795     if (connection != NULL && tp_connection_is_ready (connection))
796       ret = g_list_prepend (ret, g_object_ref (connection));
797
798   return ret;
799 }
800