Updated de translation (Andre Klapper).
[empathy-mirror.git] / libempathy-gtk / empathy-account-widget.c
blobcf01f899f2eba475b6a4e7c0923a6267e45d82f4
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-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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
25 #include <config.h>
27 #include <string.h>
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.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>
43 static gboolean
44 account_widget_entry_focus_cb (GtkWidget *widget,
45 GdkEventFocus *event,
46 McAccount *account)
48 const gchar *str;
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 (G_STR_EMPTY (str)) {
55 gchar *value = NULL;
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 : "");
61 g_free (value);
62 } else {
63 McProfile *profile;
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);
82 g_free (dup_str);
83 g_object_unref (profile);
86 return FALSE;
89 static void
90 account_widget_int_changed_cb (GtkWidget *widget,
91 McAccount *account)
93 const gchar *param_name;
94 gint value;
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");
99 if (value == 0) {
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);
104 } else {
105 DEBUG ("Setting %s to %d", param_name, value);
106 mc_account_set_param_int (account, param_name, value);
110 static void
111 account_widget_checkbutton_toggled_cb (GtkWidget *widget,
112 McAccount *account)
114 gboolean value;
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
123 * default value. */
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);
129 } else {
130 DEBUG ("Setting %s to %d", param_name, value);
131 mc_account_set_param_boolean (account, param_name, value);
135 static void
136 account_widget_forget_clicked_cb (GtkWidget *button,
137 GtkWidget *entry)
139 McAccount *account;
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), "");
150 static void
151 account_widget_password_changed_cb (GtkWidget *entry,
152 GtkWidget *button)
154 const gchar *str;
156 str = gtk_entry_get_text (GTK_ENTRY (entry));
157 gtk_widget_set_sensitive (button, !G_STR_EMPTY (str));
160 static void
161 account_widget_jabber_ssl_toggled_cb (GtkWidget *checkbutton_ssl,
162 GtkWidget *spinbutton_port)
164 McAccount *account;
165 gboolean value;
166 gint port = 0;
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);
172 if (value) {
173 if (port == 5222 || port == 0) {
174 port = 5223;
176 } else {
177 if (port == 5223 || port == 0) {
178 port = 5222;
182 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_port), port);
185 static void
186 account_widget_setup_widget (GtkWidget *widget,
187 McAccount *account,
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)) {
196 gint value = 0;
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),
203 account);
205 else if (GTK_IS_ENTRY (widget)) {
206 gchar *str = NULL;
208 mc_account_get_param_string (account, param_name, &str);
209 gtk_entry_set_text (GTK_ENTRY (widget), str ? str : "");
210 g_free (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),
218 account);
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),
228 account);
229 } else {
230 DEBUG ("Unknown type of widget for param %s", param_name);
234 static gchar *
235 account_widget_generic_format_param_name (const gchar *param_name)
237 gchar *str;
238 gchar *p;
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])) {
248 p[0] = ' ';
249 p[1] = g_ascii_toupper (p[1]);
252 p++;
255 return str;
258 static void
259 accounts_widget_generic_setup (McAccount *account,
260 GtkWidget *table_common_settings,
261 GtkWidget *table_advanced_settings)
263 McProtocol *protocol;
264 McProfile *profile;
265 GSList *params, *l;
267 profile = mc_account_get_profile (account);
268 protocol = mc_profile_get_protocol (profile);
270 if (!protocol) {
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
274 * install the CM
276 g_object_unref (profile);
277 return;
280 params = mc_protocol_get_params (protocol);
282 for (l = params; l; l = l->next) {
283 McProtocolParam *param;
284 GtkWidget *table_settings;
285 guint n_rows = 0;
286 GtkWidget *widget = NULL;
287 gchar *param_name_formatted;
289 param = l->data;
290 if (param->flags & MC_PROTOCOL_PARAM_REQUIRED) {
291 table_settings = table_common_settings;
292 } else {
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') {
300 gchar *str;
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);
305 g_free (str);
307 gtk_table_attach (GTK_TABLE (table_settings),
308 widget,
309 0, 1,
310 n_rows - 1, n_rows,
311 GTK_FILL, 0,
312 0, 0);
314 widget = gtk_entry_new ();
315 gtk_table_attach (GTK_TABLE (table_settings),
316 widget,
317 1, 2,
318 n_rows - 1, n_rows,
319 GTK_FILL | GTK_EXPAND, 0,
320 0, 0);
322 /* int types: ynqiuxt. double type is 'd' */
323 else if (param->signature[0] == 'y' ||
324 param->signature[0] == 'n' ||
325 param->signature[0] == 'q' ||
326 param->signature[0] == 'i' ||
327 param->signature[0] == 'u' ||
328 param->signature[0] == 'x' ||
329 param->signature[0] == 't' ||
330 param->signature[0] == 'd') {
331 gchar *str = NULL;
332 gdouble minint = 0;
333 gdouble maxint = 0;
334 gdouble step = 1;
336 switch (param->signature[0]) {
337 case 'y': minint = G_MININT8; maxint = G_MAXINT8; break;
338 case 'n': minint = G_MININT16; maxint = G_MAXINT16; break;
339 case 'q': minint = 0; maxint = G_MAXUINT16; break;
340 case 'i': minint = G_MININT32; maxint = G_MAXINT32; break;
341 case 'u': minint = 0; maxint = G_MAXUINT32; break;
342 case 'x': minint = G_MININT64; maxint = G_MAXINT64; break;
343 case 't': minint = 0; maxint = G_MAXUINT64; break;
344 case 'd': minint = G_MININT32; maxint = G_MAXINT32; step = 0.1; break;
347 str = g_strdup_printf (_("%s:"), param_name_formatted);
348 widget = gtk_label_new (str);
349 gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
350 g_free (str);
352 gtk_table_attach (GTK_TABLE (table_settings),
353 widget,
354 0, 1,
355 n_rows - 1, n_rows,
356 GTK_FILL, 0,
357 0, 0);
359 widget = gtk_spin_button_new_with_range (minint, maxint, step);
360 gtk_table_attach (GTK_TABLE (table_settings),
361 widget,
362 1, 2,
363 n_rows - 1, n_rows,
364 GTK_FILL | GTK_EXPAND, 0,
365 0, 0);
367 else if (param->signature[0] == 'b') {
368 widget = gtk_check_button_new_with_label (param_name_formatted);
369 gtk_table_attach (GTK_TABLE (table_settings),
370 widget,
371 0, 2,
372 n_rows - 1, n_rows,
373 GTK_FILL | GTK_EXPAND, 0,
374 0, 0);
375 } else {
376 DEBUG ("Unknown signature for param %s: %s",
377 param_name_formatted, param->signature);
380 if (widget) {
381 account_widget_setup_widget (widget, account, param->name);
384 g_free (param_name_formatted);
387 g_slist_free (params);
388 g_object_unref (profile);
389 g_object_unref (protocol);
392 static void
393 account_widget_handle_params_valist (McAccount *account,
394 GladeXML *gui,
395 const gchar *first_widget_name,
396 va_list args)
398 GtkWidget *widget;
399 const gchar *widget_name;
401 for (widget_name = first_widget_name; widget_name; widget_name = va_arg (args, gchar*)) {
402 const gchar *param_name;
404 param_name = va_arg (args, gchar*);
406 widget = glade_xml_get_widget (gui, widget_name);
408 if (!widget) {
409 g_warning ("Glade is missing widget '%s'.", widget_name);
410 continue;
413 account_widget_setup_widget (widget, account, param_name);
417 void
418 empathy_account_widget_handle_params (McAccount *account,
419 GladeXML *gui,
420 const gchar *first_widget_name,
421 ...)
423 va_list args;
425 g_return_if_fail (MC_IS_ACCOUNT (account));
427 va_start (args, first_widget_name);
428 account_widget_handle_params_valist (account, gui,
429 first_widget_name,
430 args);
431 va_end (args);
434 void
435 empathy_account_widget_add_forget_button (McAccount *account,
436 GladeXML *glade,
437 const gchar *button,
438 const gchar *entry)
440 GtkWidget *button_forget;
441 GtkWidget *entry_password;
442 gchar *password = NULL;
444 button_forget = glade_xml_get_widget (glade, button);
445 entry_password = glade_xml_get_widget (glade, entry);
447 mc_account_get_param_string (account, "password", &password);
448 gtk_widget_set_sensitive (button_forget, !G_STR_EMPTY (password));
449 g_free (password);
451 g_signal_connect (button_forget, "clicked",
452 G_CALLBACK (account_widget_forget_clicked_cb),
453 entry_password);
454 g_signal_connect (entry_password, "changed",
455 G_CALLBACK (account_widget_password_changed_cb),
456 button_forget);
459 GtkWidget *
460 empathy_account_widget_generic_new (McAccount *account)
462 GladeXML *glade;
463 GtkWidget *widget;
464 GtkWidget *table_common_settings;
465 GtkWidget *table_advanced_settings;
466 gchar *filename;
468 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
470 filename = empathy_file_lookup ("empathy-account-widget-generic.glade",
471 "libempathy-gtk");
472 glade = empathy_glade_get_file (filename,
473 "vbox_generic_settings",
474 NULL,
475 "vbox_generic_settings", &widget,
476 "table_common_settings", &table_common_settings,
477 "table_advanced_settings", &table_advanced_settings,
478 NULL);
479 g_free (filename);
481 accounts_widget_generic_setup (account, table_common_settings, table_advanced_settings);
483 g_object_unref (glade);
485 gtk_widget_show_all (widget);
487 return widget;
490 GtkWidget *
491 empathy_account_widget_salut_new (McAccount *account)
493 GladeXML *glade;
494 GtkWidget *widget;
495 gchar *filename;
497 filename = empathy_file_lookup ("empathy-account-widget-salut.glade",
498 "libempathy-gtk");
499 glade = empathy_glade_get_file (filename,
500 "vbox_salut_settings",
501 NULL,
502 "vbox_salut_settings", &widget,
503 NULL);
504 g_free (filename);
506 empathy_account_widget_handle_params (account, glade,
507 "entry_published", "published-name",
508 "entry_nickname", "nickname",
509 "entry_first_name", "first-name",
510 "entry_last_name", "last-name",
511 "entry_email", "email",
512 "entry_jid", "jid",
513 NULL);
515 g_object_unref (glade);
517 gtk_widget_show (widget);
519 return widget;
522 GtkWidget *
523 empathy_account_widget_msn_new (McAccount *account)
525 GladeXML *glade;
526 GtkWidget *widget;
527 gchar *filename;
529 filename = empathy_file_lookup ("empathy-account-widget-msn.glade",
530 "libempathy-gtk");
531 glade = empathy_glade_get_file (filename,
532 "vbox_msn_settings",
533 NULL,
534 "vbox_msn_settings", &widget,
535 NULL);
536 g_free (filename);
538 empathy_account_widget_handle_params (account, glade,
539 "entry_id", "account",
540 "entry_password", "password",
541 "entry_server", "server",
542 "spinbutton_port", "port",
543 NULL);
545 empathy_account_widget_add_forget_button (account, glade,
546 "button_forget",
547 "entry_password");
549 g_object_unref (glade);
551 gtk_widget_show (widget);
553 return widget;
556 GtkWidget *
557 empathy_account_widget_jabber_new (McAccount *account)
559 GladeXML *glade;
560 GtkWidget *widget;
561 GtkWidget *spinbutton_port;
562 GtkWidget *checkbutton_ssl;
563 gchar *filename;
565 filename = empathy_file_lookup ("empathy-account-widget-jabber.glade",
566 "libempathy-gtk");
567 glade = empathy_glade_get_file (filename,
568 "vbox_jabber_settings",
569 NULL,
570 "vbox_jabber_settings", &widget,
571 "spinbutton_port", &spinbutton_port,
572 "checkbutton_ssl", &checkbutton_ssl,
573 NULL);
574 g_free (filename);
576 empathy_account_widget_handle_params (account, glade,
577 "entry_id", "account",
578 "entry_password", "password",
579 "entry_resource", "resource",
580 "entry_server", "server",
581 "spinbutton_port", "port",
582 "spinbutton_priority", "priority",
583 "checkbutton_ssl", "old-ssl",
584 "checkbutton_ignore_ssl_errors", "ignore-ssl-errors",
585 "checkbutton_encryption", "require-encryption",
586 NULL);
588 empathy_account_widget_add_forget_button (account, glade,
589 "button_forget",
590 "entry_password");
592 g_signal_connect (checkbutton_ssl, "toggled",
593 G_CALLBACK (account_widget_jabber_ssl_toggled_cb),
594 spinbutton_port);
596 g_object_unref (glade);
598 gtk_widget_show (widget);
600 return widget;
603 GtkWidget *
604 empathy_account_widget_icq_new (McAccount *account)
606 GladeXML *glade;
607 GtkWidget *widget;
608 GtkWidget *spinbutton_port;
609 gchar *filename;
611 filename = empathy_file_lookup ("empathy-account-widget-icq.glade",
612 "libempathy-gtk");
613 glade = empathy_glade_get_file (filename,
614 "vbox_icq_settings",
615 NULL,
616 "vbox_icq_settings", &widget,
617 "spinbutton_port", &spinbutton_port,
618 NULL);
619 g_free (filename);
621 empathy_account_widget_handle_params (account, glade,
622 "entry_uin", "account",
623 "entry_password", "password",
624 "entry_server", "server",
625 "spinbutton_port", "port",
626 "entry_charset", "charset",
627 NULL);
629 empathy_account_widget_add_forget_button (account, glade,
630 "button_forget",
631 "entry_password");
633 g_object_unref (glade);
635 gtk_widget_show (widget);
637 return widget;
640 GtkWidget *
641 empathy_account_widget_aim_new (McAccount *account)
643 GladeXML *glade;
644 GtkWidget *widget;
645 GtkWidget *spinbutton_port;
646 gchar *filename;
648 filename = empathy_file_lookup ("empathy-account-widget-aim.glade",
649 "libempathy-gtk");
650 glade = empathy_glade_get_file (filename,
651 "vbox_aim_settings",
652 NULL,
653 "vbox_aim_settings", &widget,
654 "spinbutton_port", &spinbutton_port,
655 NULL);
656 g_free (filename);
658 empathy_account_widget_handle_params (account, glade,
659 "entry_screenname", "account",
660 "entry_password", "password",
661 "entry_server", "server",
662 "spinbutton_port", "port",
663 NULL);
665 empathy_account_widget_add_forget_button (account, glade,
666 "button_forget",
667 "entry_password");
669 g_object_unref (glade);
671 gtk_widget_show (widget);
673 return widget;
676 GtkWidget *
677 empathy_account_widget_yahoo_new (McAccount *account)
679 GladeXML *glade;
680 GtkWidget *widget;
681 gchar *filename;
683 filename = empathy_file_lookup ("empathy-account-widget-yahoo.glade",
684 "libempathy-gtk");
685 glade = empathy_glade_get_file (filename,
686 "vbox_yahoo_settings",
687 NULL,
688 "vbox_yahoo_settings", &widget,
689 NULL);
690 g_free (filename);
692 empathy_account_widget_handle_params (account, glade,
693 "entry_id", "account",
694 "entry_password", "password",
695 "entry_server", "server",
696 "entry_serverjp", "serverjp",
697 "entry_locale", "room-list-locale",
698 "entry_charset", "charset",
699 "spinbutton_port", "port",
700 "checkbutton_yahoojp", "yahoojp",
701 "checkbutton_ignore_invites", "ignore-invites",
702 NULL);
704 empathy_account_widget_add_forget_button (account, glade,
705 "button_forget",
706 "entry_password");
708 g_object_unref (glade);
710 gtk_widget_show (widget);
712 return widget;
715 GtkWidget *
716 empathy_account_widget_groupwise_new (McAccount *account)
718 GladeXML *glade;
719 GtkWidget *widget;
720 gchar *filename;
722 filename = empathy_file_lookup ("empathy-account-widget-groupwise.glade",
723 "libempathy-gtk");
724 glade = empathy_glade_get_file (filename,
725 "vbox_groupwise_settings",
726 NULL,
727 "vbox_groupwise_settings", &widget,
728 NULL);
729 g_free (filename);
731 empathy_account_widget_handle_params (account, glade,
732 "entry_id", "account",
733 "entry_password", "password",
734 "entry_server", "server",
735 "spinbutton_port", "port",
736 NULL);
738 empathy_account_widget_add_forget_button (account, glade,
739 "button_forget",
740 "entry_password");
742 g_object_unref (glade);
744 gtk_widget_show (widget);
746 return widget;