]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-settings.c
Merge remote-tracking branch 'glassrose/make-favorite-room-names-more-detailed-646526'
[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-presence-manager.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_account_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_migrate_password_cb (GObject *source,
460     GAsyncResult *result,
461     gpointer user_data)
462 {
463   TpAccount *account = TP_ACCOUNT (source);
464   GError *error = NULL;
465   EmpathyAccountSettings *self = user_data;
466   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
467   GHashTable *empty;
468   const gchar *unset[] = { "password", NULL };
469
470   if (!empathy_keyring_set_account_password_finish (account, result, &error))
471     {
472       DEBUG ("Failed to set password: %s", error->message);
473       g_clear_error (&error);
474       return;
475     }
476
477   /* Now clear the password MC has stored. */
478   empty = tp_asv_new (NULL, NULL);
479   tp_account_update_parameters_async (priv->account,
480       empty, unset, NULL, NULL);
481
482   g_hash_table_remove (priv->parameters, "password");
483
484   g_hash_table_unref (empty);
485 }
486
487 static void
488 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
489 {
490   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
491   const GValue *v;
492   const gchar *password;
493
494   if (!priv->supports_sasl || empathy_account_settings_get (
495           self, "password") == NULL)
496     return;
497
498   /* mission-control still has our password, although the CM
499    * supports SASL. Let's try migrating it. */
500
501   DEBUG ("Trying to migrate password parameter from MC to the "
502       "keyring ourselves for account %s",
503       tp_account_get_path_suffix (priv->account));
504
505   v = empathy_account_settings_get (self, "password");
506
507   /* I can't imagine why this would fail. */
508   if (!G_VALUE_HOLDS_STRING (v))
509     return;
510
511   password = g_value_get_string (v);
512
513   if (EMP_STR_EMPTY (password))
514     return;
515
516   empathy_keyring_set_account_password_async (priv->account, password,
517       empathy_account_settings_migrate_password_cb, self);
518
519   /* We don't want to request the password again, we
520    * already know it. */
521   priv->password_requested = TRUE;
522
523   priv->password = g_strdup (password);
524   priv->password_original = g_strdup (password);
525 }
526
527 static void
528 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
529 {
530   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
531   const TpConnectionManagerProtocol *tp_protocol;
532   GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
533
534   if (priv->ready)
535     return;
536
537   if (priv->account != NULL
538       && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
539       return;
540
541   if (!empathy_connection_managers_is_ready (priv->managers))
542     return;
543
544   if (priv->manager == NULL)
545     {
546       priv->manager = empathy_connection_managers_get_cm (
547           priv->managers, priv->cm_name);
548     }
549
550   if (priv->manager == NULL)
551     return;
552
553   g_object_ref (priv->manager);
554
555   if (priv->account != NULL)
556     {
557       g_free (priv->display_name);
558       priv->display_name =
559         g_strdup (tp_account_get_display_name (priv->account));
560
561       g_free (priv->icon_name);
562       priv->icon_name =
563         g_strdup (tp_account_get_icon_name (priv->account));
564     }
565
566   tp_protocol = tp_connection_manager_get_protocol (priv->manager,
567     priv->protocol);
568
569   if (tp_protocol == NULL)
570     {
571       tp_clear_object (&priv->manager);
572       return;
573     }
574
575   if (priv->required_params == NULL)
576     {
577       TpConnectionManagerParam *cur;
578
579       for (cur = tp_protocol->params; cur->name != NULL; cur++)
580         {
581           if (tp_connection_manager_param_is_required (cur))
582             {
583               priv->required_params = g_list_append (priv->required_params,
584                                                      g_strdup (cur->name));
585             }
586         }
587     }
588
589   if (priv->protocol_obj == NULL)
590     {
591       priv->protocol_obj = g_object_ref (
592           tp_connection_manager_get_protocol_object (priv->manager,
593               priv->protocol));
594     }
595
596   if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
597       && !priv->preparing_protocol)
598     {
599       priv->preparing_protocol = TRUE;
600       tp_proxy_prepare_async (priv->protocol_obj, features,
601           empathy_account_settings_protocol_obj_prepared_cb, self);
602       return;
603     }
604   else
605     {
606       if (tp_strv_contains (tp_protocol_get_authentication_types (
607                   priv->protocol_obj),
608               TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
609         {
610           priv->supports_sasl = TRUE;
611         }
612     }
613
614   /* NOTE: When removing MC migration code, remove this call, and the
615    * function it's calling. That's it. */
616   empathy_account_settings_try_migrating_password (self);
617
618   /* priv->account won't be a proper account if it's the account
619    * assistant showing this widget. */
620   if (priv->supports_sasl && !priv->password_requested
621       && priv->account != NULL)
622     {
623       priv->password_requested = TRUE;
624
625       /* Make this call but don't block on its readiness. We'll signal
626        * if it's updated later with ::password-retrieved. */
627       empathy_keyring_get_account_password_async (priv->account,
628           empathy_account_settings_get_password_cb, self);
629     }
630
631   priv->ready = TRUE;
632   g_object_notify (G_OBJECT (self), "ready");
633 }
634
635 static void
636 empathy_account_settings_account_ready_cb (GObject *source_object,
637     GAsyncResult *result,
638     gpointer user_data)
639 {
640   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
641   TpAccount *account = TP_ACCOUNT (source_object);
642   GError *error = NULL;
643
644   if (!tp_proxy_prepare_finish (account, result, &error))
645     {
646       DEBUG ("Failed to prepare account: %s", error->message);
647       g_error_free (error);
648       return;
649     }
650
651   empathy_account_settings_check_readyness (settings);
652 }
653
654 static void
655 empathy_account_settings_managers_ready_cb (GObject *object,
656     GParamSpec *pspec,
657     gpointer user_data)
658 {
659   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
660
661   empathy_account_settings_check_readyness (settings);
662 }
663
664 EmpathyAccountSettings *
665 empathy_account_settings_new (const gchar *connection_manager,
666     const gchar *protocol,
667     const gchar *service,
668     const char *display_name)
669 {
670   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
671       "connection-manager", connection_manager,
672       "protocol", protocol,
673       "service", service,
674       "display-name", display_name,
675       NULL);
676 }
677
678 EmpathyAccountSettings *
679 empathy_account_settings_new_for_account (TpAccount *account)
680 {
681   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
682       "account", account,
683       NULL);
684 }
685
686 TpConnectionManagerParam *
687 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
688 {
689   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
690   const TpConnectionManagerProtocol *tp_protocol;
691
692   g_return_val_if_fail (priv->manager != NULL, NULL);
693   g_return_val_if_fail (priv->protocol != NULL, NULL);
694
695   tp_protocol = tp_connection_manager_get_protocol (priv->manager,
696      priv->protocol);
697   if (tp_protocol == NULL)
698     {
699       DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
700           priv->protocol);
701       return NULL;
702     }
703
704   return tp_protocol->params;
705 }
706
707 gboolean
708 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
709 {
710   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
711
712   return priv->ready;
713 }
714
715 const gchar *
716 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
717 {
718   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
719
720   return priv->cm_name;
721 }
722
723 const gchar *
724 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
725 {
726   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
727
728   return priv->protocol;
729 }
730
731 const gchar *
732 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
733 {
734   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
735
736   return priv->service;
737 }
738
739 gchar *
740 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
741 {
742   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
743
744   return priv->icon_name;
745 }
746
747 const gchar *
748 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
749 {
750   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
751
752   return priv->display_name;
753 }
754
755 TpAccount *
756 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
757 {
758   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
759
760   return priv->account;
761 }
762
763 static gboolean
764 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
765     const gchar *param)
766 {
767   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
768   GArray *a;
769   guint i;
770
771   a = priv->unset_parameters;
772
773   for (i = 0; i < a->len; i++)
774     {
775       if (!tp_strdiff (g_array_index (a, gchar *, i), param))
776         return TRUE;
777     }
778
779   return FALSE;
780 }
781
782 static TpConnectionManagerParam *
783 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
784     const gchar *param)
785 {
786   TpConnectionManagerParam *tp_params =
787       empathy_account_settings_get_tp_params (settings);
788   TpConnectionManagerParam *p;
789
790   for (p = tp_params; p != NULL && p->name != NULL; p++)
791     {
792       if (tp_strdiff (p->name, param))
793         continue;
794
795       return p;
796     }
797
798   return NULL;
799 }
800
801 gboolean
802 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
803     const gchar *param)
804 {
805   return (empathy_account_settings_get_tp_param (settings, param) != NULL);
806 }
807
808 static void
809 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
810     const gchar *param)
811 {
812   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
813   guint idx;
814   gchar *val;
815
816   for (idx = 0; idx < priv->unset_parameters->len; idx++)
817     {
818       val = g_array_index (priv->unset_parameters, gchar *, idx);
819
820       if (!tp_strdiff (val, param))
821         {
822           priv->unset_parameters =
823             g_array_remove_index (priv->unset_parameters, idx);
824           g_free (val);
825
826           break;
827         }
828     }
829 }
830
831 const GValue *
832 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
833     const gchar *param)
834 {
835   TpConnectionManagerParam *p;
836
837   p = empathy_account_settings_get_tp_param (settings, param);
838
839   if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
840     return NULL;
841
842   return &(p->default_value);
843 }
844
845 const gchar *
846 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
847     const gchar *param)
848 {
849   TpConnectionManagerParam *p;
850
851   p = empathy_account_settings_get_tp_param (settings, param);
852
853   if (p == NULL)
854     return NULL;
855
856   return p->dbus_signature;
857 }
858
859 const GValue *
860 empathy_account_settings_get (EmpathyAccountSettings *settings,
861     const gchar *param)
862 {
863   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
864   const GValue *result = NULL;
865
866   /* Lookup the update parameters we set */
867   result = tp_asv_lookup (priv->parameters, param);
868   if (result != NULL)
869     return result;
870
871   /* If the parameters isn't unset use the accounts setting if any */
872   if (priv->account != NULL
873       && !empathy_account_settings_is_unset (settings, param))
874     {
875       const GHashTable *parameters;
876
877       parameters = tp_account_get_parameters (priv->account);
878       result = tp_asv_lookup (parameters, param);
879
880       if (result != NULL)
881         return result;
882     }
883
884   /* fallback to the default */
885   return empathy_account_settings_get_default (settings, param);
886 }
887
888 void
889 empathy_account_settings_unset (EmpathyAccountSettings *settings,
890     const gchar *param)
891 {
892   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
893   gchar *v;
894   if (empathy_account_settings_is_unset (settings, param))
895     return;
896
897   if (priv->supports_sasl && !tp_strdiff (param, "password"))
898     {
899       g_free (priv->password);
900       priv->password = NULL;
901       priv->password_changed = TRUE;
902       return;
903     }
904
905   v = g_strdup (param);
906
907   g_array_append_val (priv->unset_parameters, v);
908   g_hash_table_remove (priv->parameters, param);
909 }
910
911 void
912 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
913 {
914   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
915
916   g_hash_table_remove_all (priv->parameters);
917   empathy_account_settings_free_unset_parameters (settings);
918
919   priv->password_changed = FALSE;
920   g_free (priv->password);
921   priv->password = g_strdup (priv->password_original);
922 }
923
924 const gchar *
925 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
926     const gchar *param)
927 {
928   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
929   const GValue *v;
930
931   if (!tp_strdiff (param, "password") && priv->supports_sasl)
932     {
933       return priv->password;
934     }
935
936   v = empathy_account_settings_get (settings, param);
937
938   if (v == NULL || !G_VALUE_HOLDS_STRING (v))
939     return NULL;
940
941   return g_value_get_string (v);
942 }
943
944 const gchar * const *
945 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
946     const gchar *param)
947 {
948   const GValue *v;
949
950   v = empathy_account_settings_get (settings, param);
951
952   if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
953     return NULL;
954
955   return g_value_get_boxed (v);
956 }
957
958 gint32
959 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
960     const gchar *param)
961 {
962   const GValue *v;
963   gint32 ret = 0;
964
965   v = empathy_account_settings_get (settings, param);
966
967   if (v == NULL)
968     return 0;
969
970   switch G_VALUE_TYPE (v)
971     {
972       case G_TYPE_UCHAR:
973         ret = g_value_get_uchar (v);
974         break;
975       case G_TYPE_INT:
976         ret = g_value_get_int (v);
977         break;
978       case G_TYPE_UINT:
979         ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
980             G_MAXINT32);
981         break;
982       case G_TYPE_INT64:
983         ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
984         break;
985       case G_TYPE_UINT64:
986         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
987             G_MAXINT32);
988         break;
989       default:
990         ret = 0;
991         break;
992     }
993
994   return ret;
995 }
996
997 gint64
998 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
999     const gchar *param)
1000 {
1001   const GValue *v;
1002   gint64 ret = 0;
1003
1004   v = empathy_account_settings_get (settings, param);
1005   if (v == NULL)
1006     return 0;
1007
1008   switch G_VALUE_TYPE (v)
1009     {
1010       case G_TYPE_UCHAR:
1011         ret = g_value_get_uchar (v);
1012         break;
1013       case G_TYPE_INT:
1014         ret = g_value_get_int (v);
1015         break;
1016       case G_TYPE_UINT:
1017         ret = g_value_get_uint (v);
1018         break;
1019       case G_TYPE_INT64:
1020         ret = g_value_get_int64 (v);
1021         break;
1022       case G_TYPE_UINT64:
1023         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1024         break;
1025       default:
1026         ret = 0;
1027         break;
1028     }
1029
1030   return ret;
1031 }
1032
1033 guint32
1034 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1035     const gchar *param)
1036 {
1037   const GValue *v;
1038   guint32 ret;
1039
1040   v = empathy_account_settings_get (settings, param);
1041   if (v == NULL)
1042     return 0;
1043
1044   switch G_VALUE_TYPE (v)
1045     {
1046       case G_TYPE_UCHAR:
1047         ret = g_value_get_uchar (v);
1048         break;
1049       case G_TYPE_INT:
1050         ret = MAX (0, g_value_get_int (v));
1051         break;
1052       case G_TYPE_UINT:
1053         ret = g_value_get_uint (v);
1054         break;
1055       case G_TYPE_INT64:
1056         ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1057         break;
1058       case G_TYPE_UINT64:
1059         ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1060         break;
1061       default:
1062         ret = 0;
1063         break;
1064     }
1065
1066   return ret;
1067 }
1068
1069 guint64
1070 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1071     const gchar *param)
1072 {
1073   const GValue *v;
1074   guint64 ret = 0;
1075
1076   v = empathy_account_settings_get (settings, param);
1077
1078   if (v == NULL || !G_VALUE_HOLDS_INT (v))
1079     return 0;
1080
1081   switch G_VALUE_TYPE (v)
1082     {
1083       case G_TYPE_UCHAR:
1084         ret = g_value_get_uchar (v);
1085         break;
1086       case G_TYPE_INT:
1087         ret = MAX (0, g_value_get_int (v));
1088         break;
1089       case G_TYPE_UINT:
1090         ret = g_value_get_uint (v);
1091         break;
1092       case G_TYPE_INT64:
1093         ret = MAX (0, g_value_get_int64 (v));
1094         break;
1095       case G_TYPE_UINT64:
1096         ret = g_value_get_uint64 (v);
1097         break;
1098       default:
1099         ret = 0;
1100         break;
1101     }
1102
1103   return ret;
1104 }
1105
1106 gboolean
1107 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1108     const gchar *param)
1109 {
1110   const GValue *v;
1111
1112   v = empathy_account_settings_get (settings, param);
1113
1114   if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1115     return FALSE;
1116
1117   return g_value_get_boolean (v);
1118 }
1119
1120 void
1121 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1122     const gchar *param,
1123     const 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   if (!tp_strdiff (param, "password") && priv->supports_sasl)
1131     {
1132       g_free (priv->password);
1133       priv->password = g_strdup (value);
1134       priv->password_changed = TRUE;
1135     }
1136   else
1137     {
1138       tp_asv_set_string (priv->parameters, g_strdup (param), value);
1139     }
1140
1141   account_settings_remove_from_unset (settings, param);
1142 }
1143
1144 void
1145 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1146     const gchar *param,
1147     gchar **value)
1148 {
1149   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1150
1151   g_return_if_fail (param != NULL);
1152   g_return_if_fail (value != NULL);
1153
1154   tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1155
1156   account_settings_remove_from_unset (settings, param);
1157 }
1158
1159 void
1160 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1161     const gchar *param,
1162     gint32 value)
1163 {
1164   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1165
1166   g_return_if_fail (param != NULL);
1167
1168   tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1169
1170   account_settings_remove_from_unset (settings, param);
1171 }
1172
1173 void
1174 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1175     const gchar *param,
1176     gint64 value)
1177 {
1178   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1179
1180   g_return_if_fail (param != NULL);
1181
1182   tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1183
1184   account_settings_remove_from_unset (settings, param);
1185 }
1186
1187 void
1188 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1189     const gchar *param,
1190     guint32 value)
1191 {
1192   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1193
1194   g_return_if_fail (param != NULL);
1195
1196   tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1197
1198   account_settings_remove_from_unset (settings, param);
1199 }
1200
1201 void
1202 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1203     const gchar *param,
1204     guint64 value)
1205 {
1206   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1207
1208   g_return_if_fail (param != NULL);
1209
1210   tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1211
1212   account_settings_remove_from_unset (settings, param);
1213 }
1214
1215 void
1216 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1217     const gchar *param,
1218     gboolean value)
1219 {
1220   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1221
1222   g_return_if_fail (param != NULL);
1223
1224   tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1225
1226   account_settings_remove_from_unset (settings, param);
1227 }
1228
1229 static void
1230 account_settings_display_name_set_cb (GObject *src,
1231     GAsyncResult *res,
1232     gpointer user_data)
1233 {
1234   GError *error = NULL;
1235   TpAccount *account = TP_ACCOUNT (src);
1236   GSimpleAsyncResult *set_result = user_data;
1237
1238   tp_account_set_display_name_finish (account, res, &error);
1239
1240   if (error != NULL)
1241     {
1242       g_simple_async_result_set_from_error (set_result, error);
1243       g_error_free (error);
1244     }
1245
1246   g_simple_async_result_complete (set_result);
1247   g_object_unref (set_result);
1248 }
1249
1250 void
1251 empathy_account_settings_set_display_name_async (
1252   EmpathyAccountSettings *settings,
1253   const gchar *name,
1254   GAsyncReadyCallback callback,
1255   gpointer user_data)
1256 {
1257   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1258   GSimpleAsyncResult *result;
1259
1260   g_return_if_fail (name != NULL);
1261
1262   result = g_simple_async_result_new (G_OBJECT (settings),
1263       callback, user_data, empathy_account_settings_set_display_name_finish);
1264
1265   if (!tp_strdiff (name, priv->display_name))
1266     {
1267       /* Nothing to do */
1268       g_simple_async_result_complete_in_idle (result);
1269       return;
1270     }
1271
1272   if (priv->account == NULL)
1273     {
1274       if (priv->display_name != NULL)
1275         g_free (priv->display_name);
1276
1277       priv->display_name = g_strdup (name);
1278
1279       g_simple_async_result_complete_in_idle (result);
1280
1281       return;
1282     }
1283
1284   tp_account_set_display_name_async (priv->account, name,
1285       account_settings_display_name_set_cb, result);
1286 }
1287
1288 gboolean
1289 empathy_account_settings_set_display_name_finish (
1290   EmpathyAccountSettings *settings,
1291   GAsyncResult *result,
1292   GError **error)
1293 {
1294   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1295       error))
1296     return FALSE;
1297
1298   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1299     G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1300       FALSE);
1301
1302   return TRUE;
1303 }
1304
1305 static void
1306 account_settings_icon_name_set_cb (GObject *src,
1307     GAsyncResult *res,
1308     gpointer user_data)
1309 {
1310   GError *error = NULL;
1311   TpAccount *account = TP_ACCOUNT (src);
1312   GSimpleAsyncResult *set_result = user_data;
1313
1314   tp_account_set_icon_name_finish (account, res, &error);
1315
1316   if (error != NULL)
1317     {
1318       g_simple_async_result_set_from_error (set_result, error);
1319       g_error_free (error);
1320     }
1321
1322   g_simple_async_result_complete (set_result);
1323   g_object_unref (set_result);
1324 }
1325
1326 void
1327 empathy_account_settings_set_icon_name_async (
1328   EmpathyAccountSettings *settings,
1329   const gchar *name,
1330   GAsyncReadyCallback callback,
1331   gpointer user_data)
1332 {
1333   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1334   GSimpleAsyncResult *result;
1335
1336   g_return_if_fail (name != NULL);
1337
1338   result = g_simple_async_result_new (G_OBJECT (settings),
1339       callback, user_data, empathy_account_settings_set_icon_name_finish);
1340
1341   if (priv->account == NULL)
1342     {
1343       if (priv->icon_name != NULL)
1344         g_free (priv->icon_name);
1345
1346       priv->icon_name = g_strdup (name);
1347
1348       g_simple_async_result_complete_in_idle (result);
1349
1350       return;
1351     }
1352
1353   tp_account_set_icon_name_async (priv->account, name,
1354       account_settings_icon_name_set_cb, result);
1355 }
1356
1357 gboolean
1358 empathy_account_settings_set_icon_name_finish (
1359   EmpathyAccountSettings *settings,
1360   GAsyncResult *result,
1361   GError **error)
1362 {
1363   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1364       error))
1365     return FALSE;
1366
1367   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1368     G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1369       FALSE);
1370
1371   return TRUE;
1372 }
1373
1374 static void
1375 empathy_account_settings_processed_password (GObject *source,
1376     GAsyncResult *result,
1377     gpointer user_data,
1378     gpointer finish_func)
1379 {
1380   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1381   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1382   GSimpleAsyncResult *r;
1383   GError *error = NULL;
1384   gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1385     finish_func;
1386
1387   g_free (priv->password_original);
1388   priv->password_original = g_strdup (priv->password);
1389
1390   if (!func (TP_ACCOUNT (source), result, &error))
1391     {
1392       g_simple_async_result_set_from_error (priv->apply_result, error);
1393       g_error_free (error);
1394     }
1395
1396   empathy_account_settings_discard_changes (settings);
1397
1398   r = priv->apply_result;
1399   priv->apply_result = NULL;
1400
1401   g_simple_async_result_complete (r);
1402   g_object_unref (r);
1403 }
1404
1405 static void
1406 empathy_account_settings_set_password_cb (GObject *source,
1407     GAsyncResult *result,
1408     gpointer user_data)
1409 {
1410   empathy_account_settings_processed_password (source, result, user_data,
1411       empathy_keyring_set_account_password_finish);
1412 }
1413
1414 static void
1415 empathy_account_settings_delete_password_cb (GObject *source,
1416     GAsyncResult *result,
1417     gpointer user_data)
1418 {
1419   empathy_account_settings_processed_password (source, result, user_data,
1420       empathy_keyring_delete_account_password_finish);
1421 }
1422
1423 static void
1424 empathy_account_settings_account_updated (GObject *source,
1425     GAsyncResult *result,
1426     gpointer user_data)
1427 {
1428   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1429   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1430   GSimpleAsyncResult *r;
1431   GError *error = NULL;
1432   GStrv reconnect_required = NULL;
1433
1434   if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1435           result, &reconnect_required, &error))
1436     {
1437       g_simple_async_result_set_from_error (priv->apply_result, error);
1438       g_error_free (error);
1439       goto out;
1440     }
1441
1442   /* Only set the password in the keyring if the CM supports SASL and
1443    * it's changed. */
1444   if (priv->supports_sasl && priv->password_changed)
1445     {
1446       if (priv->password != NULL)
1447         {
1448           /* FIXME: we shouldn't save the password if we
1449            * can't (MaySaveResponse=False) but we don't have API to check that
1450            * at this point (fdo #35382). */
1451           empathy_keyring_set_account_password_async (priv->account, priv->password,
1452               empathy_account_settings_set_password_cb, settings);
1453         }
1454       else
1455         {
1456           empathy_keyring_delete_account_password_async (priv->account,
1457               empathy_account_settings_delete_password_cb, settings);
1458         }
1459
1460       return;
1461     }
1462
1463   g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1464       g_strv_length (reconnect_required) > 0);
1465
1466 out:
1467   empathy_account_settings_discard_changes (settings);
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   g_strfreev (reconnect_required);
1475 }
1476
1477 static void
1478 empathy_account_settings_created_cb (GObject *source,
1479     GAsyncResult *result,
1480     gpointer user_data)
1481 {
1482   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1483   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1484   TpAccount *account;
1485   GError *error = NULL;
1486   GSimpleAsyncResult *r;
1487
1488   account = tp_account_manager_create_account_finish (
1489     TP_ACCOUNT_MANAGER (source), result, &error);
1490
1491   if (account == NULL)
1492     {
1493       g_simple_async_result_set_from_error (priv->apply_result, error);
1494     }
1495   else
1496     {
1497       priv->account = g_object_ref (account);
1498
1499       if (priv->supports_sasl && priv->password != NULL)
1500         {
1501           /* Save the password before connecting */
1502           /* FIXME: we shouldn't save the password if we
1503            * can't (MaySaveResponse=False) but we don't have API to check that
1504            * at this point (fdo #35382). */
1505           empathy_keyring_set_account_password_async (priv->account,
1506               priv->password, empathy_account_settings_set_password_cb,
1507               settings);
1508           return;
1509         }
1510
1511       empathy_account_settings_discard_changes (settings);
1512     }
1513
1514   r = priv->apply_result;
1515   priv->apply_result = NULL;
1516
1517   g_simple_async_result_complete (r);
1518   g_object_unref (r);
1519 }
1520
1521
1522 static void
1523 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1524 {
1525   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1526   GHashTable *properties;
1527   TpConnectionPresenceType type;
1528   gchar *status;
1529   gchar *message;
1530   EmpathyPresenceManager *presence_mgr;
1531
1532   properties = tp_asv_new (NULL, NULL);
1533
1534   presence_mgr = empathy_presence_manager_dup_singleton ();
1535   type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1536       &message);
1537   g_object_unref (presence_mgr);
1538
1539   if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1540     {
1541       /* Create the account with the requested presence the same as the current
1542         * global requested presence, but don't enable it */
1543       GValueArray *presence;
1544       GValue vtype = { 0, };
1545       GValue vstatus = { 0, };
1546       GValue vmessage = { 0, };
1547
1548       presence = g_value_array_new (3);
1549
1550       g_value_init (&vtype, G_TYPE_UINT);
1551       g_value_set_uint (&vtype, type);
1552       g_value_array_append (presence, &vtype);
1553
1554       g_value_init (&vstatus, G_TYPE_STRING);
1555       g_value_take_string (&vstatus, status);
1556       g_value_array_append (presence, &vstatus);
1557
1558       g_value_init (&vmessage, G_TYPE_STRING);
1559       g_value_take_string (&vmessage, message);
1560       g_value_array_append (presence, &vmessage);
1561
1562       tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1563         TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1564     }
1565
1566   tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1567       priv->icon_name);
1568
1569   if (priv->service != NULL)
1570     tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1571
1572   tp_account_manager_create_account_async (priv->account_manager,
1573     priv->cm_name, priv->protocol, priv->display_name,
1574     priv->parameters, properties,
1575     empathy_account_settings_created_cb,
1576     settings);
1577
1578   g_hash_table_unref (properties);
1579 }
1580
1581 static void
1582 empathy_account_settings_manager_ready_cb (GObject *source_object,
1583     GAsyncResult *result,
1584     gpointer user_data)
1585 {
1586   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1587   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1588   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1589   GError *error = NULL;
1590
1591   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1592     {
1593       DEBUG ("Failed to prepare account manager: %s", error->message);
1594       g_error_free (error);
1595       return;
1596     }
1597
1598   g_assert (priv->apply_result != NULL && priv->account == NULL);
1599   empathy_account_settings_do_create_account (settings);
1600 }
1601
1602 void
1603 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1604     GAsyncReadyCallback callback,
1605     gpointer user_data)
1606 {
1607   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1608
1609   if (priv->apply_result != NULL)
1610     {
1611       g_simple_async_report_error_in_idle (G_OBJECT (settings),
1612           callback, user_data,
1613           G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1614       return;
1615     }
1616
1617   priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1618       callback, user_data, empathy_account_settings_apply_finish);
1619
1620   /* We'll have to reconnect only if we change none DBus_Property on an
1621    * existing account. */
1622   g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1623
1624   if (priv->account == NULL)
1625     {
1626       tp_account_manager_prepare_async (priv->account_manager, NULL,
1627           empathy_account_settings_manager_ready_cb, settings);
1628     }
1629   else
1630     {
1631       tp_account_update_parameters_async (priv->account,
1632           priv->parameters, (const gchar **)priv->unset_parameters->data,
1633           empathy_account_settings_account_updated, settings);
1634     }
1635 }
1636
1637 gboolean
1638 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1639     GAsyncResult *result,
1640     gboolean *reconnect_required,
1641     GError **error)
1642 {
1643   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1644       error))
1645     return FALSE;
1646
1647   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1648     G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1649
1650   if (reconnect_required != NULL)
1651     *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1652         G_SIMPLE_ASYNC_RESULT (result));
1653
1654   return TRUE;
1655 }
1656
1657 gboolean
1658 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1659     TpAccount *account)
1660 {
1661   EmpathyAccountSettingsPriv *priv;
1662   const gchar *account_path;
1663   const gchar *priv_account_path;
1664
1665   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1666   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1667
1668   priv = GET_PRIV (settings);
1669
1670   if (priv->account == NULL)
1671     return FALSE;
1672
1673   account_path = tp_proxy_get_object_path (TP_PROXY (account));
1674   priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1675
1676   return (!tp_strdiff (account_path, priv_account_path));
1677 }
1678
1679 void
1680 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1681     const gchar *param,
1682     const gchar *pattern)
1683 {
1684   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1685   GRegex *regex;
1686   GError *error = NULL;
1687
1688   regex = g_regex_new (pattern, 0, 0, &error);
1689   if (regex == NULL)
1690     {
1691       g_warning ("Failed to create reg exp: %s", error->message);
1692       g_error_free (error);
1693       return;
1694     }
1695
1696   g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1697 }
1698
1699 gboolean
1700 empathy_account_settings_parameter_is_valid (
1701     EmpathyAccountSettings *settings,
1702     const gchar *param)
1703 {
1704   EmpathyAccountSettingsPriv *priv;
1705   const GRegex *regex;
1706   const gchar *value;
1707
1708   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1709
1710   priv = GET_PRIV (settings);
1711
1712   if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1713     {
1714       /* first, look if it's set in our own parameters */
1715       if (tp_asv_lookup (priv->parameters, param))
1716         goto test_regex;
1717
1718       /* if we did not unset the parameter, look if it's in the account */
1719       if (priv->account != NULL &&
1720           !empathy_account_settings_is_unset (settings, param))
1721         {
1722           const GHashTable *account_params;
1723
1724           account_params = tp_account_get_parameters (priv->account);
1725           if (tp_asv_lookup (account_params, param))
1726             goto test_regex;
1727         }
1728
1729       return FALSE;
1730     }
1731
1732 test_regex:
1733   /* test whether parameter value matches its regex */
1734   regex = g_hash_table_lookup (priv->param_regexps, param);
1735   if (regex)
1736     {
1737       value = empathy_account_settings_get_string (settings, param);
1738       if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1739         return FALSE;
1740     }
1741
1742   return TRUE;
1743 }
1744
1745 gboolean
1746 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1747 {
1748   EmpathyAccountSettingsPriv *priv;
1749   const gchar *param;
1750   GHashTableIter iter;
1751   GList *l;
1752
1753   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1754
1755   priv = GET_PRIV (settings);
1756
1757   for (l = priv->required_params; l; l = l->next)
1758     {
1759       if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1760         return FALSE;
1761     }
1762
1763   g_hash_table_iter_init (&iter, priv->param_regexps);
1764   while (g_hash_table_iter_next (&iter, (gpointer *) &param, NULL))
1765     {
1766       if (!empathy_account_settings_parameter_is_valid (settings, param))
1767         return FALSE;
1768     }
1769
1770   return TRUE;
1771 }
1772
1773 const TpConnectionManagerProtocol *
1774 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1775 {
1776   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1777
1778   return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1779 }
1780
1781 gboolean
1782 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1783 {
1784   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1785
1786   return priv->supports_sasl;
1787 }
1788
1789 gboolean
1790 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1791     const gchar *param)
1792 {
1793   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1794
1795   return tp_protocol_has_param (priv->protocol_obj, param);
1796 }