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