]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget.c
Implement a generic simple widget
[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 if (priv->simple)
381         return;
382       else
383         table_settings = table_advanced_settings;
384
385       param_name_formatted = account_widget_generic_format_param_name
386         (param->name);
387       g_object_get (table_settings, "n-rows", &n_rows, NULL);
388       gtk_table_resize (GTK_TABLE (table_settings), ++n_rows, 2);
389
390       if (param->dbus_signature[0] == 's')
391         {
392           gchar *str;
393
394           str = g_strdup_printf (_("%s:"), param_name_formatted);
395           widget = gtk_label_new (str);
396           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
397           g_free (str);
398
399           gtk_table_attach (GTK_TABLE (table_settings),
400               widget,
401               0, 1,
402               n_rows - 1, n_rows,
403               GTK_FILL, 0,
404               0, 0);
405           gtk_widget_show (widget);
406
407           widget = gtk_entry_new ();
408           if (strcmp (param->name, "account") == 0)
409             {
410               g_signal_connect (widget, "realize",
411                   G_CALLBACK (gtk_widget_grab_focus),
412                   NULL);
413             }
414           gtk_table_attach (GTK_TABLE (table_settings),
415               widget,
416               1, 2,
417               n_rows - 1, n_rows,
418               GTK_FILL | GTK_EXPAND, 0,
419               0, 0);
420           gtk_widget_show (widget);
421         }
422       /* int types: ynqiuxt. double type is 'd' */
423       else if (param->dbus_signature[0] == 'y' ||
424           param->dbus_signature[0] == 'n' ||
425           param->dbus_signature[0] == 'q' ||
426           param->dbus_signature[0] == 'i' ||
427           param->dbus_signature[0] == 'u' ||
428           param->dbus_signature[0] == 'x' ||
429           param->dbus_signature[0] == 't' ||
430           param->dbus_signature[0] == 'd')
431         {
432           gchar   *str = NULL;
433           gdouble  minint = 0;
434           gdouble  maxint = 0;
435           gdouble  step = 1;
436
437           switch (param->dbus_signature[0])
438             {
439             case 'y': minint = G_MININT8;  maxint = G_MAXINT8;   break;
440             case 'n': minint = G_MININT16; maxint = G_MAXINT16;  break;
441             case 'q': minint = 0;          maxint = G_MAXUINT16; break;
442             case 'i': minint = G_MININT32; maxint = G_MAXINT32;  break;
443             case 'u': minint = 0;          maxint = G_MAXUINT32; break;
444             case 'x': minint = G_MININT64; maxint = G_MAXINT64;  break;
445             case 't': minint = 0;          maxint = G_MAXUINT64; break;
446             case 'd': minint = G_MININT32; maxint = G_MAXINT32;
447               step = 0.1; break;
448             }
449
450           str = g_strdup_printf (_("%s:"), param_name_formatted);
451           widget = gtk_label_new (str);
452           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
453           g_free (str);
454
455           gtk_table_attach (GTK_TABLE (table_settings),
456               widget,
457               0, 1,
458               n_rows - 1, n_rows,
459               GTK_FILL, 0,
460               0, 0);
461           gtk_widget_show (widget);
462
463           widget = gtk_spin_button_new_with_range (minint, maxint, step);
464           gtk_table_attach (GTK_TABLE (table_settings),
465               widget,
466               1, 2,
467               n_rows - 1, n_rows,
468               GTK_FILL | GTK_EXPAND, 0,
469               0, 0);
470           gtk_widget_show (widget);
471         }
472       else if (param->dbus_signature[0] == 'b')
473         {
474           widget = gtk_check_button_new_with_label (param_name_formatted);
475           gtk_table_attach (GTK_TABLE (table_settings),
476               widget,
477               0, 2,
478               n_rows - 1, n_rows,
479               GTK_FILL | GTK_EXPAND, 0,
480               0, 0);
481           gtk_widget_show (widget);
482         }
483       else
484         {
485           DEBUG ("Unknown signature for param %s: %s",
486               param_name_formatted, param->dbus_signature);
487         }
488
489       if (widget)
490         account_widget_setup_widget (self, widget, param->name);
491
492       g_free (param_name_formatted);
493     }
494 }
495
496 static void
497 account_widget_handle_params_valist (EmpathyAccountWidget *self,
498     const gchar *first_widget,
499     va_list args)
500 {
501   GObject *object;
502   const gchar *name;
503
504   for (name = first_widget; name; name = va_arg (args, const gchar *))
505     {
506       const gchar *param_name;
507
508       param_name = va_arg (args, const gchar *);
509       object = gtk_builder_get_object (self->ui_details->gui, name);
510
511       if (!object)
512         {
513           g_warning ("Builder is missing object '%s'.", name);
514           continue;
515         }
516
517       account_widget_setup_widget (self, GTK_WIDGET (object), param_name);
518     }
519 }
520
521 static void
522 account_widget_apply_clicked_cb (GtkWidget *button,
523     EmpathyAccountWidget *self)
524 {
525   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
526
527   empathy_account_settings_apply_async (priv->settings, NULL, NULL);
528 }
529
530 static void
531 account_widget_setup_generic (EmpathyAccountWidget *self)
532 {
533   GtkWidget *table_common_settings;
534   GtkWidget *table_advanced_settings;
535
536   table_common_settings = GTK_WIDGET (gtk_builder_get_object
537       (self->ui_details->gui, "table_common_settings"));
538   table_advanced_settings = GTK_WIDGET (gtk_builder_get_object
539       (self->ui_details->gui, "table_advanced_settings"));
540
541   accounts_widget_generic_setup (self, table_common_settings,
542       table_advanced_settings);
543
544   g_object_unref (self->ui_details->gui);
545 }
546
547 static void
548 account_widget_settings_ready_cb (EmpathyAccountSettings *settings,
549     GParamSpec *pspec,
550     gpointer user_data)
551 {
552   EmpathyAccountWidget *self = user_data;
553   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
554
555   if (empathy_account_settings_is_ready (priv->settings))
556     account_widget_setup_generic (self);
557 }
558
559 static void
560 account_widget_build_generic (EmpathyAccountWidget *self,
561     const char *filename)
562 {
563   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
564   GtkWidget *expander_advanced;
565
566   self->ui_details->gui = empathy_builder_get_file (filename,
567       "vbox_generic_settings", &self->ui_details->widget,
568       "expander_advanced_settings", &expander_advanced,
569       NULL);
570
571   if (priv->simple)
572     gtk_widget_hide (expander_advanced);
573
574   g_object_ref (self->ui_details->gui);
575
576   if (empathy_account_settings_is_ready (priv->settings))
577     account_widget_setup_generic (self);
578   else
579     g_signal_connect (priv->settings, "notify::ready",
580         G_CALLBACK (account_widget_settings_ready_cb), self);
581 }
582
583 static void
584 account_widget_build_salut (EmpathyAccountWidget *self,
585     const char *filename)
586 {
587   self->ui_details->gui = empathy_builder_get_file (filename,
588       "vbox_salut_settings", &self->ui_details->widget,
589       NULL);
590
591   empathy_account_widget_handle_params (self,
592       "entry_published", "published-name",
593       "entry_nickname", "nickname",
594       "entry_first_name", "first-name",
595       "entry_last_name", "last-name",
596       "entry_email", "email",
597       "entry_jid", "jid",
598       NULL);
599
600   self->ui_details->default_focus = g_strdup ("entry_nickname");
601 }
602
603 static void
604 account_widget_build_msn (EmpathyAccountWidget *self,
605     const char *filename)
606 {
607   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
608
609   if (priv->simple)
610     {
611       self->ui_details->gui = empathy_builder_get_file (filename,
612           "vbox_msn_simple", &self->ui_details->widget,
613           NULL);
614
615       empathy_account_widget_handle_params (self,
616           "entry_id_simple", "account",
617           "entry_password_simple", "password",
618           NULL);
619
620       self->ui_details->default_focus = g_strdup ("entry_id_simple");
621     }
622   else
623     {
624       self->ui_details->gui = empathy_builder_get_file (filename,
625           "vbox_msn_settings", &self->ui_details->widget,
626           NULL);
627
628       empathy_account_widget_handle_params (self,
629           "entry_id", "account",
630           "entry_password", "password",
631           "entry_server", "server",
632           "spinbutton_port", "port",
633           NULL);
634
635       self->ui_details->default_focus = g_strdup ("entry_id");
636       self->ui_details->add_forget = TRUE;
637     }
638 }
639
640 static void
641 account_widget_build_jabber (EmpathyAccountWidget *self,
642     const char *filename)
643 {
644   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
645   GtkWidget *spinbutton_port;
646   GtkWidget *checkbutton_ssl;
647
648   if (priv->simple)
649     {
650       self->ui_details->gui = empathy_builder_get_file (filename,
651           "vbox_jabber_simple", &self->ui_details->widget,
652           NULL);
653       
654       empathy_account_widget_handle_params (self,
655           "entry_id_simple", "account",
656           "entry_password_simple", "password",
657           NULL);
658
659       self->ui_details->default_focus = g_strdup ("entry_id_simple");
660     }
661   else
662     {
663       self->ui_details->gui = empathy_builder_get_file (filename,
664           "vbox_jabber_settings", &self->ui_details->widget,
665           "spinbutton_port", &spinbutton_port,
666           "checkbutton_ssl", &checkbutton_ssl,
667           NULL);
668
669       empathy_account_widget_handle_params (self,
670           "entry_id", "account",
671           "entry_password", "password",
672           "entry_resource", "resource",
673           "entry_server", "server",
674           "spinbutton_port", "port",
675           "spinbutton_priority", "priority",
676           "checkbutton_ssl", "old-ssl",
677           "checkbutton_ignore_ssl_errors", "ignore-ssl-errors",
678           "checkbutton_encryption", "require-encryption",
679           NULL);
680
681       self->ui_details->default_focus = g_strdup ("entry_id");
682       self->ui_details->add_forget = TRUE;
683       priv->spinbutton_port = spinbutton_port;
684
685       g_signal_connect (checkbutton_ssl, "toggled",
686           G_CALLBACK (account_widget_jabber_ssl_toggled_cb),
687           self);
688     }
689 }
690
691 static void
692 account_widget_build_icq (EmpathyAccountWidget *self,
693     const char *filename)
694 {
695   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
696   GtkWidget *spinbutton_port;
697
698   if (priv->simple)
699     {
700       self->ui_details->gui = empathy_builder_get_file (filename,
701           "vbox_icq_simple", &self->ui_details->widget,
702           NULL);
703
704       empathy_account_widget_handle_params (self,
705           "entry_uin_simple", "account",
706           "entry_password_simple", "password",
707           NULL);
708
709       self->ui_details->default_focus = g_strdup ("entry_uin_simple");
710     }
711   else
712     {
713       self->ui_details->gui = empathy_builder_get_file (filename,
714           "vbox_icq_settings", &self->ui_details->widget,
715           "spinbutton_port", &spinbutton_port,
716           NULL);
717
718       empathy_account_widget_handle_params (self,
719           "entry_uin", "account",
720           "entry_password", "password",
721           "entry_server", "server",
722           "spinbutton_port", "port",
723           "entry_charset", "charset",
724           NULL);
725
726       self->ui_details->default_focus = g_strdup ("entry_uin");
727       self->ui_details->add_forget = TRUE;
728     }
729 }
730
731 static void
732 account_widget_build_aim (EmpathyAccountWidget *self,
733     const char *filename)
734 {
735   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
736   GtkWidget *spinbutton_port;
737
738   if (priv->simple)
739     {
740       self->ui_details->gui = empathy_builder_get_file (filename,
741           "vbox_aim_simple", &self->ui_details->widget,
742           NULL);
743
744       empathy_account_widget_handle_params (self,
745           "entry_screenname_simple", "account",
746           "entry_password_simple", "password",
747           NULL);
748
749       self->ui_details->default_focus = g_strdup ("entry_screenname_simple");
750     }
751   else
752     {
753       self->ui_details->gui = empathy_builder_get_file (filename,
754           "vbox_aim_settings", &self->ui_details->widget,
755           "spinbutton_port", &spinbutton_port,
756           NULL);
757
758       empathy_account_widget_handle_params (self,
759           "entry_screenname", "account",
760           "entry_password", "password",
761           "entry_server", "server",
762           "spinbutton_port", "port",
763           NULL);
764
765       self->ui_details->default_focus = g_strdup ("entry_screenname");
766       self->ui_details->add_forget = TRUE;
767     }
768 }
769
770 static void
771 account_widget_build_yahoo (EmpathyAccountWidget *self,
772     const char *filename)
773 {
774   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
775   
776   if (priv->simple)
777     {
778       self->ui_details->gui = empathy_builder_get_file (filename,
779           "vbox_yahoo_simple", &self->ui_details->widget,
780           NULL);
781
782       empathy_account_widget_handle_params (self,
783           "entry_id_simple", "account",
784           "entry_password_simple", "password",
785           NULL);
786
787       self->ui_details->default_focus = g_strdup ("entry_id_simple");
788     }
789   else
790     {
791       self->ui_details->gui = empathy_builder_get_file (filename,
792           "vbox_yahoo_settings", &self->ui_details->widget,
793           NULL);
794
795       empathy_account_widget_handle_params (self,
796           "entry_id", "account",
797           "entry_password", "password",
798           "entry_server", "server",
799           "entry_locale", "room-list-locale",
800           "entry_charset", "charset",
801           "spinbutton_port", "port",
802           "checkbutton_yahoojp", "yahoojp",
803           "checkbutton_ignore_invites", "ignore-invites",
804           NULL);
805
806       self->ui_details->default_focus = g_strdup ("entry_id");
807       self->ui_details->add_forget = TRUE;
808     }
809 }
810
811 static void
812 account_widget_build_groupwise (EmpathyAccountWidget *self,
813     const char *filename)
814 {
815   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
816
817   if (priv->simple)
818     {
819       self->ui_details->gui = empathy_builder_get_file (filename,
820           "vbox_groupwise_simple", &self->ui_details->widget,
821           NULL);
822
823       empathy_account_widget_handle_params (self,
824           "entry_id_simple", "account",
825           "entry_password_simple", "password",
826           NULL);
827
828       self->ui_details->default_focus = g_strdup ("entry_id_simple");
829     }
830   else
831     {
832       self->ui_details->gui = empathy_builder_get_file (filename,
833           "vbox_groupwise_settings", &self->ui_details->widget,
834           NULL);
835
836       empathy_account_widget_handle_params (self,
837           "entry_id", "account",
838           "entry_password", "password",
839           "entry_server", "server",
840           "spinbutton_port", "port",
841           NULL);
842
843       self->ui_details->default_focus = g_strdup ("entry_id");
844       self->ui_details->add_forget = TRUE;
845     }
846 }
847
848 static void
849 account_widget_destroy_cb (GtkWidget *widget,
850     EmpathyAccountWidget *self)
851 {
852   g_object_unref (self);
853 }
854
855 static void
856 do_set_property (GObject *object,
857     guint prop_id,
858     const GValue *value,
859     GParamSpec *pspec)
860 {
861   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
862
863   switch (prop_id)
864     {
865     case PROP_PROTOCOL:
866       priv->protocol = g_value_dup_string (value);
867       break;
868     case PROP_SETTINGS:
869       priv->settings = g_value_dup_object (value);
870       break;
871     case PROP_SIMPLE:
872       priv->simple = g_value_get_boolean (value);
873       break;
874     default:
875       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
876     }
877 }
878
879 static void
880 do_get_property (GObject *object,
881     guint prop_id,
882     GValue *value,
883     GParamSpec *pspec)
884 {
885   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
886
887   switch (prop_id)
888     {
889     case PROP_PROTOCOL:
890       g_value_set_string (value, priv->protocol);
891       break;
892     case PROP_SETTINGS:
893       g_value_set_object (value, priv->settings);
894       break;
895     case PROP_SIMPLE:
896       g_value_set_boolean (value, priv->simple);
897       break;
898     default:
899       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
900     }
901 }
902
903 static void
904 do_constructed (GObject *obj)
905 {
906   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
907   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
908   char *uiname, *filename;
909
910   uiname = g_strconcat ("empathy-account-widget-", priv->protocol,
911       ".ui", NULL);
912   filename = empathy_file_lookup (uiname, "libempathy-gtk");
913
914   if (!tp_strdiff (priv->protocol, "local-xmpp"))
915     account_widget_build_salut (self, filename);
916   else if (!tp_strdiff (priv->protocol, "msn"))
917     account_widget_build_msn (self, filename);
918   else if (!tp_strdiff (priv->protocol, "jabber"))
919     account_widget_build_jabber (self, filename);
920   else if (!tp_strdiff (priv->protocol, "icq"))
921     account_widget_build_icq (self, filename);
922   else if (!tp_strdiff (priv->protocol, "aim"))
923     account_widget_build_aim (self, filename);
924   else if (!tp_strdiff (priv->protocol, "yahoo"))
925     account_widget_build_yahoo (self, filename);
926   else if (!tp_strdiff (priv->protocol, "groupwise"))
927     account_widget_build_groupwise (self, filename);
928   else if (!tp_strdiff (priv->protocol, "irc"))
929     empathy_account_widget_irc_build (self, filename);
930   else if (!tp_strdiff (priv->protocol, "sip"))
931     empathy_account_widget_sip_build (self, filename);
932   else
933     {
934       g_free (filename);
935
936       filename = empathy_file_lookup (
937           "empathy-account-widget-generic.ui", "libempathy-gtk");
938       account_widget_build_generic (self, filename);
939     }
940
941   g_free (uiname);
942   g_free (filename);
943
944   /* handle default focus */
945   if (self->ui_details->default_focus != NULL)
946     {
947       GObject *default_focus_entry;
948
949       default_focus_entry = gtk_builder_get_object
950         (self->ui_details->gui, self->ui_details->default_focus);
951       g_signal_connect (default_focus_entry, "realize",
952           G_CALLBACK (gtk_widget_grab_focus),
953           NULL);
954     }
955
956   /* handle forget button */
957   if (self->ui_details->add_forget)
958     {
959       const gchar *password = NULL;
960
961       priv->button_forget = GTK_WIDGET (gtk_builder_get_object
962           (self->ui_details->gui, "button_forget"));
963       priv->entry_password = GTK_WIDGET (gtk_builder_get_object
964           (self->ui_details->gui, "entry_password"));
965
966       password = empathy_account_settings_get_string (priv->settings,
967           "password");
968       gtk_widget_set_sensitive (priv->button_forget,
969           !EMP_STR_EMPTY (password));
970
971       g_signal_connect (priv->button_forget, "clicked",
972           G_CALLBACK (account_widget_forget_clicked_cb),
973           self);
974       g_signal_connect (priv->entry_password, "changed",
975           G_CALLBACK (account_widget_password_changed_cb),
976           self);
977     }
978
979   /* handle apply button */
980   if (!priv->simple)
981     {
982       priv->apply_button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
983       gtk_box_pack_end (GTK_BOX (self->ui_details->widget), priv->apply_button,
984           FALSE, FALSE, 3);
985
986       g_signal_connect (priv->apply_button, "clicked",
987           G_CALLBACK (account_widget_apply_clicked_cb),
988           self);
989       account_widget_handle_apply_sensitivity (self);
990       gtk_widget_show (priv->apply_button);
991     }
992
993   /* hook up to widget destruction to unref ourselves */
994   g_signal_connect (self->ui_details->widget, "destroy",
995       G_CALLBACK (account_widget_destroy_cb), self);
996
997   empathy_builder_unref_and_keep_widget (self->ui_details->gui,
998       self->ui_details->widget);
999   self->ui_details->gui = NULL;
1000 }
1001
1002 static void
1003 do_dispose (GObject *obj)
1004 {
1005   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
1006   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
1007
1008   if (priv->dispose_run)
1009     return;
1010
1011   priv->dispose_run = TRUE;
1012
1013   if (priv->settings != NULL)
1014     {
1015       g_object_unref (priv->settings);
1016       priv->settings = NULL;
1017     }
1018
1019   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose != NULL)
1020     G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose (obj);
1021 }
1022
1023 static void
1024 do_finalize (GObject *obj)
1025 {
1026   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
1027   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
1028
1029   g_free (self->ui_details->default_focus);
1030   g_slice_free (EmpathyAccountWidgetUIDetails, self->ui_details);
1031
1032   g_free (priv->protocol);
1033
1034   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize != NULL)
1035     G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize (obj);
1036 }
1037
1038 static void
1039 empathy_account_widget_class_init (EmpathyAccountWidgetClass *klass)
1040 {
1041   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1042   GParamSpec *param_spec;
1043
1044   oclass->get_property = do_get_property;
1045   oclass->set_property = do_set_property;
1046   oclass->constructed = do_constructed;
1047   oclass->dispose = do_dispose;
1048   oclass->finalize = do_finalize;
1049
1050   param_spec = g_param_spec_string ("protocol",
1051       "protocol", "The protocol of the account",
1052       NULL,
1053       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1054   g_object_class_install_property (oclass, PROP_PROTOCOL, param_spec);
1055
1056   param_spec = g_param_spec_object ("settings",
1057       "settings", "The settings of the account",
1058       EMPATHY_TYPE_ACCOUNT_SETTINGS,
1059       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1060   g_object_class_install_property (oclass, PROP_SETTINGS, param_spec);
1061
1062   param_spec = g_param_spec_boolean ("simple",
1063       "simple", "Whether the account widget is a simple or an advanced one",
1064       FALSE,
1065       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1066   g_object_class_install_property (oclass, PROP_SIMPLE, param_spec);
1067
1068   signals[HANDLE_APPLY] =
1069     g_signal_new ("handle-apply", G_TYPE_FROM_CLASS (klass),
1070         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1071         g_cclosure_marshal_VOID__BOOLEAN,
1072         G_TYPE_NONE,
1073         1, G_TYPE_BOOLEAN);
1074
1075   g_type_class_add_private (klass, sizeof (EmpathyAccountWidgetPriv));
1076 }
1077
1078 static void
1079 empathy_account_widget_init (EmpathyAccountWidget *self)
1080 {
1081   EmpathyAccountWidgetPriv *priv =
1082     G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_ACCOUNT_WIDGET,
1083         EmpathyAccountWidgetPriv);
1084
1085   self->priv = priv;
1086   priv->dispose_run = FALSE;
1087
1088   self->ui_details = g_slice_new0 (EmpathyAccountWidgetUIDetails);
1089 }
1090
1091 /* public methods */
1092
1093 void
1094 empathy_account_widget_handle_params (EmpathyAccountWidget *self,
1095     const gchar *first_widget,
1096     ...)
1097 {
1098   va_list args;
1099
1100   va_start (args, first_widget);
1101   account_widget_handle_params_valist (self, first_widget, args);
1102   va_end (args);
1103 }
1104
1105 GtkWidget *
1106 empathy_account_widget_new_for_protocol (const char *protocol,
1107     EmpathyAccountSettings *settings)
1108 {
1109   EmpathyAccountWidget *self;
1110   EmpathyAccountWidgetPriv *priv;
1111
1112   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), NULL);
1113   g_return_val_if_fail (settings != NULL, NULL);
1114
1115   self = g_object_new
1116     (EMPATHY_TYPE_ACCOUNT_WIDGET, "protocol", protocol,
1117         "settings", settings, NULL);
1118   priv = GET_PRIV (self);
1119
1120   return self->ui_details->widget;
1121 }
1122
1123 GtkWidget *
1124 empathy_account_widget_simple_new_for_protocol (const char *protocol,
1125     EmpathyAccountSettings *settings, EmpathyAccountWidget **object)
1126 {
1127   EmpathyAccountWidget *self;
1128
1129   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), NULL);
1130   g_return_val_if_fail (protocol != NULL, NULL);
1131
1132   self = g_object_new
1133     (EMPATHY_TYPE_ACCOUNT_WIDGET, "protocol", protocol,
1134         "settings", settings, "simple", TRUE, NULL);
1135
1136   *object = self;
1137
1138   return self->ui_details->widget;
1139 }