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