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