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