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