]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-settings.c
8a0e61fbf0fda61707711748856aa9584c3b0d6c
[empathy.git] / libempathy / empathy-account-settings.c
1 /*
2  * empathy-account-settings.c - Source for EmpathyAccountSettings
3  * Copyright (C) 2009 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <telepathy-glib/account-manager.h>
27 #include <telepathy-glib/util.h>
28 #include <telepathy-glib/interfaces.h>
29 #include <telepathy-glib/gtypes.h>
30
31 #include "empathy-account-settings.h"
32 #include "empathy-connection-managers.h"
33 #include "empathy-keyring.h"
34 #include "empathy-utils.h"
35 #include "empathy-presence-manager.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
41
42 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
43
44 enum {
45   PROP_ACCOUNT = 1,
46   PROP_CM_NAME,
47   PROP_PROTOCOL,
48   PROP_SERVICE,
49   PROP_DISPLAY_NAME,
50   PROP_DISPLAY_NAME_OVERRIDDEN,
51   PROP_READY
52 };
53
54 enum {
55   PASSWORD_RETRIEVED = 1,
56   LAST_SIGNAL
57 };
58
59 static gulong signals[LAST_SIGNAL] = { 0, };
60
61 /* private structure */
62 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
63
64 struct _EmpathyAccountSettingsPriv
65 {
66   gboolean dispose_has_run;
67   EmpathyConnectionManagers *managers;
68   TpAccountManager *account_manager;
69
70   TpConnectionManager *manager;
71   TpProtocol *protocol_obj;
72
73   TpAccount *account;
74   gchar *cm_name;
75   gchar *protocol;
76   gchar *service;
77   gchar *display_name;
78   gchar *icon_name;
79   gboolean display_name_overridden;
80   gboolean ready;
81
82   gboolean supports_sasl;
83   gboolean password_changed;
84
85   gchar *password;
86   gchar *password_original;
87
88   gboolean password_retrieved;
89   gboolean password_requested;
90
91   /* Parameter name (gchar *) -> parameter value (GVariant) */
92   GHashTable *parameters;
93   /* Keys are parameter names from the hash above (gchar *).
94    * Values are regular expresions that should match corresponding parameter
95    * values (GRegex *). Possible regexp patterns are defined in
96    * empathy-account-widget.c */
97   GHashTable *param_regexps;
98   GArray *unset_parameters;
99   GList *required_params;
100
101   gulong managers_ready_id;
102   gboolean preparing_protocol;
103
104   /* If TRUE, the account should have 'tel' in its
105    * Account.Interface.Addressing.URISchemes property. */
106   gboolean uri_scheme_tel;
107   /* If TRUE, Service property needs to be updated when applying changes */
108   gboolean update_service;
109
110   GSimpleAsyncResult *apply_result;
111 };
112
113 static void
114 empathy_account_settings_init (EmpathyAccountSettings *obj)
115 {
116   EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
117     EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
118
119   obj->priv = priv;
120
121   /* allocate any data required by the object here */
122   priv->managers = empathy_connection_managers_dup_singleton ();
123   priv->account_manager = tp_account_manager_dup ();
124
125   priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
126     g_free, (GDestroyNotify) g_variant_unref);
127
128   priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
129     g_free, (GDestroyNotify) g_regex_unref);
130
131   priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
132
133   priv->required_params = NULL;
134 }
135
136 static void empathy_account_settings_dispose (GObject *object);
137 static void empathy_account_settings_finalize (GObject *object);
138 static void empathy_account_settings_account_ready_cb (GObject *source_object,
139     GAsyncResult *result, gpointer user_data);
140 static void empathy_account_settings_managers_ready_cb (GObject *obj,
141     GParamSpec *pspec, gpointer user_data);
142 static void empathy_account_settings_check_readyness (
143     EmpathyAccountSettings *self);
144
145 static void
146 empathy_account_settings_set_property (GObject *object,
147     guint prop_id,
148     const GValue *value,
149     GParamSpec *pspec)
150 {
151   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
152   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
153
154   switch (prop_id)
155     {
156       case PROP_ACCOUNT:
157         priv->account = g_value_dup_object (value);
158         break;
159       case PROP_CM_NAME:
160         priv->cm_name = g_value_dup_string (value);
161         break;
162       case PROP_PROTOCOL:
163         priv->protocol = g_value_dup_string (value);
164         break;
165       case PROP_SERVICE:
166         priv->service = g_value_dup_string (value);
167         break;
168       case PROP_DISPLAY_NAME:
169         priv->display_name = g_value_dup_string (value);
170         break;
171       case PROP_DISPLAY_NAME_OVERRIDDEN:
172         priv->display_name_overridden = g_value_get_boolean (value);
173         break;
174       default:
175         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176         break;
177     }
178 }
179
180 static void
181 empathy_account_settings_get_property (GObject *object,
182     guint prop_id,
183     GValue *value,
184     GParamSpec *pspec)
185 {
186   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
187   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
188
189   switch (prop_id)
190     {
191       case PROP_ACCOUNT:
192         g_value_set_object (value, priv->account);
193         break;
194       case PROP_CM_NAME:
195         g_value_set_string (value, priv->cm_name);
196         break;
197       case PROP_PROTOCOL:
198         g_value_set_string (value, priv->protocol);
199         break;
200       case PROP_SERVICE:
201         g_value_set_string (value, priv->service);
202         break;
203       case PROP_DISPLAY_NAME:
204         g_value_set_string (value, priv->display_name);
205         break;
206       case PROP_DISPLAY_NAME_OVERRIDDEN:
207         g_value_set_boolean (value, priv->display_name_overridden);
208         break;
209       case PROP_READY:
210         g_value_set_boolean (value, priv->ready);
211         break;
212       default:
213         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214         break;
215     }
216 }
217
218 static void
219 empathy_account_settings_constructed (GObject *object)
220 {
221   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
222   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
223
224   if (priv->account != NULL)
225     {
226       g_free (priv->cm_name);
227       g_free (priv->protocol);
228       g_free (priv->service);
229
230       priv->cm_name =
231         g_strdup (tp_account_get_connection_manager (priv->account));
232       priv->protocol =
233         g_strdup (tp_account_get_protocol (priv->account));
234       priv->service =
235         g_strdup (tp_account_get_service (priv->account));
236       priv->icon_name = g_strdup
237         (tp_account_get_icon_name (priv->account));
238     }
239   else
240     {
241       priv->icon_name = empathy_protocol_icon_name (priv->protocol);
242     }
243
244   g_assert (priv->cm_name != NULL && priv->protocol != NULL);
245
246   empathy_account_settings_check_readyness (self);
247
248   if (!priv->ready)
249     {
250       GQuark features[] = {
251           TP_ACCOUNT_FEATURE_CORE,
252           TP_ACCOUNT_FEATURE_STORAGE,
253           TP_ACCOUNT_FEATURE_ADDRESSING,
254           0 };
255
256       if (priv->account != NULL)
257         {
258           tp_proxy_prepare_async (priv->account, features,
259               empathy_account_settings_account_ready_cb, self);
260         }
261
262       tp_g_signal_connect_object (priv->managers, "notify::ready",
263         G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
264     }
265
266   if (G_OBJECT_CLASS (
267         empathy_account_settings_parent_class)->constructed != NULL)
268     G_OBJECT_CLASS (
269         empathy_account_settings_parent_class)->constructed (object);
270 }
271
272
273 static void
274 empathy_account_settings_class_init (
275     EmpathyAccountSettingsClass *empathy_account_settings_class)
276 {
277   GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
278
279   g_type_class_add_private (empathy_account_settings_class, sizeof
280       (EmpathyAccountSettingsPriv));
281
282   object_class->dispose = empathy_account_settings_dispose;
283   object_class->finalize = empathy_account_settings_finalize;
284   object_class->set_property = empathy_account_settings_set_property;
285   object_class->get_property = empathy_account_settings_get_property;
286   object_class->constructed = empathy_account_settings_constructed;
287
288   g_object_class_install_property (object_class, PROP_ACCOUNT,
289     g_param_spec_object ("account",
290       "Account",
291       "The TpAccount backing these settings",
292       TP_TYPE_ACCOUNT,
293       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
294
295   g_object_class_install_property (object_class, PROP_CM_NAME,
296     g_param_spec_string ("connection-manager",
297       "connection-manager",
298       "The name of the connection manager this account uses",
299       NULL,
300       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
301
302   g_object_class_install_property (object_class, PROP_PROTOCOL,
303     g_param_spec_string ("protocol",
304       "Protocol",
305       "The name of the protocol this account uses",
306       NULL,
307       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
308
309   g_object_class_install_property (object_class, PROP_SERVICE,
310     g_param_spec_string ("service",
311       "Service",
312       "The service of this account, or NULL",
313       NULL,
314       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
315
316   g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
317     g_param_spec_string ("display-name",
318       "display-name",
319       "The display name account these settings belong to",
320       NULL,
321       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
322
323   g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
324       g_param_spec_boolean ("display-name-overridden",
325         "display-name-overridden",
326         "Whether the display name for this account has been manually "
327         "overridden",
328         FALSE,
329         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
330
331   g_object_class_install_property (object_class, PROP_READY,
332     g_param_spec_boolean ("ready",
333       "Ready",
334       "Whether this account is ready to be used",
335       FALSE,
336       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
337
338   signals[PASSWORD_RETRIEVED] =
339       g_signal_new ("password-retrieved",
340           G_TYPE_FROM_CLASS (empathy_account_settings_class),
341           G_SIGNAL_RUN_LAST, 0, NULL, NULL,
342           g_cclosure_marshal_generic,
343           G_TYPE_NONE, 0);
344 }
345
346 static void
347 empathy_account_settings_dispose (GObject *object)
348 {
349   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
350   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
351
352   if (priv->dispose_has_run)
353     return;
354
355   priv->dispose_has_run = TRUE;
356
357   if (priv->managers_ready_id != 0)
358     g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
359   priv->managers_ready_id = 0;
360
361   tp_clear_object (&priv->managers);
362   tp_clear_object (&priv->manager);
363   tp_clear_object (&priv->account_manager);
364   tp_clear_object (&priv->account);
365   tp_clear_object (&priv->protocol_obj);
366
367   /* release any references held by the object here */
368   if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
369     G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
370 }
371
372 static void
373 empathy_account_settings_free_unset_parameters (
374     EmpathyAccountSettings *settings)
375 {
376   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
377   guint i;
378
379   for (i = 0 ; i < priv->unset_parameters->len; i++)
380     g_free (g_array_index (priv->unset_parameters, gchar *, i));
381
382   g_array_set_size (priv->unset_parameters, 0);
383 }
384
385 static void
386 empathy_account_settings_finalize (GObject *object)
387 {
388   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
389   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
390   GList *l;
391
392   /* free any data held directly by the object here */
393   g_free (priv->cm_name);
394   g_free (priv->protocol);
395   g_free (priv->service);
396   g_free (priv->display_name);
397   g_free (priv->icon_name);
398   g_free (priv->password);
399   g_free (priv->password_original);
400
401   if (priv->required_params != NULL)
402     {
403       for (l = priv->required_params; l; l = l->next)
404         g_free (l->data);
405       g_list_free (priv->required_params);
406     }
407
408   g_hash_table_unref (priv->parameters);
409   g_hash_table_unref (priv->param_regexps);
410
411   empathy_account_settings_free_unset_parameters (self);
412   g_array_unref (priv->unset_parameters);
413
414   G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
415 }
416
417 static void
418 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
419     GAsyncResult *result,
420     gpointer user_data)
421 {
422   EmpathyAccountSettings *self = user_data;
423   GError *error = NULL;
424
425   if (!tp_proxy_prepare_finish (source, result, &error))
426     {
427       DEBUG ("Failed to prepare protocol object: %s", error->message);
428       g_clear_error (&error);
429       return;
430     }
431
432   empathy_account_settings_check_readyness (self);
433 }
434
435 static void
436 empathy_account_settings_get_password_cb (GObject *source,
437     GAsyncResult *result,
438     gpointer user_data)
439 {
440   EmpathyAccountSettings *self = user_data;
441   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
442   const gchar *password;
443   GError *error = NULL;
444
445   password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
446       result, &error);
447
448   if (error != NULL)
449     {
450       DEBUG ("Failed to get password: %s", error->message);
451       g_clear_error (&error);
452     }
453
454   /* It doesn't really matter if getting the password failed; that
455    * just means that it's not there, or let's act like that at
456    * least. */
457
458   g_assert (priv->password == NULL);
459
460   priv->password = g_strdup (password);
461   priv->password_original = g_strdup (password);
462
463   g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
464 }
465
466 static void
467 empathy_account_settings_migrate_password_cb (GObject *source,
468     GAsyncResult *result,
469     gpointer user_data)
470 {
471   TpAccount *account = TP_ACCOUNT (source);
472   GError *error = NULL;
473   EmpathyAccountSettings *self = user_data;
474   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
475   GHashTable *empty;
476   const gchar *unset[] = { "password", NULL };
477
478   if (!empathy_keyring_set_account_password_finish (account, result, &error))
479     {
480       DEBUG ("Failed to set password: %s", error->message);
481       g_clear_error (&error);
482       return;
483     }
484
485   /* Now clear the password MC has stored. */
486   empty = tp_asv_new (NULL, NULL);
487   tp_account_update_parameters_async (priv->account,
488       empty, unset, NULL, NULL);
489
490   g_hash_table_remove (priv->parameters, "password");
491
492   g_hash_table_unref (empty);
493 }
494
495 static const GValue * empathy_account_settings_get (
496     EmpathyAccountSettings *settings,
497     const gchar *param);
498
499 static void
500 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
501 {
502   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
503   const GValue *v;
504   const gchar *password;
505
506   if (!priv->supports_sasl || empathy_account_settings_get (
507           self, "password") == NULL)
508     return;
509
510   /* mission-control still has our password, although the CM
511    * supports SASL. Let's try migrating it. */
512
513   DEBUG ("Trying to migrate password parameter from MC to the "
514       "keyring ourselves for account %s",
515       tp_account_get_path_suffix (priv->account));
516
517   v = empathy_account_settings_get (self, "password");
518
519   /* I can't imagine why this would fail. */
520   if (!G_VALUE_HOLDS_STRING (v))
521     return;
522
523   password = g_value_get_string (v);
524
525   if (EMP_STR_EMPTY (password))
526     return;
527
528   empathy_keyring_set_account_password_async (priv->account, password,
529       empathy_account_settings_migrate_password_cb, self);
530
531   /* We don't want to request the password again, we
532    * already know it. */
533   priv->password_requested = TRUE;
534
535   priv->password = g_strdup (password);
536   priv->password_original = g_strdup (password);
537 }
538
539 static void
540 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
541 {
542   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
543   GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
544
545   if (priv->ready)
546     return;
547
548   if (priv->account != NULL
549       && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
550       return;
551
552   if (!empathy_connection_managers_is_ready (priv->managers))
553     return;
554
555   if (priv->manager == NULL)
556     {
557       priv->manager = empathy_connection_managers_get_cm (
558           priv->managers, priv->cm_name);
559     }
560
561   if (priv->manager == NULL)
562     return;
563
564   g_object_ref (priv->manager);
565
566   if (priv->account != NULL)
567     {
568       g_free (priv->display_name);
569       priv->display_name =
570         g_strdup (tp_account_get_display_name (priv->account));
571
572       g_free (priv->icon_name);
573       priv->icon_name =
574         g_strdup (tp_account_get_icon_name (priv->account));
575
576       priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
577     }
578
579   if (priv->protocol_obj == NULL)
580     {
581       priv->protocol_obj = g_object_ref (
582           tp_connection_manager_get_protocol_object (priv->manager,
583             priv->protocol));
584     }
585
586   if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
587       && !priv->preparing_protocol)
588     {
589       priv->preparing_protocol = TRUE;
590       tp_proxy_prepare_async (priv->protocol_obj, features,
591           empathy_account_settings_protocol_obj_prepared_cb, self);
592       return;
593     }
594   else
595     {
596       if (tp_strv_contains (tp_protocol_get_authentication_types (
597                   priv->protocol_obj),
598               TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
599         {
600           priv->supports_sasl = TRUE;
601         }
602     }
603
604   if (priv->required_params == NULL)
605     {
606       GList *params, *l;
607
608       params = tp_protocol_dup_params (priv->protocol_obj);
609       for (l = params; l != NULL; l = g_list_next (l))
610         {
611           TpConnectionManagerParam *cur = l->data;
612
613           if (tp_connection_manager_param_is_required (cur))
614             {
615               priv->required_params = g_list_append (priv->required_params,
616                   g_strdup (tp_connection_manager_param_get_name (cur)));
617             }
618         }
619
620        g_list_free_full (params,
621            (GDestroyNotify) tp_connection_manager_param_free);
622     }
623
624   /* NOTE: When removing MC migration code, remove this call, and the
625    * function it's calling. That's it. */
626   empathy_account_settings_try_migrating_password (self);
627
628   /* priv->account won't be a proper account if it's the account
629    * assistant showing this widget. */
630   if (priv->supports_sasl && !priv->password_requested
631       && priv->account != NULL)
632     {
633       priv->password_requested = TRUE;
634
635       /* Make this call but don't block on its readiness. We'll signal
636        * if it's updated later with ::password-retrieved. */
637       empathy_keyring_get_account_password_async (priv->account,
638           empathy_account_settings_get_password_cb, self);
639     }
640
641   priv->ready = TRUE;
642   g_object_notify (G_OBJECT (self), "ready");
643 }
644
645 static void
646 empathy_account_settings_account_ready_cb (GObject *source_object,
647     GAsyncResult *result,
648     gpointer user_data)
649 {
650   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
651   TpAccount *account = TP_ACCOUNT (source_object);
652   GError *error = NULL;
653
654   if (!tp_proxy_prepare_finish (account, result, &error))
655     {
656       DEBUG ("Failed to prepare account: %s", error->message);
657       g_error_free (error);
658       return;
659     }
660
661   empathy_account_settings_check_readyness (settings);
662 }
663
664 static void
665 empathy_account_settings_managers_ready_cb (GObject *object,
666     GParamSpec *pspec,
667     gpointer user_data)
668 {
669   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
670
671   empathy_account_settings_check_readyness (settings);
672 }
673
674 EmpathyAccountSettings *
675 empathy_account_settings_new (const gchar *connection_manager,
676     const gchar *protocol,
677     const gchar *service,
678     const char *display_name)
679 {
680   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
681       "connection-manager", connection_manager,
682       "protocol", protocol,
683       "service", service,
684       "display-name", display_name,
685       NULL);
686 }
687
688 EmpathyAccountSettings *
689 empathy_account_settings_new_for_account (TpAccount *account)
690 {
691   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
692       "account", account,
693       NULL);
694 }
695
696 GList *
697 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
698 {
699   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
700
701   g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
702
703   return tp_protocol_dup_params (priv->protocol_obj);
704 }
705
706 gboolean
707 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
708 {
709   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
710
711   return priv->ready;
712 }
713
714 const gchar *
715 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
716 {
717   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
718
719   return priv->cm_name;
720 }
721
722 const gchar *
723 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
724 {
725   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
726
727   return priv->protocol;
728 }
729
730 const gchar *
731 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
732 {
733   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
734
735   return priv->service;
736 }
737
738 void
739 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
740     const gchar *service)
741 {
742   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
743
744   if (!tp_strdiff (priv->service, service))
745     return;
746
747   g_free (priv->service);
748   priv->service = g_strdup (service);
749   g_object_notify (G_OBJECT (settings), "service");
750   priv->update_service = TRUE;
751 }
752
753 gchar *
754 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
755 {
756   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
757
758   return priv->icon_name;
759 }
760
761 const gchar *
762 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
763 {
764   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
765
766   return priv->display_name;
767 }
768
769 TpAccount *
770 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
771 {
772   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
773
774   return priv->account;
775 }
776
777 static gboolean
778 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
779     const gchar *param)
780 {
781   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
782   GArray *a;
783   guint i;
784
785   a = priv->unset_parameters;
786
787   for (i = 0; i < a->len; i++)
788     {
789       if (!tp_strdiff (g_array_index (a, gchar *, i), param))
790         return TRUE;
791     }
792
793   return FALSE;
794 }
795
796 static const TpConnectionManagerParam *
797 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
798     const gchar *param)
799 {
800   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
801
802   return tp_protocol_get_param (priv->protocol_obj, param);
803 }
804
805 gboolean
806 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
807     const gchar *param)
808 {
809   return (empathy_account_settings_get_tp_param (settings, param) != NULL);
810 }
811
812 static void
813 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
814     const gchar *param)
815 {
816   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
817   guint idx;
818   gchar *val;
819
820   for (idx = 0; idx < priv->unset_parameters->len; idx++)
821     {
822       val = g_array_index (priv->unset_parameters, gchar *, idx);
823
824       if (!tp_strdiff (val, param))
825         {
826           priv->unset_parameters =
827             g_array_remove_index (priv->unset_parameters, idx);
828           g_free (val);
829
830           break;
831         }
832     }
833 }
834
835 const GValue *
836 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
837     const gchar *param)
838 {
839   const TpConnectionManagerParam *p;
840
841   p = empathy_account_settings_get_tp_param (settings, param);
842
843   if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
844     return NULL;
845
846   return &(p->default_value);
847 }
848
849 const gchar *
850 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
851     const gchar *param)
852 {
853   const TpConnectionManagerParam *p;
854
855   p = empathy_account_settings_get_tp_param (settings, param);
856
857   if (p == NULL)
858     return NULL;
859
860   return tp_connection_manager_param_get_dbus_signature (p);
861 }
862
863 static const GValue *
864 empathy_account_settings_get (EmpathyAccountSettings *settings,
865     const gchar *param)
866 {
867   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
868   const GValue *result = NULL;
869   GVariant *variant;
870
871   /* Lookup the update parameters we set */
872   variant = g_hash_table_lookup (priv->parameters, param);
873   if (variant != NULL)
874     {
875       GValue v = G_VALUE_INIT;
876
877       dbus_g_value_parse_g_variant (variant, &v);
878       result = (const GValue *) tp_g_value_slice_dup (&v);
879
880       g_value_unset (&v);
881       /* FIXME: this GValue is leaked */
882       return result;
883     }
884
885   /* If the parameters isn't unset use the accounts setting if any */
886   if (priv->account != NULL
887       && !empathy_account_settings_is_unset (settings, param))
888     {
889       const GHashTable *parameters;
890
891       parameters = tp_account_get_parameters (priv->account);
892       result = tp_asv_lookup (parameters, param);
893
894       if (result != NULL)
895         return result;
896     }
897
898   /* fallback to the default */
899   return empathy_account_settings_get_default (settings, param);
900 }
901
902 void
903 empathy_account_settings_unset (EmpathyAccountSettings *settings,
904     const gchar *param)
905 {
906   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
907   gchar *v;
908   if (empathy_account_settings_is_unset (settings, param))
909     return;
910
911   if (priv->supports_sasl && !tp_strdiff (param, "password"))
912     {
913       g_free (priv->password);
914       priv->password = NULL;
915       priv->password_changed = TRUE;
916       return;
917     }
918
919   v = g_strdup (param);
920
921   g_array_append_val (priv->unset_parameters, v);
922   g_hash_table_remove (priv->parameters, param);
923 }
924
925 void
926 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
927 {
928   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
929
930   g_hash_table_remove_all (priv->parameters);
931   empathy_account_settings_free_unset_parameters (settings);
932
933   priv->password_changed = FALSE;
934   g_free (priv->password);
935   priv->password = g_strdup (priv->password_original);
936
937   if (priv->account != NULL)
938     priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
939   else
940     priv->uri_scheme_tel = FALSE;
941 }
942
943 const gchar *
944 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
945     const gchar *param)
946 {
947   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
948   const GValue *v;
949
950   if (!tp_strdiff (param, "password") && priv->supports_sasl)
951     {
952       return priv->password;
953     }
954
955   v = empathy_account_settings_get (settings, param);
956
957   if (v == NULL || !G_VALUE_HOLDS_STRING (v))
958     return NULL;
959
960   return g_value_get_string (v);
961 }
962
963 const gchar * const *
964 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
965     const gchar *param)
966 {
967   const GValue *v;
968
969   v = empathy_account_settings_get (settings, param);
970
971   if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
972     return NULL;
973
974   return g_value_get_boxed (v);
975 }
976
977 gint32
978 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
979     const gchar *param)
980 {
981   const GValue *v;
982   gint32 ret = 0;
983
984   v = empathy_account_settings_get (settings, param);
985
986   if (v == NULL)
987     return 0;
988
989   switch G_VALUE_TYPE (v)
990     {
991       case G_TYPE_UCHAR:
992         ret = g_value_get_uchar (v);
993         break;
994       case G_TYPE_INT:
995         ret = g_value_get_int (v);
996         break;
997       case G_TYPE_UINT:
998         ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
999             G_MAXINT32);
1000         break;
1001       case G_TYPE_INT64:
1002         ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
1003         break;
1004       case G_TYPE_UINT64:
1005         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
1006             G_MAXINT32);
1007         break;
1008       default:
1009         ret = 0;
1010         break;
1011     }
1012
1013   return ret;
1014 }
1015
1016 gint64
1017 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
1018     const gchar *param)
1019 {
1020   const GValue *v;
1021   gint64 ret = 0;
1022
1023   v = empathy_account_settings_get (settings, param);
1024   if (v == NULL)
1025     return 0;
1026
1027   switch G_VALUE_TYPE (v)
1028     {
1029       case G_TYPE_UCHAR:
1030         ret = g_value_get_uchar (v);
1031         break;
1032       case G_TYPE_INT:
1033         ret = g_value_get_int (v);
1034         break;
1035       case G_TYPE_UINT:
1036         ret = g_value_get_uint (v);
1037         break;
1038       case G_TYPE_INT64:
1039         ret = g_value_get_int64 (v);
1040         break;
1041       case G_TYPE_UINT64:
1042         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1043         break;
1044       default:
1045         ret = 0;
1046         break;
1047     }
1048
1049   return ret;
1050 }
1051
1052 guint32
1053 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1054     const gchar *param)
1055 {
1056   const GValue *v;
1057   guint32 ret;
1058
1059   v = empathy_account_settings_get (settings, param);
1060   if (v == NULL)
1061     return 0;
1062
1063   switch G_VALUE_TYPE (v)
1064     {
1065       case G_TYPE_UCHAR:
1066         ret = g_value_get_uchar (v);
1067         break;
1068       case G_TYPE_INT:
1069         ret = MAX (0, g_value_get_int (v));
1070         break;
1071       case G_TYPE_UINT:
1072         ret = g_value_get_uint (v);
1073         break;
1074       case G_TYPE_INT64:
1075         ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1076         break;
1077       case G_TYPE_UINT64:
1078         ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1079         break;
1080       default:
1081         ret = 0;
1082         break;
1083     }
1084
1085   return ret;
1086 }
1087
1088 guint64
1089 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1090     const gchar *param)
1091 {
1092   const GValue *v;
1093   guint64 ret = 0;
1094
1095   v = empathy_account_settings_get (settings, param);
1096
1097   if (v == NULL || !G_VALUE_HOLDS_INT (v))
1098     return 0;
1099
1100   switch G_VALUE_TYPE (v)
1101     {
1102       case G_TYPE_UCHAR:
1103         ret = g_value_get_uchar (v);
1104         break;
1105       case G_TYPE_INT:
1106         ret = MAX (0, g_value_get_int (v));
1107         break;
1108       case G_TYPE_UINT:
1109         ret = g_value_get_uint (v);
1110         break;
1111       case G_TYPE_INT64:
1112         ret = MAX (0, g_value_get_int64 (v));
1113         break;
1114       case G_TYPE_UINT64:
1115         ret = g_value_get_uint64 (v);
1116         break;
1117       default:
1118         ret = 0;
1119         break;
1120     }
1121
1122   return ret;
1123 }
1124
1125 gboolean
1126 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1127     const gchar *param)
1128 {
1129   const GValue *v;
1130
1131   v = empathy_account_settings_get (settings, param);
1132
1133   if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1134     return FALSE;
1135
1136   return g_value_get_boolean (v);
1137 }
1138
1139 void
1140 empathy_account_settings_set (EmpathyAccountSettings *settings,
1141     const gchar *param,
1142     GVariant *v)
1143 {
1144   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1145
1146   g_return_if_fail (param != NULL);
1147   g_return_if_fail (v != NULL);
1148
1149   if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1150       g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1151     {
1152       g_free (priv->password);
1153       priv->password = g_variant_dup_string (v, NULL);
1154       priv->password_changed = TRUE;
1155     }
1156   else
1157     {
1158       g_hash_table_insert (priv->parameters, g_strdup (param),
1159           g_variant_ref_sink (v));
1160     }
1161
1162   account_settings_remove_from_unset (settings, param);
1163 }
1164
1165 static void
1166 account_settings_display_name_set_cb (GObject *src,
1167     GAsyncResult *res,
1168     gpointer user_data)
1169 {
1170   GError *error = NULL;
1171   TpAccount *account = TP_ACCOUNT (src);
1172   GSimpleAsyncResult *set_result = user_data;
1173
1174   tp_account_set_display_name_finish (account, res, &error);
1175
1176   if (error != NULL)
1177     {
1178       g_simple_async_result_set_from_error (set_result, error);
1179       g_error_free (error);
1180     }
1181
1182   g_simple_async_result_complete (set_result);
1183   g_object_unref (set_result);
1184 }
1185
1186 void
1187 empathy_account_settings_set_display_name_async (
1188   EmpathyAccountSettings *settings,
1189   const gchar *name,
1190   GAsyncReadyCallback callback,
1191   gpointer user_data)
1192 {
1193   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1194   GSimpleAsyncResult *result;
1195
1196   g_return_if_fail (name != NULL);
1197
1198   result = g_simple_async_result_new (G_OBJECT (settings),
1199       callback, user_data, empathy_account_settings_set_display_name_finish);
1200
1201   if (!tp_strdiff (name, priv->display_name))
1202     {
1203       /* Nothing to do */
1204       g_simple_async_result_complete_in_idle (result);
1205       return;
1206     }
1207
1208   if (priv->account == NULL)
1209     {
1210       if (priv->display_name != NULL)
1211         g_free (priv->display_name);
1212
1213       priv->display_name = g_strdup (name);
1214
1215       g_simple_async_result_complete_in_idle (result);
1216
1217       return;
1218     }
1219
1220   tp_account_set_display_name_async (priv->account, name,
1221       account_settings_display_name_set_cb, result);
1222 }
1223
1224 gboolean
1225 empathy_account_settings_set_display_name_finish (
1226   EmpathyAccountSettings *settings,
1227   GAsyncResult *result,
1228   GError **error)
1229 {
1230   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1231       error))
1232     return FALSE;
1233
1234   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1235     G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1236       FALSE);
1237
1238   return TRUE;
1239 }
1240
1241 static void
1242 account_settings_icon_name_set_cb (GObject *src,
1243     GAsyncResult *res,
1244     gpointer user_data)
1245 {
1246   GError *error = NULL;
1247   TpAccount *account = TP_ACCOUNT (src);
1248   GSimpleAsyncResult *set_result = user_data;
1249
1250   tp_account_set_icon_name_finish (account, res, &error);
1251
1252   if (error != NULL)
1253     {
1254       g_simple_async_result_set_from_error (set_result, error);
1255       g_error_free (error);
1256     }
1257
1258   g_simple_async_result_complete (set_result);
1259   g_object_unref (set_result);
1260 }
1261
1262 void
1263 empathy_account_settings_set_icon_name_async (
1264   EmpathyAccountSettings *settings,
1265   const gchar *name,
1266   GAsyncReadyCallback callback,
1267   gpointer user_data)
1268 {
1269   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1270   GSimpleAsyncResult *result;
1271
1272   g_return_if_fail (name != NULL);
1273
1274   result = g_simple_async_result_new (G_OBJECT (settings),
1275       callback, user_data, empathy_account_settings_set_icon_name_finish);
1276
1277   if (priv->account == NULL)
1278     {
1279       if (priv->icon_name != NULL)
1280         g_free (priv->icon_name);
1281
1282       priv->icon_name = g_strdup (name);
1283
1284       g_simple_async_result_complete_in_idle (result);
1285
1286       return;
1287     }
1288
1289   tp_account_set_icon_name_async (priv->account, name,
1290       account_settings_icon_name_set_cb, result);
1291 }
1292
1293 gboolean
1294 empathy_account_settings_set_icon_name_finish (
1295   EmpathyAccountSettings *settings,
1296   GAsyncResult *result,
1297   GError **error)
1298 {
1299   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1300       error))
1301     return FALSE;
1302
1303   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1304     G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1305       FALSE);
1306
1307   return TRUE;
1308 }
1309
1310 static void
1311 empathy_account_settings_processed_password (GObject *source,
1312     GAsyncResult *result,
1313     gpointer user_data,
1314     gpointer finish_func)
1315 {
1316   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1317   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1318   GSimpleAsyncResult *r;
1319   GError *error = NULL;
1320   gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1321     finish_func;
1322
1323   g_free (priv->password_original);
1324   priv->password_original = g_strdup (priv->password);
1325
1326   if (!func (TP_ACCOUNT (source), result, &error))
1327     {
1328       g_simple_async_result_set_from_error (priv->apply_result, error);
1329       g_error_free (error);
1330     }
1331
1332   empathy_account_settings_discard_changes (settings);
1333
1334   r = priv->apply_result;
1335   priv->apply_result = NULL;
1336
1337   g_simple_async_result_complete (r);
1338   g_object_unref (r);
1339 }
1340
1341 static void
1342 empathy_account_settings_set_password_cb (GObject *source,
1343     GAsyncResult *result,
1344     gpointer user_data)
1345 {
1346   empathy_account_settings_processed_password (source, result, user_data,
1347       empathy_keyring_set_account_password_finish);
1348 }
1349
1350 static void
1351 empathy_account_settings_delete_password_cb (GObject *source,
1352     GAsyncResult *result,
1353     gpointer user_data)
1354 {
1355   empathy_account_settings_processed_password (source, result, user_data,
1356       empathy_keyring_delete_account_password_finish);
1357 }
1358
1359 static void
1360 update_account_uri_schemes (EmpathyAccountSettings *self)
1361 {
1362   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1363
1364   if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1365         priv->account))
1366     return;
1367
1368   tp_account_set_uri_scheme_association_async (priv->account, "tel",
1369       priv->uri_scheme_tel, NULL, NULL);
1370 }
1371
1372 static void
1373 set_service_cb (GObject *source,
1374     GAsyncResult *result,
1375     gpointer user_data)
1376 {
1377   GError *error = NULL;
1378
1379   if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1380     {
1381       DEBUG ("Failed to set Account.Service: %s", error->message);
1382       g_error_free (error);
1383     }
1384 }
1385
1386 static void
1387 update_account_service (EmpathyAccountSettings *self)
1388 {
1389   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1390
1391   if (!priv->update_service)
1392     return;
1393
1394   tp_account_set_service_async (priv->account,
1395       priv->service != NULL ? priv->service : "", set_service_cb, self);
1396 }
1397
1398 static void
1399 empathy_account_settings_account_updated (GObject *source,
1400     GAsyncResult *result,
1401     gpointer user_data)
1402 {
1403   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1404   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1405   GSimpleAsyncResult *r;
1406   GError *error = NULL;
1407   GStrv reconnect_required = NULL;
1408
1409   if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1410           result, &reconnect_required, &error))
1411     {
1412       g_simple_async_result_set_from_error (priv->apply_result, error);
1413       g_error_free (error);
1414       goto out;
1415     }
1416
1417   /* Only set the password in the keyring if the CM supports SASL and
1418    * it's changed. */
1419   if (priv->supports_sasl && priv->password_changed)
1420     {
1421       if (priv->password != NULL)
1422         {
1423           /* FIXME: we shouldn't save the password if we
1424            * can't (MaySaveResponse=False) but we don't have API to check that
1425            * at this point (fdo #35382). */
1426           empathy_keyring_set_account_password_async (priv->account, priv->password,
1427               empathy_account_settings_set_password_cb, settings);
1428         }
1429       else
1430         {
1431           empathy_keyring_delete_account_password_async (priv->account,
1432               empathy_account_settings_delete_password_cb, settings);
1433         }
1434
1435       return;
1436     }
1437
1438   update_account_uri_schemes (settings);
1439   update_account_service (settings);
1440
1441   g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1442       g_strv_length (reconnect_required) > 0);
1443
1444 out:
1445   empathy_account_settings_discard_changes (settings);
1446
1447   r = priv->apply_result;
1448   priv->apply_result = NULL;
1449
1450   g_simple_async_result_complete (r);
1451   g_object_unref (r);
1452   g_strfreev (reconnect_required);
1453 }
1454
1455 static void
1456 empathy_account_settings_created_cb (GObject *source,
1457     GAsyncResult *result,
1458     gpointer user_data)
1459 {
1460   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1461   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1462   GError *error = NULL;
1463   GSimpleAsyncResult *r;
1464
1465   priv->account = tp_account_request_create_account_finish (
1466       TP_ACCOUNT_REQUEST (source), result, &error);
1467
1468   if (priv->account == NULL)
1469     {
1470       g_simple_async_result_set_from_error (priv->apply_result, error);
1471     }
1472   else
1473     {
1474       if (priv->supports_sasl && priv->password != NULL)
1475         {
1476           /* Save the password before connecting */
1477           /* FIXME: we shouldn't save the password if we
1478            * can't (MaySaveResponse=False) but we don't have API to check that
1479            * at this point (fdo #35382). */
1480           empathy_keyring_set_account_password_async (priv->account,
1481               priv->password, empathy_account_settings_set_password_cb,
1482               settings);
1483           return;
1484         }
1485
1486       update_account_uri_schemes (settings);
1487
1488       empathy_account_settings_discard_changes (settings);
1489     }
1490
1491   r = priv->apply_result;
1492   priv->apply_result = NULL;
1493
1494   g_simple_async_result_complete (r);
1495   g_object_unref (r);
1496 }
1497
1498 static void
1499 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1500 {
1501   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1502   TpAccountRequest *account_req;
1503   TpConnectionPresenceType type;
1504   gchar *status;
1505   gchar *message;
1506   EmpathyPresenceManager *presence_mgr;
1507   GHashTableIter iter;
1508   gpointer k, v;
1509
1510   account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1511       priv->protocol, "New Account");
1512
1513   presence_mgr = empathy_presence_manager_dup_singleton ();
1514   type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1515       &message);
1516   g_object_unref (presence_mgr);
1517
1518   if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1519     {
1520       tp_account_request_set_requested_presence (account_req, type,
1521           status, message);
1522     }
1523
1524   tp_account_request_set_icon_name (account_req, priv->icon_name);
1525
1526   tp_account_request_set_display_name (account_req, priv->display_name);
1527
1528   if (priv->service != NULL)
1529     tp_account_request_set_service (account_req, priv->service);
1530
1531   g_hash_table_iter_init (&iter, priv->parameters);
1532   while (g_hash_table_iter_next (&iter, &k, &v))
1533     {
1534       const gchar *key = k;
1535       GVariant *value = v;
1536
1537       tp_account_request_set_parameter (account_req, key, value);
1538     }
1539
1540   tp_account_request_create_account_async (account_req,
1541       empathy_account_settings_created_cb, self);
1542 }
1543
1544 static GVariant *
1545 build_parameters_variant (EmpathyAccountSettings *self)
1546 {
1547   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1548   GVariantBuilder *builder;
1549   GHashTableIter iter;
1550   gpointer k, v;
1551
1552   builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1553
1554   g_hash_table_iter_init (&iter, priv->parameters);
1555   while (g_hash_table_iter_next (&iter, &k, &v))
1556     {
1557       const gchar *key = k;
1558       GVariant *value = v;
1559       GVariant *entry;
1560
1561       entry = g_variant_new_dict_entry (g_variant_new_string (key),
1562           g_variant_new_variant (value));
1563
1564       g_variant_builder_add_value (builder, entry);
1565     }
1566
1567   return g_variant_builder_end (builder);
1568 }
1569
1570 void
1571 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1572     GAsyncReadyCallback callback,
1573     gpointer user_data)
1574 {
1575   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1576
1577   if (priv->apply_result != NULL)
1578     {
1579       g_simple_async_report_error_in_idle (G_OBJECT (settings),
1580           callback, user_data,
1581           G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1582       return;
1583     }
1584
1585   priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1586       callback, user_data, empathy_account_settings_apply_finish);
1587
1588   /* We'll have to reconnect only if we change none DBus_Property on an
1589    * existing account. */
1590   g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1591
1592   if (priv->account == NULL)
1593     {
1594       g_assert (priv->apply_result != NULL && priv->account == NULL);
1595
1596       empathy_account_settings_do_create_account (settings);
1597     }
1598   else
1599     {
1600       tp_account_update_parameters_vardict_async (priv->account,
1601           build_parameters_variant (settings),
1602           (const gchar **) priv->unset_parameters->data,
1603           empathy_account_settings_account_updated, settings);
1604     }
1605 }
1606
1607 gboolean
1608 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1609     GAsyncResult *result,
1610     gboolean *reconnect_required,
1611     GError **error)
1612 {
1613   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1614       error))
1615     return FALSE;
1616
1617   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1618     G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1619
1620   if (reconnect_required != NULL)
1621     *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1622         G_SIMPLE_ASYNC_RESULT (result));
1623
1624   return TRUE;
1625 }
1626
1627 gboolean
1628 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1629     TpAccount *account)
1630 {
1631   EmpathyAccountSettingsPriv *priv;
1632   const gchar *account_path;
1633   const gchar *priv_account_path;
1634
1635   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1636   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1637
1638   priv = GET_PRIV (settings);
1639
1640   if (priv->account == NULL)
1641     return FALSE;
1642
1643   account_path = tp_proxy_get_object_path (TP_PROXY (account));
1644   priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1645
1646   return (!tp_strdiff (account_path, priv_account_path));
1647 }
1648
1649 void
1650 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1651     const gchar *param,
1652     const gchar *pattern)
1653 {
1654   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1655   GRegex *regex;
1656   GError *error = NULL;
1657
1658   regex = g_regex_new (pattern, 0, 0, &error);
1659   if (regex == NULL)
1660     {
1661       g_warning ("Failed to create reg exp: %s", error->message);
1662       g_error_free (error);
1663       return;
1664     }
1665
1666   g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1667 }
1668
1669 gboolean
1670 empathy_account_settings_parameter_is_valid (
1671     EmpathyAccountSettings *settings,
1672     const gchar *param)
1673 {
1674   EmpathyAccountSettingsPriv *priv;
1675   const GRegex *regex;
1676   const gchar *value;
1677
1678   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1679
1680   priv = GET_PRIV (settings);
1681
1682   if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1683     {
1684       /* first, look if it's set in our own parameters */
1685       if (g_hash_table_lookup (priv->parameters, param) != NULL)
1686         goto test_regex;
1687
1688       /* if we did not unset the parameter, look if it's in the account */
1689       if (priv->account != NULL &&
1690           !empathy_account_settings_is_unset (settings, param))
1691         {
1692           const GHashTable *account_params;
1693
1694           account_params = tp_account_get_parameters (priv->account);
1695           if (tp_asv_lookup (account_params, param))
1696             goto test_regex;
1697         }
1698
1699       return FALSE;
1700     }
1701
1702 test_regex:
1703   /* test whether parameter value matches its regex */
1704   regex = g_hash_table_lookup (priv->param_regexps, param);
1705   if (regex)
1706     {
1707       value = empathy_account_settings_get_string (settings, param);
1708       if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1709         return FALSE;
1710     }
1711
1712   return TRUE;
1713 }
1714
1715 gboolean
1716 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1717 {
1718   EmpathyAccountSettingsPriv *priv;
1719   const gchar *param;
1720   GHashTableIter iter;
1721   GList *l;
1722
1723   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1724
1725   priv = GET_PRIV (settings);
1726
1727   for (l = priv->required_params; l; l = l->next)
1728     {
1729       if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1730         return FALSE;
1731     }
1732
1733   g_hash_table_iter_init (&iter, priv->param_regexps);
1734   while (g_hash_table_iter_next (&iter, (gpointer *) &param, NULL))
1735     {
1736       if (!empathy_account_settings_parameter_is_valid (settings, param))
1737         return FALSE;
1738     }
1739
1740   return TRUE;
1741 }
1742
1743 TpProtocol *
1744 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1745 {
1746   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1747
1748   return priv->protocol_obj;
1749 }
1750
1751 gboolean
1752 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1753 {
1754   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1755
1756   return priv->supports_sasl;
1757 }
1758
1759 gboolean
1760 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1761     const gchar *param)
1762 {
1763   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1764
1765   return tp_protocol_has_param (priv->protocol_obj, param);
1766 }
1767
1768 void
1769 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1770     gboolean associate)
1771 {
1772   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1773
1774   priv->uri_scheme_tel = associate;
1775 }
1776
1777 gboolean
1778 empathy_account_settings_has_uri_scheme_tel (
1779     EmpathyAccountSettings *self)
1780 {
1781   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1782
1783   return priv->uri_scheme_tel;
1784 }