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