]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-manager.c
No need to check if create_results exists in dispose
[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   GHashTableIter iter;
494   GSimpleAsyncResult *result;
495
496   if (priv->dispose_run)
497     return;
498
499   priv->dispose_run = TRUE;
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   g_hash_table_iter_init (&iter, priv->create_results);
506   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &result))
507     {
508       g_simple_async_result_set_error (result, G_IO_ERROR,
509           G_IO_ERROR_CANCELLED, "The account manager was disposed while "
510           "creating the account");
511       g_simple_async_result_complete (result);
512       g_object_unref (result);
513     }
514   g_hash_table_remove_all (priv->create_results);
515
516   if (priv->dbus != NULL)
517     {
518       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus,
519         TP_ACCOUNT_MANAGER_BUS_NAME, account_manager_name_owner_cb, manager);
520
521       g_object_unref (priv->dbus);
522       priv->dbus = NULL;
523     }
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 do_get_property (GObject *object,
553     guint prop_id,
554     GValue *value,
555     GParamSpec *pspec)
556 {
557   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (object);
558   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
559
560   switch (prop_id)
561     {
562       case PROP_READY:
563         g_value_set_boolean (value, priv->ready);
564         break;
565       default:
566         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567         break;
568     }
569 }
570
571 static void
572 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
573 {
574   GObjectClass *oclass = G_OBJECT_CLASS (klass);
575
576   oclass->finalize = do_finalize;
577   oclass->dispose = do_dispose;
578   oclass->constructor = do_constructor;
579   oclass->get_property = do_get_property;
580
581   g_object_class_install_property (oclass, PROP_READY,
582     g_param_spec_boolean ("ready",
583       "Ready",
584       "Whether the initial state dump from the account manager is finished",
585       FALSE,
586       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
587
588   signals[ACCOUNT_CREATED] =
589     g_signal_new ("account-created",
590                   G_TYPE_FROM_CLASS (klass),
591                   G_SIGNAL_RUN_LAST,
592                   0,
593                   NULL, NULL,
594                   g_cclosure_marshal_VOID__OBJECT,
595                   G_TYPE_NONE,
596                   1, EMPATHY_TYPE_ACCOUNT);
597
598   signals[ACCOUNT_DELETED] =
599     g_signal_new ("account-deleted",
600                   G_TYPE_FROM_CLASS (klass),
601                   G_SIGNAL_RUN_LAST,
602                   0,
603                   NULL, NULL,
604                   g_cclosure_marshal_VOID__OBJECT,
605                   G_TYPE_NONE,
606                   1, EMPATHY_TYPE_ACCOUNT);
607
608   signals[ACCOUNT_ENABLED] =
609     g_signal_new ("account-enabled",
610                   G_TYPE_FROM_CLASS (klass),
611                   G_SIGNAL_RUN_LAST,
612                   0,
613                   NULL, NULL,
614                   g_cclosure_marshal_VOID__OBJECT,
615                   G_TYPE_NONE,
616                   1, EMPATHY_TYPE_ACCOUNT);
617
618   signals[ACCOUNT_DISABLED] =
619     g_signal_new ("account-disabled",
620                   G_TYPE_FROM_CLASS (klass),
621                   G_SIGNAL_RUN_LAST,
622                   0,
623                   NULL, NULL,
624                   g_cclosure_marshal_VOID__OBJECT,
625                   G_TYPE_NONE,
626                   1, EMPATHY_TYPE_ACCOUNT);
627
628   signals[ACCOUNT_CHANGED] =
629     g_signal_new ("account-changed",
630                   G_TYPE_FROM_CLASS (klass),
631                   G_SIGNAL_RUN_LAST,
632                   0,
633                   NULL, NULL,
634                   g_cclosure_marshal_VOID__OBJECT,
635                   G_TYPE_NONE,
636                   1, EMPATHY_TYPE_ACCOUNT);
637
638   signals[ACCOUNT_CONNECTION_CHANGED] =
639     g_signal_new ("account-connection-changed",
640                   G_TYPE_FROM_CLASS (klass),
641                   G_SIGNAL_RUN_LAST,
642                   0,
643                   NULL, NULL,
644                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
645                   G_TYPE_NONE,
646                   4, EMPATHY_TYPE_ACCOUNT,
647                   G_TYPE_INT,   /* reason */
648                   G_TYPE_UINT,  /* actual connection */
649                   G_TYPE_UINT); /* previous connection */
650
651   signals[GLOBAL_PRESENCE_CHANGED] =
652     g_signal_new ("global-presence-changed",
653                   G_TYPE_FROM_CLASS (klass),
654                   G_SIGNAL_RUN_LAST,
655                   0,
656                   NULL, NULL,
657                   _empathy_marshal_VOID__UINT_STRING_STRING,
658                   G_TYPE_NONE,
659                   3, G_TYPE_UINT, /* Presence type */
660                   G_TYPE_STRING,  /* status */
661                   G_TYPE_STRING); /* stauts message*/
662
663   signals[NEW_CONNECTION] =
664     g_signal_new ("new-connection",
665                   G_TYPE_FROM_CLASS (klass),
666                   G_SIGNAL_RUN_LAST,
667                   0,
668                   NULL, NULL,
669                   g_cclosure_marshal_VOID__OBJECT,
670                   G_TYPE_NONE,
671                   1, TP_TYPE_CONNECTION);
672
673   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
674 }
675
676 /* public methods */
677
678 EmpathyAccountManager *
679 empathy_account_manager_dup_singleton (void)
680 {
681   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
682 }
683
684 gboolean
685 empathy_account_manager_is_ready (EmpathyAccountManager *manager)
686 {
687   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
688
689   return priv->ready;
690 }
691
692 int
693 empathy_account_manager_get_connected_accounts (EmpathyAccountManager *manager)
694 {
695   EmpathyAccountManagerPriv *priv;
696
697   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
698
699   priv = GET_PRIV (manager);
700
701   return priv->connected;
702 }
703
704 int
705 empathy_account_manager_get_connecting_accounts (EmpathyAccountManager *manager)
706 {
707   EmpathyAccountManagerPriv *priv;
708
709   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
710
711   priv = GET_PRIV (manager);
712
713   return priv->connecting;
714 }
715
716 /**
717  * empathy_account_manager_get_count:
718  * @manager: a #EmpathyAccountManager
719  *
720  * Get the number of accounts.
721  *
722  * Returns: the number of accounts.
723  **/
724 int
725 empathy_account_manager_get_count (EmpathyAccountManager *manager)
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_size (priv->accounts);
734 }
735
736 EmpathyAccount *
737 empathy_account_manager_get_account_for_connection (
738     EmpathyAccountManager *manager,
739     TpConnection          *connection)
740 {
741   EmpathyAccountManagerPriv *priv;
742   GHashTableIter iter;
743   gpointer value;
744
745   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
746
747   priv = GET_PRIV (manager);
748
749   g_hash_table_iter_init (&iter, priv->accounts);
750   while (g_hash_table_iter_next (&iter, NULL, &value))
751     {
752       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
753
754       if (connection == empathy_account_get_connection (account))
755           return account;
756     }
757
758   return NULL;
759 }
760
761 EmpathyAccount *
762 empathy_account_manager_get_account (EmpathyAccountManager *manager,
763     const gchar *unique_name)
764 {
765   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
766
767   return g_hash_table_lookup (priv->accounts, unique_name);
768 }
769
770 GList *
771 empathy_account_manager_dup_accounts (EmpathyAccountManager *manager)
772 {
773   EmpathyAccountManagerPriv *priv;
774   GList *ret;
775
776   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
777
778   priv = GET_PRIV (manager);
779
780   ret = g_hash_table_get_values (priv->accounts);
781   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
782
783   return ret;
784 }
785
786 /**
787  * empathy_account_manager_dup_connections:
788  * @manager: a #EmpathyAccountManager
789  *
790  * Get a #GList of all ready #TpConnection. The list must be freed with
791  * g_list_free, and its elements must be unreffed.
792  *
793  * Returns: the list of connections
794  **/
795 GList *
796 empathy_account_manager_dup_connections (EmpathyAccountManager *manager)
797 {
798   EmpathyAccountManagerPriv *priv;
799   GHashTableIter iter;
800   gpointer value;
801   GList *ret = NULL;
802
803   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
804
805   priv = GET_PRIV (manager);
806
807   g_hash_table_iter_init (&iter, priv->accounts);
808   while (g_hash_table_iter_next (&iter, NULL, &value))
809     {
810       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
811       TpConnection *connection;
812
813       connection = empathy_account_get_connection (account);
814       if (connection != NULL)
815         ret = g_list_prepend (ret, g_object_ref (connection));
816     }
817
818   return ret;
819 }
820
821 void
822 empathy_account_manager_remove (EmpathyAccountManager *manager,
823     EmpathyAccount *account)
824 {
825   /* FIXME */
826 }
827
828
829 void
830 empathy_account_manager_request_global_presence (
831   EmpathyAccountManager *manager,
832   TpConnectionPresenceType type,
833   const gchar *status,
834   const gchar *message)
835 {
836   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
837   GHashTableIter iter;
838   gpointer value;
839
840   DEBUG ("request global presence, type: %d, status: %s, message: %s",
841          type, status, message);
842
843   g_hash_table_iter_init (&iter, priv->accounts);
844   while (g_hash_table_iter_next (&iter, NULL, &value))
845     {
846       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
847       gboolean ready;
848
849       g_object_get (account, "ready", &ready, NULL);
850
851       if (ready)
852         empathy_account_request_presence (account, type, status, message);
853     }
854
855   /* save the requested global presence, to use it in case we create
856    * new accounts or some accounts become ready.
857    */
858   priv->desired_presence = type;
859
860   if (tp_strdiff (priv->desired_status, status))
861     {
862       g_free (priv->desired_status);
863       priv->desired_status = g_strdup (status);
864     }
865
866   if (tp_strdiff (priv->desired_status_message, message))
867     {
868       g_free (priv->desired_status_message);
869       priv->desired_status_message = g_strdup (message);
870     }
871 }
872
873 TpConnectionPresenceType
874 empathy_account_manager_get_global_presence (
875   EmpathyAccountManager *manager,
876   gchar **status,
877   gchar **message)
878 {
879   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
880
881   if (status != NULL)
882     *status = g_strdup (priv->global_status);
883   if (message != NULL)
884     *message = g_strdup (priv->global_status_message);
885
886   return priv->global_presence;
887 }
888
889 static void
890 empathy_account_manager_created_cb (TpAccountManager *proxy,
891     const gchar *account_path,
892     const GError *error,
893     gpointer user_data,
894     GObject *weak_object)
895 {
896   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
897   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
898   GSimpleAsyncResult *my_res = user_data;
899   EmpathyAccount *account;
900
901   if (error != NULL)
902     {
903       g_simple_async_result_set_from_error (my_res,
904           (GError *) error);
905       g_simple_async_result_complete (my_res);
906       g_object_unref (my_res);
907
908       return;
909     }
910
911   account = account_manager_add_account (manager, account_path);
912
913   g_hash_table_insert (priv->create_results, account, my_res);
914 }
915
916 void
917 empathy_account_manager_create_account_async (EmpathyAccountManager *manager,
918     const gchar *connection_manager,
919     const gchar *protocol,
920     const gchar *display_name,
921     GHashTable *parameters,
922     GHashTable *properties,
923     GAsyncReadyCallback callback,
924     gpointer user_data)
925 {
926   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
927   GSimpleAsyncResult *res;
928
929   res = g_simple_async_result_new
930     (G_OBJECT (manager), callback, user_data,
931      empathy_account_manager_create_account_finish);
932
933   tp_cli_account_manager_call_create_account (priv->tp_manager,
934       -1,
935       connection_manager,
936       protocol,
937       display_name,
938       parameters,
939       properties,
940       empathy_account_manager_created_cb,
941       res,
942       NULL,
943       G_OBJECT (manager));
944 }
945
946 EmpathyAccount *
947 empathy_account_manager_create_account_finish (
948   EmpathyAccountManager *manager, GAsyncResult *result, GError **error)
949 {
950   EmpathyAccount *retval;
951
952   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
953       error))
954     return NULL;
955
956   g_return_val_if_fail (g_simple_async_result_is_valid (result,
957     G_OBJECT (manager), empathy_account_manager_create_account_finish), NULL);
958
959   retval = EMPATHY_ACCOUNT (g_simple_async_result_get_op_res_gpointer (
960     G_SIMPLE_ASYNC_RESULT (result)));
961
962   return retval;
963 }
964