]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-manager.c
Merge branch 'mc5', fixes bug #590165
[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  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
20  */
21
22 #include "config.h"
23
24 #include <telepathy-glib/util.h>
25 #include <telepathy-glib/account-manager.h>
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/defs.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/interfaces.h>
30
31 #include "empathy-account-manager.h"
32 #include "empathy-marshal.h"
33 #include "empathy-utils.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
36 #include <libempathy/empathy-debug.h>
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountManager)
39
40 #define MC5_BUS_NAME "org.freedesktop.Telepathy.MissionControl5"
41
42 typedef struct {
43   /* (owned) unique name -> (reffed) EmpathyAccount */
44   GHashTable       *accounts;
45   int               connected;
46   int               connecting;
47   gboolean          dispose_run;
48   gboolean          ready;
49   TpAccountManager *tp_manager;
50   TpDBusDaemon *dbus;
51
52   /* global presence */
53   EmpathyAccount *global_account;
54
55   TpConnectionPresenceType global_presence;
56   gchar *global_status;
57   gchar *global_status_message;
58
59   /* requested global presence, could be different
60    * from the actual global one.
61    */
62   TpConnectionPresenceType requested_presence;
63   gchar *requested_status;
64   gchar *requested_status_message;
65
66   GHashTable *create_results;
67 } EmpathyAccountManagerPriv;
68
69 enum {
70   ACCOUNT_CREATED,
71   ACCOUNT_DELETED,
72   ACCOUNT_ENABLED,
73   ACCOUNT_DISABLED,
74   ACCOUNT_CHANGED,
75   ACCOUNT_CONNECTION_CHANGED,
76   GLOBAL_PRESENCE_CHANGED,
77   NEW_CONNECTION,
78   LAST_SIGNAL
79 };
80
81 enum {
82   PROP_READY = 1,
83 };
84
85 static guint signals[LAST_SIGNAL];
86 static EmpathyAccountManager *manager_singleton = NULL;
87
88 G_DEFINE_TYPE (EmpathyAccountManager, empathy_account_manager, G_TYPE_OBJECT);
89
90 static void
91 emp_account_connection_cb (EmpathyAccount *account,
92   GParamSpec *spec,
93   gpointer manager)
94 {
95   TpConnection *connection = empathy_account_get_connection (account);
96
97   DEBUG ("Signalling connection %p of account %s",
98       connection, empathy_account_get_unique_name (account));
99
100   if (connection != NULL)
101     g_signal_emit (manager, signals[NEW_CONNECTION], 0, connection);
102 }
103
104 static void
105 emp_account_enabled_cb (EmpathyAccount *account,
106   GParamSpec *spec,
107   gpointer manager)
108 {
109   if (empathy_account_is_enabled (account))
110     g_signal_emit (manager, signals[ACCOUNT_ENABLED], 0, account);
111   else
112     g_signal_emit (manager, signals[ACCOUNT_DISABLED], 0, account);
113 }
114
115 static void
116 emp_account_status_changed_cb (EmpathyAccount *account,
117   TpConnectionStatus old,
118   TpConnectionStatus new,
119   TpConnectionStatusReason reason,
120   gpointer user_data)
121 {
122   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
123   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
124
125   switch (old)
126     {
127       case TP_CONNECTION_STATUS_CONNECTING:
128         priv->connecting--;
129         break;
130       case TP_CONNECTION_STATUS_CONNECTED:
131         priv->connected--;
132         break;
133       default:
134         break;
135     }
136
137   switch (new)
138     {
139       case TP_CONNECTION_STATUS_CONNECTING:
140         priv->connecting++;
141         break;
142       case TP_CONNECTION_STATUS_CONNECTED:
143         priv->connected++;
144         break;
145       default:
146         break;
147     }
148
149   g_signal_emit (manager, signals[ACCOUNT_CONNECTION_CHANGED], 0,
150     account, reason, new, old);
151 }
152
153 static void
154 emp_account_manager_update_global_presence (EmpathyAccountManager *manager)
155 {
156   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
157   TpConnectionPresenceType presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
158   EmpathyAccount *account = NULL;
159   GHashTableIter iter;
160   gpointer value;
161
162   /* Make the global presence is equal to the presence of the account with the
163    * highest availability */
164
165   g_hash_table_iter_init (&iter, priv->accounts);
166   while (g_hash_table_iter_next (&iter, NULL, &value))
167     {
168       EmpathyAccount *a = EMPATHY_ACCOUNT (value);
169       TpConnectionPresenceType p;
170
171       g_object_get (a, "presence", &p, NULL);
172
173       if (tp_connection_presence_type_cmp_availability (p, presence) > 0)
174         {
175           account = a;
176           presence = p;
177         }
178     }
179
180   priv->global_account = account;
181   g_free (priv->global_status);
182   g_free (priv->global_status_message);
183
184   if (account == NULL)
185     {
186       priv->global_presence = presence;
187       priv->global_status = NULL;
188       priv->global_status_message = NULL;
189       return;
190     }
191
192   g_object_get (account,
193     "presence", &priv->global_presence,
194     "status", &priv->global_status,
195     "status-message", &priv->global_status_message,
196     NULL);
197
198   DEBUG ("Updated global presence to: %s (%d) \"%s\"",
199     priv->global_status, priv->global_presence, priv->global_status_message);
200 }
201
202 static void
203 emp_account_presence_changed_cb (EmpathyAccount *account,
204   TpConnectionPresenceType presence,
205   const gchar *status,
206   const gchar *status_message,
207   gpointer user_data)
208 {
209   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
210   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
211
212   if (tp_connection_presence_type_cmp_availability (presence,
213       priv->global_presence) > 0)
214     {
215       priv->global_account = account;
216
217       priv->global_presence = presence;
218
219       g_free (priv->global_status);
220       priv->global_status = g_strdup (status);
221
222       g_free (priv->global_status_message);
223       priv->global_status_message = g_strdup (status_message);
224
225       goto signal;
226     }
227   else if (priv->global_account == account)
228     {
229       emp_account_manager_update_global_presence (manager);
230       goto signal;
231     }
232
233   return;
234 signal:
235     g_signal_emit (manager, signals[GLOBAL_PRESENCE_CHANGED], 0,
236       priv->global_presence, priv->global_status, priv->global_status_message);
237 }
238
239 static void
240 emp_account_removed_cb (EmpathyAccount *account, gpointer user_data)
241 {
242   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
243   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
244
245   g_object_ref (account);
246   g_hash_table_remove (priv->accounts,
247     empathy_account_get_unique_name (account));
248
249   g_signal_emit (manager, signals[ACCOUNT_DELETED], 0, account);
250   g_object_unref (account);
251 }
252
253 static void
254 empathy_account_manager_check_ready (EmpathyAccountManager *manager)
255 {
256   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
257   GHashTableIter iter;
258   gpointer value;
259
260   if (priv->ready)
261     return;
262
263   g_hash_table_iter_init (&iter, priv->accounts);
264   while (g_hash_table_iter_next (&iter, NULL, &value))
265     {
266       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
267       gboolean ready;
268
269       g_object_get (account, "ready", &ready, NULL);
270
271       if (!ready)
272         return;
273     }
274
275   /* Rerequest global presence on the initial set of accounts for cases where a
276    * global presence was requested before the manager was ready */
277   if (priv->requested_presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
278     empathy_account_manager_request_global_presence (manager,
279       priv->requested_presence,
280       priv->requested_status,
281       priv->requested_status_message);
282
283   priv->ready = TRUE;
284   g_object_notify (G_OBJECT (manager), "ready");
285 }
286
287 static void
288 account_manager_account_ready_cb (GObject *obj,
289     GParamSpec *spec,
290     gpointer user_data)
291 {
292   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
293   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
294   EmpathyAccount *account = EMPATHY_ACCOUNT (obj);
295   GSimpleAsyncResult *result;
296   gboolean ready;
297
298   g_object_get (account, "ready", &ready, NULL);
299
300   if (!ready)
301     return;
302
303   /* see if there's any pending callbacks for this account */
304   result = g_hash_table_lookup (priv->create_results, account);
305   if (result != NULL)
306     {
307       g_simple_async_result_set_op_res_gpointer (
308           G_SIMPLE_ASYNC_RESULT (result), account, NULL);
309
310       g_simple_async_result_complete (result);
311
312       g_hash_table_remove (priv->create_results, account);
313       g_object_unref (result);
314     }
315
316   g_signal_emit (manager, signals[ACCOUNT_CREATED], 0, account);
317
318   g_signal_connect (account, "notify::connection",
319     G_CALLBACK (emp_account_connection_cb), manager);
320
321   g_signal_connect (account, "notify::enabled",
322     G_CALLBACK (emp_account_enabled_cb), manager);
323
324   g_signal_connect (account, "status-changed",
325     G_CALLBACK (emp_account_status_changed_cb), manager);
326
327   g_signal_connect (account, "presence-changed",
328     G_CALLBACK (emp_account_presence_changed_cb), manager);
329
330   g_signal_connect (account, "removed",
331     G_CALLBACK (emp_account_removed_cb), manager);
332
333   empathy_account_manager_check_ready (manager);
334 }
335
336 EmpathyAccount *
337 empathy_account_manager_get_account (EmpathyAccountManager *manager,
338   const gchar *path)
339 {
340   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
341
342   return g_hash_table_lookup (priv->accounts, path);
343 }
344
345 EmpathyAccount *
346 empathy_account_manager_ensure_account (EmpathyAccountManager *manager,
347   const gchar *path)
348 {
349   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
350   EmpathyAccount *account;
351
352   account = g_hash_table_lookup (priv->accounts, path);
353   if (account != NULL)
354     return account;
355
356   account = empathy_account_new (priv->dbus, path);
357   g_hash_table_insert (priv->accounts, g_strdup (path), account);
358
359   g_signal_connect (account, "notify::ready",
360     G_CALLBACK (account_manager_account_ready_cb), manager);
361
362   return account;
363 }
364
365
366 static void
367 account_manager_got_all_cb (TpProxy *proxy,
368     GHashTable *properties,
369     const GError *error,
370     gpointer user_data,
371     GObject *weak_object)
372 {
373   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
374   GPtrArray *accounts;
375   int i;
376
377   if (error != NULL)
378     {
379       DEBUG ("Failed to get account manager properties: %s", error->message);
380       return;
381     }
382
383   accounts = tp_asv_get_boxed (properties, "ValidAccounts",
384     EMPATHY_ARRAY_TYPE_OBJECT);
385
386   if (accounts != NULL)
387     {
388       for (i = 0; i < accounts->len; i++)
389         {
390           gchar *name = g_ptr_array_index (accounts, i);
391
392           empathy_account_manager_ensure_account (manager, name);
393         }
394     }
395
396   empathy_account_manager_check_ready (manager);
397 }
398
399 static void
400 account_validity_changed_cb (TpAccountManager *proxy,
401     const gchar *path,
402     gboolean valid,
403     gpointer user_data,
404     GObject *weak_object)
405 {
406   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
407
408   if (!valid)
409     return;
410
411   empathy_account_manager_ensure_account (manager, path);
412 }
413
414 static void
415 account_manager_name_owner_cb (TpDBusDaemon *proxy,
416     const gchar *name,
417     const gchar *new_owner,
418     gpointer user_data)
419 {
420   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
421   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
422
423   tp_dbus_daemon_cancel_name_owner_watch (proxy, name,
424     account_manager_name_owner_cb, user_data);
425
426   priv->tp_manager = tp_account_manager_new (priv->dbus);
427
428   tp_cli_account_manager_connect_to_account_validity_changed (
429       priv->tp_manager,
430       account_validity_changed_cb,
431       NULL,
432       NULL,
433       G_OBJECT (manager),
434       NULL);
435
436   tp_cli_dbus_properties_call_get_all (priv->tp_manager, -1,
437     TP_IFACE_ACCOUNT_MANAGER,
438     account_manager_got_all_cb,
439     NULL,
440     NULL,
441     G_OBJECT (manager));
442 }
443
444 static void
445 empathy_account_manager_init (EmpathyAccountManager *manager)
446 {
447   EmpathyAccountManagerPriv *priv;
448   TpProxy *mc5_proxy;
449
450   priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
451       EMPATHY_TYPE_ACCOUNT_MANAGER, EmpathyAccountManagerPriv);
452
453   manager->priv = priv;
454   priv->connected = priv->connecting = 0;
455   priv->global_presence = TP_CONNECTION_PRESENCE_TYPE_UNSET;
456
457   priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
458       g_free, (GDestroyNotify) g_object_unref);
459
460   priv->create_results = g_hash_table_new (g_direct_hash, g_direct_equal);
461
462   priv->dbus = tp_dbus_daemon_dup (NULL);
463
464   tp_dbus_daemon_watch_name_owner (priv->dbus,
465       TP_ACCOUNT_MANAGER_BUS_NAME,
466       account_manager_name_owner_cb,
467       manager,
468       NULL);
469
470   /* trigger MC5 starting */
471   mc5_proxy = g_object_new (TP_TYPE_PROXY,
472     "dbus-daemon", priv->dbus,
473     "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (priv->dbus)),
474     "bus-name", MC5_BUS_NAME,
475     "object-path", "/",
476     NULL);
477
478   tp_cli_dbus_peer_call_ping (mc5_proxy, -1, NULL, NULL, NULL, NULL);
479
480   g_object_unref (mc5_proxy);
481 }
482
483 static void
484 do_finalize (GObject *obj)
485 {
486   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
487   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
488
489   g_hash_table_destroy (priv->create_results);
490   g_hash_table_destroy (priv->accounts);
491
492   g_free (priv->global_status);
493   g_free (priv->global_status_message);
494
495   g_free (priv->requested_status);
496   g_free (priv->requested_status_message);
497
498   G_OBJECT_CLASS (empathy_account_manager_parent_class)->finalize (obj);
499 }
500
501 static void
502 do_dispose (GObject *obj)
503 {
504   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
505   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
506   GHashTableIter iter;
507   GSimpleAsyncResult *result;
508
509   if (priv->dispose_run)
510     return;
511
512   priv->dispose_run = TRUE;
513
514   /* the manager is being destroyed while there are account creation
515    * processes pending; this should not happen, but emit the callbacks
516    * with an error anyway.
517    */
518   g_hash_table_iter_init (&iter, priv->create_results);
519   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &result))
520     {
521       g_simple_async_result_set_error (result, G_IO_ERROR,
522           G_IO_ERROR_CANCELLED, "The account manager was disposed while "
523           "creating the account");
524       g_simple_async_result_complete (result);
525       g_object_unref (result);
526     }
527   g_hash_table_remove_all (priv->create_results);
528
529   if (priv->dbus != NULL)
530     {
531       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus,
532         TP_ACCOUNT_MANAGER_BUS_NAME, account_manager_name_owner_cb, manager);
533
534       g_object_unref (priv->dbus);
535       priv->dbus = NULL;
536     }
537
538   G_OBJECT_CLASS (empathy_account_manager_parent_class)->dispose (obj);
539 }
540
541 static GObject *
542 do_constructor (GType type,
543                 guint n_construct_params,
544                 GObjectConstructParam *construct_params)
545 {
546   GObject *retval;
547
548   if (!manager_singleton)
549     {
550       retval = G_OBJECT_CLASS (empathy_account_manager_parent_class)->constructor (type,
551                                                                                    n_construct_params,
552                                                                                    construct_params);
553       manager_singleton = EMPATHY_ACCOUNT_MANAGER (retval);
554       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
555     }
556   else
557     {
558       retval = g_object_ref (manager_singleton);
559     }
560
561   return retval;
562 }
563
564 static void
565 do_get_property (GObject *object,
566     guint prop_id,
567     GValue *value,
568     GParamSpec *pspec)
569 {
570   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (object);
571   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
572
573   switch (prop_id)
574     {
575       case PROP_READY:
576         g_value_set_boolean (value, priv->ready);
577         break;
578       default:
579         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580         break;
581     }
582 }
583
584 static void
585 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
586 {
587   GObjectClass *oclass = G_OBJECT_CLASS (klass);
588
589   oclass->finalize = do_finalize;
590   oclass->dispose = do_dispose;
591   oclass->constructor = do_constructor;
592   oclass->get_property = do_get_property;
593
594   g_object_class_install_property (oclass, PROP_READY,
595     g_param_spec_boolean ("ready",
596       "Ready",
597       "Whether the initial state dump from the account manager is finished",
598       FALSE,
599       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
600
601   signals[ACCOUNT_CREATED] =
602     g_signal_new ("account-created",
603                   G_TYPE_FROM_CLASS (klass),
604                   G_SIGNAL_RUN_LAST,
605                   0,
606                   NULL, NULL,
607                   g_cclosure_marshal_VOID__OBJECT,
608                   G_TYPE_NONE,
609                   1, EMPATHY_TYPE_ACCOUNT);
610
611   signals[ACCOUNT_DELETED] =
612     g_signal_new ("account-deleted",
613                   G_TYPE_FROM_CLASS (klass),
614                   G_SIGNAL_RUN_LAST,
615                   0,
616                   NULL, NULL,
617                   g_cclosure_marshal_VOID__OBJECT,
618                   G_TYPE_NONE,
619                   1, EMPATHY_TYPE_ACCOUNT);
620
621   signals[ACCOUNT_ENABLED] =
622     g_signal_new ("account-enabled",
623                   G_TYPE_FROM_CLASS (klass),
624                   G_SIGNAL_RUN_LAST,
625                   0,
626                   NULL, NULL,
627                   g_cclosure_marshal_VOID__OBJECT,
628                   G_TYPE_NONE,
629                   1, EMPATHY_TYPE_ACCOUNT);
630
631   signals[ACCOUNT_DISABLED] =
632     g_signal_new ("account-disabled",
633                   G_TYPE_FROM_CLASS (klass),
634                   G_SIGNAL_RUN_LAST,
635                   0,
636                   NULL, NULL,
637                   g_cclosure_marshal_VOID__OBJECT,
638                   G_TYPE_NONE,
639                   1, EMPATHY_TYPE_ACCOUNT);
640
641   signals[ACCOUNT_CHANGED] =
642     g_signal_new ("account-changed",
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, EMPATHY_TYPE_ACCOUNT);
650
651   signals[ACCOUNT_CONNECTION_CHANGED] =
652     g_signal_new ("account-connection-changed",
653                   G_TYPE_FROM_CLASS (klass),
654                   G_SIGNAL_RUN_LAST,
655                   0,
656                   NULL, NULL,
657                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
658                   G_TYPE_NONE,
659                   4, EMPATHY_TYPE_ACCOUNT,
660                   G_TYPE_INT,   /* reason */
661                   G_TYPE_UINT,  /* actual connection */
662                   G_TYPE_UINT); /* previous connection */
663
664   signals[GLOBAL_PRESENCE_CHANGED] =
665     g_signal_new ("global-presence-changed",
666                   G_TYPE_FROM_CLASS (klass),
667                   G_SIGNAL_RUN_LAST,
668                   0,
669                   NULL, NULL,
670                   _empathy_marshal_VOID__UINT_STRING_STRING,
671                   G_TYPE_NONE,
672                   3, G_TYPE_UINT, /* Presence type */
673                   G_TYPE_STRING,  /* status */
674                   G_TYPE_STRING); /* stauts message*/
675
676   signals[NEW_CONNECTION] =
677     g_signal_new ("new-connection",
678                   G_TYPE_FROM_CLASS (klass),
679                   G_SIGNAL_RUN_LAST,
680                   0,
681                   NULL, NULL,
682                   g_cclosure_marshal_VOID__OBJECT,
683                   G_TYPE_NONE,
684                   1, TP_TYPE_CONNECTION);
685
686   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
687 }
688
689 /* public methods */
690
691 EmpathyAccountManager *
692 empathy_account_manager_dup_singleton (void)
693 {
694   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
695 }
696
697 gboolean
698 empathy_account_manager_is_ready (EmpathyAccountManager *manager)
699 {
700   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
701
702   return priv->ready;
703 }
704
705 int
706 empathy_account_manager_get_connected_accounts (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 priv->connected;
715 }
716
717 int
718 empathy_account_manager_get_connecting_accounts (EmpathyAccountManager *manager)
719 {
720   EmpathyAccountManagerPriv *priv;
721
722   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
723
724   priv = GET_PRIV (manager);
725
726   return priv->connecting;
727 }
728
729 /**
730  * empathy_account_manager_get_count:
731  * @manager: a #EmpathyAccountManager
732  *
733  * Get the number of accounts.
734  *
735  * Returns: the number of accounts.
736  **/
737 int
738 empathy_account_manager_get_count (EmpathyAccountManager *manager)
739 {
740   EmpathyAccountManagerPriv *priv;
741
742   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
743
744   priv = GET_PRIV (manager);
745
746   return g_hash_table_size (priv->accounts);
747 }
748
749 EmpathyAccount *
750 empathy_account_manager_get_account_for_connection (
751     EmpathyAccountManager *manager,
752     TpConnection          *connection)
753 {
754   EmpathyAccountManagerPriv *priv;
755   GHashTableIter iter;
756   gpointer value;
757
758   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
759
760   priv = GET_PRIV (manager);
761
762   g_hash_table_iter_init (&iter, priv->accounts);
763   while (g_hash_table_iter_next (&iter, NULL, &value))
764     {
765       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
766
767       if (connection == empathy_account_get_connection (account))
768           return account;
769     }
770
771   return NULL;
772 }
773
774 GList *
775 empathy_account_manager_dup_accounts (EmpathyAccountManager *manager)
776 {
777   EmpathyAccountManagerPriv *priv;
778   GList *ret;
779
780   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
781
782   priv = GET_PRIV (manager);
783
784   ret = g_hash_table_get_values (priv->accounts);
785   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
786
787   return ret;
788 }
789
790 /**
791  * empathy_account_manager_dup_connections:
792  * @manager: a #EmpathyAccountManager
793  *
794  * Get a #GList of all ready #TpConnection. The list must be freed with
795  * g_list_free, and its elements must be unreffed.
796  *
797  * Returns: the list of connections
798  **/
799 GList *
800 empathy_account_manager_dup_connections (EmpathyAccountManager *manager)
801 {
802   EmpathyAccountManagerPriv *priv;
803   GHashTableIter iter;
804   gpointer value;
805   GList *ret = NULL;
806
807   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
808
809   priv = GET_PRIV (manager);
810
811   g_hash_table_iter_init (&iter, priv->accounts);
812   while (g_hash_table_iter_next (&iter, NULL, &value))
813     {
814       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
815       TpConnection *connection;
816
817       connection = empathy_account_get_connection (account);
818       if (connection != NULL)
819         ret = g_list_prepend (ret, g_object_ref (connection));
820     }
821
822   return ret;
823 }
824
825 void
826 empathy_account_manager_request_global_presence (
827   EmpathyAccountManager *manager,
828   TpConnectionPresenceType type,
829   const gchar *status,
830   const gchar *message)
831 {
832   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
833   GHashTableIter iter;
834   gpointer value;
835
836   DEBUG ("request global presence, type: %d, status: %s, message: %s",
837          type, status, message);
838
839   g_hash_table_iter_init (&iter, priv->accounts);
840   while (g_hash_table_iter_next (&iter, NULL, &value))
841     {
842       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
843       gboolean ready;
844
845       g_object_get (account, "ready", &ready, NULL);
846
847       if (ready)
848         empathy_account_request_presence (account, type, status, message);
849     }
850
851   /* save the requested global presence, to use it in case we create
852    * new accounts or some accounts become ready.
853    */
854   priv->requested_presence = type;
855
856   if (tp_strdiff (priv->requested_status, status))
857     {
858       g_free (priv->requested_status);
859       priv->requested_status = g_strdup (status);
860     }
861
862   if (tp_strdiff (priv->requested_status_message, message))
863     {
864       g_free (priv->requested_status_message);
865       priv->requested_status_message = g_strdup (message);
866     }
867 }
868
869 TpConnectionPresenceType
870 empathy_account_manager_get_requested_global_presence (
871   EmpathyAccountManager *manager,
872   gchar **status,
873   gchar **message)
874 {
875   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
876
877   if (status != NULL)
878     *status = g_strdup (priv->requested_status);
879   if (message != NULL)
880     *message = g_strdup (priv->requested_status_message);
881
882   return priv->requested_presence;
883 }
884
885 TpConnectionPresenceType
886 empathy_account_manager_get_global_presence (
887   EmpathyAccountManager *manager,
888   gchar **status,
889   gchar **message)
890 {
891   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
892
893   if (status != NULL)
894     *status = g_strdup (priv->global_status);
895   if (message != NULL)
896     *message = g_strdup (priv->global_status_message);
897
898   return priv->global_presence;
899 }
900
901 static void
902 empathy_account_manager_created_cb (TpAccountManager *proxy,
903     const gchar *account_path,
904     const GError *error,
905     gpointer user_data,
906     GObject *weak_object)
907 {
908   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
909   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
910   GSimpleAsyncResult *my_res = user_data;
911   EmpathyAccount *account;
912
913   if (error != NULL)
914     {
915       g_simple_async_result_set_from_error (my_res,
916           (GError *) error);
917       g_simple_async_result_complete (my_res);
918       g_object_unref (my_res);
919
920       return;
921     }
922
923   account = empathy_account_manager_ensure_account (manager, account_path);
924
925   g_hash_table_insert (priv->create_results, account, my_res);
926 }
927
928 void
929 empathy_account_manager_create_account_async (EmpathyAccountManager *manager,
930     const gchar *connection_manager,
931     const gchar *protocol,
932     const gchar *display_name,
933     GHashTable *parameters,
934     GHashTable *properties,
935     GAsyncReadyCallback callback,
936     gpointer user_data)
937 {
938   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
939   GSimpleAsyncResult *res;
940
941   res = g_simple_async_result_new
942     (G_OBJECT (manager), callback, user_data,
943      empathy_account_manager_create_account_finish);
944
945   tp_cli_account_manager_call_create_account (priv->tp_manager,
946       -1,
947       connection_manager,
948       protocol,
949       display_name,
950       parameters,
951       properties,
952       empathy_account_manager_created_cb,
953       res,
954       NULL,
955       G_OBJECT (manager));
956 }
957
958 EmpathyAccount *
959 empathy_account_manager_create_account_finish (
960   EmpathyAccountManager *manager, GAsyncResult *result, GError **error)
961 {
962   EmpathyAccount *retval;
963
964   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
965       error))
966     return NULL;
967
968   g_return_val_if_fail (g_simple_async_result_is_valid (result,
969     G_OBJECT (manager), empathy_account_manager_create_account_finish), NULL);
970
971   retval = EMPATHY_ACCOUNT (g_simple_async_result_get_op_res_gpointer (
972     G_SIMPLE_ASYNC_RESULT (result)));
973
974   return retval;
975 }
976