]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget.c
Fix trailing whitespace
[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 dispose_run;
61 } EmpathyAccountWidgetPriv;
62
63 enum {
64   PROP_PROTOCOL = 1,
65   PROP_SETTINGS
66 };
67
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountWidget)
69
70 static void
71 account_widget_handle_apply_sensitivity (EmpathyAccountWidget *self)
72 {
73   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
74
75   gtk_widget_set_sensitive (priv->apply_button,
76       empathy_account_settings_is_valid (priv->settings));
77 }
78
79 static gboolean
80 account_widget_entry_focus_cb (GtkWidget *widget,
81     GdkEventFocus *event,
82     EmpathyAccountWidget *self)
83 {
84   const gchar *str;
85   const gchar *param_name;
86   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
87
88   str = gtk_entry_get_text (GTK_ENTRY (widget));
89   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
90
91   if (EMP_STR_EMPTY (str))
92     {
93       const gchar *value = NULL;
94
95       empathy_account_settings_unset (priv->settings, param_name);
96       value = empathy_account_settings_get_string (priv->settings, param_name);
97       DEBUG ("Unset %s and restore to %s", param_name, value);
98       gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
99     }
100   else
101     {
102       DEBUG ("Setting %s to %s", param_name,
103           strstr (param_name, "password") ? "***" : str);
104       empathy_account_settings_set_string (priv->settings, param_name, str);
105     }
106
107   account_widget_handle_apply_sensitivity (self);
108
109   return FALSE;
110 }
111
112 static void
113 account_widget_int_changed_cb (GtkWidget *widget,
114     EmpathyAccountWidget *self)
115 {
116   const gchar *param_name;
117   gint value;
118   const gchar *signature;
119   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
120
121   value = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget));
122   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
123
124   signature = empathy_settings_get_dbus_signature (priv->settings, param_name);
125   g_return_if_fail (signature != NULL);
126
127   DEBUG ("Setting %s to %d", param_name, value);
128
129   switch ((int)*signature)
130     {
131     case DBUS_TYPE_INT16:
132     case DBUS_TYPE_INT32:
133       empathy_account_settings_set_int32 (priv->settings, param_name, value);
134       break;
135     case DBUS_TYPE_INT64:
136       empathy_account_settings_set_int64 (priv->settings, param_name, value);
137       break;
138     case DBUS_TYPE_UINT16:
139     case DBUS_TYPE_UINT32:
140       empathy_account_settings_set_uint32 (priv->settings, param_name, value);
141       break;
142     case DBUS_TYPE_UINT64:
143       empathy_account_settings_set_uint64 (priv->settings, param_name, value);
144       break;
145     default:
146       g_return_if_reached ();
147     }
148
149   account_widget_handle_apply_sensitivity (self);
150 }
151
152 static void
153 account_widget_checkbutton_toggled_cb (GtkWidget *widget,
154     EmpathyAccountWidget *self)
155 {
156   gboolean     value;
157   gboolean     default_value;
158   const gchar *param_name;
159   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
160
161   value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
162   param_name = g_object_get_data (G_OBJECT (widget), "param_name");
163
164   /* FIXME: This is ugly! checkbox don't have a "not-set" value so we
165    * always unset the param and set the value if different from the
166    * default value. */
167   empathy_account_settings_unset (priv->settings, param_name);
168   default_value = empathy_account_settings_get_boolean (priv->settings, param_name);
169
170   if (default_value == value)
171     {
172       DEBUG ("Unset %s and restore to %d", param_name, default_value);
173     }
174   else
175     {
176       DEBUG ("Setting %s to %d", param_name, value);
177       empathy_account_settings_set_boolean (priv->settings, param_name, value);
178     }
179
180   account_widget_handle_apply_sensitivity (self);
181 }
182
183 static void
184 account_widget_forget_clicked_cb (GtkWidget *button,
185     EmpathyAccountWidget *self)
186 {
187   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
188   const gchar *param_name;
189
190   param_name = g_object_get_data (G_OBJECT (priv->entry_password), "param_name");
191
192   DEBUG ("Unset %s", param_name);
193   empathy_account_settings_unset (priv->settings, param_name);
194   gtk_entry_set_text (GTK_ENTRY (priv->entry_password), "");
195
196   account_widget_handle_apply_sensitivity (self);
197 }
198
199 static void
200 account_widget_password_changed_cb (GtkWidget *entry,
201     EmpathyAccountWidget *self)
202 {
203   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
204   const gchar *str;
205
206   str = gtk_entry_get_text (GTK_ENTRY (entry));
207   gtk_widget_set_sensitive (priv->button_forget, !EMP_STR_EMPTY (str));
208 }
209
210 static void
211 account_widget_jabber_ssl_toggled_cb (GtkWidget *checkbutton_ssl,
212     EmpathyAccountWidget *self)
213 {
214   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
215   gboolean   value;
216   gint32       port = 0;
217
218   value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_ssl));
219   port = empathy_account_settings_get_uint32 (priv->settings, "port");
220
221   if (value)
222     {
223       if (port == 5222 || port == 0)
224         port = 5223;
225     }
226   else
227     {
228       if (port == 5223 || port == 0)
229         port = 5222;
230     }
231
232   gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->spinbutton_port), port);
233 }
234
235 static void
236 account_widget_setup_widget (EmpathyAccountWidget *self,
237     GtkWidget *widget,
238     const gchar *param_name)
239 {
240   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
241
242   g_object_set_data_full (G_OBJECT (widget), "param_name",
243       g_strdup (param_name), g_free);
244
245   if (GTK_IS_SPIN_BUTTON (widget))
246     {
247       gint value = 0;
248       const gchar *signature;
249
250       signature = empathy_settings_get_dbus_signature (priv->settings, param_name);
251       g_return_if_fail (signature != NULL);
252
253       switch ((int)*signature)
254         {
255         case DBUS_TYPE_INT16:
256         case DBUS_TYPE_INT32:
257           value = empathy_account_settings_get_int32 (priv->settings, param_name);
258           break;
259         case DBUS_TYPE_INT64:
260           value = empathy_account_settings_get_int64 (priv->settings, param_name);
261           break;
262         case DBUS_TYPE_UINT16:
263         case DBUS_TYPE_UINT32:
264           value = empathy_account_settings_get_uint32 (priv->settings, param_name);
265           break;
266         case DBUS_TYPE_UINT64:
267           value = empathy_account_settings_get_uint64 (priv->settings, param_name);
268           break;
269         default:
270           g_return_if_reached ();
271         }
272
273       gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
274
275       g_signal_connect (widget, "value-changed",
276           G_CALLBACK (account_widget_int_changed_cb),
277           self);
278     }
279   else if (GTK_IS_ENTRY (widget))
280     {
281       const gchar *str = NULL;
282
283       str = empathy_account_settings_get_string (priv->settings, param_name);
284       gtk_entry_set_text (GTK_ENTRY (widget), str ? str : "");
285
286       if (strstr (param_name, "password"))
287         {
288           gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
289         }
290
291       g_signal_connect (widget, "focus-out-event",
292           G_CALLBACK (account_widget_entry_focus_cb),
293           self);
294     }
295   else if (GTK_IS_TOGGLE_BUTTON (widget))
296     {
297       gboolean value = FALSE;
298
299       value = empathy_account_settings_get_boolean (priv->settings, param_name);
300       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
301
302       g_signal_connect (widget, "toggled",
303           G_CALLBACK (account_widget_checkbutton_toggled_cb),
304           self);
305     }
306   else
307     {
308       DEBUG ("Unknown type of widget for param %s", param_name);
309     }
310 }
311
312 static gchar *
313 account_widget_generic_format_param_name (const gchar *param_name)
314 {
315   gchar *str;
316   gchar *p;
317
318   str = g_strdup (param_name);
319
320   if (str && g_ascii_isalpha (str[0]))
321     str[0] = g_ascii_toupper (str[0]);
322
323   while ((p = strchr (str, '-')) != NULL)
324     {
325       if (p[1] != '\0' && g_ascii_isalpha (p[1]))
326         {
327           p[0] = ' ';
328           p[1] = g_ascii_toupper (p[1]);
329         }
330
331       p++;
332     }
333
334   return str;
335 }
336
337 static void
338 accounts_widget_generic_setup (EmpathyAccountWidget *self,
339     GtkWidget *table_common_settings,
340     GtkWidget *table_advanced_settings)
341 {
342   TpConnectionManagerParam *params, *param;
343   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
344
345   params = empathy_account_settings_get_tp_params (priv->settings);
346
347   for (param = params; param != NULL && param->name != NULL; param++)
348     {
349       GtkWidget       *table_settings;
350       guint            n_rows = 0;
351       GtkWidget       *widget = NULL;
352       gchar           *param_name_formatted;
353
354       if (param->flags & TP_CONN_MGR_PARAM_FLAG_REQUIRED)
355         table_settings = table_common_settings;
356       else
357         table_settings = table_advanced_settings;
358
359       param_name_formatted = account_widget_generic_format_param_name (param->name);
360       g_object_get (table_settings, "n-rows", &n_rows, NULL);
361       gtk_table_resize (GTK_TABLE (table_settings), ++n_rows, 2);
362
363       if (param->dbus_signature[0] == 's')
364         {
365           gchar *str;
366
367           str = g_strdup_printf (_("%s:"), param_name_formatted);
368           widget = gtk_label_new (str);
369           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
370           g_free (str);
371
372           gtk_table_attach (GTK_TABLE (table_settings),
373               widget,
374               0, 1,
375               n_rows - 1, n_rows,
376               GTK_FILL, 0,
377               0, 0);
378           gtk_widget_show (widget);
379
380           widget = gtk_entry_new ();
381           if (strcmp (param->name, "account") == 0)
382             {
383               g_signal_connect (widget, "realize",
384                   G_CALLBACK (gtk_widget_grab_focus),
385                   NULL);
386             }
387           gtk_table_attach (GTK_TABLE (table_settings),
388               widget,
389               1, 2,
390               n_rows - 1, n_rows,
391               GTK_FILL | GTK_EXPAND, 0,
392               0, 0);
393           gtk_widget_show (widget);
394         }
395       /* int types: ynqiuxt. double type is 'd' */
396       else if (param->dbus_signature[0] == 'y' ||
397           param->dbus_signature[0] == 'n' ||
398           param->dbus_signature[0] == 'q' ||
399           param->dbus_signature[0] == 'i' ||
400           param->dbus_signature[0] == 'u' ||
401           param->dbus_signature[0] == 'x' ||
402           param->dbus_signature[0] == 't' ||
403           param->dbus_signature[0] == 'd')
404         {
405           gchar   *str = NULL;
406           gdouble  minint = 0;
407           gdouble  maxint = 0;
408           gdouble  step = 1;
409
410           switch (param->dbus_signature[0])
411             {
412             case 'y': minint = G_MININT8;  maxint = G_MAXINT8;   break;
413             case 'n': minint = G_MININT16; maxint = G_MAXINT16;  break;
414             case 'q': minint = 0;          maxint = G_MAXUINT16; break;
415             case 'i': minint = G_MININT32; maxint = G_MAXINT32;  break;
416             case 'u': minint = 0;          maxint = G_MAXUINT32; break;
417             case 'x': minint = G_MININT64; maxint = G_MAXINT64;  break;
418             case 't': minint = 0;          maxint = G_MAXUINT64; break;
419             case 'd': minint = G_MININT32; maxint = G_MAXINT32; step = 0.1; break;
420             }
421
422           str = g_strdup_printf (_("%s:"), param_name_formatted);
423           widget = gtk_label_new (str);
424           gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
425           g_free (str);
426
427           gtk_table_attach (GTK_TABLE (table_settings),
428               widget,
429               0, 1,
430               n_rows - 1, n_rows,
431               GTK_FILL, 0,
432               0, 0);
433           gtk_widget_show (widget);
434
435           widget = gtk_spin_button_new_with_range (minint, maxint, step);
436           gtk_table_attach (GTK_TABLE (table_settings),
437               widget,
438               1, 2,
439               n_rows - 1, n_rows,
440               GTK_FILL | GTK_EXPAND, 0,
441               0, 0);
442           gtk_widget_show (widget);
443         }
444       else if (param->dbus_signature[0] == 'b')
445         {
446           widget = gtk_check_button_new_with_label (param_name_formatted);
447           gtk_table_attach (GTK_TABLE (table_settings),
448               widget,
449               0, 2,
450               n_rows - 1, n_rows,
451               GTK_FILL | GTK_EXPAND, 0,
452               0, 0);
453           gtk_widget_show (widget);
454         }
455       else
456         {
457           DEBUG ("Unknown signature for param %s: %s",
458               param_name_formatted, param->dbus_signature);
459         }
460
461       if (widget)
462         account_widget_setup_widget (self, widget, param->name);
463
464       g_free (param_name_formatted);
465     }
466 }
467
468 static void
469 account_widget_handle_params_valist (EmpathyAccountWidget *self,
470     const gchar *first_widget,
471     va_list args)
472 {
473   GObject *object;
474   const gchar *name;
475
476   for (name = first_widget; name; name = va_arg (args, const gchar *))
477     {
478       const gchar *param_name;
479
480       param_name = va_arg (args, const gchar *);
481       object = gtk_builder_get_object (self->ui_details->gui, name);
482
483       if (!object)
484         {
485           g_warning ("Builder is missing object '%s'.", name);
486           continue;
487         }
488
489       account_widget_setup_widget (self, GTK_WIDGET (object), param_name);
490     }
491 }
492
493 static void
494 account_widget_apply_clicked_cb (GtkWidget *button,
495     EmpathyAccountWidget *self)
496 {
497   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
498
499   empathy_account_settings_apply_async (priv->settings, NULL, NULL);
500 }
501
502 static void
503 account_widget_setup_generic (EmpathyAccountWidget *self)
504 {
505   GtkWidget *table_common_settings;
506   GtkWidget *table_advanced_settings;
507
508   table_common_settings = GTK_WIDGET (gtk_builder_get_object (self->ui_details->gui,
509           "table_common_settings"));
510   table_advanced_settings = GTK_WIDGET (gtk_builder_get_object (self->ui_details->gui,
511           "table_advanced_settings"));
512
513   accounts_widget_generic_setup (self, table_common_settings,
514       table_advanced_settings);
515
516   g_object_unref (self->ui_details->gui);
517 }
518
519 static void
520 account_widget_settings_ready_cb (EmpathyAccountSettings *settings,
521     GParamSpec *pspec,
522     gpointer user_data)
523 {
524   EmpathyAccountWidget *self = user_data;
525   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
526
527   if (empathy_account_settings_is_ready (priv->settings))
528     account_widget_setup_generic (self);
529 }
530
531 static void
532 account_widget_build_generic (EmpathyAccountWidget *self,
533     const char *filename)
534 {
535   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
536   self->ui_details->gui = empathy_builder_get_file (filename,
537       "vbox_generic_settings", &self->ui_details->widget,
538       NULL);
539
540   g_object_ref (self->ui_details->gui);
541
542   if (empathy_account_settings_is_ready (priv->settings))
543     account_widget_setup_generic (self);
544   else
545     g_signal_connect (priv->settings, "notify::ready",
546         G_CALLBACK (account_widget_settings_ready_cb), self);
547 }
548
549 static void
550 account_widget_build_salut (EmpathyAccountWidget *self,
551     const char *filename)
552 {
553   self->ui_details->gui = empathy_builder_get_file (filename,
554       "vbox_salut_settings", &self->ui_details->widget,
555       NULL);
556
557   empathy_account_widget_handle_params (self,
558       "entry_published", "published-name",
559       "entry_nickname", "nickname",
560       "entry_first_name", "first-name",
561       "entry_last_name", "last-name",
562       "entry_email", "email",
563       "entry_jid", "jid",
564       NULL);
565
566   self->ui_details->default_focus = g_strdup ("entry_nickname");
567 }
568
569 static void
570 account_widget_build_msn (EmpathyAccountWidget *self,
571     const char *filename)
572 {
573   self->ui_details->gui = empathy_builder_get_file (filename,
574       "vbox_msn_settings", &self->ui_details->widget,
575       NULL);
576
577   empathy_account_widget_handle_params (self,
578       "entry_id", "account",
579       "entry_password", "password",
580       "entry_server", "server",
581       "spinbutton_port", "port",
582       NULL);
583
584   self->ui_details->default_focus = g_strdup ("entry_id");
585   self->ui_details->add_forget = TRUE;
586 }
587
588 static void
589 account_widget_build_jabber (EmpathyAccountWidget *self,
590     const char *filename)
591 {
592   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
593   GtkWidget *spinbutton_port;
594   GtkWidget *checkbutton_ssl;
595
596   self->ui_details->gui = empathy_builder_get_file (filename,
597       "vbox_jabber_settings", &self->ui_details->widget,
598       "spinbutton_port", &spinbutton_port,
599       "checkbutton_ssl", &checkbutton_ssl,
600       NULL);
601
602   empathy_account_widget_handle_params (self,
603       "entry_id", "account",
604       "entry_password", "password",
605       "entry_resource", "resource",
606       "entry_server", "server",
607       "spinbutton_port", "port",
608       "spinbutton_priority", "priority",
609       "checkbutton_ssl", "old-ssl",
610       "checkbutton_ignore_ssl_errors", "ignore-ssl-errors",
611       "checkbutton_encryption", "require-encryption",
612       NULL);
613
614   self->ui_details->default_focus = g_strdup ("entry_id");
615   self->ui_details->add_forget = TRUE;
616   priv->spinbutton_port = spinbutton_port;
617
618   g_signal_connect (checkbutton_ssl, "toggled",
619       G_CALLBACK (account_widget_jabber_ssl_toggled_cb),
620       self);
621 }
622
623 static void
624 account_widget_build_icq (EmpathyAccountWidget *self,
625     const char *filename)
626 {
627   GtkWidget *spinbutton_port;
628
629   self->ui_details->gui = empathy_builder_get_file (filename,
630       "vbox_icq_settings", &self->ui_details->widget,
631       "spinbutton_port", &spinbutton_port,
632       NULL);
633
634   empathy_account_widget_handle_params (self,
635       "entry_uin", "account",
636       "entry_password", "password",
637       "entry_server", "server",
638       "spinbutton_port", "port",
639       "entry_charset", "charset",
640       NULL);
641
642   self->ui_details->default_focus = g_strdup ("entry_uin");
643   self->ui_details->add_forget = TRUE;
644 }
645
646 static void
647 account_widget_build_aim (EmpathyAccountWidget *self,
648     const char *filename)
649 {
650   GtkWidget *spinbutton_port;
651
652   self->ui_details->gui = empathy_builder_get_file (filename,
653       "vbox_aim_settings", &self->ui_details->widget,
654       "spinbutton_port", &spinbutton_port,
655       NULL);
656
657   empathy_account_widget_handle_params (self,
658       "entry_screenname", "account",
659       "entry_password", "password",
660       "entry_server", "server",
661       "spinbutton_port", "port",
662       NULL);
663
664   self->ui_details->default_focus = g_strdup ("entry_screenname");
665   self->ui_details->add_forget = TRUE;
666 }
667
668 static void
669 account_widget_build_yahoo (EmpathyAccountWidget *self,
670     const char *filename)
671 {
672   self->ui_details->gui = empathy_builder_get_file (filename,
673       "vbox_yahoo_settings", &self->ui_details->widget,
674       NULL);
675
676   empathy_account_widget_handle_params (self,
677       "entry_id", "account",
678       "entry_password", "password",
679       "entry_server", "server",
680       "entry_locale", "room-list-locale",
681       "entry_charset", "charset",
682       "spinbutton_port", "port",
683       "checkbutton_yahoojp", "yahoojp",
684       "checkbutton_ignore_invites", "ignore-invites",
685       NULL);
686
687   self->ui_details->default_focus = g_strdup ("entry_id");
688   self->ui_details->add_forget = TRUE;
689 }
690
691 static void
692 account_widget_build_groupwise (EmpathyAccountWidget *self,
693     const char *filename)
694 {
695   self->ui_details->gui = empathy_builder_get_file (filename,
696       "vbox_groupwise_settings", &self->ui_details->widget,
697       NULL);
698
699   empathy_account_widget_handle_params (self,
700       "entry_id", "account",
701       "entry_password", "password",
702       "entry_server", "server",
703       "spinbutton_port", "port",
704       NULL);
705
706   self->ui_details->default_focus = g_strdup ("entry_id");
707   self->ui_details->add_forget = TRUE;
708 }
709
710 static void
711 account_widget_destroy_cb (GtkWidget *widget,
712     EmpathyAccountWidget *self)
713 {
714   g_object_unref (self);
715 }
716
717 static void
718 do_set_property (GObject *object,
719     guint prop_id,
720     const GValue *value,
721     GParamSpec *pspec)
722 {
723   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
724
725   switch (prop_id)
726     {
727     case PROP_PROTOCOL:
728       priv->protocol = g_value_dup_string (value);
729       break;
730     case PROP_SETTINGS:
731       priv->settings = g_value_dup_object (value);
732       break;
733     default:
734       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
735     }
736 }
737
738 static void
739 do_get_property (GObject *object,
740     guint prop_id,
741     GValue *value,
742     GParamSpec *pspec)
743 {
744   EmpathyAccountWidgetPriv *priv = GET_PRIV (object);
745
746   switch (prop_id)
747     {
748     case PROP_PROTOCOL:
749       g_value_set_string (value, priv->protocol);
750       break;
751     case PROP_SETTINGS:
752       g_value_set_object (value, priv->settings);
753       break;
754     default:
755       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
756     }
757 }
758
759 static void
760 do_constructed (GObject *obj)
761 {
762   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
763   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
764   char *uiname, *filename;
765
766   uiname = g_strconcat ("empathy-account-widget-", priv->protocol,
767       ".ui", NULL);
768   filename = empathy_file_lookup (uiname, "libempathy-gtk");
769
770   if (!tp_strdiff (priv->protocol, "local-xmpp"))
771     account_widget_build_salut (self, filename);
772   else if (!tp_strdiff (priv->protocol, "msn"))
773     account_widget_build_msn (self, filename);
774   else if (!tp_strdiff (priv->protocol, "jabber"))
775     account_widget_build_jabber (self, filename);
776   else if (!tp_strdiff (priv->protocol, "icq"))
777     account_widget_build_icq (self, filename);
778   else if (!tp_strdiff (priv->protocol, "aim"))
779     account_widget_build_aim (self, filename);
780   else if (!tp_strdiff (priv->protocol, "yahoo"))
781     account_widget_build_yahoo (self, filename);
782   else if (!tp_strdiff (priv->protocol, "groupwise"))
783     account_widget_build_groupwise (self, filename);
784   else if (!tp_strdiff (priv->protocol, "irc"))
785     empathy_account_widget_irc_build (self, filename);
786   else if (!tp_strdiff (priv->protocol, "sip"))
787     empathy_account_widget_sip_build (self, filename);
788   else if (!tp_strdiff (priv->protocol, "generic"))
789     account_widget_build_generic (self, filename);
790   else
791     {
792       g_free (uiname);
793       g_free (filename);
794       g_critical ("Unknown protocol, can't build the account widget");
795       return;
796     }
797
798   g_free (uiname);
799   g_free (filename);
800
801   /* handle default focus */
802   if (self->ui_details->default_focus != NULL)
803     {
804       GObject *default_focus_entry;
805
806       default_focus_entry = gtk_builder_get_object
807         (self->ui_details->gui, self->ui_details->default_focus);
808       g_signal_connect (default_focus_entry, "realize",
809           G_CALLBACK (gtk_widget_grab_focus),
810           NULL);
811     }
812
813   /* handle forget button */
814   if (self->ui_details->add_forget)
815     {
816       const gchar *password = NULL;
817
818       priv->button_forget = GTK_WIDGET (gtk_builder_get_object (self->ui_details->gui, "button_forget"));
819       priv->entry_password = GTK_WIDGET (gtk_builder_get_object (self->ui_details->gui, "entry_password"));
820
821       password = empathy_account_settings_get_string (priv->settings, "password");
822       gtk_widget_set_sensitive (priv->button_forget, !EMP_STR_EMPTY (password));
823
824       g_signal_connect (priv->button_forget, "clicked",
825           G_CALLBACK (account_widget_forget_clicked_cb),
826           self);
827       g_signal_connect (priv->entry_password, "changed",
828           G_CALLBACK (account_widget_password_changed_cb),
829           self);
830     }
831
832   /* handle apply button */
833   priv->apply_button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
834   gtk_box_pack_end (GTK_BOX (self->ui_details->widget), priv->apply_button, FALSE, FALSE, 3);
835
836   g_signal_connect (priv->apply_button, "clicked",
837       G_CALLBACK (account_widget_apply_clicked_cb),
838       self);
839   account_widget_handle_apply_sensitivity (self);
840   gtk_widget_show (priv->apply_button);
841
842   /* hook up to widget destruction to unref ourselves */
843   g_signal_connect (self->ui_details->widget, "destroy",
844       G_CALLBACK (account_widget_destroy_cb), self);
845
846   empathy_builder_unref_and_keep_widget (self->ui_details->gui, self->ui_details->widget);
847 }
848
849 static void
850 do_dispose (GObject *obj)
851 {
852   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
853   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
854
855   if (priv->dispose_run)
856     return;
857
858   priv->dispose_run = TRUE;
859
860   if (priv->settings != NULL)
861     {
862       g_object_unref (priv->settings);
863       priv->settings = NULL;
864     }
865
866   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose != NULL)
867     G_OBJECT_CLASS (empathy_account_widget_parent_class)->dispose (obj);
868 }
869
870 static void
871 do_finalize (GObject *obj)
872 {
873   EmpathyAccountWidget *self = EMPATHY_ACCOUNT_WIDGET (obj);
874   EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
875
876   g_free (self->ui_details->default_focus);
877   g_slice_free (EmpathyAccountWidgetUIDetails, self->ui_details);
878
879   g_free (priv->protocol);
880
881   if (G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize != NULL)
882     G_OBJECT_CLASS (empathy_account_widget_parent_class)->finalize (obj);
883 }
884
885 static void
886 empathy_account_widget_class_init (EmpathyAccountWidgetClass *klass)
887 {
888   GObjectClass *oclass = G_OBJECT_CLASS (klass);
889   GParamSpec *param_spec;
890
891   oclass->get_property = do_get_property;
892   oclass->set_property = do_set_property;
893   oclass->constructed = do_constructed;
894   oclass->dispose = do_dispose;
895   oclass->finalize = do_finalize;
896
897   param_spec = g_param_spec_string ("protocol",
898       "protocol", "The protocol of the account",
899       NULL,
900       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
901   g_object_class_install_property (oclass, PROP_PROTOCOL, param_spec);
902
903   param_spec = g_param_spec_object ("settings",
904       "settings", "The settings of the account",
905       EMPATHY_TYPE_ACCOUNT_SETTINGS,
906       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
907   g_object_class_install_property (oclass, PROP_SETTINGS, param_spec);
908
909   g_type_class_add_private (klass, sizeof (EmpathyAccountWidgetPriv));
910 }
911
912 static void
913 empathy_account_widget_init (EmpathyAccountWidget *self)
914 {
915   EmpathyAccountWidgetPriv *priv =
916     G_TYPE_INSTANCE_GET_PRIVATE ((self), EMPATHY_TYPE_ACCOUNT_WIDGET,
917         EmpathyAccountWidgetPriv);
918
919   self->priv = priv;
920   priv->dispose_run = FALSE;
921
922   self->ui_details = g_slice_new0 (EmpathyAccountWidgetUIDetails);
923 }
924
925 /* public methods */
926
927 void
928 empathy_account_widget_handle_params (EmpathyAccountWidget *self,
929     const gchar *first_widget,
930     ...)
931 {
932   va_list args;
933
934   va_start (args, first_widget);
935   account_widget_handle_params_valist (self, first_widget, args);
936   va_end (args);
937 }
938
939 GtkWidget *
940 empathy_account_widget_new_for_protocol (const char *protocol,
941     EmpathyAccountSettings *settings)
942 {
943   EmpathyAccountWidget *self;
944   EmpathyAccountWidgetPriv *priv;
945
946   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), NULL);
947   g_return_val_if_fail (settings != NULL, NULL);
948
949   self = g_object_new
950     (EMPATHY_TYPE_ACCOUNT_WIDGET, "protocol", protocol,
951         "settings", settings, NULL);
952   priv = GET_PRIV (self);
953
954   return self->ui_details->widget;
955 }