]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-manager.c
Updated Basque language
[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_ensure_all_accounts (EmpathyAccountManager *manager,
368     GPtrArray *accounts)
369 {
370   int i, missing_accounts;
371   GHashTableIter iter;
372   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
373   gpointer value;
374   EmpathyAccount *account;
375   gboolean found = FALSE;
376   const gchar *name;
377
378   /* ensure all accounts coming from MC5 first */
379   for (i = 0; i < accounts->len; i++)
380     {
381       name = g_ptr_array_index (accounts, i);
382
383       account = empathy_account_manager_ensure_account (manager, name);
384       empathy_account_refresh_properties (account);
385     }
386
387   missing_accounts = empathy_account_manager_get_count (manager) -
388     accounts->len;
389
390   if (missing_accounts > 0)
391     {
392       /* look for accounts we have and the Tp AccountManager doesn't,
393        * and remove them from our cache.
394        */
395
396       DEBUG ("%d missing accounts", missing_accounts);
397
398       g_hash_table_iter_init (&iter, priv->accounts);
399
400       while (g_hash_table_iter_next (&iter, NULL, &value) &&
401              missing_accounts > 0)
402         {
403           account = value;
404
405           /* look for this account in the AccountManager provided array */
406           for (i = 0; i < accounts->len; i++)
407             {
408               name = g_ptr_array_index (accounts, i);
409
410               if (!tp_strdiff
411                   (name, empathy_account_get_unique_name (account)))
412                 {
413                   found = TRUE;
414                   break;
415                 }
416             }
417
418           if (!found)
419             {
420               DEBUG ("Account %s was not found, remove it from the cache",
421                      empathy_account_get_unique_name (account));
422
423               g_object_ref (account);
424               g_hash_table_iter_remove (&iter);
425               g_signal_emit (manager, signals[ACCOUNT_DELETED], 0, account);
426               g_object_unref (account);
427
428               missing_accounts--;
429             }
430
431           found = FALSE;
432         }
433     }
434 }
435
436 static void
437 account_manager_got_all_cb (TpProxy *proxy,
438     GHashTable *properties,
439     const GError *error,
440     gpointer user_data,
441     GObject *weak_object)
442 {
443   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
444   GPtrArray *accounts;
445
446   if (error != NULL)
447     {
448       DEBUG ("Failed to get account manager properties: %s", error->message);
449       return;
450     }
451
452   accounts = tp_asv_get_boxed (properties, "ValidAccounts",
453     EMPATHY_ARRAY_TYPE_OBJECT);
454
455   if (accounts != NULL)
456     account_manager_ensure_all_accounts (manager, accounts);
457
458   empathy_account_manager_check_ready (manager);
459 }
460
461 static void
462 account_validity_changed_cb (TpAccountManager *proxy,
463     const gchar *path,
464     gboolean valid,
465     gpointer user_data,
466     GObject *weak_object)
467 {
468   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
469
470   if (!valid)
471     return;
472
473   empathy_account_manager_ensure_account (manager, path);
474 }
475
476 static void
477 account_manager_start_mc5 (TpDBusDaemon *bus)
478 {
479   TpProxy *mc5_proxy;
480
481   /* trigger MC5 starting */
482   mc5_proxy = g_object_new (TP_TYPE_PROXY,
483     "dbus-daemon", bus,
484     "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (bus)),
485     "bus-name", MC5_BUS_NAME,
486     "object-path", "/",
487     NULL);
488
489   tp_cli_dbus_peer_call_ping (mc5_proxy, -1, NULL, NULL, NULL, NULL);
490
491   g_object_unref (mc5_proxy);
492 }
493
494 static void
495 account_manager_name_owner_cb (TpDBusDaemon *proxy,
496     const gchar *name,
497     const gchar *new_owner,
498     gpointer user_data)
499 {
500   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (user_data);
501   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
502
503   DEBUG ("Name owner changed for %s, new name: %s", name, new_owner);
504
505   if (EMP_STR_EMPTY (new_owner))
506     {
507       /* MC5 quit or crashed for some reason, let's start it again */
508       account_manager_start_mc5 (priv->dbus);
509
510       if (priv->tp_manager != NULL)
511         g_object_unref (priv->tp_manager);
512
513       priv->tp_manager = NULL;
514       return;
515     }
516
517   if (priv->tp_manager == NULL)
518     {
519       priv->tp_manager = tp_account_manager_new (priv->dbus);
520
521       tp_cli_account_manager_connect_to_account_validity_changed (
522           priv->tp_manager,
523           account_validity_changed_cb,
524           NULL,
525           NULL,
526           G_OBJECT (manager),
527           NULL);
528
529       tp_cli_dbus_properties_call_get_all (priv->tp_manager, -1,
530           TP_IFACE_ACCOUNT_MANAGER,
531           account_manager_got_all_cb,
532           NULL,
533           NULL,
534           G_OBJECT (manager));
535     }
536 }
537
538 static void
539 empathy_account_manager_init (EmpathyAccountManager *manager)
540 {
541   EmpathyAccountManagerPriv *priv;
542
543   priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
544       EMPATHY_TYPE_ACCOUNT_MANAGER, EmpathyAccountManagerPriv);
545
546   manager->priv = priv;
547   priv->connected = priv->connecting = 0;
548   priv->global_presence = TP_CONNECTION_PRESENCE_TYPE_UNSET;
549
550   priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
551       g_free, (GDestroyNotify) g_object_unref);
552
553   priv->create_results = g_hash_table_new (g_direct_hash, g_direct_equal);
554
555   priv->dbus = tp_dbus_daemon_dup (NULL);
556
557   tp_dbus_daemon_watch_name_owner (priv->dbus,
558       TP_ACCOUNT_MANAGER_BUS_NAME,
559       account_manager_name_owner_cb,
560       manager,
561       NULL);
562
563   account_manager_start_mc5 (priv->dbus);
564 }
565
566 static void
567 do_finalize (GObject *obj)
568 {
569   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
570   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
571
572   g_hash_table_destroy (priv->create_results);
573   g_hash_table_destroy (priv->accounts);
574
575   g_free (priv->global_status);
576   g_free (priv->global_status_message);
577
578   g_free (priv->requested_status);
579   g_free (priv->requested_status_message);
580
581   G_OBJECT_CLASS (empathy_account_manager_parent_class)->finalize (obj);
582 }
583
584 static void
585 do_dispose (GObject *obj)
586 {
587   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (obj);
588   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
589   GHashTableIter iter;
590   GSimpleAsyncResult *result;
591
592   if (priv->dispose_run)
593     return;
594
595   priv->dispose_run = TRUE;
596
597   /* the manager is being destroyed while there are account creation
598    * processes pending; this should not happen, but emit the callbacks
599    * with an error anyway.
600    */
601   g_hash_table_iter_init (&iter, priv->create_results);
602   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &result))
603     {
604       g_simple_async_result_set_error (result, G_IO_ERROR,
605           G_IO_ERROR_CANCELLED, "The account manager was disposed while "
606           "creating the account");
607       g_simple_async_result_complete (result);
608       g_object_unref (result);
609     }
610   g_hash_table_remove_all (priv->create_results);
611
612   if (priv->dbus != NULL)
613     {
614       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus,
615         TP_ACCOUNT_MANAGER_BUS_NAME, account_manager_name_owner_cb, manager);
616
617       g_object_unref (priv->dbus);
618       priv->dbus = NULL;
619     }
620
621   G_OBJECT_CLASS (empathy_account_manager_parent_class)->dispose (obj);
622 }
623
624 static GObject *
625 do_constructor (GType type,
626                 guint n_construct_params,
627                 GObjectConstructParam *construct_params)
628 {
629   GObject *retval;
630
631   if (!manager_singleton)
632     {
633       retval = G_OBJECT_CLASS
634         (empathy_account_manager_parent_class)->constructor (type,
635             n_construct_params, construct_params);
636       manager_singleton = EMPATHY_ACCOUNT_MANAGER (retval);
637       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
638     }
639   else
640     {
641       retval = g_object_ref (manager_singleton);
642     }
643
644   return retval;
645 }
646
647 static void
648 do_get_property (GObject *object,
649     guint prop_id,
650     GValue *value,
651     GParamSpec *pspec)
652 {
653   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (object);
654   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
655
656   switch (prop_id)
657     {
658       case PROP_READY:
659         g_value_set_boolean (value, priv->ready);
660         break;
661       default:
662         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
663         break;
664     }
665 }
666
667 static void
668 empathy_account_manager_class_init (EmpathyAccountManagerClass *klass)
669 {
670   GObjectClass *oclass = G_OBJECT_CLASS (klass);
671
672   oclass->finalize = do_finalize;
673   oclass->dispose = do_dispose;
674   oclass->constructor = do_constructor;
675   oclass->get_property = do_get_property;
676
677   g_object_class_install_property (oclass, PROP_READY,
678     g_param_spec_boolean ("ready",
679       "Ready",
680       "Whether the initial state dump from the account manager is finished",
681       FALSE,
682       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
683
684   signals[ACCOUNT_CREATED] =
685     g_signal_new ("account-created",
686                   G_TYPE_FROM_CLASS (klass),
687                   G_SIGNAL_RUN_LAST,
688                   0,
689                   NULL, NULL,
690                   g_cclosure_marshal_VOID__OBJECT,
691                   G_TYPE_NONE,
692                   1, EMPATHY_TYPE_ACCOUNT);
693
694   signals[ACCOUNT_DELETED] =
695     g_signal_new ("account-deleted",
696                   G_TYPE_FROM_CLASS (klass),
697                   G_SIGNAL_RUN_LAST,
698                   0,
699                   NULL, NULL,
700                   g_cclosure_marshal_VOID__OBJECT,
701                   G_TYPE_NONE,
702                   1, EMPATHY_TYPE_ACCOUNT);
703
704   signals[ACCOUNT_ENABLED] =
705     g_signal_new ("account-enabled",
706                   G_TYPE_FROM_CLASS (klass),
707                   G_SIGNAL_RUN_LAST,
708                   0,
709                   NULL, NULL,
710                   g_cclosure_marshal_VOID__OBJECT,
711                   G_TYPE_NONE,
712                   1, EMPATHY_TYPE_ACCOUNT);
713
714   signals[ACCOUNT_DISABLED] =
715     g_signal_new ("account-disabled",
716                   G_TYPE_FROM_CLASS (klass),
717                   G_SIGNAL_RUN_LAST,
718                   0,
719                   NULL, NULL,
720                   g_cclosure_marshal_VOID__OBJECT,
721                   G_TYPE_NONE,
722                   1, EMPATHY_TYPE_ACCOUNT);
723
724   signals[ACCOUNT_CHANGED] =
725     g_signal_new ("account-changed",
726                   G_TYPE_FROM_CLASS (klass),
727                   G_SIGNAL_RUN_LAST,
728                   0,
729                   NULL, NULL,
730                   g_cclosure_marshal_VOID__OBJECT,
731                   G_TYPE_NONE,
732                   1, EMPATHY_TYPE_ACCOUNT);
733
734   signals[ACCOUNT_CONNECTION_CHANGED] =
735     g_signal_new ("account-connection-changed",
736                   G_TYPE_FROM_CLASS (klass),
737                   G_SIGNAL_RUN_LAST,
738                   0,
739                   NULL, NULL,
740                   _empathy_marshal_VOID__OBJECT_INT_UINT_UINT,
741                   G_TYPE_NONE,
742                   4, EMPATHY_TYPE_ACCOUNT,
743                   G_TYPE_INT,   /* reason */
744                   G_TYPE_UINT,  /* actual connection */
745                   G_TYPE_UINT); /* previous connection */
746
747   signals[GLOBAL_PRESENCE_CHANGED] =
748     g_signal_new ("global-presence-changed",
749                   G_TYPE_FROM_CLASS (klass),
750                   G_SIGNAL_RUN_LAST,
751                   0,
752                   NULL, NULL,
753                   _empathy_marshal_VOID__UINT_STRING_STRING,
754                   G_TYPE_NONE,
755                   3, G_TYPE_UINT, /* Presence type */
756                   G_TYPE_STRING,  /* status */
757                   G_TYPE_STRING); /* stauts message*/
758
759   signals[NEW_CONNECTION] =
760     g_signal_new ("new-connection",
761                   G_TYPE_FROM_CLASS (klass),
762                   G_SIGNAL_RUN_LAST,
763                   0,
764                   NULL, NULL,
765                   g_cclosure_marshal_VOID__OBJECT,
766                   G_TYPE_NONE,
767                   1, TP_TYPE_CONNECTION);
768
769   g_type_class_add_private (oclass, sizeof (EmpathyAccountManagerPriv));
770 }
771
772 /* public methods */
773
774 EmpathyAccountManager *
775 empathy_account_manager_dup_singleton (void)
776 {
777   return g_object_new (EMPATHY_TYPE_ACCOUNT_MANAGER, NULL);
778 }
779
780 gboolean
781 empathy_account_manager_is_ready (EmpathyAccountManager *manager)
782 {
783   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
784
785   return priv->ready;
786 }
787
788 int
789 empathy_account_manager_get_connected_accounts (EmpathyAccountManager *manager)
790 {
791   EmpathyAccountManagerPriv *priv;
792
793   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
794
795   priv = GET_PRIV (manager);
796
797   return priv->connected;
798 }
799
800 int
801 empathy_account_manager_get_connecting_accounts (
802     EmpathyAccountManager *manager)
803 {
804   EmpathyAccountManagerPriv *priv;
805
806   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
807
808   priv = GET_PRIV (manager);
809
810   return priv->connecting;
811 }
812
813 /**
814  * empathy_account_manager_get_count:
815  * @manager: a #EmpathyAccountManager
816  *
817  * Get the number of accounts.
818  *
819  * Returns: the number of accounts.
820  **/
821 int
822 empathy_account_manager_get_count (EmpathyAccountManager *manager)
823 {
824   EmpathyAccountManagerPriv *priv;
825
826   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
827
828   priv = GET_PRIV (manager);
829
830   return g_hash_table_size (priv->accounts);
831 }
832
833 EmpathyAccount *
834 empathy_account_manager_get_account_for_connection (
835     EmpathyAccountManager *manager,
836     TpConnection          *connection)
837 {
838   EmpathyAccountManagerPriv *priv;
839   GHashTableIter iter;
840   gpointer value;
841
842   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), 0);
843
844   priv = GET_PRIV (manager);
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
851       if (connection == empathy_account_get_connection (account))
852           return account;
853     }
854
855   return NULL;
856 }
857
858 GList *
859 empathy_account_manager_dup_accounts (EmpathyAccountManager *manager)
860 {
861   EmpathyAccountManagerPriv *priv;
862   GList *ret;
863
864   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
865
866   priv = GET_PRIV (manager);
867
868   ret = g_hash_table_get_values (priv->accounts);
869   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
870
871   return ret;
872 }
873
874 /**
875  * empathy_account_manager_dup_connections:
876  * @manager: a #EmpathyAccountManager
877  *
878  * Get a #GList of all ready #TpConnection. The list must be freed with
879  * g_list_free, and its elements must be unreffed.
880  *
881  * Returns: the list of connections
882  **/
883 GList *
884 empathy_account_manager_dup_connections (EmpathyAccountManager *manager)
885 {
886   EmpathyAccountManagerPriv *priv;
887   GHashTableIter iter;
888   gpointer value;
889   GList *ret = NULL;
890
891   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_MANAGER (manager), NULL);
892
893   priv = GET_PRIV (manager);
894
895   g_hash_table_iter_init (&iter, priv->accounts);
896   while (g_hash_table_iter_next (&iter, NULL, &value))
897     {
898       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
899       TpConnection *connection;
900
901       connection = empathy_account_get_connection (account);
902       if (connection != NULL)
903         ret = g_list_prepend (ret, g_object_ref (connection));
904     }
905
906   return ret;
907 }
908
909 void
910 empathy_account_manager_request_global_presence (
911   EmpathyAccountManager *manager,
912   TpConnectionPresenceType type,
913   const gchar *status,
914   const gchar *message)
915 {
916   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
917   GHashTableIter iter;
918   gpointer value;
919
920   DEBUG ("request global presence, type: %d, status: %s, message: %s",
921          type, status, message);
922
923   g_hash_table_iter_init (&iter, priv->accounts);
924   while (g_hash_table_iter_next (&iter, NULL, &value))
925     {
926       EmpathyAccount *account = EMPATHY_ACCOUNT (value);
927       gboolean ready;
928
929       g_object_get (account, "ready", &ready, NULL);
930
931       if (ready)
932         empathy_account_request_presence (account, type, status, message);
933     }
934
935   /* save the requested global presence, to use it in case we create
936    * new accounts or some accounts become ready.
937    */
938   priv->requested_presence = type;
939
940   if (tp_strdiff (priv->requested_status, status))
941     {
942       g_free (priv->requested_status);
943       priv->requested_status = g_strdup (status);
944     }
945
946   if (tp_strdiff (priv->requested_status_message, message))
947     {
948       g_free (priv->requested_status_message);
949       priv->requested_status_message = g_strdup (message);
950     }
951 }
952
953 TpConnectionPresenceType
954 empathy_account_manager_get_requested_global_presence (
955   EmpathyAccountManager *manager,
956   gchar **status,
957   gchar **message)
958 {
959   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
960
961   if (status != NULL)
962     *status = g_strdup (priv->requested_status);
963   if (message != NULL)
964     *message = g_strdup (priv->requested_status_message);
965
966   return priv->requested_presence;
967 }
968
969 TpConnectionPresenceType
970 empathy_account_manager_get_global_presence (
971   EmpathyAccountManager *manager,
972   gchar **status,
973   gchar **message)
974 {
975   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
976
977   if (status != NULL)
978     *status = g_strdup (priv->global_status);
979   if (message != NULL)
980     *message = g_strdup (priv->global_status_message);
981
982   return priv->global_presence;
983 }
984
985 static void
986 empathy_account_manager_created_cb (TpAccountManager *proxy,
987     const gchar *account_path,
988     const GError *error,
989     gpointer user_data,
990     GObject *weak_object)
991 {
992   EmpathyAccountManager *manager = EMPATHY_ACCOUNT_MANAGER (weak_object);
993   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
994   GSimpleAsyncResult *my_res = user_data;
995   EmpathyAccount *account;
996
997   if (error != NULL)
998     {
999       g_simple_async_result_set_from_error (my_res,
1000           (GError *) error);
1001       g_simple_async_result_complete (my_res);
1002       g_object_unref (my_res);
1003
1004       return;
1005     }
1006
1007   account = empathy_account_manager_ensure_account (manager, account_path);
1008
1009   g_hash_table_insert (priv->create_results, account, my_res);
1010 }
1011
1012 void
1013 empathy_account_manager_create_account_async (EmpathyAccountManager *manager,
1014     const gchar *connection_manager,
1015     const gchar *protocol,
1016     const gchar *display_name,
1017     GHashTable *parameters,
1018     GHashTable *properties,
1019     GAsyncReadyCallback callback,
1020     gpointer user_data)
1021 {
1022   EmpathyAccountManagerPriv *priv = GET_PRIV (manager);
1023   GSimpleAsyncResult *res;
1024
1025   res = g_simple_async_result_new
1026     (G_OBJECT (manager), callback, user_data,
1027      empathy_account_manager_create_account_finish);
1028
1029   tp_cli_account_manager_call_create_account (priv->tp_manager,
1030       -1,
1031       connection_manager,
1032       protocol,
1033       display_name,
1034       parameters,
1035       properties,
1036       empathy_account_manager_created_cb,
1037       res,
1038       NULL,
1039       G_OBJECT (manager));
1040 }
1041
1042 EmpathyAccount *
1043 empathy_account_manager_create_account_finish (
1044   EmpathyAccountManager *manager, GAsyncResult *result, GError **error)
1045 {
1046   EmpathyAccount *retval;
1047
1048   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1049       error))
1050     return NULL;
1051
1052   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1053     G_OBJECT (manager), empathy_account_manager_create_account_finish), NULL);
1054
1055   retval = EMPATHY_ACCOUNT (g_simple_async_result_get_op_res_gpointer (
1056     G_SIMPLE_ASYNC_RESULT (result)));
1057
1058   return retval;
1059 }
1060