]> git.0d.be Git - empathy.git/blob - libempathy/empathy-account-settings.c
Merge branch 'sasl'
[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-utils.h"
33 #include "empathy-idle.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
36 #include <libempathy/empathy-debug.h>
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
39
40 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
41
42 enum {
43   PROP_ACCOUNT = 1,
44   PROP_CM_NAME,
45   PROP_PROTOCOL,
46   PROP_SERVICE,
47   PROP_DISPLAY_NAME,
48   PROP_DISPLAY_NAME_OVERRIDDEN,
49   PROP_READY
50 };
51
52 /* private structure */
53 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
54
55 struct _EmpathyAccountSettingsPriv
56 {
57   gboolean dispose_has_run;
58   EmpathyConnectionManagers *managers;
59   TpAccountManager *account_manager;
60
61   TpConnectionManager *manager;
62
63   TpAccount *account;
64   gchar *cm_name;
65   gchar *protocol;
66   gchar *service;
67   gchar *display_name;
68   gchar *icon_name;
69   gboolean display_name_overridden;
70   gboolean ready;
71
72   /* Parameter name (gchar *) -> parameter value (GValue) */
73   GHashTable *parameters;
74   /* Keys are parameter names from the hash above (gchar *).
75    * Values are regular expresions that should match corresponding parameter
76    * values (GRegex *). Possible regexp patterns are defined in
77    * empathy-account-widget.c */
78   GHashTable *param_regexps;
79   GArray *unset_parameters;
80   GList *required_params;
81
82   gulong managers_ready_id;
83
84   GSimpleAsyncResult *apply_result;
85 };
86
87 static void
88 empathy_account_settings_init (EmpathyAccountSettings *obj)
89 {
90   EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
91     EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
92
93   obj->priv = priv;
94
95   /* allocate any data required by the object here */
96   priv->managers = empathy_connection_managers_dup_singleton ();
97   priv->account_manager = tp_account_manager_dup ();
98
99   priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
100     g_free, (GDestroyNotify) tp_g_value_slice_free);
101
102   priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
103     g_free, (GDestroyNotify) g_regex_unref);
104
105   priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
106
107   priv->required_params = NULL;
108 }
109
110 static void empathy_account_settings_dispose (GObject *object);
111 static void empathy_account_settings_finalize (GObject *object);
112 static void empathy_account_settings_account_ready_cb (GObject *source_object,
113     GAsyncResult *result, gpointer user_data);
114 static void empathy_account_settings_managers_ready_cb (GObject *obj,
115     GParamSpec *pspec, gpointer user_data);
116 static void empathy_account_settings_check_readyness (
117     EmpathyAccountSettings *self);
118
119 static void
120 empathy_account_settings_set_property (GObject *object,
121     guint prop_id,
122     const GValue *value,
123     GParamSpec *pspec)
124 {
125   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
126   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
127
128   switch (prop_id)
129     {
130       case PROP_ACCOUNT:
131         priv->account = g_value_dup_object (value);
132         break;
133       case PROP_CM_NAME:
134         priv->cm_name = g_value_dup_string (value);
135         break;
136       case PROP_PROTOCOL:
137         priv->protocol = g_value_dup_string (value);
138         break;
139       case PROP_SERVICE:
140         priv->service = g_value_dup_string (value);
141         break;
142       case PROP_DISPLAY_NAME:
143         priv->display_name = g_value_dup_string (value);
144         break;
145       case PROP_DISPLAY_NAME_OVERRIDDEN:
146         priv->display_name_overridden = g_value_get_boolean (value);
147         break;
148       default:
149         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150         break;
151     }
152 }
153
154 static void
155 empathy_account_settings_get_property (GObject *object,
156     guint prop_id,
157     GValue *value,
158     GParamSpec *pspec)
159 {
160   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
161   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
162
163   switch (prop_id)
164     {
165       case PROP_ACCOUNT:
166         g_value_set_object (value, priv->account);
167         break;
168       case PROP_CM_NAME:
169         g_value_set_string (value, priv->cm_name);
170         break;
171       case PROP_PROTOCOL:
172         g_value_set_string (value, priv->protocol);
173         break;
174       case PROP_SERVICE:
175         g_value_set_string (value, priv->service);
176         break;
177       case PROP_DISPLAY_NAME:
178         g_value_set_string (value, priv->display_name);
179         break;
180       case PROP_DISPLAY_NAME_OVERRIDDEN:
181         g_value_set_boolean (value, priv->display_name_overridden);
182         break;
183       case PROP_READY:
184         g_value_set_boolean (value, priv->ready);
185         break;
186       default:
187         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188         break;
189     }
190 }
191
192 static void
193 empathy_account_settings_constructed (GObject *object)
194 {
195   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
196   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
197
198   if (priv->account != NULL)
199     {
200       g_free (priv->cm_name);
201       g_free (priv->protocol);
202       g_free (priv->service);
203
204       priv->cm_name =
205         g_strdup (tp_account_get_connection_manager (priv->account));
206       priv->protocol =
207         g_strdup (tp_account_get_protocol (priv->account));
208       priv->service =
209         g_strdup (tp_account_get_service (priv->account));
210       priv->icon_name = g_strdup
211         (tp_account_get_icon_name (priv->account));
212     }
213   else
214     {
215       priv->icon_name = empathy_protocol_icon_name (priv->protocol);
216     }
217
218   g_assert (priv->cm_name != NULL && priv->protocol != NULL);
219
220   empathy_account_settings_check_readyness (self);
221
222   if (!priv->ready)
223     {
224       GQuark features[] = {
225           TP_ACCOUNT_FEATURE_CORE,
226           TP_ACCOUNT_FEATURE_STORAGE,
227           0 };
228
229       tp_proxy_prepare_async (priv->account, features,
230           empathy_account_settings_account_ready_cb, self);
231       tp_g_signal_connect_object (priv->managers, "notify::ready",
232         G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
233     }
234
235   if (G_OBJECT_CLASS (
236         empathy_account_settings_parent_class)->constructed != NULL)
237     G_OBJECT_CLASS (
238         empathy_account_settings_parent_class)->constructed (object);
239 }
240
241
242 static void
243 empathy_account_settings_class_init (
244     EmpathyAccountSettingsClass *empathy_account_settings_class)
245 {
246   GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
247
248   g_type_class_add_private (empathy_account_settings_class, sizeof
249       (EmpathyAccountSettingsPriv));
250
251   object_class->dispose = empathy_account_settings_dispose;
252   object_class->finalize = empathy_account_settings_finalize;
253   object_class->set_property = empathy_account_settings_set_property;
254   object_class->get_property = empathy_account_settings_get_property;
255   object_class->constructed = empathy_account_settings_constructed;
256
257   g_object_class_install_property (object_class, PROP_ACCOUNT,
258     g_param_spec_object ("account",
259       "Account",
260       "The TpAccount backing these settings",
261       TP_TYPE_ACCOUNT,
262       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
263
264   g_object_class_install_property (object_class, PROP_CM_NAME,
265     g_param_spec_string ("connection-manager",
266       "connection-manager",
267       "The name of the connection manager this account uses",
268       NULL,
269       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
270
271   g_object_class_install_property (object_class, PROP_PROTOCOL,
272     g_param_spec_string ("protocol",
273       "Protocol",
274       "The name of the protocol this account uses",
275       NULL,
276       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
277
278   g_object_class_install_property (object_class, PROP_SERVICE,
279     g_param_spec_string ("service",
280       "Service",
281       "The service of this account, or NULL",
282       NULL,
283       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
284
285   g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
286     g_param_spec_string ("display-name",
287       "display-name",
288       "The display name account these settings belong to",
289       NULL,
290       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
291
292   g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
293       g_param_spec_boolean ("display-name-overridden",
294         "display-name-overridden",
295         "Whether the display name for this account has been manually "
296         "overridden",
297         FALSE,
298         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
299
300   g_object_class_install_property (object_class, PROP_READY,
301     g_param_spec_boolean ("ready",
302       "Ready",
303       "Whether this account is ready to be used",
304       FALSE,
305       G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
306 }
307
308 static void
309 empathy_account_settings_dispose (GObject *object)
310 {
311   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
312   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
313
314   if (priv->dispose_has_run)
315     return;
316
317   priv->dispose_has_run = TRUE;
318
319   if (priv->managers_ready_id != 0)
320     g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
321   priv->managers_ready_id = 0;
322
323   if (priv->managers != NULL)
324     g_object_unref (priv->managers);
325   priv->managers = NULL;
326
327   if (priv->manager != NULL)
328     g_object_unref (priv->manager);
329   priv->manager = NULL;
330
331   if (priv->account_manager != NULL)
332     g_object_unref (priv->account_manager);
333   priv->account_manager = NULL;
334
335   if (priv->account != NULL)
336     g_object_unref (priv->account);
337   priv->account = NULL;
338
339   /* release any references held by the object here */
340   if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
341     G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
342 }
343
344 static void
345 empathy_account_settings_free_unset_parameters (
346     EmpathyAccountSettings *settings)
347 {
348   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
349   guint i;
350
351   for (i = 0 ; i < priv->unset_parameters->len; i++)
352     g_free (g_array_index (priv->unset_parameters, gchar *, i));
353
354   g_array_set_size (priv->unset_parameters, 0);
355 }
356
357 static void
358 empathy_account_settings_finalize (GObject *object)
359 {
360   EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
361   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
362   GList *l;
363
364   /* free any data held directly by the object here */
365   g_free (priv->cm_name);
366   g_free (priv->protocol);
367   g_free (priv->service);
368   g_free (priv->display_name);
369   g_free (priv->icon_name);
370
371   if (priv->required_params != NULL)
372     {
373       for (l = priv->required_params; l; l = l->next)
374         g_free (l->data);
375       g_list_free (priv->required_params);
376     }
377
378   g_hash_table_destroy (priv->parameters);
379   g_hash_table_destroy (priv->param_regexps);
380
381   empathy_account_settings_free_unset_parameters (self);
382   g_array_free (priv->unset_parameters, TRUE);
383
384   G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
385 }
386
387 static void
388 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
389 {
390   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
391   const TpConnectionManagerProtocol *tp_protocol;
392
393   if (priv->ready)
394     return;
395
396   if (priv->account != NULL
397       && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
398       return;
399
400   if (!empathy_connection_managers_is_ready (priv->managers))
401     return;
402
403   priv->manager = empathy_connection_managers_get_cm (
404     priv->managers, priv->cm_name);
405
406   if (priv->manager == NULL)
407     return;
408
409   if (priv->account != NULL)
410     {
411       g_free (priv->display_name);
412       priv->display_name =
413         g_strdup (tp_account_get_display_name (priv->account));
414
415       g_free (priv->icon_name);
416       priv->icon_name =
417         g_strdup (tp_account_get_icon_name (priv->account));
418     }
419
420   tp_protocol = tp_connection_manager_get_protocol (priv->manager,
421     priv->protocol);
422
423   if (tp_protocol == NULL)
424     {
425       priv->manager = NULL;
426       return;
427     }
428
429   if (priv->required_params == NULL)
430     {
431       TpConnectionManagerParam *cur;
432
433       for (cur = tp_protocol->params; cur->name != NULL; cur++)
434         {
435           if (tp_connection_manager_param_is_required (cur))
436             {
437               priv->required_params = g_list_append (priv->required_params,
438                                                      g_strdup (cur->name));
439             }
440         }
441     }
442
443   g_object_ref (priv->manager);
444
445   priv->ready = TRUE;
446   g_object_notify (G_OBJECT (self), "ready");
447 }
448
449 static void
450 empathy_account_settings_account_ready_cb (GObject *source_object,
451     GAsyncResult *result,
452     gpointer user_data)
453 {
454   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
455   TpAccount *account = TP_ACCOUNT (source_object);
456   GError *error = NULL;
457
458   if (!tp_proxy_prepare_finish (account, result, &error))
459     {
460       DEBUG ("Failed to prepare account: %s", error->message);
461       g_error_free (error);
462       return;
463     }
464
465   empathy_account_settings_check_readyness (settings);
466 }
467
468 static void
469 empathy_account_settings_managers_ready_cb (GObject *object,
470     GParamSpec *pspec,
471     gpointer user_data)
472 {
473   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
474
475   empathy_account_settings_check_readyness (settings);
476 }
477
478 EmpathyAccountSettings *
479 empathy_account_settings_new (const gchar *connection_manager,
480     const gchar *protocol,
481     const gchar *service,
482     const char *display_name)
483 {
484   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
485       "connection-manager", connection_manager,
486       "protocol", protocol,
487       "service", service,
488       "display-name", display_name,
489       NULL);
490 }
491
492 EmpathyAccountSettings *
493 empathy_account_settings_new_for_account (TpAccount *account)
494 {
495   return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
496       "account", account,
497       NULL);
498 }
499
500 TpConnectionManagerParam *
501 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
502 {
503   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
504   const TpConnectionManagerProtocol *tp_protocol;
505
506   g_return_val_if_fail (priv->manager != NULL, NULL);
507   g_return_val_if_fail (priv->protocol != NULL, NULL);
508
509   tp_protocol = tp_connection_manager_get_protocol (priv->manager,
510      priv->protocol);
511   if (tp_protocol == NULL)
512     {
513       DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
514           priv->protocol);
515       return NULL;
516     }
517
518   return tp_protocol->params;
519 }
520
521 gboolean
522 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
523 {
524   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
525
526   return priv->ready;
527 }
528
529 const gchar *
530 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
531 {
532   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
533
534   return priv->cm_name;
535 }
536
537 const gchar *
538 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
539 {
540   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
541
542   return priv->protocol;
543 }
544
545 const gchar *
546 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
547 {
548   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
549
550   return priv->service;
551 }
552
553 gchar *
554 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
555 {
556   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
557
558   return priv->icon_name;
559 }
560
561 const gchar *
562 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
563 {
564   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
565
566   return priv->display_name;
567 }
568
569 TpAccount *
570 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
571 {
572   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
573
574   return priv->account;
575 }
576
577 static gboolean
578 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
579     const gchar *param)
580 {
581   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
582   GArray *a;
583   guint i;
584
585   a = priv->unset_parameters;
586
587   for (i = 0; i < a->len; i++)
588     {
589       if (!tp_strdiff (g_array_index (a, gchar *, i), param))
590         return TRUE;
591     }
592
593   return FALSE;
594 }
595
596 static TpConnectionManagerParam *
597 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
598     const gchar *param)
599 {
600   TpConnectionManagerParam *tp_params =
601       empathy_account_settings_get_tp_params (settings);
602   TpConnectionManagerParam *p;
603
604   for (p = tp_params; p != NULL && p->name != NULL; p++)
605     {
606       if (tp_strdiff (p->name, param))
607         continue;
608
609       return p;
610     }
611
612   return NULL;
613 }
614
615 static void
616 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
617     const gchar *param)
618 {
619   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
620   guint idx;
621   gchar *val;
622
623   for (idx = 0; idx < priv->unset_parameters->len; idx++)
624     {
625       val = g_array_index (priv->unset_parameters, gchar *, idx);
626
627       if (!tp_strdiff (val, param))
628         {
629           priv->unset_parameters =
630             g_array_remove_index (priv->unset_parameters, idx);
631           g_free (val);
632
633           break;
634         }
635     }
636 }
637
638 const GValue *
639 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
640     const gchar *param)
641 {
642   TpConnectionManagerParam *p;
643
644   p = empathy_account_settings_get_tp_param (settings, param);
645
646   if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
647     return NULL;
648
649   return &(p->default_value);
650 }
651
652 const gchar *
653 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
654     const gchar *param)
655 {
656   TpConnectionManagerParam *p;
657
658   p = empathy_account_settings_get_tp_param (settings, param);
659
660   if (p == NULL)
661     return NULL;
662
663   return p->dbus_signature;
664 }
665
666 const GValue *
667 empathy_account_settings_get (EmpathyAccountSettings *settings,
668     const gchar *param)
669 {
670   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
671   const GValue *result = NULL;
672
673   /* Lookup the update parameters we set */
674   result = tp_asv_lookup (priv->parameters, param);
675   if (result != NULL)
676     return result;
677
678   /* If the parameters isn't unset use the accounts setting if any */
679   if (priv->account != NULL
680       && !empathy_account_settings_is_unset (settings, param))
681     {
682       const GHashTable *parameters;
683
684       parameters = tp_account_get_parameters (priv->account);
685       result = tp_asv_lookup (parameters, param);
686
687       if (result != NULL)
688         return result;
689     }
690
691   /* fallback to the default */
692   return empathy_account_settings_get_default (settings, param);
693 }
694
695 void
696 empathy_account_settings_unset (EmpathyAccountSettings *settings,
697     const gchar *param)
698 {
699   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
700   gchar *v;
701   if (empathy_account_settings_is_unset (settings, param))
702     return;
703
704   v = g_strdup (param);
705
706   g_array_append_val (priv->unset_parameters, v);
707   g_hash_table_remove (priv->parameters, param);
708 }
709
710 void
711 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
712 {
713   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
714
715   g_hash_table_remove_all (priv->parameters);
716   empathy_account_settings_free_unset_parameters (settings);
717 }
718
719 const gchar *
720 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
721     const gchar *param)
722 {
723   const GValue *v;
724
725   v = empathy_account_settings_get (settings, param);
726
727   if (v == NULL || !G_VALUE_HOLDS_STRING (v))
728     return NULL;
729
730   return g_value_get_string (v);
731 }
732
733 const gchar * const *
734 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
735     const gchar *param)
736 {
737   const GValue *v;
738
739   v = empathy_account_settings_get (settings, param);
740
741   if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
742     return NULL;
743
744   return g_value_get_boxed (v);
745 }
746
747 gint32
748 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
749     const gchar *param)
750 {
751   const GValue *v;
752   gint32 ret = 0;
753
754   v = empathy_account_settings_get (settings, param);
755
756   if (v == NULL)
757     return 0;
758
759   switch G_VALUE_TYPE (v)
760     {
761       case G_TYPE_UCHAR:
762         ret = g_value_get_uchar (v);
763         break;
764       case G_TYPE_INT:
765         ret = g_value_get_int (v);
766         break;
767       case G_TYPE_UINT:
768         ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
769             G_MAXINT32);
770         break;
771       case G_TYPE_INT64:
772         ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
773         break;
774       case G_TYPE_UINT64:
775         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
776             G_MAXINT32);
777         break;
778       default:
779         ret = 0;
780         break;
781     }
782
783   return ret;
784 }
785
786 gint64
787 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
788     const gchar *param)
789 {
790   const GValue *v;
791   gint64 ret = 0;
792
793   v = empathy_account_settings_get (settings, param);
794   if (v == NULL)
795     return 0;
796
797   switch G_VALUE_TYPE (v)
798     {
799       case G_TYPE_UCHAR:
800         ret = g_value_get_uchar (v);
801         break;
802       case G_TYPE_INT:
803         ret = g_value_get_int (v);
804         break;
805       case G_TYPE_UINT:
806         ret = g_value_get_uint (v);
807         break;
808       case G_TYPE_INT64:
809         ret = g_value_get_int64 (v);
810         break;
811       case G_TYPE_UINT64:
812         ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
813         break;
814       default:
815         ret = 0;
816         break;
817     }
818
819   return ret;
820 }
821
822 guint32
823 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
824     const gchar *param)
825 {
826   const GValue *v;
827   guint32 ret;
828
829   v = empathy_account_settings_get (settings, param);
830   if (v == NULL)
831     return 0;
832
833   switch G_VALUE_TYPE (v)
834     {
835       case G_TYPE_UCHAR:
836         ret = g_value_get_uchar (v);
837         break;
838       case G_TYPE_INT:
839         ret = MAX (0, g_value_get_int (v));
840         break;
841       case G_TYPE_UINT:
842         ret = g_value_get_uint (v);
843         break;
844       case G_TYPE_INT64:
845         ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
846         break;
847       case G_TYPE_UINT64:
848         ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
849         break;
850       default:
851         ret = 0;
852         break;
853     }
854
855   return ret;
856 }
857
858 guint64
859 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
860     const gchar *param)
861 {
862   const GValue *v;
863   guint64 ret = 0;
864
865   v = empathy_account_settings_get (settings, param);
866
867   if (v == NULL || !G_VALUE_HOLDS_INT (v))
868     return 0;
869
870   switch G_VALUE_TYPE (v)
871     {
872       case G_TYPE_UCHAR:
873         ret = g_value_get_uchar (v);
874         break;
875       case G_TYPE_INT:
876         ret = MAX (0, g_value_get_int (v));
877         break;
878       case G_TYPE_UINT:
879         ret = g_value_get_uint (v);
880         break;
881       case G_TYPE_INT64:
882         ret = MAX (0, g_value_get_int64 (v));
883         break;
884       case G_TYPE_UINT64:
885         ret = g_value_get_uint64 (v);
886         break;
887       default:
888         ret = 0;
889         break;
890     }
891
892   return ret;
893 }
894
895 gboolean
896 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
897     const gchar *param)
898 {
899   const GValue *v;
900
901   v = empathy_account_settings_get (settings, param);
902
903   if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
904     return FALSE;
905
906   return g_value_get_boolean (v);
907 }
908
909 void
910 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
911     const gchar *param,
912     const gchar *value)
913 {
914   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
915
916   g_return_if_fail (param != NULL);
917   g_return_if_fail (value != NULL);
918
919   tp_asv_set_string (priv->parameters, g_strdup (param), value);
920
921   account_settings_remove_from_unset (settings, param);
922 }
923
924 void
925 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
926     const gchar *param,
927     gchar **value)
928 {
929   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
930
931   g_return_if_fail (param != NULL);
932   g_return_if_fail (value != NULL);
933
934   tp_asv_set_strv (priv->parameters, g_strdup (param), value);
935
936   account_settings_remove_from_unset (settings, param);
937 }
938
939 void
940 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
941     const gchar *param,
942     gint32 value)
943 {
944   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
945
946   g_return_if_fail (param != NULL);
947
948   tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
949
950   account_settings_remove_from_unset (settings, param);
951 }
952
953 void
954 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
955     const gchar *param,
956     gint64 value)
957 {
958   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
959
960   g_return_if_fail (param != NULL);
961
962   tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
963
964   account_settings_remove_from_unset (settings, param);
965 }
966
967 void
968 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
969     const gchar *param,
970     guint32 value)
971 {
972   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
973
974   g_return_if_fail (param != NULL);
975
976   tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
977
978   account_settings_remove_from_unset (settings, param);
979 }
980
981 void
982 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
983     const gchar *param,
984     guint64 value)
985 {
986   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
987
988   g_return_if_fail (param != NULL);
989
990   tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
991
992   account_settings_remove_from_unset (settings, param);
993 }
994
995 void
996 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
997     const gchar *param,
998     gboolean value)
999 {
1000   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1001
1002   g_return_if_fail (param != NULL);
1003
1004   tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1005
1006   account_settings_remove_from_unset (settings, param);
1007 }
1008
1009 static void
1010 account_settings_display_name_set_cb (GObject *src,
1011     GAsyncResult *res,
1012     gpointer user_data)
1013 {
1014   GError *error = NULL;
1015   TpAccount *account = TP_ACCOUNT (src);
1016   GSimpleAsyncResult *set_result = user_data;
1017
1018   tp_account_set_display_name_finish (account, res, &error);
1019
1020   if (error != NULL)
1021     {
1022       g_simple_async_result_set_from_error (set_result, error);
1023       g_error_free (error);
1024     }
1025
1026   g_simple_async_result_complete (set_result);
1027   g_object_unref (set_result);
1028 }
1029
1030 void
1031 empathy_account_settings_set_display_name_async (
1032   EmpathyAccountSettings *settings,
1033   const gchar *name,
1034   GAsyncReadyCallback callback,
1035   gpointer user_data)
1036 {
1037   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1038   GSimpleAsyncResult *result;
1039
1040   g_return_if_fail (name != NULL);
1041
1042   result = g_simple_async_result_new (G_OBJECT (settings),
1043       callback, user_data, empathy_account_settings_set_display_name_finish);
1044
1045   if (!tp_strdiff (name, priv->display_name))
1046     {
1047       /* Nothing to do */
1048       g_simple_async_result_complete_in_idle (result);
1049       return;
1050     }
1051
1052   if (priv->account == NULL)
1053     {
1054       if (priv->display_name != NULL)
1055         g_free (priv->display_name);
1056
1057       priv->display_name = g_strdup (name);
1058
1059       g_simple_async_result_complete_in_idle (result);
1060
1061       return;
1062     }
1063
1064   tp_account_set_display_name_async (priv->account, name,
1065       account_settings_display_name_set_cb, result);
1066 }
1067
1068 gboolean
1069 empathy_account_settings_set_display_name_finish (
1070   EmpathyAccountSettings *settings,
1071   GAsyncResult *result,
1072   GError **error)
1073 {
1074   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1075       error))
1076     return FALSE;
1077
1078   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1079     G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1080       FALSE);
1081
1082   return TRUE;
1083 }
1084
1085 static void
1086 account_settings_icon_name_set_cb (GObject *src,
1087     GAsyncResult *res,
1088     gpointer user_data)
1089 {
1090   GError *error = NULL;
1091   TpAccount *account = TP_ACCOUNT (src);
1092   GSimpleAsyncResult *set_result = user_data;
1093
1094   tp_account_set_icon_name_finish (account, res, &error);
1095
1096   if (error != NULL)
1097     {
1098       g_simple_async_result_set_from_error (set_result, error);
1099       g_error_free (error);
1100     }
1101
1102   g_simple_async_result_complete (set_result);
1103   g_object_unref (set_result);
1104 }
1105
1106 void
1107 empathy_account_settings_set_icon_name_async (
1108   EmpathyAccountSettings *settings,
1109   const gchar *name,
1110   GAsyncReadyCallback callback,
1111   gpointer user_data)
1112 {
1113   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1114   GSimpleAsyncResult *result;
1115
1116   g_return_if_fail (name != NULL);
1117
1118   result = g_simple_async_result_new (G_OBJECT (settings),
1119       callback, user_data, empathy_account_settings_set_icon_name_finish);
1120
1121   if (priv->account == NULL)
1122     {
1123       if (priv->icon_name != NULL)
1124         g_free (priv->icon_name);
1125
1126       priv->icon_name = g_strdup (name);
1127
1128       g_simple_async_result_complete_in_idle (result);
1129
1130       return;
1131     }
1132
1133   tp_account_set_icon_name_async (priv->account, name,
1134       account_settings_icon_name_set_cb, result);
1135 }
1136
1137 gboolean
1138 empathy_account_settings_set_icon_name_finish (
1139   EmpathyAccountSettings *settings,
1140   GAsyncResult *result,
1141   GError **error)
1142 {
1143   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1144       error))
1145     return FALSE;
1146
1147   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1148     G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1149       FALSE);
1150
1151   return TRUE;
1152 }
1153
1154 static void
1155 empathy_account_settings_account_updated (GObject *source,
1156     GAsyncResult *result,
1157     gpointer user_data)
1158 {
1159   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1160   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1161   GSimpleAsyncResult *r;
1162   GError *error = NULL;
1163
1164   if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1165           result, NULL, &error))
1166     {
1167       g_simple_async_result_set_from_error (priv->apply_result, error);
1168       g_error_free (error);
1169     }
1170   else
1171     {
1172       empathy_account_settings_discard_changes (settings);
1173     }
1174
1175   r = priv->apply_result;
1176   priv->apply_result = NULL;
1177
1178   g_simple_async_result_complete (r);
1179   g_object_unref (r);
1180 }
1181
1182 static void
1183 empathy_account_settings_created_cb (GObject *source,
1184     GAsyncResult *result,
1185     gpointer user_data)
1186 {
1187   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1188   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1189   TpAccount *account;
1190   GError *error = NULL;
1191   GSimpleAsyncResult *r;
1192
1193   account = tp_account_manager_create_account_finish (
1194     TP_ACCOUNT_MANAGER (source), result, &error);
1195
1196   if (account == NULL)
1197     {
1198       g_simple_async_result_set_from_error (priv->apply_result, error);
1199     }
1200   else
1201     {
1202       priv->account = g_object_ref (account);
1203       empathy_account_settings_discard_changes (settings);
1204     }
1205
1206   r = priv->apply_result;
1207   priv->apply_result = NULL;
1208
1209   g_simple_async_result_complete (r);
1210   g_object_unref (r);
1211 }
1212
1213
1214 static void
1215 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1216 {
1217   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1218   GHashTable *properties;
1219   TpConnectionPresenceType type;
1220   gchar *status;
1221   gchar *message;
1222   EmpathyIdle *idle;
1223
1224   properties = tp_asv_new (NULL, NULL);
1225
1226   idle = empathy_idle_dup_singleton ();
1227   type = empathy_idle_get_requested_presence (idle, &status, &message);
1228   g_object_unref (idle);
1229
1230   if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1231     {
1232       /* Create the account with the requested presence the same as the current
1233         * global requested presence, but don't enable it */
1234       GValueArray *presence;
1235       GValue vtype = { 0, };
1236       GValue vstatus = { 0, };
1237       GValue vmessage = { 0, };
1238
1239       presence = g_value_array_new (3);
1240
1241       g_value_init (&vtype, G_TYPE_UINT);
1242       g_value_set_uint (&vtype, type);
1243       g_value_array_append (presence, &vtype);
1244
1245       g_value_init (&vstatus, G_TYPE_STRING);
1246       g_value_take_string (&vstatus, status);
1247       g_value_array_append (presence, &vstatus);
1248
1249       g_value_init (&vmessage, G_TYPE_STRING);
1250       g_value_take_string (&vmessage, message);
1251       g_value_array_append (presence, &vmessage);
1252
1253       tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1254         TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1255     }
1256
1257   tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1258       priv->icon_name);
1259
1260   if (priv->service != NULL)
1261     tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1262
1263   tp_account_manager_create_account_async (priv->account_manager,
1264     priv->cm_name, priv->protocol, priv->display_name,
1265     priv->parameters, properties,
1266     empathy_account_settings_created_cb,
1267     settings);
1268
1269   g_hash_table_unref (properties);
1270 }
1271
1272 static void
1273 empathy_account_settings_manager_ready_cb (GObject *source_object,
1274     GAsyncResult *result,
1275     gpointer user_data)
1276 {
1277   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1278   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1279   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1280   GError *error = NULL;
1281
1282   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1283     {
1284       DEBUG ("Failed to prepare account manager: %s", error->message);
1285       g_error_free (error);
1286       return;
1287     }
1288
1289   g_assert (priv->apply_result != NULL && priv->account == NULL);
1290   empathy_account_settings_do_create_account (settings);
1291 }
1292
1293 void
1294 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1295     GAsyncReadyCallback callback,
1296     gpointer user_data)
1297 {
1298   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1299
1300   if (priv->apply_result != NULL)
1301     {
1302       g_simple_async_report_error_in_idle (G_OBJECT (settings),
1303           callback, user_data,
1304           G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1305       return;
1306     }
1307
1308   priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1309       callback, user_data, empathy_account_settings_apply_finish);
1310
1311   if (priv->account == NULL)
1312     {
1313       tp_account_manager_prepare_async (priv->account_manager, NULL,
1314           empathy_account_settings_manager_ready_cb, settings);
1315     }
1316   else
1317     {
1318       tp_account_update_parameters_async (priv->account,
1319           priv->parameters, (const gchar **)priv->unset_parameters->data,
1320           empathy_account_settings_account_updated, settings);
1321     }
1322 }
1323
1324 gboolean
1325 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1326     GAsyncResult *result,
1327     GError **error)
1328 {
1329   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1330       error))
1331     return FALSE;
1332
1333   g_return_val_if_fail (g_simple_async_result_is_valid (result,
1334     G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1335
1336   return TRUE;
1337 }
1338
1339 gboolean
1340 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1341     TpAccount *account)
1342 {
1343   EmpathyAccountSettingsPriv *priv;
1344   const gchar *account_path;
1345   const gchar *priv_account_path;
1346
1347   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1348   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1349
1350   priv = GET_PRIV (settings);
1351
1352   if (priv->account == NULL)
1353     return FALSE;
1354
1355   account_path = tp_proxy_get_object_path (TP_PROXY (account));
1356   priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1357
1358   return (!tp_strdiff (account_path, priv_account_path));
1359 }
1360
1361 void
1362 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1363     const gchar *param,
1364     const gchar *pattern)
1365 {
1366   EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1367   GRegex *regex;
1368
1369   regex = g_regex_new (pattern, 0, 0, NULL);
1370   g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1371 }
1372
1373 gboolean
1374 empathy_account_settings_parameter_is_valid (
1375     EmpathyAccountSettings *settings,
1376     const gchar *param)
1377 {
1378   EmpathyAccountSettingsPriv *priv;
1379   const GRegex *regex;
1380   const gchar *value;
1381
1382   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1383
1384   priv = GET_PRIV (settings);
1385
1386   if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1387     {
1388       /* first, look if it's set in our own parameters */
1389       if (tp_asv_lookup (priv->parameters, param))
1390         goto test_regex;
1391
1392       /* if we did not unset the parameter, look if it's in the account */
1393       if (priv->account != NULL &&
1394           !empathy_account_settings_is_unset (settings, param))
1395         {
1396           const GHashTable *account_params;
1397
1398           account_params = tp_account_get_parameters (priv->account);
1399           if (tp_asv_lookup (account_params, param))
1400             goto test_regex;
1401         }
1402
1403       return FALSE;
1404     }
1405
1406 test_regex:
1407   /* test whether parameter value matches its regex */
1408   regex = g_hash_table_lookup (priv->param_regexps, param);
1409   if (regex)
1410     {
1411       value = empathy_account_settings_get_string (settings, param);
1412       if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1413         return FALSE;
1414     }
1415
1416   return TRUE;
1417 }
1418
1419 gboolean
1420 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1421 {
1422   EmpathyAccountSettingsPriv *priv;
1423   const gchar *param;
1424   GHashTableIter iter;
1425   GList *l;
1426
1427   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1428
1429   priv = GET_PRIV (settings);
1430
1431   for (l = priv->required_params; l; l = l->next)
1432     {
1433       if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1434         return FALSE;
1435     }
1436
1437   g_hash_table_iter_init (&iter, priv->param_regexps);
1438   while (g_hash_table_iter_next (&iter, (gpointer *) &param, NULL))
1439     {
1440       if (!empathy_account_settings_parameter_is_valid (settings, param))
1441         return FALSE;
1442     }
1443
1444   return TRUE;
1445 }
1446
1447 const TpConnectionManagerProtocol *
1448 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1449 {
1450   EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1451
1452   return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1453 }