]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget.c
Add simple implementations for many widgets
[empathy.git] / libempathy-gtk / empathy-account-widget.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  * Copyright (C) 2007-2009 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program 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  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29
30 #include <gtk/gtk.h>
31 #include <glib/gi18n-lib.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-account.h>
35
36 #include <telepathy-glib/connection-manager.h>
37 #include <telepathy-glib/util.h>
38 #include <dbus/dbus-protocol.h>
39
40 #include "empathy-account-widget.h"
41 #include "empathy-account-widget-private.h"
42 #include "empathy-account-widget-sip.h"
43 #include "empathy-account-widget-irc.h"
44 #include "empathy-ui-utils.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
47 #include <libempathy/empathy-debug.h>
48
49 G_DEFINE_TYPE (EmpathyAccountWidget, empathy_account_widget, G_TYPE_OBJECT)
50
51 typedef struct {
52   char *protocol;
53   EmpathyAccountSettings *settings;
54
55   GtkWidget *apply_button;
56   GtkWidget *entry_password;
57   GtkWidget *button_forget;
58   GtkWidget *spinbutton_port;
59
60   gboolean simple;
61
62   gboolean dispose_run;
63 } EmpathyAccountWidgetPriv;
64
65 enum {
66   PROP_PROTOCOL = 1,
67   PROP_SETTINGS,
68   PROP_SIMPLE
69 };
70
71 enum {
72   HANDLE_APPLY,
73   LAST_SIGNAL
74 };
75
76 static guint signals[LAST_SIGNAL] = { 0 };
77
78 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountWidget)
79
80 static void
81 account_widget_handle_apply_sensitivity (EmpathyAccountWidget *self)
82 {
83   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
84   gboolean is_valid;
85
86   is_valid = empathy_account_settings_is_valid (priv->settings);
87
88   if (!priv->simple)
89     gtk_widget_set_sensitive (priv->apply_button, is_valid);
90
91   g_signal_emit (self, signals[HANDLE_APPLY], 0, is_valid);
92 }
93
94 static gboolean
95 account_widget_entry_focus_cb (GtkWidget *widget,
96     GdkEventFocus *event,
97     EmpathyAccountWidget *self)
98 {
99   const gchar *str;
100   const gchar *param_name;
101   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
102
103   str = gtk_entry_get_text (GTK_ENTRY (widget));
104   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
105
106   if (EMP_STR_EMPTY (str))
107     {
108       const gchar *value = NULL;
109
110       empathy_account_settings_unset (priv->settings, param_name);
111       value = empathy_account_settings_get_string (priv->settings, param_name);
112       DEBUG ("Unset %s and restore to %s", param_name, value);
113       gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
114     }
115   else
116     {
117       DEBUG ("Setting %s to %s", param_name,
118           strstr (param_name, "password") ? "***" : str);
119       empathy_account_settings_set_string (priv->settings, param_name, str);
120     }
121
122   account_widget_handle_apply_sensitivity (self);
123
124   return FALSE;
125 }
126
127 static void
128 account_widget_int_changed_cb (GtkWidget *widget,
129     EmpathyAccountWidget *self)
130 {
131   const gchar *param_name;
132   gint value;
133   const gchar *signature;
134   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
135
136   value = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget));
137   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
138
139   signature = empathy_account_settings_get_dbus_signature (priv->settings,
140     param_name);
141   g_return_if_fail (signature != NULL);
142
143   DEBUG ("Setting %s to %d", param_name, value);
144
145   switch ((int)*signature)
146     {
147     case DBUS_TYPE_INT16:
148     case DBUS_TYPE_INT32:
149       empathy_account_settings_set_int32 (priv->settings, param_name, value);
150       break;
151     case DBUS_TYPE_INT64:
152       empathy_account_settings_set_int64 (priv->settings, param_name, value);
153       break;
154     case DBUS_TYPE_UINT16:
155     case DBUS_TYPE_UINT32:
156       empathy_account_settings_set_uint32 (priv->settings, param_name, value);
157       break;
158     case DBUS_TYPE_UINT64:
159       empathy_account_settings_set_uint64 (priv->settings, param_name, value);
160       break;
161     default:
162       g_return_if_reached ();
163     }
164
165   account_widget_handle_apply_sensitivity (self);
166 }
167
168 static void
169 account_widget_checkbutton_toggled_cb (GtkWidget *widget,
170     EmpathyAccountWidget *self)
171 {
172   gboolean     value;
173   gboolean     default_value;
174   const gchar *param_name;
175   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
176
177   value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
178   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
179
180   /* FIXME: This is ugly! checkbox don't have a "not-set" value so we
181    * always unset the param and set the value if different from the
182    * default value. */
183   empathy_account_settings_unset (priv->settings, param_name);
184   default_value = empathy_account_settings_get_boolean (priv->settings,
185       param_name);
186
187   if (default_value == value)
188     {
189       DEBUG ("Unset %s and restore to %d", param_name, default_value);
190     }
191   else
192     {
193       DEBUG ("Setting %s to %d", param_name, value);
194       empathy_account_settings_set_boolean (priv->settings, param_name, value);
195     }
196
197   account_widget_handle_apply_sensitivity (self);
198 }
199
200 static void
201 account_widget_forget_clicked_cb (GtkWidget *button,
202     EmpathyAccountWidget *self)
203 {
204   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
205   const gchar *param_name;
206
207   param_name = g_object_get_data (G_OBJECT (priv->entry_password),
208       "param_name");
209
210   DEBUG ("Unset %s", param_name);
211   empathy_account_settings_unset (priv->settings, param_name);
212   gtk_entry_set_text (GTK_ENTRY (priv->entry_password), "");
213
214   account_widget_handle_apply_sensitivity (self);
215 }
216
217 static void
218 account_widget_password_changed_cb (GtkWidget *entry,
219     EmpathyAccountWidget *self)
220 {
221   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
222   const gchar *str;
223
224   str = gtk_entry_get_text (GTK_ENTRY (entry));
225   gtk_widget_set_sensitive (priv->button_forget, !EMP_STR_EMPTY (str));
226 }
227
228 static void
229 account_widget_jabber_ssl_toggled_cb (GtkWidget *checkbutton_ssl,
230     EmpathyAccountWidget *self)
231 {
232   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
233   gboolean   value;
234   gint32       port = 0;
235
236   value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_ssl));
237   port = empathy_account_settings_get_uint32 (priv->settings, "port");
238
239   if (value)
240     {
241       if (port == 5222 || port == 0)
242         port = 5223;
243     }
244   else
245     {
246       if (port == 5223 || port == 0)
247         port = 5222;
248     }
249
250   gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->spinbutton_port), port);
251 }
252
253 static void
254 account_widget_setup_widget (EmpathyAccountWidget *self,
255     GtkWidget *widget,
256     const gchar *param_name)
257 {
258   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
259
260   g_object_set_data_full (G_OBJECT (widget), "param_name",
261       g_strdup (param_name), g_free);
262
263   if (GTK_IS_SPIN_BUTTON (widget))
264     {
265       gint value = 0;
266       const gchar *signature;
267
268       signature = empathy_account_settings_get_dbus_signature (priv->settings,
269           param_name);
270       g_return_if_fail (signature != NULL);
271
272       switch ((int)*signature)
273         {
274           case DBUS_TYPE_INT16:
275           case DBUS_TYPE_INT32:
276             value = empathy_account_settings_get_int32 (priv->settings,
277               param_name);
278             break;
279           case DBUS_TYPE_INT64:
280             value = empathy_account_settings_get_int64 (priv->settings,
281               param_name);
282             break;
283           case DBUS_TYPE_UINT16:
284           case DBUS_TYPE_UINT32:
285             value = empathy_account_settings_get_uint32 (priv->settings,
286               param_name);
287             break;
288           case DBUS_TYPE_UINT64:
289             value = empathy_account_settings_get_uint64 (priv->settings,
290                 param_name);
291             break;
292           default:
293             g_return_if_reached ();
294         }
295
296       gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
297
298       g_signal_connect (widget, "value-changed",
299           G_CALLBACK (account_widget_int_changed_cb),
300           self);
301     }
302   else if (GTK_IS_ENTRY (widget))
303     {
304       const gchar *str = NULL;
305
306       str = empathy_account_settings_get_string (priv->settings, param_name);
307       gtk_entry_set_text (GTK_ENTRY (widget), str ? str : "");
308
309       if (strstr (param_name, "password"))
310         {
311           gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
312         }
313
314       g_signal_connect (widget, "focus-out-event",
315           G_CALLBACK (account_widget_entry_focus_cb),
316           self);
317     }
318   else if (GTK_IS_TOGGLE_BUTTON (widget))
319     {
320       gboolean value = FALSE;
321
322       value = empathy_account_settings_get_boolean (priv->settings,
323           param_name);
324       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
325
326       g_signal_connect (widget, "toggled",
327           G_CALLBACK (account_widget_checkbutton_toggled_cb),
328           self);
329     }
330   else
331     {
332       DEBUG ("Unknown type of widget for param %s", param_name);
333     }
334 }
335
336 static gchar *
337 account_widget_generic_format_param_name (const gchar *param_name)
338 {
339   gchar *str;
340   gchar *p;
341
342   str = g_strdup (param_name);
343
344   if (str && g_ascii_isalpha (str[0]))
345     str[0] = g_ascii_toupper (str[0]);
346
347   while ((p = strchr (str, '-')) != NULL)
348     {
349       if (p[1] != '\0' && g_ascii_isalpha (p[1]))
350         {
351           p[0] = ' ';
352           p[1] = g_ascii_toupper (p[1]);
353         }
354
355       p++;
356     }
357
358   return str;
359 }
360
361 static void
362 accounts_widget_generic_setup (EmpathyAccountWidget *self,
363     GtkWidget *table_common_settings,
364     GtkWidget *table_advanced_settings)
365 {
366   TpConnectionManagerParam *params, *param;
367   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
368
369   params = empathy_account_settings_get_tp_params (priv->settings);
370
371   for (param = params; param != NULL && param->name != NULL; param++)
372     {
373       GtkWidget       *table_settings;
374       guint            n_rows = 0;
375       GtkWidget       *widget = NULL;
376       gchar           *param_name_formatted;
377
378       if (param->flags & TP_CONN_MGR_PARAM_FLAG_REQUIRED)
379         table_settings = table_common_settings;
380       else
381         table_settings = table_advanced_settings;
382
383       param_name_formatted = account_widget_generic_format_param_name
384         (param->name);
385       g_object_get (table_settings, "n-rows", &n_rows, NULL);
386       gtk_table_resize (GTK_TABLE (table_settings), ++n_rows, 2);
387
388       if (param->dbus_signature[0] == 's')
389         {
390           gchar *str;
391
392           str = g_strdup_printf (_("%s:"), param_name_formatted);
393           widget = gtk_label_new (str);
394           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
395           g_free (str);
396
397           gtk_table_attach (GTK_TABLE (table_settings),
398               widget,
399               0, 1,
400               n_rows - 1, n_rows,
401               GTK_FILL, 0,
402               0, 0);
403           gtk_widget_show (widget);
404
405           widget = gtk_entry_new ();
406           if (strcmp (param->name, "account") == 0)
407             {
408               g_signal_connect (widget, "realize",
409                   G_CALLBACK (gtk_widget_grab_focus),
410                   NULL);
411             }
412           gtk_table_attach (GTK_TABLE (table_settings),
413               widget,
414               1, 2,
415               n_rows - 1, n_rows,
416               GTK_FILL | GTK_EXPAND, 0,
417               0, 0);
418           gtk_widget_show (widget);
419         }
420       /* int types: ynqiuxt. double type is 'd' */
421       else if (param->dbus_signature[0] == 'y' ||
422           param->dbus_signature[0] == 'n' ||
423           param->dbus_signature[0] == 'q' ||
424           param->dbus_signature[0] == 'i' ||
425           param->dbus_signature[0] == 'u' ||
426           param->dbus_signature[0] == 'x' ||
427           param->dbus_signature[0] == 't' ||
428           param->dbus_signature[0] == 'd')
429         {
430           gchar   *str = NULL;
431           gdouble  minint = 0;
432           gdouble  maxint = 0;
433           gdouble  step = 1;
434
435           switch (param->dbus_signature[0])
436             {
437             case 'y': minint = G_MININT8;  maxint = G_MAXINT8;   break;
438             case 'n': minint = G_MININT16; maxint = G_MAXINT16;  break;
439             case 'q': minint = 0;          maxint = G_MAXUINT16; break;
440             case 'i': minint = G_MININT32; maxint = G_MAXINT32;  break;
441             case 'u': minint = 0;          maxint = G_MAXUINT32; break;
442             case 'x': minint = G_MININT64; maxint = G_MAXINT64;  break;
443             case 't': minint = 0;          maxint = G_MAXUINT64; break;
444             case 'd': minint = G_MININT32; maxint = G_MAXINT32;
445               step = 0.1; break;
446             }
447
448           str = g_strdup_printf (_("%s:"), param_name_formatted);
449           widget = gtk_label_new (str);
450           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
451           g_free (str);
452
453           gtk_table_attach (GTK_TABLE (table_settings),
454               widget,
455               0, 1,
456               n_rows - 1, n_rows,
457               GTK_FILL, 0,
458               0, 0);
459           gtk_widget_show (widget);
460
461           widget = gtk_spin_button_new_with_range (minint, maxint, step);
462           gtk_table_attach (GTK_TABLE (table_settings),
463               widget,
464               1, 2,
465               n_rows - 1, n_rows,
466               GTK_FILL | GTK_EXPAND, 0,
467               0, 0);
468           gtk_widget_show (widget);
469         }
470       else if (param->dbus_signature[0] == 'b')
471         {
472           widget = gtk_check_button_new_with_label (param_name_formatted);
473           gtk_table_attach (GTK_TABLE (table_settings),
474               widget,
475               0, 2,
476               n_rows - 1, n_rows,
477               GTK_FILL | GTK_EXPAND, 0,
478               0, 0);
479           gtk_widget_show (widget);
480         }
481       else
482         {
483           DEBUG ("Unknown signature for param %s: %s",
484               param_name_formatted, param->dbus_signature);
485         }
486
487       if (widget)
488         account_widget_setup_widget (self, widget, param->name);
489
490       g_free (param_name_formatted);
491     }
492 }
493
494 static void
495 account_widget_handle_params_valist (EmpathyAccountWidget *self,
496     const gchar *first_widget,
497     va_list args)
498 {
499   GObject *object;
500   const gchar *name;
501
502   for (name = first_widget; name; name = va_arg (args, const gchar *))
503     {
504       const gchar *param_name;
505
506       param_name = va_arg (args, const gchar *);
507       object = gtk_builder_get_object (self->ui_details->gui, name);
508
509       if (!object)
510         {
511           g_warning ("Builder is missing object '%s'.", name);
512           continue;
513         }
514
515       account_widget_setup_widget (self, GTK_WIDGET (object), param_name);
516     }
517 }
518
519 static void
520 account_widget_apply_clicked_cb (GtkWidget *button,
521     EmpathyAccountWidget *self)
522 {
523   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
524
525   empathy_account_settings_apply_async (priv->settings, NULL, NULL);
526 }
527
528 static void
529 account_widget_setup_generic (EmpathyAccountWidget *self)
530 {
531   GtkWidget *table_common_settings;
532   GtkWidget *table_advanced_settings;
533
534   table_common_settings = GTK_WIDGET (gtk_builder_get_object
535       (self->ui_details->gui, "table_common_settings"));
536   table_advanced_settings = GTK_WIDGET (gtk_builder_get_object
537       (self->ui_details->gui, "table_advanced_settings"));
538
539   accounts_widget_generic_setup (self, table_common_settings,
540       table_advanced_settings);
541
542   g_object_unref (self->ui_details->gui);
543 }
544
545 static void
546 account_widget_settings_ready_cb (EmpathyAccountSettings *settings,
547     GParamSpec *pspec,
548     gpointer user_data)
549 {
550   EmpathyAccountWidget *self = user_data;
551   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
552
553   if (empathy_account_settings_is_ready (priv->settings))
554     account_widget_setup_generic (self);
555 }
556
557 static void
558 account_widget_build_generic (EmpathyAccountWidget *self,
559     const char *filename)
560 {
561   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
562   self->ui_details->gui = empathy_builder_get_file (filename,
563       "vbox_generic_settings", &self->ui_details->widget,
564       NULL);
565
566   g_object_ref (self->ui_details->gui);
567
568   if (empathy_account_settings_is_ready (priv->settings))
569     account_widget_setup_generic (self);
570   else
571     g_signal_connect (priv->settings, "notify::ready",
572         G_CALLBACK (account_widget_settings_ready_cb), self);
573 }
574
575 static void
576 account_widget_build_salut (EmpathyAccountWidget *self,
577     const char *filename)
578 {
579   self->ui_details->gui = empathy_builder_get_file (filename,
580       "vbox_salut_settings", &self->ui_details->widget,
581       NULL);
582
583   empathy_account_widget_handle_params (self,
584       "entry_published", "published-name",
585       "entry_nickname", "nickname",
586       "entry_first_name", "first-name",
587       "entry_last_name", "last-name",
588       "entry_email", "email",
589       "entry_jid", "jid",
590       NULL);
591
592   self->ui_details->default_focus = g_strdup ("entry_nickname");
593 }
594
595 static void
596 account_widget_build_msn (EmpathyAccountWidget *self,
597     const char *filename)
598 {
599   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
600
601   if (priv->simple)
602     {
603       self->ui_details->gui = empathy_builder_get_file (filename,
604           "vbox_msn_simple", &self->ui_details->widget,
605           NULL);
606
607       empathy_account_widget_handle_params (self,
608           "entry_id_simple", "account",
609           "entry_password_simple", "password",
610           NULL);
611
612       self->ui_details->default_focus = g_strdup ("entry_id_simple");
613     }
614   else
615     {
616       self->ui_details->gui = empathy_builder_get_file (filename,
617           "vbox_msn_settings", &self->ui_details->widget,
618           NULL);
619
620       empathy_account_widget_handle_params (self,
621           "entry_id", "account",
622           "entry_password", "password",
623           "entry_server", "server",
624           "spinbutton_port", "port",
625           NULL);
626
627       self->ui_details->default_focus = g_strdup ("entry_id");
628       self->ui_details->add_forget = TRUE;
629     }
630 }
631
632 static void
633 account_widget_build_jabber (EmpathyAccountWidget *self,
634     const char *filename)
635 {
636   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
637   GtkWidget *spinbutton_port;
638   GtkWidget *checkbutton_ssl;
639
640   if (priv->simple)
641     {
642       self->ui_details->gui = empathy_builder_get_file (filename,
643           "vbox_jabber_simple", &self->ui_details->widget,
644           NULL);
645       
646       empathy_account_widget_handle_params (self,
647           "entry_id_simple", "account",
648           "entry_password_simple", "password",
649           NULL);
650
651       self->ui_details->default_focus = g_strdup ("entry_id_simple");
652     }
653   else
654     {
655       self->ui_details->gui = empathy_builder_get_file (filename,
656           "vbox_jabber_settings", &self->ui_details->widget,
657           "spinbutton_port", &spinbutton_port,
658           "checkbutton_ssl", &checkbutton_ssl,
659           NULL);
660
661       empathy_account_widget_handle_params (self,
662           "entry_id", "account",
663           "entry_password", "password",
664           "entry_resource", "resource",
665           "entry_server", "server",
666           "spinbutton_port", "port",
667           "spinbutton_priority", "priority",
668           "checkbutton_ssl", "old-ssl",
669           "checkbutton_ignore_ssl_errors", "ignore-ssl-errors",
670           "checkbutton_encryption", "require-encryption",
671           NULL);
672
673       self->ui_details->default_focus = g_strdup ("entry_id");
674       self->ui_details->add_forget = TRUE;
675       priv->spinbutton_port = spinbutton_port;
676
677       g_signal_connect (checkbutton_ssl, "toggled",
678           G_CALLBACK (account_widget_jabber_ssl_toggled_cb),
679           self);
680     }
681 }
682
683 static void
684 account_widget_build_icq (EmpathyAccountWidget *self,
685     const char *filename)
686 {
687   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
688   GtkWidget *spinbutton_port;
689
690   if (priv->simple)
691     {
692       self->ui_details->gui = empathy_builder_get_file (filename,
693           "vbox_icq_simple", &self->ui_details->widget,
694           NULL);
695
696       empathy_account_widget_handle_params (self,
697           "entry_uin_simple", "account",
698           "entry_password_simple", "password",
699           NULL);
700
701       self->ui_details->default_focus = g_strdup ("entry_uin_simple");
702     }
703   else
704     {
705       self->ui_details->gui = empathy_builder_get_file (filename,
706           "vbox_icq_settings", &self->ui_details->widget,
707           "spinbutton_port", &spinbutton_port,
708           NULL);
709
710       empathy_account_widget_handle_params (self,
711           "entry_uin", "account",
712           "entry_password", "password",
713           "entry_server", "server",
714           "spinbutton_port", "port",
715           "entry_charset", "charset",
716           NULL);
717
718       self->ui_details->default_focus = g_strdup ("entry_uin");
719       self->ui_details->add_forget = TRUE;
720     }
721 }
722
723 static void
724 account_widget_build_aim (EmpathyAccountWidget *self,
725     const char *filename)
726 {
727   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
728   GtkWidget *spinbutton_port;
729
730   if (priv->simple)
731     {
732       self->ui_details->gui = empathy_builder_get_file (filename,
733           "vbox_aim_simple", &self->ui_details->widget,
734           NULL);
735
736       empathy_account_widget_handle_params (self,
737           "entry_screenname_simple", "account",
738           "entry_password_simple", "password",
739           NULL);
740
741       self->ui_details->default_focus = g_strdup ("entry_screenname_simple");
742     }
743   else
744     {
745       self->ui_details->gui = empathy_builder_get_file (filename,
746           "vbox_aim_settings", &self->ui_details->widget,
747           "spinbutton_port", &spinbutton_port,
748           NULL);
749
750       empathy_account_widget_handle_params (self,
751           "entry_screenname", "account",
752           "entry_password", "password",
753           "entry_server", "server",
754           "spinbutton_port", "port",
755           NULL);
756
757       self->ui_details->default_focus = g_strdup ("entry_screenname");
758       self->ui_details->add_forget = TRUE;
759     }
760 }
761
762 static void
763 account_widget_build_yahoo (EmpathyAccountWidget *self,
764     const char *filename)
765 {
766   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
767   
768   if (priv->simple)
769     {
770       self->ui_details->gui = empathy_builder_get_file (filename,
771           "vbox_yahoo_simple", &self->ui_details->widget,
772           NULL);
773
774       empathy_account_widget_handle_params (self,
775           "entry_id_simple", "account",
776           "entry_password_simple", "password",
777           NULL);
778
779       self->ui_details->default_focus = g_strdup ("entry_id_simple");
780     }
781   else
782     {
783       self->ui_details->gui = empathy_builder_get_file (filename,
784           "vbox_yahoo_settings", &self->ui_details->widget,
785           NULL);
786
787       empathy_account_widget_handle_params (self,
788           "entry_id", "account",
789           "entry_password", "password",
790           "entry_server", "server",
791           "entry_locale", "room-list-locale",
792           "entry_charset", "charset",
793           "spinbutton_port", "port",
794           "checkbutton_yahoojp", "yahoojp",
795           "checkbutton_ignore_invites", "ignore-invites",
796           NULL);
797
798       self->ui_details->default_focus = g_strdup ("entry_id");
799       self->ui_details->add_forget = TRUE;
800     }
801 }
802
803 static void
804 account_widget_build_groupwise (EmpathyAccountWidget *self,
805     const char *filename)
806 {
807   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
808   
809   if (priv->simple)
810     {
811       self->ui_details->gui = empathy_builder_get_file (filename,
812           "vbox_groupwise_simple", &self->ui_details->widget,
813           NULL);
814
815       empathy_account_widget_handle_params (self,
816           "entry_id_simple", "account",
817           "entry_password_simple", "password",
818           NULL);
819
820       self->ui_details->default_focus = g_strdup ("entry_id_simple");
821     }
822   else
823     {
824       self->ui_details->gui = empathy_builder_get_file (filename,
825           "vbox_groupwise_settings", &self->ui_details->widget,
826           NULL);
827
828       empathy_account_widget_handle_params (self,
829           "entry_id", "account",
830           "entry_password", "password",
831           "entry_server", "server",
832           "spinbutton_port", "port",
833           NULL);
834
835       self->ui_details->default_focus = g_strdup ("entry_id");
836       self->ui_details->add_forget = TRUE;
837     }
838 }
839
840 static void
841 account_widget_destroy_cb (GtkWidget *widget,
842     EmpathyAccountWidget *self)
843 {
844   g_object_unref (self);
845 }
846
847 static void
848 do_set_property (GObject *object,
849     guint prop_id,
850     const GValue *value,
851     GParamSpec *pspec)
852 {
853   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
854
855   switch (prop_id)
856     {
857     case PROP_PROTOCOL:
858       priv->protocol = g_value_dup_string (value);
859       break;
860     case PROP_SETTINGS:
861       priv->settings = g_value_dup_object (value);
862       break;
863     case PROP_SIMPLE:
864       priv->simple = g_value_get_boolean (value);
865       break;
866     default:
867       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
868     }
869 }
870
871 static void
872 do_get_property (GObject *object,
873     guint prop_id,
874     GValue *value,
875     GParamSpec *pspec)
876 {
877   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
878
879   switch (prop_id)
880     {
881     case PROP_PROTOCOL:
882       g_value_set_string (value, priv->protocol);
883       break;
884     case PROP_SETTINGS:
885       g_value_set_object (value, priv->settings);
886       break;
887     case PROP_SIMPLE:
888       g_value_set_boolean (value, priv->simple);
889     default:
890       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
891     }
892 }
893
894 static void
895 do_constructed (GObject *obj)
896 {
897   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
898   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
899   char *uiname, *filename;
900
901   uiname = g_strconcat ("empathy-account-widget-", priv->protocol,
902       ".ui", NULL);
903   filename = empathy_file_lookup (uiname, "libempathy-gtk");
904
905   if (!tp_strdiff (priv->protocol, "local-xmpp"))
906     account_widget_build_salut (self, filename);
907   else if (!tp_strdiff (priv->protocol, "msn"))
908     account_widget_build_msn (self, filename);
909   else if (!tp_strdiff (priv->protocol, "jabber"))
910     account_widget_build_jabber (self, filename);
911   else if (!tp_strdiff (priv->protocol, "icq"))
912     account_widget_build_icq (self, filename);
913   else if (!tp_strdiff (priv->protocol, "aim"))
914     account_widget_build_aim (self, filename);
915   else if (!tp_strdiff (priv->protocol, "yahoo"))
916     account_widget_build_yahoo (self, filename);
917   else if (!tp_strdiff (priv->protocol, "groupwise"))
918     account_widget_build_groupwise (self, filename);
919   else if (!tp_strdiff (priv->protocol, "irc"))
920     empathy_account_widget_irc_build (self, filename);
921   else if (!tp_strdiff (priv->protocol, "sip"))
922     empathy_account_widget_sip_build (self, filename);
923   else
924     account_widget_build_generic (self, filename);
925
926   g_free (uiname);
927   g_free (filename);
928
929   /* handle default focus */
930   if (self->ui_details->default_focus != NULL)
931     {
932       GObject *default_focus_entry;
933
934       default_focus_entry = gtk_builder_get_object
935         (self->ui_details->gui, self->ui_details->default_focus);
936       g_signal_connect (default_focus_entry, "realize",
937           G_CALLBACK (gtk_widget_grab_focus),
938           NULL);
939     }
940
941   /* handle forget button */
942   if (self->ui_details->add_forget)
943     {
944       const gchar *password = NULL;
945
946       priv->button_forget = GTK_WIDGET (gtk_builder_get_object
947           (self->ui_details->gui, "button_forget"));
948       priv->entry_password = GTK_WIDGET (gtk_builder_get_object
949           (self->ui_details->gui, "entry_password"));
950
951       password = empathy_account_settings_get_string (priv->settings,
952           "password");
953       gtk_widget_set_sensitive (priv->button_forget,
954           !EMP_STR_EMPTY (password));
955
956       g_signal_connect (priv->button_forget, "clicked",
957           G_CALLBACK (account_widget_forget_clicked_cb),
958           self);
959       g_signal_connect (priv->entry_password, "changed",
960           G_CALLBACK (account_widget_password_changed_cb),
961           self);
962     }
963
964   /* handle apply button */
965   if (!priv->simple)
966     {
967       priv->apply_button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
968       gtk_box_pack_end (GTK_BOX (self->ui_details->widget), priv->apply_button,
969           FALSE, FALSE, 3);
970
971       g_signal_connect (priv->apply_button, "clicked",
972           G_CALLBACK (account_widget_apply_clicked_cb),
973           self);
974       account_widget_handle_apply_sensitivity (self);
975       gtk_widget_show (priv->apply_button);
976     }
977
978   /* hook up to widget destruction to unref ourselves */
979   g_signal_connect (self->ui_details->widget, "destroy",
980       G_CALLBACK (account_widget_destroy_cb), self);
981
982   empathy_builder_unref_and_keep_widget (self->ui_details->gui,
983       self->ui_details->widget);
984   self->ui_details->gui = NULL;
985 }
986
987 static void
988 do_dispose (GObject *obj)
989 {
990   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
991   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
992
993   if (priv->dispose_run)
994     return;
995
996   priv->dispose_run = TRUE;
997
998   if (priv->settings != NULL)
999     {
1000       g_object_unref (priv->settings);
1001       priv->settings = NULL;
1002     }
1003
1004   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose != NULL)
1005     G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose (obj);
1006 }
1007
1008 static void
1009 do_finalize (GObject *obj)
1010 {
1011   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
1012   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
1013
1014   g_free (self->ui_details->default_focus);
1015   g_slice_free (EmpathyAccountWidgetUIDetails, self->ui_details);
1016
1017   g_free (priv->protocol);
1018
1019   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize != NULL)
1020     G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize (obj);
1021 }
1022
1023 static void
1024 empathy_account_widget_class_init (EmpathyAccountWidgetClass *klass)
1025 {
1026   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1027   GParamSpec *param_spec;
1028
1029   oclass->get_property = do_get_property;
1030   oclass->set_property = do_set_property;
1031   oclass->constructed = do_constructed;
1032   oclass->dispose = do_dispose;
1033   oclass->finalize = do_finalize;
1034
1035   param_spec = g_param_spec_string ("protocol",
1036       "protocol", "The protocol of the account",
1037       NULL,
1038       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1039   g_object_class_install_property (oclass, PROP_PROTOCOL, param_spec);
1040
1041   param_spec = g_param_spec_object ("settings",
1042       "settings", "The settings of the account",
1043       EMPATHY_TYPE_ACCOUNT_SETTINGS,
1044       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1045   g_object_class_install_property (oclass, PROP_SETTINGS, param_spec);
1046
1047   param_spec = g_param_spec_boolean ("simple",
1048       "simple", "Whether the account widget is a simple or an advanced one",
1049       FALSE,
1050       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1051   g_object_class_install_property (oclass, PROP_SIMPLE, param_spec);
1052
1053   signals[HANDLE_APPLY] =
1054     g_signal_new ("handle-apply", G_TYPE_FROM_CLASS (klass),
1055         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1056         g_cclosure_marshal_VOID__BOOLEAN,
1057         G_TYPE_NONE,
1058         1, G_TYPE_BOOLEAN);
1059
1060   g_type_class_add_private (klass, sizeof (EmpathyAccountWidgetPriv));
1061 }
1062
1063 static void
1064 empathy_account_widget_init (EmpathyAccountWidget *self)
1065 {
1066   EmpathyAccountWidgetPriv *priv =
1067     G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_ACCOUNT_WIDGET,
1068         EmpathyAccountWidgetPriv);
1069
1070   self->priv = priv;
1071   priv->dispose_run = FALSE;
1072
1073   self->ui_details = g_slice_new0 (EmpathyAccountWidgetUIDetails);
1074 }
1075
1076 /* public methods */
1077
1078 void
1079 empathy_account_widget_handle_params (EmpathyAccountWidget *self,
1080     const gchar *first_widget,
1081     ...)
1082 {
1083   va_list args;
1084
1085   va_start (args, first_widget);
1086   account_widget_handle_params_valist (self, first_widget, args);
1087   va_end (args);
1088 }
1089
1090 GtkWidget *
1091 empathy_account_widget_new_for_protocol (const char *protocol,
1092     EmpathyAccountSettings *settings)
1093 {
1094   EmpathyAccountWidget *self;
1095   EmpathyAccountWidgetPriv *priv;
1096
1097   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), NULL);
1098   g_return_val_if_fail (settings != NULL, NULL);
1099
1100   self = g_object_new
1101     (EMPATHY_TYPE_ACCOUNT_WIDGET, "protocol", protocol,
1102         "settings", settings, NULL);
1103   priv = GET_PRIV (self);
1104
1105   return self->ui_details->widget;
1106 }
1107
1108 GtkWidget *
1109 empathy_account_widget_simple_new_for_protocol (const char *protocol,
1110     EmpathyAccountSettings *settings, EmpathyAccountWidget **object)
1111 {
1112   EmpathyAccountWidget *self;
1113
1114   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), NULL);
1115   g_return_val_if_fail (protocol != NULL, NULL);
1116
1117   self = g_object_new
1118     (EMPATHY_TYPE_ACCOUNT_WIDGET, "protocol", protocol,
1119         "settings", settings, "simple", TRUE, NULL);
1120
1121   *object = self;
1122
1123   return self->ui_details->widget;
1124 }