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