]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-settings.c
Prevent segv attempting to free uninitialised value.
[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 static void
802 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
803     const gchar *param)
804 {
805   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
806   guint idx;
807   gchar *val;
808
809   for (idx = 0; idx < priv->unset_parameters->len; idx++)
810     {
811       val = g_array_index (priv->unset_parameters, gchar *, idx);
812
813       if (!tp_strdiff (val, param))
814         {
815           priv->unset_parameters =
816             g_array_remove_index (priv->unset_parameters, idx);
817           g_free (val);
818
819           break;
820         }
821     }
822 }
823
824 const GValue *
825 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
826     const gchar *param)
827 {
828   TpConnectionManagerParam *p;
829
830   p = empathy_account_settings_get_tp_param (settings, param);
831
832   if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
833     return NULL;
834
835   return &(p->default_value);
836 }
837
838 const gchar *
839 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
840     const gchar *param)
841 {
842   TpConnectionManagerParam *p;
843
844   p = empathy_account_settings_get_tp_param (settings, param);
845
846   if (p == NULL)
847     return NULL;
848
849   return p->dbus_signature;
850 }
851
852 const GValue *
853 empathy_account_settings_get (EmpathyAccountSettings *settings,
854     const gchar *param)
855 {
856   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
857   const GValue *result = NULL;
858
859   /* Lookup the update parameters we set */
860   result = tp_asv_lookup (priv->parameters, param);
861   if (result != NULL)
862     return result;
863
864   /* If the parameters isn't unset use the accounts setting if any */
865   if (priv->account != NULL
866       && !empathy_account_settings_is_unset (settings, param))
867     {
868       const GHashTable *parameters;
869
870       parameters = tp_account_get_parameters (priv->account);
871       result = tp_asv_lookup (parameters, param);
872
873       if (result != NULL)
874         return result;
875     }
876
877   /* fallback to the default */
878   return empathy_account_settings_get_default (settings, param);
879 }
880
881 void
882 empathy_account_settings_unset (EmpathyAccountSettings *settings,
883     const gchar *param)
884 {
885   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
886   gchar *v;
887   if (empathy_account_settings_is_unset (settings, param))
888     return;
889
890   if (priv->supports_sasl && !tp_strdiff (param, "password"))
891     {
892       g_free (priv->password);
893       priv->password = NULL;
894       priv->password_changed = TRUE;
895       return;
896     }
897
898   v = g_strdup (param);
899
900   g_array_append_val (priv->unset_parameters, v);
901   g_hash_table_remove (priv->parameters, param);
902 }
903
904 void
905 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
906 {
907   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
908
909   g_hash_table_remove_all (priv->parameters);
910   empathy_account_settings_free_unset_parameters (settings);
911
912   priv->password_changed = FALSE;
913   g_free (priv->password);
914   priv->password = g_strdup (priv->password_original);
915 }
916
917 const gchar *
918 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
919     const gchar *param)
920 {
921   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
922   const GValue *v;
923
924   if (!tp_strdiff (param, "password") && priv->supports_sasl)
925     {
926       return priv->password;
927     }
928
929   v = empathy_account_settings_get (settings, param);
930
931   if (v == NULL || !G_VALUE_HOLDS_STRING (v))
932     return NULL;
933
934   return g_value_get_string (v);
935 }
936
937 const gchar * const *
938 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
939     const gchar *param)
940 {
941   const GValue *v;
942
943   v = empathy_account_settings_get (settings, param);
944
945   if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
946     return NULL;
947
948   return g_value_get_boxed (v);
949 }
950
951 gint32
952 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
953     const gchar *param)
954 {
955   const GValue *v;
956   gint32 ret = 0;
957
958   v = empathy_account_settings_get (settings, param);
959
960   if (v == NULL)
961     return 0;
962
963   switch G_VALUE_TYPE (v)
964     {
965       case G_TYPE_UCHAR:
966         ret = g_value_get_uchar (v);
967         break;
968       case G_TYPE_INT:
969         ret = g_value_get_int (v);
970         break;
971       case G_TYPE_UINT:
972         ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
973             G_MAXINT32);
974         break;
975       case G_TYPE_INT64:
976         ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
977         break;
978       case G_TYPE_UINT64:
979         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
980             G_MAXINT32);
981         break;
982       default:
983         ret = 0;
984         break;
985     }
986
987   return ret;
988 }
989
990 gint64
991 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
992     const gchar *param)
993 {
994   const GValue *v;
995   gint64 ret = 0;
996
997   v = empathy_account_settings_get (settings, param);
998   if (v == NULL)
999     return 0;
1000
1001   switch G_VALUE_TYPE (v)
1002     {
1003       case G_TYPE_UCHAR:
1004         ret = g_value_get_uchar (v);
1005         break;
1006       case G_TYPE_INT:
1007         ret = g_value_get_int (v);
1008         break;
1009       case G_TYPE_UINT:
1010         ret = g_value_get_uint (v);
1011         break;
1012       case G_TYPE_INT64:
1013         ret = g_value_get_int64 (v);
1014         break;
1015       case G_TYPE_UINT64:
1016         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1017         break;
1018       default:
1019         ret = 0;
1020         break;
1021     }
1022
1023   return ret;
1024 }
1025
1026 guint32
1027 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1028     const gchar *param)
1029 {
1030   const GValue *v;
1031   guint32 ret;
1032
1033   v = empathy_account_settings_get (settings, param);
1034   if (v == NULL)
1035     return 0;
1036
1037   switch G_VALUE_TYPE (v)
1038     {
1039       case G_TYPE_UCHAR:
1040         ret = g_value_get_uchar (v);
1041         break;
1042       case G_TYPE_INT:
1043         ret = MAX (0, g_value_get_int (v));
1044         break;
1045       case G_TYPE_UINT:
1046         ret = g_value_get_uint (v);
1047         break;
1048       case G_TYPE_INT64:
1049         ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1050         break;
1051       case G_TYPE_UINT64:
1052         ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1053         break;
1054       default:
1055         ret = 0;
1056         break;
1057     }
1058
1059   return ret;
1060 }
1061
1062 guint64
1063 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1064     const gchar *param)
1065 {
1066   const GValue *v;
1067   guint64 ret = 0;
1068
1069   v = empathy_account_settings_get (settings, param);
1070
1071   if (v == NULL || !G_VALUE_HOLDS_INT (v))
1072     return 0;
1073
1074   switch G_VALUE_TYPE (v)
1075     {
1076       case G_TYPE_UCHAR:
1077         ret = g_value_get_uchar (v);
1078         break;
1079       case G_TYPE_INT:
1080         ret = MAX (0, g_value_get_int (v));
1081         break;
1082       case G_TYPE_UINT:
1083         ret = g_value_get_uint (v);
1084         break;
1085       case G_TYPE_INT64:
1086         ret = MAX (0, g_value_get_int64 (v));
1087         break;
1088       case G_TYPE_UINT64:
1089         ret = g_value_get_uint64 (v);
1090         break;
1091       default:
1092         ret = 0;
1093         break;
1094     }
1095
1096   return ret;
1097 }
1098
1099 gboolean
1100 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1101     const gchar *param)
1102 {
1103   const GValue *v;
1104
1105   v = empathy_account_settings_get (settings, param);
1106
1107   if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1108     return FALSE;
1109
1110   return g_value_get_boolean (v);
1111 }
1112
1113 void
1114 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1115     const gchar *param,
1116     const gchar *value)
1117 {
1118   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1119
1120   g_return_if_fail (param != NULL);
1121   g_return_if_fail (value != NULL);
1122
1123   if (!tp_strdiff (param, "password") && priv->supports_sasl)
1124     {
1125       g_free (priv->password);
1126       priv->password = g_strdup (value);
1127       priv->password_changed = TRUE;
1128     }
1129   else
1130     {
1131       tp_asv_set_string (priv->parameters, g_strdup (param), value);
1132     }
1133
1134   account_settings_remove_from_unset (settings, param);
1135 }
1136
1137 void
1138 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1139     const gchar *param,
1140     gchar **value)
1141 {
1142   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1143
1144   g_return_if_fail (param != NULL);
1145   g_return_if_fail (value != NULL);
1146
1147   tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1148
1149   account_settings_remove_from_unset (settings, param);
1150 }
1151
1152 void
1153 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1154     const gchar *param,
1155     gint32 value)
1156 {
1157   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1158
1159   g_return_if_fail (param != NULL);
1160
1161   tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1162
1163   account_settings_remove_from_unset (settings, param);
1164 }
1165
1166 void
1167 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1168     const gchar *param,
1169     gint64 value)
1170 {
1171   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1172
1173   g_return_if_fail (param != NULL);
1174
1175   tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1176
1177   account_settings_remove_from_unset (settings, param);
1178 }
1179
1180 void
1181 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1182     const gchar *param,
1183     guint32 value)
1184 {
1185   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1186
1187   g_return_if_fail (param != NULL);
1188
1189   tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1190
1191   account_settings_remove_from_unset (settings, param);
1192 }
1193
1194 void
1195 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1196     const gchar *param,
1197     guint64 value)
1198 {
1199   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1200
1201   g_return_if_fail (param != NULL);
1202
1203   tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1204
1205   account_settings_remove_from_unset (settings, param);
1206 }
1207
1208 void
1209 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1210     const gchar *param,
1211     gboolean value)
1212 {
1213   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1214
1215   g_return_if_fail (param != NULL);
1216
1217   tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1218
1219   account_settings_remove_from_unset (settings, param);
1220 }
1221
1222 static void
1223 account_settings_display_name_set_cb (GObject *src,
1224     GAsyncResult *res,
1225     gpointer user_data)
1226 {
1227   GError *error = NULL;
1228   TpAccount *account = TP_ACCOUNT (src);
1229   GSimpleAsyncResult *set_result = user_data;
1230
1231   tp_account_set_display_name_finish (account, res, &error);
1232
1233   if (error != NULL)
1234     {
1235       g_simple_async_result_set_from_error (set_result, error);
1236       g_error_free (error);
1237     }
1238
1239   g_simple_async_result_complete (set_result);
1240   g_object_unref (set_result);
1241 }
1242
1243 void
1244 empathy_account_settings_set_display_name_async (
1245   EmpathyAccountSettings *settings,
1246   const gchar *name,
1247   GAsyncReadyCallback callback,
1248   gpointer user_data)
1249 {
1250   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1251   GSimpleAsyncResult *result;
1252
1253   g_return_if_fail (name != NULL);
1254
1255   result = g_simple_async_result_new (G_OBJECT (settings),
1256       callback, user_data, empathy_account_settings_set_display_name_finish);
1257
1258   if (!tp_strdiff (name, priv->display_name))
1259     {
1260       /* Nothing to do */
1261       g_simple_async_result_complete_in_idle (result);
1262       return;
1263     }
1264
1265   if (priv->account == NULL)
1266     {
1267       if (priv->display_name != NULL)
1268         g_free (priv->display_name);
1269
1270       priv->display_name = g_strdup (name);
1271
1272       g_simple_async_result_complete_in_idle (result);
1273
1274       return;
1275     }
1276
1277   tp_account_set_display_name_async (priv->account, name,
1278       account_settings_display_name_set_cb, result);
1279 }
1280
1281 gboolean
1282 empathy_account_settings_set_display_name_finish (
1283   EmpathyAccountSettings *settings,
1284   GAsyncResult *result,
1285   GError **error)
1286 {
1287   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1288       error))
1289     return FALSE;
1290
1291   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1292     G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1293       FALSE);
1294
1295   return TRUE;
1296 }
1297
1298 static void
1299 account_settings_icon_name_set_cb (GObject *src,
1300     GAsyncResult *res,
1301     gpointer user_data)
1302 {
1303   GError *error = NULL;
1304   TpAccount *account = TP_ACCOUNT (src);
1305   GSimpleAsyncResult *set_result = user_data;
1306
1307   tp_account_set_icon_name_finish (account, res, &error);
1308
1309   if (error != NULL)
1310     {
1311       g_simple_async_result_set_from_error (set_result, error);
1312       g_error_free (error);
1313     }
1314
1315   g_simple_async_result_complete (set_result);
1316   g_object_unref (set_result);
1317 }
1318
1319 void
1320 empathy_account_settings_set_icon_name_async (
1321   EmpathyAccountSettings *settings,
1322   const gchar *name,
1323   GAsyncReadyCallback callback,
1324   gpointer user_data)
1325 {
1326   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1327   GSimpleAsyncResult *result;
1328
1329   g_return_if_fail (name != NULL);
1330
1331   result = g_simple_async_result_new (G_OBJECT (settings),
1332       callback, user_data, empathy_account_settings_set_icon_name_finish);
1333
1334   if (priv->account == NULL)
1335     {
1336       if (priv->icon_name != NULL)
1337         g_free (priv->icon_name);
1338
1339       priv->icon_name = g_strdup (name);
1340
1341       g_simple_async_result_complete_in_idle (result);
1342
1343       return;
1344     }
1345
1346   tp_account_set_icon_name_async (priv->account, name,
1347       account_settings_icon_name_set_cb, result);
1348 }
1349
1350 gboolean
1351 empathy_account_settings_set_icon_name_finish (
1352   EmpathyAccountSettings *settings,
1353   GAsyncResult *result,
1354   GError **error)
1355 {
1356   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1357       error))
1358     return FALSE;
1359
1360   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1361     G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1362       FALSE);
1363
1364   return TRUE;
1365 }
1366
1367 static void
1368 empathy_account_settings_processed_password (GObject *source,
1369     GAsyncResult *result,
1370     gpointer user_data,
1371     gpointer finish_func)
1372 {
1373   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1374   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1375   GSimpleAsyncResult *r;
1376   GError *error = NULL;
1377   gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1378     finish_func;
1379
1380   g_free (priv->password_original);
1381   priv->password_original = g_strdup (priv->password);
1382
1383   if (!func (TP_ACCOUNT (source), result, &error))
1384     {
1385       g_simple_async_result_set_from_error (priv->apply_result, error);
1386       g_error_free (error);
1387     }
1388
1389   empathy_account_settings_discard_changes (settings);
1390
1391   r = priv->apply_result;
1392   priv->apply_result = NULL;
1393
1394   g_simple_async_result_complete (r);
1395   g_object_unref (r);
1396 }
1397
1398 static void
1399 empathy_account_settings_set_password_cb (GObject *source,
1400     GAsyncResult *result,
1401     gpointer user_data)
1402 {
1403   empathy_account_settings_processed_password (source, result, user_data,
1404       empathy_keyring_set_account_password_finish);
1405 }
1406
1407 static void
1408 empathy_account_settings_delete_password_cb (GObject *source,
1409     GAsyncResult *result,
1410     gpointer user_data)
1411 {
1412   empathy_account_settings_processed_password (source, result, user_data,
1413       empathy_keyring_delete_account_password_finish);
1414 }
1415
1416 static void
1417 empathy_account_settings_account_updated (GObject *source,
1418     GAsyncResult *result,
1419     gpointer user_data)
1420 {
1421   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1422   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1423   GSimpleAsyncResult *r;
1424   GError *error = NULL;
1425   GStrv reconnect_required = NULL;
1426
1427   if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1428           result, &reconnect_required, &error))
1429     {
1430       g_simple_async_result_set_from_error (priv->apply_result, error);
1431       g_error_free (error);
1432       goto out;
1433     }
1434
1435   /* Only set the password in the keyring if the CM supports SASL and
1436    * it's changed. */
1437   if (priv->supports_sasl && priv->password_changed)
1438     {
1439       if (priv->password != NULL)
1440         {
1441           empathy_keyring_set_account_password_async (priv->account, priv->password,
1442               empathy_account_settings_set_password_cb, settings);
1443         }
1444       else
1445         {
1446           empathy_keyring_delete_account_password_async (priv->account,
1447               empathy_account_settings_delete_password_cb, settings);
1448         }
1449
1450       return;
1451     }
1452
1453   g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1454       g_strv_length (reconnect_required) > 0);
1455
1456 out:
1457   empathy_account_settings_discard_changes (settings);
1458
1459   r = priv->apply_result;
1460   priv->apply_result = NULL;
1461
1462   g_simple_async_result_complete (r);
1463   g_object_unref (r);
1464   g_strfreev (reconnect_required);
1465 }
1466
1467 static void
1468 empathy_account_settings_created_cb (GObject *source,
1469     GAsyncResult *result,
1470     gpointer user_data)
1471 {
1472   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1473   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1474   TpAccount *account;
1475   GError *error = NULL;
1476   GSimpleAsyncResult *r;
1477
1478   account = tp_account_manager_create_account_finish (
1479     TP_ACCOUNT_MANAGER (source), result, &error);
1480
1481   if (account == NULL)
1482     {
1483       g_simple_async_result_set_from_error (priv->apply_result, error);
1484     }
1485   else
1486     {
1487       priv->account = g_object_ref (account);
1488       empathy_account_settings_discard_changes (settings);
1489     }
1490
1491   r = priv->apply_result;
1492   priv->apply_result = NULL;
1493
1494   g_simple_async_result_complete (r);
1495   g_object_unref (r);
1496 }
1497
1498
1499 static void
1500 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1501 {
1502   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1503   GHashTable *properties;
1504   TpConnectionPresenceType type;
1505   gchar *status;
1506   gchar *message;
1507   EmpathyPresenceManager *presence_mgr;
1508
1509   properties = tp_asv_new (NULL, NULL);
1510
1511   presence_mgr = empathy_presence_manager_dup_singleton ();
1512   type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1513       &message);
1514   g_object_unref (presence_mgr);
1515
1516   if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1517     {
1518       /* Create the account with the requested presence the same as the current
1519         * global requested presence, but don't enable it */
1520       GValueArray *presence;
1521       GValue vtype = { 0, };
1522       GValue vstatus = { 0, };
1523       GValue vmessage = { 0, };
1524
1525       presence = g_value_array_new (3);
1526
1527       g_value_init (&vtype, G_TYPE_UINT);
1528       g_value_set_uint (&vtype, type);
1529       g_value_array_append (presence, &vtype);
1530
1531       g_value_init (&vstatus, G_TYPE_STRING);
1532       g_value_take_string (&vstatus, status);
1533       g_value_array_append (presence, &vstatus);
1534
1535       g_value_init (&vmessage, G_TYPE_STRING);
1536       g_value_take_string (&vmessage, message);
1537       g_value_array_append (presence, &vmessage);
1538
1539       tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1540         TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1541     }
1542
1543   tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1544       priv->icon_name);
1545
1546   if (priv->service != NULL)
1547     tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1548
1549   tp_account_manager_create_account_async (priv->account_manager,
1550     priv->cm_name, priv->protocol, priv->display_name,
1551     priv->parameters, properties,
1552     empathy_account_settings_created_cb,
1553     settings);
1554
1555   g_hash_table_unref (properties);
1556 }
1557
1558 static void
1559 empathy_account_settings_manager_ready_cb (GObject *source_object,
1560     GAsyncResult *result,
1561     gpointer user_data)
1562 {
1563   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1564   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1565   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1566   GError *error = NULL;
1567
1568   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1569     {
1570       DEBUG ("Failed to prepare account manager: %s", error->message);
1571       g_error_free (error);
1572       return;
1573     }
1574
1575   g_assert (priv->apply_result != NULL && priv->account == NULL);
1576   empathy_account_settings_do_create_account (settings);
1577 }
1578
1579 void
1580 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1581     GAsyncReadyCallback callback,
1582     gpointer user_data)
1583 {
1584   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1585
1586   if (priv->apply_result != NULL)
1587     {
1588       g_simple_async_report_error_in_idle (G_OBJECT (settings),
1589           callback, user_data,
1590           G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1591       return;
1592     }
1593
1594   priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1595       callback, user_data, empathy_account_settings_apply_finish);
1596
1597   /* We'll have to reconnect only if we change none DBus_Property on an
1598    * existing account. */
1599   g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1600
1601   if (priv->account == NULL)
1602     {
1603       tp_account_manager_prepare_async (priv->account_manager, NULL,
1604           empathy_account_settings_manager_ready_cb, settings);
1605     }
1606   else
1607     {
1608       tp_account_update_parameters_async (priv->account,
1609           priv->parameters, (const gchar **)priv->unset_parameters->data,
1610           empathy_account_settings_account_updated, settings);
1611     }
1612 }
1613
1614 gboolean
1615 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1616     GAsyncResult *result,
1617     gboolean *reconnect_required,
1618     GError **error)
1619 {
1620   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1621       error))
1622     return FALSE;
1623
1624   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1625     G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1626
1627   if (reconnect_required != NULL)
1628     *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1629         G_SIMPLE_ASYNC_RESULT (result));
1630
1631   return TRUE;
1632 }
1633
1634 gboolean
1635 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1636     TpAccount *account)
1637 {
1638   EmpathyAccountSettingsPriv *priv;
1639   const gchar *account_path;
1640   const gchar *priv_account_path;
1641
1642   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1643   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1644
1645   priv = GET_PRIV (settings);
1646
1647   if (priv->account == NULL)
1648     return FALSE;
1649
1650   account_path = tp_proxy_get_object_path (TP_PROXY (account));
1651   priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1652
1653   return (!tp_strdiff (account_path, priv_account_path));
1654 }
1655
1656 void
1657 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1658     const gchar *param,
1659     const gchar *pattern)
1660 {
1661   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1662   GRegex *regex;
1663   GError *error = NULL;
1664
1665   regex = g_regex_new (pattern, 0, 0, &error);
1666   if (regex == NULL)
1667     {
1668       g_warning ("Failed to create reg exp: %s", error->message);
1669       g_error_free (error);
1670       return;
1671     }
1672
1673   g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1674 }
1675
1676 gboolean
1677 empathy_account_settings_parameter_is_valid (
1678     EmpathyAccountSettings *settings,
1679     const gchar *param)
1680 {
1681   EmpathyAccountSettingsPriv *priv;
1682   const GRegex *regex;
1683   const gchar *value;
1684
1685   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1686
1687   priv = GET_PRIV (settings);
1688
1689   if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1690     {
1691       /* first, look if it's set in our own parameters */
1692       if (tp_asv_lookup (priv->parameters, param))
1693         goto test_regex;
1694
1695       /* if we did not unset the parameter, look if it's in the account */
1696       if (priv->account != NULL &&
1697           !empathy_account_settings_is_unset (settings, param))
1698         {
1699           const GHashTable *account_params;
1700
1701           account_params = tp_account_get_parameters (priv->account);
1702           if (tp_asv_lookup (account_params, param))
1703             goto test_regex;
1704         }
1705
1706       return FALSE;
1707     }
1708
1709 test_regex:
1710   /* test whether parameter value matches its regex */
1711   regex = g_hash_table_lookup (priv->param_regexps, param);
1712   if (regex)
1713     {
1714       value = empathy_account_settings_get_string (settings, param);
1715       if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1716         return FALSE;
1717     }
1718
1719   return TRUE;
1720 }
1721
1722 gboolean
1723 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1724 {
1725   EmpathyAccountSettingsPriv *priv;
1726   const gchar *param;
1727   GHashTableIter iter;
1728   GList *l;
1729
1730   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1731
1732   priv = GET_PRIV (settings);
1733
1734   for (l = priv->required_params; l; l = l->next)
1735     {
1736       if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1737         return FALSE;
1738     }
1739
1740   g_hash_table_iter_init (&iter, priv->param_regexps);
1741   while (g_hash_table_iter_next (&iter, (gpointer *) &param, NULL))
1742     {
1743       if (!empathy_account_settings_parameter_is_valid (settings, param))
1744         return FALSE;
1745     }
1746
1747   return TRUE;
1748 }
1749
1750 const TpConnectionManagerProtocol *
1751 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1752 {
1753   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1754
1755   return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1756 }
1757
1758 gboolean
1759 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1760 {
1761   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1762
1763   return priv->supports_sasl;
1764 }