]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-settings.c
9f7297b4e7b003ddae29f3f2e2e62c055ed6841f
[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_string (EmpathyAccountSettings *settings,
1141     const gchar *param,
1142     const gchar *value)
1143 {
1144   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1145
1146   g_return_if_fail (param != NULL);
1147   g_return_if_fail (value != NULL);
1148
1149   if (!tp_strdiff (param, "password") && priv->supports_sasl)
1150     {
1151       g_free (priv->password);
1152       priv->password = g_strdup (value);
1153       priv->password_changed = TRUE;
1154     }
1155   else
1156     {
1157       g_hash_table_insert (priv->parameters, g_strdup (param),
1158           g_variant_ref_sink (g_variant_new_string (value)));
1159     }
1160
1161   account_settings_remove_from_unset (settings, param);
1162 }
1163
1164 void
1165 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1166     const gchar *param,
1167     const gchar * const *value)
1168 {
1169   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1170
1171   g_return_if_fail (param != NULL);
1172   g_return_if_fail (value != NULL);
1173
1174   g_hash_table_insert (priv->parameters, g_strdup (param),
1175       g_variant_ref_sink (g_variant_new_strv (value, -1)));
1176
1177   account_settings_remove_from_unset (settings, param);
1178 }
1179
1180 void
1181 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1182     const gchar *param,
1183     gint32 value)
1184 {
1185   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1186
1187   g_return_if_fail (param != NULL);
1188
1189   g_hash_table_insert (priv->parameters, g_strdup (param),
1190       g_variant_ref_sink (g_variant_new_int32 (value)));
1191
1192   account_settings_remove_from_unset (settings, param);
1193 }
1194
1195 void
1196 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1197     const gchar *param,
1198     gint64 value)
1199 {
1200   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1201
1202   g_return_if_fail (param != NULL);
1203
1204   g_hash_table_insert (priv->parameters, g_strdup (param),
1205       g_variant_ref_sink (g_variant_new_int64 (value)));
1206
1207   account_settings_remove_from_unset (settings, param);
1208 }
1209
1210 void
1211 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1212     const gchar *param,
1213     guint32 value)
1214 {
1215   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1216
1217   g_return_if_fail (param != NULL);
1218
1219   g_hash_table_insert (priv->parameters, g_strdup (param),
1220       g_variant_ref_sink (g_variant_new_uint32 (value)));
1221
1222   account_settings_remove_from_unset (settings, param);
1223 }
1224
1225 void
1226 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1227     const gchar *param,
1228     guint64 value)
1229 {
1230   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1231
1232   g_return_if_fail (param != NULL);
1233
1234   g_hash_table_insert (priv->parameters, g_strdup (param),
1235       g_variant_ref_sink (g_variant_new_uint64 (value)));
1236
1237   account_settings_remove_from_unset (settings, param);
1238 }
1239
1240 void
1241 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1242     const gchar *param,
1243     gboolean value)
1244 {
1245   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1246
1247   g_return_if_fail (param != NULL);
1248
1249   g_hash_table_insert (priv->parameters, g_strdup (param),
1250       g_variant_ref_sink (g_variant_new_boolean (value)));
1251
1252   account_settings_remove_from_unset (settings, param);
1253 }
1254
1255 static void
1256 account_settings_display_name_set_cb (GObject *src,
1257     GAsyncResult *res,
1258     gpointer user_data)
1259 {
1260   GError *error = NULL;
1261   TpAccount *account = TP_ACCOUNT (src);
1262   GSimpleAsyncResult *set_result = user_data;
1263
1264   tp_account_set_display_name_finish (account, res, &error);
1265
1266   if (error != NULL)
1267     {
1268       g_simple_async_result_set_from_error (set_result, error);
1269       g_error_free (error);
1270     }
1271
1272   g_simple_async_result_complete (set_result);
1273   g_object_unref (set_result);
1274 }
1275
1276 void
1277 empathy_account_settings_set_display_name_async (
1278   EmpathyAccountSettings *settings,
1279   const gchar *name,
1280   GAsyncReadyCallback callback,
1281   gpointer user_data)
1282 {
1283   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1284   GSimpleAsyncResult *result;
1285
1286   g_return_if_fail (name != NULL);
1287
1288   result = g_simple_async_result_new (G_OBJECT (settings),
1289       callback, user_data, empathy_account_settings_set_display_name_finish);
1290
1291   if (!tp_strdiff (name, priv->display_name))
1292     {
1293       /* Nothing to do */
1294       g_simple_async_result_complete_in_idle (result);
1295       return;
1296     }
1297
1298   if (priv->account == NULL)
1299     {
1300       if (priv->display_name != NULL)
1301         g_free (priv->display_name);
1302
1303       priv->display_name = g_strdup (name);
1304
1305       g_simple_async_result_complete_in_idle (result);
1306
1307       return;
1308     }
1309
1310   tp_account_set_display_name_async (priv->account, name,
1311       account_settings_display_name_set_cb, result);
1312 }
1313
1314 gboolean
1315 empathy_account_settings_set_display_name_finish (
1316   EmpathyAccountSettings *settings,
1317   GAsyncResult *result,
1318   GError **error)
1319 {
1320   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1321       error))
1322     return FALSE;
1323
1324   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1325     G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1326       FALSE);
1327
1328   return TRUE;
1329 }
1330
1331 static void
1332 account_settings_icon_name_set_cb (GObject *src,
1333     GAsyncResult *res,
1334     gpointer user_data)
1335 {
1336   GError *error = NULL;
1337   TpAccount *account = TP_ACCOUNT (src);
1338   GSimpleAsyncResult *set_result = user_data;
1339
1340   tp_account_set_icon_name_finish (account, res, &error);
1341
1342   if (error != NULL)
1343     {
1344       g_simple_async_result_set_from_error (set_result, error);
1345       g_error_free (error);
1346     }
1347
1348   g_simple_async_result_complete (set_result);
1349   g_object_unref (set_result);
1350 }
1351
1352 void
1353 empathy_account_settings_set_icon_name_async (
1354   EmpathyAccountSettings *settings,
1355   const gchar *name,
1356   GAsyncReadyCallback callback,
1357   gpointer user_data)
1358 {
1359   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1360   GSimpleAsyncResult *result;
1361
1362   g_return_if_fail (name != NULL);
1363
1364   result = g_simple_async_result_new (G_OBJECT (settings),
1365       callback, user_data, empathy_account_settings_set_icon_name_finish);
1366
1367   if (priv->account == NULL)
1368     {
1369       if (priv->icon_name != NULL)
1370         g_free (priv->icon_name);
1371
1372       priv->icon_name = g_strdup (name);
1373
1374       g_simple_async_result_complete_in_idle (result);
1375
1376       return;
1377     }
1378
1379   tp_account_set_icon_name_async (priv->account, name,
1380       account_settings_icon_name_set_cb, result);
1381 }
1382
1383 gboolean
1384 empathy_account_settings_set_icon_name_finish (
1385   EmpathyAccountSettings *settings,
1386   GAsyncResult *result,
1387   GError **error)
1388 {
1389   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1390       error))
1391     return FALSE;
1392
1393   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1394     G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1395       FALSE);
1396
1397   return TRUE;
1398 }
1399
1400 static void
1401 empathy_account_settings_processed_password (GObject *source,
1402     GAsyncResult *result,
1403     gpointer user_data,
1404     gpointer finish_func)
1405 {
1406   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1407   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1408   GSimpleAsyncResult *r;
1409   GError *error = NULL;
1410   gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1411     finish_func;
1412
1413   g_free (priv->password_original);
1414   priv->password_original = g_strdup (priv->password);
1415
1416   if (!func (TP_ACCOUNT (source), result, &error))
1417     {
1418       g_simple_async_result_set_from_error (priv->apply_result, error);
1419       g_error_free (error);
1420     }
1421
1422   empathy_account_settings_discard_changes (settings);
1423
1424   r = priv->apply_result;
1425   priv->apply_result = NULL;
1426
1427   g_simple_async_result_complete (r);
1428   g_object_unref (r);
1429 }
1430
1431 static void
1432 empathy_account_settings_set_password_cb (GObject *source,
1433     GAsyncResult *result,
1434     gpointer user_data)
1435 {
1436   empathy_account_settings_processed_password (source, result, user_data,
1437       empathy_keyring_set_account_password_finish);
1438 }
1439
1440 static void
1441 empathy_account_settings_delete_password_cb (GObject *source,
1442     GAsyncResult *result,
1443     gpointer user_data)
1444 {
1445   empathy_account_settings_processed_password (source, result, user_data,
1446       empathy_keyring_delete_account_password_finish);
1447 }
1448
1449 static void
1450 update_account_uri_schemes (EmpathyAccountSettings *self)
1451 {
1452   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1453
1454   if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1455         priv->account))
1456     return;
1457
1458   tp_account_set_uri_scheme_association_async (priv->account, "tel",
1459       priv->uri_scheme_tel, NULL, NULL);
1460 }
1461
1462 static void
1463 set_service_cb (GObject *source,
1464     GAsyncResult *result,
1465     gpointer user_data)
1466 {
1467   GError *error = NULL;
1468
1469   if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1470     {
1471       DEBUG ("Failed to set Account.Service: %s", error->message);
1472       g_error_free (error);
1473     }
1474 }
1475
1476 static void
1477 update_account_service (EmpathyAccountSettings *self)
1478 {
1479   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1480
1481   if (!priv->update_service)
1482     return;
1483
1484   tp_account_set_service_async (priv->account,
1485       priv->service != NULL ? priv->service : "", set_service_cb, self);
1486 }
1487
1488 static void
1489 empathy_account_settings_account_updated (GObject *source,
1490     GAsyncResult *result,
1491     gpointer user_data)
1492 {
1493   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1494   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1495   GSimpleAsyncResult *r;
1496   GError *error = NULL;
1497   GStrv reconnect_required = NULL;
1498
1499   if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1500           result, &reconnect_required, &error))
1501     {
1502       g_simple_async_result_set_from_error (priv->apply_result, error);
1503       g_error_free (error);
1504       goto out;
1505     }
1506
1507   /* Only set the password in the keyring if the CM supports SASL and
1508    * it's changed. */
1509   if (priv->supports_sasl && priv->password_changed)
1510     {
1511       if (priv->password != NULL)
1512         {
1513           /* FIXME: we shouldn't save the password if we
1514            * can't (MaySaveResponse=False) but we don't have API to check that
1515            * at this point (fdo #35382). */
1516           empathy_keyring_set_account_password_async (priv->account, priv->password,
1517               empathy_account_settings_set_password_cb, settings);
1518         }
1519       else
1520         {
1521           empathy_keyring_delete_account_password_async (priv->account,
1522               empathy_account_settings_delete_password_cb, settings);
1523         }
1524
1525       return;
1526     }
1527
1528   update_account_uri_schemes (settings);
1529   update_account_service (settings);
1530
1531   g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1532       g_strv_length (reconnect_required) > 0);
1533
1534 out:
1535   empathy_account_settings_discard_changes (settings);
1536
1537   r = priv->apply_result;
1538   priv->apply_result = NULL;
1539
1540   g_simple_async_result_complete (r);
1541   g_object_unref (r);
1542   g_strfreev (reconnect_required);
1543 }
1544
1545 static void
1546 empathy_account_settings_created_cb (GObject *source,
1547     GAsyncResult *result,
1548     gpointer user_data)
1549 {
1550   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1551   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1552   GError *error = NULL;
1553   GSimpleAsyncResult *r;
1554
1555   priv->account = tp_account_request_create_account_finish (
1556       TP_ACCOUNT_REQUEST (source), result, &error);
1557
1558   if (priv->account == NULL)
1559     {
1560       g_simple_async_result_set_from_error (priv->apply_result, error);
1561     }
1562   else
1563     {
1564       if (priv->supports_sasl && priv->password != NULL)
1565         {
1566           /* Save the password before connecting */
1567           /* FIXME: we shouldn't save the password if we
1568            * can't (MaySaveResponse=False) but we don't have API to check that
1569            * at this point (fdo #35382). */
1570           empathy_keyring_set_account_password_async (priv->account,
1571               priv->password, empathy_account_settings_set_password_cb,
1572               settings);
1573           return;
1574         }
1575
1576       update_account_uri_schemes (settings);
1577
1578       empathy_account_settings_discard_changes (settings);
1579     }
1580
1581   r = priv->apply_result;
1582   priv->apply_result = NULL;
1583
1584   g_simple_async_result_complete (r);
1585   g_object_unref (r);
1586 }
1587
1588 static void
1589 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1590 {
1591   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1592   TpAccountRequest *account_req;
1593   TpConnectionPresenceType type;
1594   gchar *status;
1595   gchar *message;
1596   EmpathyPresenceManager *presence_mgr;
1597   GHashTableIter iter;
1598   gpointer k, v;
1599
1600   account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1601       priv->protocol, "New Account");
1602
1603   presence_mgr = empathy_presence_manager_dup_singleton ();
1604   type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1605       &message);
1606   g_object_unref (presence_mgr);
1607
1608   if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1609     {
1610       tp_account_request_set_requested_presence (account_req, type,
1611           status, message);
1612     }
1613
1614   tp_account_request_set_icon_name (account_req, priv->icon_name);
1615
1616   tp_account_request_set_display_name (account_req, priv->display_name);
1617
1618   if (priv->service != NULL)
1619     tp_account_request_set_service (account_req, priv->service);
1620
1621   g_hash_table_iter_init (&iter, priv->parameters);
1622   while (g_hash_table_iter_next (&iter, &k, &v))
1623     {
1624       const gchar *key = k;
1625       GVariant *value = v;
1626
1627       tp_account_request_set_parameter (account_req, key, value);
1628     }
1629
1630   tp_account_request_create_account_async (account_req,
1631       empathy_account_settings_created_cb, self);
1632 }
1633
1634 static GVariant *
1635 build_parameters_variant (EmpathyAccountSettings *self)
1636 {
1637   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1638   GVariantBuilder *builder;
1639   GHashTableIter iter;
1640   gpointer k, v;
1641
1642   builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1643
1644   g_hash_table_iter_init (&iter, priv->parameters);
1645   while (g_hash_table_iter_next (&iter, &k, &v))
1646     {
1647       const gchar *key = k;
1648       GVariant *value = v;
1649       GVariant *entry;
1650
1651       entry = g_variant_new_dict_entry (g_variant_new_string (key),
1652           g_variant_new_variant (value));
1653
1654       g_variant_builder_add_value (builder, entry);
1655     }
1656
1657   return g_variant_builder_end (builder);
1658 }
1659
1660 void
1661 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1662     GAsyncReadyCallback callback,
1663     gpointer user_data)
1664 {
1665   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1666
1667   if (priv->apply_result != NULL)
1668     {
1669       g_simple_async_report_error_in_idle (G_OBJECT (settings),
1670           callback, user_data,
1671           G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1672       return;
1673     }
1674
1675   priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1676       callback, user_data, empathy_account_settings_apply_finish);
1677
1678   /* We'll have to reconnect only if we change none DBus_Property on an
1679    * existing account. */
1680   g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1681
1682   if (priv->account == NULL)
1683     {
1684       g_assert (priv->apply_result != NULL && priv->account == NULL);
1685
1686       empathy_account_settings_do_create_account (settings);
1687     }
1688   else
1689     {
1690       tp_account_update_parameters_vardict_async (priv->account,
1691           build_parameters_variant (settings),
1692           (const gchar **) priv->unset_parameters->data,
1693           empathy_account_settings_account_updated, settings);
1694     }
1695 }
1696
1697 gboolean
1698 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1699     GAsyncResult *result,
1700     gboolean *reconnect_required,
1701     GError **error)
1702 {
1703   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1704       error))
1705     return FALSE;
1706
1707   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1708     G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1709
1710   if (reconnect_required != NULL)
1711     *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1712         G_SIMPLE_ASYNC_RESULT (result));
1713
1714   return TRUE;
1715 }
1716
1717 gboolean
1718 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1719     TpAccount *account)
1720 {
1721   EmpathyAccountSettingsPriv *priv;
1722   const gchar *account_path;
1723   const gchar *priv_account_path;
1724
1725   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1726   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1727
1728   priv = GET_PRIV (settings);
1729
1730   if (priv->account == NULL)
1731     return FALSE;
1732
1733   account_path = tp_proxy_get_object_path (TP_PROXY (account));
1734   priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1735
1736   return (!tp_strdiff (account_path, priv_account_path));
1737 }
1738
1739 void
1740 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1741     const gchar *param,
1742     const gchar *pattern)
1743 {
1744   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1745   GRegex *regex;
1746   GError *error = NULL;
1747
1748   regex = g_regex_new (pattern, 0, 0, &error);
1749   if (regex == NULL)
1750     {
1751       g_warning ("Failed to create reg exp: %s", error->message);
1752       g_error_free (error);
1753       return;
1754     }
1755
1756   g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1757 }
1758
1759 gboolean
1760 empathy_account_settings_parameter_is_valid (
1761     EmpathyAccountSettings *settings,
1762     const gchar *param)
1763 {
1764   EmpathyAccountSettingsPriv *priv;
1765   const GRegex *regex;
1766   const gchar *value;
1767
1768   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1769
1770   priv = GET_PRIV (settings);
1771
1772   if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1773     {
1774       /* first, look if it's set in our own parameters */
1775       if (g_hash_table_lookup (priv->parameters, param) != NULL)
1776         goto test_regex;
1777
1778       /* if we did not unset the parameter, look if it's in the account */
1779       if (priv->account != NULL &&
1780           !empathy_account_settings_is_unset (settings, param))
1781         {
1782           const GHashTable *account_params;
1783
1784           account_params = tp_account_get_parameters (priv->account);
1785           if (tp_asv_lookup (account_params, param))
1786             goto test_regex;
1787         }
1788
1789       return FALSE;
1790     }
1791
1792 test_regex:
1793   /* test whether parameter value matches its regex */
1794   regex = g_hash_table_lookup (priv->param_regexps, param);
1795   if (regex)
1796     {
1797       value = empathy_account_settings_get_string (settings, param);
1798       if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1799         return FALSE;
1800     }
1801
1802   return TRUE;
1803 }
1804
1805 gboolean
1806 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1807 {
1808   EmpathyAccountSettingsPriv *priv;
1809   const gchar *param;
1810   GHashTableIter iter;
1811   GList *l;
1812
1813   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1814
1815   priv = GET_PRIV (settings);
1816
1817   for (l = priv->required_params; l; l = l->next)
1818     {
1819       if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1820         return FALSE;
1821     }
1822
1823   g_hash_table_iter_init (&iter, priv->param_regexps);
1824   while (g_hash_table_iter_next (&iter, (gpointer *) &param, NULL))
1825     {
1826       if (!empathy_account_settings_parameter_is_valid (settings, param))
1827         return FALSE;
1828     }
1829
1830   return TRUE;
1831 }
1832
1833 TpProtocol *
1834 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1835 {
1836   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1837
1838   return priv->protocol_obj;
1839 }
1840
1841 gboolean
1842 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1843 {
1844   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1845
1846   return priv->supports_sasl;
1847 }
1848
1849 gboolean
1850 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1851     const gchar *param)
1852 {
1853   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1854
1855   return tp_protocol_has_param (priv->protocol_obj, param);
1856 }
1857
1858 void
1859 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1860     gboolean associate)
1861 {
1862   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1863
1864   priv->uri_scheme_tel = associate;
1865 }
1866
1867 gboolean
1868 empathy_account_settings_has_uri_scheme_tel (
1869     EmpathyAccountSettings *self)
1870 {
1871   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1872
1873   return priv->uri_scheme_tel;
1874 }