1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
30 #include <glib/gi18n-lib.h>
32 #include <libmissioncontrol/mc-account.h>
33 #include <libmissioncontrol/mc-protocol.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-account-widget.h"
38 #include "empathy-ui-utils.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
41 #include <libempathy/empathy-debug.h>
44 account_widget_entry_focus_cb (GtkWidget
*widget
,
49 const gchar
*param_name
;
51 str
= gtk_entry_get_text (GTK_ENTRY (widget
));
52 param_name
= g_object_get_data (G_OBJECT (widget
), "param_name");
54 if (EMP_STR_EMPTY (str
)) {
57 mc_account_unset_param (account
, param_name
);
58 mc_account_get_param_string (account
, param_name
, &value
);
59 DEBUG ("Unset %s and restore to %s", param_name
, value
);
60 gtk_entry_set_text (GTK_ENTRY (widget
), value
? value
: "");
64 const gchar
*domain
= NULL
;
65 gchar
*dup_str
= NULL
;
67 profile
= mc_account_get_profile (account
);
68 if (mc_profile_get_capabilities (profile
) &
69 MC_PROFILE_CAPABILITY_SPLIT_ACCOUNT
) {
70 domain
= mc_profile_get_default_account_domain (profile
);
73 if (domain
&& !strstr (str
, "@") &&
74 strcmp (param_name
, "account") == 0) {
75 DEBUG ("Adding @%s suffix to account", domain
);
76 str
= dup_str
= g_strconcat (str
, "@", domain
, NULL
);
77 gtk_entry_set_text (GTK_ENTRY (widget
), str
);
79 DEBUG ("Setting %s to %s", param_name
,
80 strstr (param_name
, "password") ? "***" : str
);
81 mc_account_set_param_string (account
, param_name
, str
);
83 g_object_unref (profile
);
90 account_widget_int_changed_cb (GtkWidget
*widget
,
93 const gchar
*param_name
;
96 value
= gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (widget
));
97 param_name
= g_object_get_data (G_OBJECT (widget
), "param_name");
100 mc_account_unset_param (account
, param_name
);
101 mc_account_get_param_int (account
, param_name
, &value
);
102 DEBUG ("Unset %s and restore to %d", param_name
, value
);
103 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget
), value
);
105 DEBUG ("Setting %s to %d", param_name
, value
);
106 mc_account_set_param_int (account
, param_name
, value
);
111 account_widget_checkbutton_toggled_cb (GtkWidget
*widget
,
115 gboolean default_value
;
116 const gchar
*param_name
;
118 value
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget
));
119 param_name
= g_object_get_data (G_OBJECT (widget
), "param_name");
121 /* FIXME: This is ugly! checkbox don't have a "not-set" value so we
122 * always unset the param and set the value if different from the
124 mc_account_unset_param (account
, param_name
);
125 mc_account_get_param_boolean (account
, param_name
, &default_value
);
127 if (default_value
== value
) {
128 DEBUG ("Unset %s and restore to %d", param_name
, default_value
);
130 DEBUG ("Setting %s to %d", param_name
, value
);
131 mc_account_set_param_boolean (account
, param_name
, value
);
136 account_widget_forget_clicked_cb (GtkWidget
*button
,
140 const gchar
*param_name
;
142 param_name
= g_object_get_data (G_OBJECT (entry
), "param_name");
143 account
= g_object_get_data (G_OBJECT (entry
), "account");
145 DEBUG ("Unset %s", param_name
);
146 mc_account_unset_param (account
, param_name
);
147 gtk_entry_set_text (GTK_ENTRY (entry
), "");
151 account_widget_password_changed_cb (GtkWidget
*entry
,
156 str
= gtk_entry_get_text (GTK_ENTRY (entry
));
157 gtk_widget_set_sensitive (button
, !EMP_STR_EMPTY (str
));
161 account_widget_jabber_ssl_toggled_cb (GtkWidget
*checkbutton_ssl
,
162 GtkWidget
*spinbutton_port
)
168 value
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_ssl
));
169 account
= g_object_get_data (G_OBJECT (spinbutton_port
), "account");
170 mc_account_get_param_int (account
, "port", &port
);
173 if (port
== 5222 || port
== 0) {
177 if (port
== 5223 || port
== 0) {
182 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_port
), port
);
186 account_widget_setup_widget (GtkWidget
*widget
,
188 const gchar
*param_name
)
190 g_object_set_data_full (G_OBJECT (widget
), "param_name",
191 g_strdup (param_name
), g_free
);
192 g_object_set_data_full (G_OBJECT (widget
), "account",
193 g_object_ref (account
), g_object_unref
);
195 if (GTK_IS_SPIN_BUTTON (widget
)) {
198 mc_account_get_param_int (account
, param_name
, &value
);
199 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget
), value
);
201 g_signal_connect (widget
, "value-changed",
202 G_CALLBACK (account_widget_int_changed_cb
),
205 else if (GTK_IS_ENTRY (widget
)) {
208 mc_account_get_param_string (account
, param_name
, &str
);
209 gtk_entry_set_text (GTK_ENTRY (widget
), str
? str
: "");
212 if (strstr (param_name
, "password")) {
213 gtk_entry_set_visibility (GTK_ENTRY (widget
), FALSE
);
216 g_signal_connect (widget
, "focus-out-event",
217 G_CALLBACK (account_widget_entry_focus_cb
),
220 else if (GTK_IS_TOGGLE_BUTTON (widget
)) {
221 gboolean value
= FALSE
;
223 mc_account_get_param_boolean (account
, param_name
, &value
);
224 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget
), value
);
226 g_signal_connect (widget
, "toggled",
227 G_CALLBACK (account_widget_checkbutton_toggled_cb
),
230 DEBUG ("Unknown type of widget for param %s", param_name
);
235 account_widget_generic_format_param_name (const gchar
*param_name
)
240 str
= g_strdup (param_name
);
242 if (str
&& g_ascii_isalpha (str
[0])) {
243 str
[0] = g_ascii_toupper (str
[0]);
246 while ((p
= strchr (str
, '-')) != NULL
) {
247 if (p
[1] != '\0' && g_ascii_isalpha (p
[1])) {
249 p
[1] = g_ascii_toupper (p
[1]);
259 accounts_widget_generic_setup (McAccount
*account
,
260 GtkWidget
*table_common_settings
,
261 GtkWidget
*table_advanced_settings
)
263 McProtocol
*protocol
;
267 profile
= mc_account_get_profile (account
);
268 protocol
= mc_profile_get_protocol (profile
);
271 /* The CM is not installed, MC shouldn't list them
272 * see SF bug #1688779
273 * FIXME: We should display something asking the user to
276 g_object_unref (profile
);
280 params
= mc_protocol_get_params (protocol
);
282 for (l
= params
; l
; l
= l
->next
) {
283 McProtocolParam
*param
;
284 GtkWidget
*table_settings
;
286 GtkWidget
*widget
= NULL
;
287 gchar
*param_name_formatted
;
290 if (param
->flags
& MC_PROTOCOL_PARAM_REQUIRED
) {
291 table_settings
= table_common_settings
;
293 table_settings
= table_advanced_settings
;
295 param_name_formatted
= account_widget_generic_format_param_name (param
->name
);
296 g_object_get (table_settings
, "n-rows", &n_rows
, NULL
);
297 gtk_table_resize (GTK_TABLE (table_settings
), ++n_rows
, 2);
299 if (param
->signature
[0] == 's') {
302 str
= g_strdup_printf (_("%s:"), param_name_formatted
);
303 widget
= gtk_label_new (str
);
304 gtk_misc_set_alignment (GTK_MISC (widget
), 0, 0.5);
307 gtk_table_attach (GTK_TABLE (table_settings
),
313 gtk_widget_show (widget
);
315 widget
= gtk_entry_new ();
316 if (strcmp (param
->name
, "account") == 0) {
317 g_signal_connect (widget
, "realize",
318 G_CALLBACK (gtk_widget_grab_focus
),
321 gtk_table_attach (GTK_TABLE (table_settings
),
325 GTK_FILL
| GTK_EXPAND
, 0,
327 gtk_widget_show (widget
);
329 /* int types: ynqiuxt. double type is 'd' */
330 else if (param
->signature
[0] == 'y' ||
331 param
->signature
[0] == 'n' ||
332 param
->signature
[0] == 'q' ||
333 param
->signature
[0] == 'i' ||
334 param
->signature
[0] == 'u' ||
335 param
->signature
[0] == 'x' ||
336 param
->signature
[0] == 't' ||
337 param
->signature
[0] == 'd') {
343 switch (param
->signature
[0]) {
344 case 'y': minint
= G_MININT8
; maxint
= G_MAXINT8
; break;
345 case 'n': minint
= G_MININT16
; maxint
= G_MAXINT16
; break;
346 case 'q': minint
= 0; maxint
= G_MAXUINT16
; break;
347 case 'i': minint
= G_MININT32
; maxint
= G_MAXINT32
; break;
348 case 'u': minint
= 0; maxint
= G_MAXUINT32
; break;
349 case 'x': minint
= G_MININT64
; maxint
= G_MAXINT64
; break;
350 case 't': minint
= 0; maxint
= G_MAXUINT64
; break;
351 case 'd': minint
= G_MININT32
; maxint
= G_MAXINT32
; step
= 0.1; break;
354 str
= g_strdup_printf (_("%s:"), param_name_formatted
);
355 widget
= gtk_label_new (str
);
356 gtk_misc_set_alignment (GTK_MISC (widget
), 0, 0.5);
359 gtk_table_attach (GTK_TABLE (table_settings
),
365 gtk_widget_show (widget
);
367 widget
= gtk_spin_button_new_with_range (minint
, maxint
, step
);
368 gtk_table_attach (GTK_TABLE (table_settings
),
372 GTK_FILL
| GTK_EXPAND
, 0,
374 gtk_widget_show (widget
);
376 else if (param
->signature
[0] == 'b') {
377 widget
= gtk_check_button_new_with_label (param_name_formatted
);
378 gtk_table_attach (GTK_TABLE (table_settings
),
382 GTK_FILL
| GTK_EXPAND
, 0,
384 gtk_widget_show (widget
);
386 DEBUG ("Unknown signature for param %s: %s",
387 param_name_formatted
, param
->signature
);
391 account_widget_setup_widget (widget
, account
, param
->name
);
394 g_free (param_name_formatted
);
397 g_slist_free (params
);
398 g_object_unref (profile
);
399 g_object_unref (protocol
);
403 account_widget_handle_params_valist (McAccount
*account
,
405 const gchar
*first_widget
,
411 for (name
= first_widget
; name
; name
= va_arg (args
, const gchar
*)) {
412 const gchar
*param_name
;
414 param_name
= va_arg (args
, const gchar
*);
415 object
= gtk_builder_get_object (gui
, name
);
418 g_warning ("Builder is missing object '%s'.", name
);
422 account_widget_setup_widget (GTK_WIDGET (object
), account
, param_name
);
427 empathy_account_widget_handle_params (McAccount
*account
,
429 const gchar
*first_widget
,
434 g_return_if_fail (MC_IS_ACCOUNT (account
));
435 g_return_if_fail (GTK_IS_BUILDER (gui
));
437 va_start (args
, first_widget
);
438 account_widget_handle_params_valist (account
, gui
, first_widget
, args
);
443 empathy_account_widget_add_forget_button (McAccount
*account
,
448 GtkWidget
*button_forget
;
449 GtkWidget
*entry_password
;
450 gchar
*password
= NULL
;
452 button_forget
= GTK_WIDGET (gtk_builder_get_object (gui
, button
));
453 entry_password
= GTK_WIDGET (gtk_builder_get_object (gui
, entry
));
455 mc_account_get_param_string (account
, "password", &password
);
456 gtk_widget_set_sensitive (button_forget
, !EMP_STR_EMPTY (password
));
459 g_signal_connect (button_forget
, "clicked",
460 G_CALLBACK (account_widget_forget_clicked_cb
),
462 g_signal_connect (entry_password
, "changed",
463 G_CALLBACK (account_widget_password_changed_cb
),
468 empathy_account_widget_set_default_focus (GtkBuilder
*gui
,
471 GObject
*default_focus_entry
;
473 default_focus_entry
= gtk_builder_get_object (gui
, entry
);
474 g_signal_connect (default_focus_entry
, "realize",
475 G_CALLBACK (gtk_widget_grab_focus
),
480 empathy_account_widget_generic_new (McAccount
*account
)
484 GtkWidget
*table_common_settings
;
485 GtkWidget
*table_advanced_settings
;
488 g_return_val_if_fail (MC_IS_ACCOUNT (account
), NULL
);
490 filename
= empathy_file_lookup ("empathy-account-widget-generic.ui",
492 gui
= empathy_builder_get_file (filename
,
493 "vbox_generic_settings", &widget
,
494 "table_common_settings", &table_common_settings
,
495 "table_advanced_settings", &table_advanced_settings
,
499 accounts_widget_generic_setup (account
, table_common_settings
, table_advanced_settings
);
501 return empathy_builder_unref_and_keep_widget (gui
, widget
);
505 empathy_account_widget_salut_new (McAccount
*account
)
511 filename
= empathy_file_lookup ("empathy-account-widget-salut.ui",
513 gui
= empathy_builder_get_file (filename
,
514 "vbox_salut_settings", &widget
,
518 empathy_account_widget_handle_params (account
, gui
,
519 "entry_published", "published-name",
520 "entry_nickname", "nickname",
521 "entry_first_name", "first-name",
522 "entry_last_name", "last-name",
523 "entry_email", "email",
527 empathy_account_widget_set_default_focus (gui
, "entry_nickname");
529 return empathy_builder_unref_and_keep_widget (gui
, widget
);
533 empathy_account_widget_msn_new (McAccount
*account
)
539 filename
= empathy_file_lookup ("empathy-account-widget-msn.ui",
541 gui
= empathy_builder_get_file (filename
,
542 "vbox_msn_settings", &widget
,
546 empathy_account_widget_handle_params (account
, gui
,
547 "entry_id", "account",
548 "entry_password", "password",
549 "entry_server", "server",
550 "spinbutton_port", "port",
553 empathy_account_widget_add_forget_button (account
, gui
,
557 empathy_account_widget_set_default_focus (gui
, "entry_id");
559 return empathy_builder_unref_and_keep_widget (gui
, widget
);
563 empathy_account_widget_jabber_new (McAccount
*account
)
567 GtkWidget
*spinbutton_port
;
568 GtkWidget
*checkbutton_ssl
;
571 filename
= empathy_file_lookup ("empathy-account-widget-jabber.ui",
573 gui
= empathy_builder_get_file (filename
,
574 "vbox_jabber_settings", &widget
,
575 "spinbutton_port", &spinbutton_port
,
576 "checkbutton_ssl", &checkbutton_ssl
,
580 empathy_account_widget_handle_params (account
, gui
,
581 "entry_id", "account",
582 "entry_password", "password",
583 "entry_resource", "resource",
584 "entry_server", "server",
585 "spinbutton_port", "port",
586 "spinbutton_priority", "priority",
587 "checkbutton_ssl", "old-ssl",
588 "checkbutton_ignore_ssl_errors", "ignore-ssl-errors",
589 "checkbutton_encryption", "require-encryption",
592 empathy_account_widget_add_forget_button (account
, gui
,
596 empathy_account_widget_set_default_focus (gui
, "entry_id");
598 g_signal_connect (checkbutton_ssl
, "toggled",
599 G_CALLBACK (account_widget_jabber_ssl_toggled_cb
),
602 return empathy_builder_unref_and_keep_widget (gui
, widget
);
606 empathy_account_widget_icq_new (McAccount
*account
)
610 GtkWidget
*spinbutton_port
;
613 filename
= empathy_file_lookup ("empathy-account-widget-icq.ui",
615 gui
= empathy_builder_get_file (filename
,
616 "vbox_icq_settings", &widget
,
617 "spinbutton_port", &spinbutton_port
,
621 empathy_account_widget_handle_params (account
, gui
,
622 "entry_uin", "account",
623 "entry_password", "password",
624 "entry_server", "server",
625 "spinbutton_port", "port",
626 "entry_charset", "charset",
629 empathy_account_widget_add_forget_button (account
, gui
,
633 empathy_account_widget_set_default_focus (gui
, "entry_uin");
635 return empathy_builder_unref_and_keep_widget (gui
, widget
);
639 empathy_account_widget_aim_new (McAccount
*account
)
643 GtkWidget
*spinbutton_port
;
646 filename
= empathy_file_lookup ("empathy-account-widget-aim.ui",
648 gui
= empathy_builder_get_file (filename
,
649 "vbox_aim_settings", &widget
,
650 "spinbutton_port", &spinbutton_port
,
654 empathy_account_widget_handle_params (account
, gui
,
655 "entry_screenname", "account",
656 "entry_password", "password",
657 "entry_server", "server",
658 "spinbutton_port", "port",
661 empathy_account_widget_add_forget_button (account
, gui
,
665 empathy_account_widget_set_default_focus (gui
, "entry_screenname");
667 return empathy_builder_unref_and_keep_widget (gui
, widget
);
671 empathy_account_widget_yahoo_new (McAccount
*account
)
677 filename
= empathy_file_lookup ("empathy-account-widget-yahoo.ui",
679 gui
= empathy_builder_get_file (filename
,
680 "vbox_yahoo_settings", &widget
,
684 empathy_account_widget_handle_params (account
, gui
,
685 "entry_id", "account",
686 "entry_password", "password",
687 "entry_server", "server",
688 "entry_locale", "room-list-locale",
689 "entry_charset", "charset",
690 "spinbutton_port", "port",
691 "checkbutton_yahoojp", "yahoojp",
692 "checkbutton_ignore_invites", "ignore-invites",
695 empathy_account_widget_add_forget_button (account
, gui
,
699 empathy_account_widget_set_default_focus (gui
, "entry_id");
701 return empathy_builder_unref_and_keep_widget (gui
, widget
);
705 empathy_account_widget_groupwise_new (McAccount
*account
)
711 filename
= empathy_file_lookup ("empathy-account-widget-groupwise.ui",
713 gui
= empathy_builder_get_file (filename
,
714 "vbox_groupwise_settings", &widget
,
718 empathy_account_widget_handle_params (account
, gui
,
719 "entry_id", "account",
720 "entry_password", "password",
721 "entry_server", "server",
722 "spinbutton_port", "port",
725 empathy_account_widget_add_forget_button (account
, gui
,
729 empathy_account_widget_set_default_focus (gui
, "entry_id");
731 return empathy_builder_unref_and_keep_widget (gui
, widget
);