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