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