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