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