1 /* Copyright (c) 2007, Nokia Corporation
7 #include "modest-account-settings-dialog.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <gtk/gtkstock.h>
18 #include <hildon-widgets/hildon-caption.h>
19 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
20 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
21 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
22 #include "maemo/easysetup/modest-easysetup-serversecurity-combo-box.h"
23 #include "maemo/easysetup/modest-easysetup-secureauth-combo-box.h"
24 #include "maemo/easysetup/modest-validating-entry.h"
25 #include "modest-text-utils.h"
26 #include "modest-account-mgr.h"
27 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
28 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
29 #include "maemo/modest-connection-specific-smtp-window.h"
30 #include <gconf/gconf-client.h>
31 #include <string.h> /* For strlen(). */
33 /* Include config.h so that _() works: */
38 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
40 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
42 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
43 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
45 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
47 struct _ModestAccountSettingsDialogPrivate
52 enable_buttons (ModestAccountSettingsDialog *self);
55 save_configuration (ModestAccountSettingsDialog *dialog);
58 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
59 GValue *value, GParamSpec *pspec)
61 switch (property_id) {
63 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
69 const GValue *value, GParamSpec *pspec)
71 switch (property_id) {
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78 modest_account_settings_dialog_dispose (GObject *object)
80 if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
81 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
85 modest_account_settings_dialog_finalize (GObject *object)
87 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
89 if (self->account_name)
90 g_free (self->account_name);
92 if (self->original_account_title)
93 g_free (self->original_account_title);
95 if (self->account_manager)
96 g_object_unref (G_OBJECT (self->account_manager));
98 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
102 show_error (GtkWindow *parent_window, const gchar* text);
105 show_ok (GtkWindow *parent_window, const gchar* text);
109 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
111 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
113 enable_buttons(self);
117 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
119 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
121 enable_buttons(self);
124 /** This is a convenience function to create a caption containing a mandatory widget.
125 * When the widget is edited, the enable_buttons() vfunc will be called.
127 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
132 HildonCaptionStatus flag)
134 GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
136 /* The translated strings seem to already contain the *,
137 * but this code can be used if that is not true in future.
140 /* Add a * character to indicate mandatory fields,
141 * as specified in our "Email UI Specification": */
142 if (flag == HILDON_CAPTION_MANDATORY) {
143 gchar* title = g_strdup_printf("%s*", value);
144 caption = hildon_caption_new (group, title, control, icon, flag);
148 caption = hildon_caption_new (group, value, control, icon, flag);
151 /* Connect to the appropriate changed signal for the widget,
152 * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
154 if (GTK_IS_ENTRY (control)) {
155 g_signal_connect (G_OBJECT (control), "changed",
156 G_CALLBACK (on_caption_entry_changed), self);
159 else if (GTK_IS_COMBO_BOX (control)) {
160 g_signal_connect (G_OBJECT (control), "changed",
161 G_CALLBACK (on_caption_combobox_changed), self);
168 create_page_account_details (ModestAccountSettingsDialog *self)
170 GtkWidget *box = gtk_vbox_new (FALSE, 2);
171 GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
172 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
173 gtk_widget_show (label);
175 /* Create a size group to be used by all captions.
176 * Note that HildonCaption does not create a default size group if we do not specify one.
177 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
178 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
180 /* The description widgets: */
181 self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
182 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"),
183 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
184 gtk_widget_show (self->entry_account_title);
185 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
186 gtk_widget_show (caption);
188 /* Prevent the use of some characters in the account title,
189 * as required by our UI specification: */
190 GList *list_prevent = NULL;
191 list_prevent = g_list_append (list_prevent, "\\");
192 list_prevent = g_list_append (list_prevent, "/");
193 list_prevent = g_list_append (list_prevent, ":");
194 list_prevent = g_list_append (list_prevent, "*");
195 list_prevent = g_list_append (list_prevent, "?");
196 list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
197 list_prevent = g_list_append (list_prevent, "“");
198 list_prevent = g_list_append (list_prevent, "<");
199 list_prevent = g_list_append (list_prevent, ">");
200 list_prevent = g_list_append (list_prevent, "|");
201 list_prevent = g_list_append (list_prevent, "^");
202 easysetup_validating_entry_set_unallowed_characters (
203 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
204 g_list_free (list_prevent);
206 /* Set max length as in the UI spec:
207 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
208 gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
210 /* TODO: The Retrieve and Limit Retrieve combo boxes. */
212 /* The leave-messages widgets: */
213 if(!self->checkbox_leave_messages)
214 self->checkbox_leave_messages =
215 gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
216 gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
217 gtk_widget_show (self->checkbox_leave_messages);
220 gtk_widget_show (GTK_WIDGET (box));
222 return GTK_WIDGET (box);
226 on_button_signature (GtkButton *button, gpointer user_data)
232 create_page_user_details (ModestAccountSettingsDialog *self)
234 GtkWidget *box = gtk_vbox_new (FALSE, 2);
236 /* Create a size group to be used by all captions.
237 * Note that HildonCaption does not create a default size group if we do not specify one.
238 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
239 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
241 /* The name widgets: */
242 self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
243 /* Set max length as in the UI spec:
244 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
245 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
246 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
247 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
248 gtk_widget_show (self->entry_user_name);
249 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
250 gtk_widget_show (caption);
252 /* Prevent the use of some characters in the name,
253 * as required by our UI specification: */
254 GList *list_prevent = NULL;
255 list_prevent = g_list_append (list_prevent, "<");
256 list_prevent = g_list_append (list_prevent, ">");
257 easysetup_validating_entry_set_unallowed_characters (
258 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
259 g_list_free (list_prevent);
261 /* The username widgets: */
262 self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
263 caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
264 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
265 gtk_widget_show (self->entry_user_username);
266 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
267 gtk_widget_show (caption);
269 /* Prevent the use of some characters in the username,
270 * as required by our UI specification: */
271 easysetup_validating_entry_set_unallowed_characters_whitespace (
272 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
274 /* Set max length as in the UI spec:
275 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
276 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
278 /* The password widgets: */
279 self->entry_user_password = gtk_entry_new ();
280 gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
281 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
282 caption = create_caption_new_with_asterix (self, sizegroup,
283 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
284 gtk_widget_show (self->entry_user_password);
285 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
286 gtk_widget_show (caption);
288 /* The email address widgets: */
289 self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
290 caption = create_caption_new_with_asterix (self, sizegroup,
291 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
292 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
293 gtk_widget_show (self->entry_user_email);
294 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
295 gtk_widget_show (caption);
297 /* Set max length as in the UI spec:
298 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
299 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
302 /* Signature button: */
303 if (!self->button_signature)
304 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
305 caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"),
306 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
307 gtk_widget_show (self->button_signature);
308 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
309 gtk_widget_show (caption);
311 g_signal_connect (G_OBJECT (self->button_signature), "clicked",
312 G_CALLBACK (on_button_signature), self);
314 gtk_widget_show (GTK_WIDGET (box));
316 return GTK_WIDGET (box);
319 /** Change the caption title for the incoming server,
320 * as specified in the UI spec:
322 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
325 (protocol == MODEST_PROTOCOL_STORE_POP ?
326 _("mail_fi_emailtype_pop3") :
327 _("mail_fi_emailtype_imap") );
330 /* Note that this produces a compiler warning,
331 * because the compiler does not know that the translated string will have a %s in it.
332 * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
333 gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
334 g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
335 g_free(incomingserver_title);
338 /** Change the caption title for the incoming server,
339 * as specified in the UI spec:
341 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
343 /* Fill the combo with appropriately titled choices for POP or IMAP. */
344 /* The choices are the same, but the titles are different, as in the UI spec. */
345 easysetup_serversecurity_combo_box_fill (
346 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
349 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
351 GtkWidget *box = gtk_vbox_new (FALSE, 2);
353 /* Create a size group to be used by all captions.
354 * Note that HildonCaption does not create a default size group if we do not specify one.
355 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
356 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
358 /* The incoming server widgets: */
359 if(!self->entry_incomingserver)
360 self->entry_incomingserver = gtk_entry_new ();
362 if (self->caption_incoming)
363 gtk_widget_destroy (self->caption_incoming);
365 /* The caption title will be updated in update_incoming_server_title().
366 * so this default text will never be seen: */
367 /* (Note: Changing the title seems pointless. murrayc) */
368 self->caption_incoming = create_caption_new_with_asterix (self, sizegroup,
369 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
370 gtk_widget_show (self->entry_incomingserver);
371 gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
372 gtk_widget_show (self->caption_incoming);
374 /* The secure connection widgets: */
375 /* This will be filled by update_incoming_server_security_choices(). */
376 if (!self->combo_incoming_security)
377 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
378 GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
379 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
380 gtk_widget_show (self->combo_incoming_security);
381 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
382 gtk_widget_show (caption);
384 /* The port widgets: */
385 /* TODO: There are various rules about this in the UI spec. */
386 if (!self->entry_incoming_port)
387 self->entry_incoming_port = GTK_WIDGET (gtk_entry_new ());
388 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
389 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
390 gtk_widget_show (self->entry_incoming_port);
391 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
392 gtk_widget_show (caption);
394 /* The secure authentication widgets: */
395 if(!self->checkbox_incoming_auth)
396 self->checkbox_incoming_auth =
397 gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
398 gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
399 gtk_widget_show (self->checkbox_incoming_auth);
401 gtk_widget_show (GTK_WIDGET (box));
403 return GTK_WIDGET (box);
407 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
409 GtkWidget *widget = GTK_WIDGET (user_data);
411 /* Enable the widget only if the toggle button is active: */
412 const gboolean enable = gtk_toggle_button_get_active (togglebutton);
413 gtk_widget_set_sensitive (widget, enable);
416 /* Make the sensitivity of a widget depend on a toggle button.
419 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
421 g_signal_connect (G_OBJECT (button), "toggled",
422 G_CALLBACK (on_toggle_button_changed), widget);
424 /* Set the starting sensitivity: */
425 on_toggle_button_changed (button, widget);
429 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
431 /* Destroy the window when it is closed: */
432 gtk_widget_destroy (GTK_WIDGET (window));
436 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
439 ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
441 /* Show the window: */
442 /* TODO: Retrieve the chosen settings,
443 * so we can supply them when creating the connection somehow.
445 GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
446 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
447 g_signal_connect (G_OBJECT (window), "hide",
448 G_CALLBACK (on_smtp_servers_window_hide), self);
449 gtk_widget_show (window);
453 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
455 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
457 ModestProtocol protocol_security =
458 easysetup_secureauth_combo_box_get_active_secureauth (
459 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
460 const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
462 gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
463 gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
466 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
468 GtkWidget *box = gtk_vbox_new (FALSE, 2);
470 /* Create a size group to be used by all captions.
471 * Note that HildonCaption does not create a default size group if we do not specify one.
472 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
473 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
475 /* The outgoing server widgets: */
476 if (!self->entry_outgoingserver)
477 self->entry_outgoingserver = gtk_entry_new ();
478 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
479 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
480 gtk_widget_show (self->entry_outgoingserver);
481 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
482 gtk_widget_show (caption);
484 /* The secure authentication widgets: */
485 if (!self->combo_outgoing_auth)
486 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
487 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
488 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
489 gtk_widget_show (self->combo_outgoing_auth);
490 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
491 gtk_widget_show (caption);
493 /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
494 g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
496 /* The username widgets: */
497 self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
498 self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
499 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
500 gtk_widget_show (self->entry_outgoing_username);
501 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
502 gtk_widget_show (self->caption_outgoing_username);
504 /* Prevent the use of some characters in the username,
505 * as required by our UI specification: */
506 easysetup_validating_entry_set_unallowed_characters_whitespace (
507 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
509 /* Set max length as in the UI spec:
510 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
511 gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
513 /* The password widgets: */
514 self->entry_outgoing_password = gtk_entry_new ();
515 gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
516 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
517 self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup,
518 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
519 gtk_widget_show (self->entry_outgoing_password);
520 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
521 gtk_widget_show (self->caption_outgoing_password);
523 /* The secure connection widgets: */
524 /* This will be filled and set with easysetup_serversecurity_combo_box_fill()
525 * and easysetup_serversecurity_combo_box_set_active_serversecurity().
527 if (!self->combo_outgoing_security)
528 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
529 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
530 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
531 gtk_widget_show (self->combo_outgoing_security);
532 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
533 gtk_widget_show (caption);
535 /* The port widgets: */
536 if (!self->entry_outgoing_port)
537 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
538 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
539 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
540 gtk_widget_show (self->entry_outgoing_port);
541 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
542 gtk_widget_show (caption);
544 /* connection-specific checkbox: */
545 if (!self->checkbox_outgoing_smtp_specific) {
546 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
547 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific),
550 gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
551 gtk_widget_show (self->checkbox_outgoing_smtp_specific);
553 /* Connection-specific SMTP-Severs Edit button: */
554 if (!self->button_outgoing_smtp_servers)
555 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
556 caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"),
557 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
558 gtk_widget_show (self->button_outgoing_smtp_servers);
559 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
560 gtk_widget_show (caption);
562 /* Only enable the button when the checkbox is checked: */
563 enable_widget_for_togglebutton (self->button_outgoing_smtp_servers,
564 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
566 g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
567 G_CALLBACK (on_button_outgoing_smtp_servers), self);
570 gtk_widget_show (GTK_WIDGET (box));
572 return GTK_WIDGET (box);
576 check_data (ModestAccountSettingsDialog *self)
578 /* Check that the title is not already in use: */
579 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
580 if ((!account_title) || (strlen(account_title) == 0))
581 return FALSE; /* Should be prevented already anyway. */
583 if (strcmp(account_title, self->original_account_title) != 0) {
584 /* Check the changed title: */
585 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (self->account_manager,
589 /* Warn the user via a dialog: */
590 show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
596 /* Check that the email address is valud: */
597 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
598 if ((!email_address) || (strlen(email_address) == 0))
601 if (!modest_text_utils_validate_email_address (email_address)) {
602 /* Warn the user via a dialog: */
603 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
605 /* Return focus to the email address entry: */
606 gtk_widget_grab_focus (self->entry_user_email);
611 /* TODO: The UI Spec wants us to check that the servernames are valid,
612 * but does not specify how.
618 on_response (GtkDialog *wizard_dialog,
622 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
623 enable_buttons (self);
625 gboolean prevent_response = FALSE;
627 /* Warn about unsaved changes: */
628 /* TODO: Actually detect whether changes were made. */
629 if (response_id == GTK_RESPONSE_CANCEL) {
630 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
633 GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
634 _("imum_nc_wizard_confirm_lose_changes") ));
636 const gint dialog_response = gtk_dialog_run (dialog);
637 gtk_widget_destroy (GTK_WIDGET (dialog));
639 if (dialog_response != GTK_RESPONSE_OK)
640 prevent_response = TRUE;
642 /* Check for invalid input: */
643 else if (!check_data (self)) {
644 prevent_response = TRUE;
647 if (prevent_response) {
648 /* This is a nasty hack. murrayc. */
649 /* Don't let the dialog close */
650 g_signal_stop_emission_by_name (wizard_dialog, "response");
655 if (response_id == GTK_RESPONSE_OK) {
656 /* Try to save the changes: */
657 const gboolean saved = save_configuration (self);
659 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
661 show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
666 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
668 /* Create the notebook to be used by the GtkDialog base class:
669 * Each page of the notebook will be a page of the wizard: */
670 GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
673 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
675 /* Get the account manager object,
676 * so we can check for existing accounts,
677 * and create new accounts: */
678 self->account_manager = modest_runtime_get_account_mgr ();
679 g_assert (self->account_manager);
680 g_object_ref (self->account_manager);
682 /* Create the common pages,
684 self->page_account_details = create_page_account_details (self);
685 self->page_user_details = create_page_user_details (self);
686 self->page_incoming = create_page_incoming (self);
687 self->page_outgoing = create_page_outgoing (self);
689 /* Add the notebook pages: */
690 gtk_notebook_append_page (notebook, self->page_account_details,
691 gtk_label_new (_("mcen_ti_account_settings_account")));
692 gtk_notebook_append_page (notebook, self->page_user_details,
693 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
694 gtk_notebook_append_page (notebook, self->page_incoming,
695 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
696 gtk_notebook_append_page (notebook, self->page_outgoing,
697 gtk_label_new (_("mcen_ti_advsetup_sending")));
699 GtkDialog *dialog = GTK_DIALOG (self);
700 gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
701 gtk_widget_show (GTK_WIDGET (notebook));
703 /* Add the buttons: */
704 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
705 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
707 /* Connect to the dialog's response signal: */
708 /* We use connect-before
709 * so we can stop the signal emission,
710 * to stop the default signal handler from closing the dialog.
712 g_signal_connect (G_OBJECT (self), "response",
713 G_CALLBACK (on_response), self);
716 ModestAccountSettingsDialog*
717 modest_account_settings_dialog_new (void)
719 return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
722 /** Update the UI with the stored account details, so they can be edited.
723 * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
725 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
730 /* Save the account name so we can refer to it later: */
731 if (dialog->account_name)
732 g_free (dialog->account_name);
733 dialog->account_name = g_strdup (account_name);
736 /* Get the account data for this account name: */
737 ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager,
740 g_printerr ("modest: failed to get account data for %s\n", account_name);
744 /* Save the account title so we can refer to it if the user changes it: */
745 if (dialog->original_account_title)
746 g_free (dialog->original_account_title);
747 dialog->original_account_title = g_strdup (account_data->display_name);
750 if (!(account_data->store_account)) {
751 g_printerr ("modest: account has no stores: %s\n", account_name);
755 /* Show the account data in the widgets: */
757 /* Note that we never show the non-display name in the UI.
758 * (Though the display name defaults to the non-display name at the start.) */
759 gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
760 account_data->display_name ? account_data->display_name : "");
762 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name),
763 account_data->fullname ? account_data->fullname : "");
764 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email),
765 account_data->email ? account_data->email : "");
767 const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
768 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
769 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
772 ModestServerAccountData *incoming_account = account_data->store_account;
773 if (incoming_account) {
774 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
775 incoming_account->username ? incoming_account->username : "");
776 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password),
777 incoming_account->password ? incoming_account->password : "");
779 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver),
780 incoming_account->hostname ? incoming_account->hostname : "");
782 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(incoming_account);
783 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth),
784 secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
786 update_incoming_server_title (dialog, incoming_account->proto);
787 update_incoming_server_security_choices (dialog, incoming_account->proto);
789 const ModestProtocol security = modest_server_account_data_get_option_security (incoming_account);
790 easysetup_serversecurity_combo_box_set_active_serversecurity (
791 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
795 ModestProtocol proto;
803 ModestServerAccountData *outgoing_account = account_data->transport_account;
804 if (outgoing_account) {
805 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver),
806 outgoing_account->hostname ? outgoing_account->hostname : "");
808 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username),
809 outgoing_account->username ? outgoing_account->username : "");
810 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password),
811 outgoing_account->password ? outgoing_account->password : "");
813 /* Get the secure-auth setting: */
814 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(outgoing_account);
815 easysetup_secureauth_combo_box_set_active_secureauth (
816 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
817 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
819 easysetup_serversecurity_combo_box_fill (
820 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
822 /* Get the security setting: */
823 const ModestProtocol security = modest_server_account_data_get_option_security (outgoing_account);
824 easysetup_serversecurity_combo_box_set_active_serversecurity (
825 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
827 /* TODO: set port. */
830 /* account_data->is_enabled, */
831 /*account_data->is_default, */
833 /* account_data->store_account->proto */
835 modest_account_mgr_free_account_data (dialog->account_manager, account_data);
839 save_configuration (ModestAccountSettingsDialog *dialog)
841 g_assert (dialog->account_name);
843 const gchar* account_name = dialog->account_name;
845 /* Set the account data from the widgets: */
846 const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
847 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
848 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
852 const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
853 test = modest_account_mgr_set_string (dialog->account_manager, account_name,
854 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
858 const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
859 test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
860 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
865 gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
866 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
867 g_assert (incoming_account_name);
869 const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
870 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
871 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
875 const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
876 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
877 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
881 const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
882 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
883 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
887 const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active
888 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))
889 ? MODEST_PROTOCOL_AUTH_PASSWORD
890 : MODEST_PROTOCOL_AUTH_NONE;
891 modest_server_account_set_option_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
893 const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
894 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
895 modest_server_account_set_option_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
897 g_free (incoming_account_name);
900 gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
901 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
902 g_assert (outgoing_account_name);
904 hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
905 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
906 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
910 username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
911 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
912 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
916 password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
917 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
918 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
922 const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
923 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
924 modest_server_account_set_option_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
926 const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
927 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
928 modest_server_account_set_option_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);
930 g_free (outgoing_account_name);
933 /* Set the changed account title last, to simplify the previous code: */
934 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
935 if ((!account_title) || (strlen(account_title) == 0))
936 return FALSE; /* Should be prevented already anyway. */
938 if (strcmp(account_title, account_name) != 0) {
939 /* Change the title: */
940 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
941 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
949 static gboolean entry_is_empty (GtkWidget *entry)
954 const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
955 if ((!text) || (strlen(text) == 0))
962 enable_buttons (ModestAccountSettingsDialog *self)
964 gboolean enable_ok = TRUE;
966 /* The account details title is mandatory: */
967 if (entry_is_empty(self->entry_account_title))
970 /* The user details username is mandatory: */
971 if (entry_is_empty(self->entry_user_username))
974 /* The user details email address is mandatory: */
975 if (enable_ok && entry_is_empty (self->entry_user_email))
978 /* The custom incoming server is mandatory: */
979 if (entry_is_empty(self->entry_incomingserver))
982 /* Enable the buttons,
983 * identifying them via their associated response codes:
985 GtkDialog *dialog_base = GTK_DIALOG (self);
986 gtk_dialog_set_response_sensitive (dialog_base,
992 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
994 GObjectClass *object_class = G_OBJECT_CLASS (klass);
995 g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
998 object_class->get_property = modest_account_settings_dialog_get_property;
999 object_class->set_property = modest_account_settings_dialog_set_property;
1000 object_class->dispose = modest_account_settings_dialog_dispose;
1001 object_class->finalize = modest_account_settings_dialog_finalize;
1005 show_error (GtkWindow *parent_window, const gchar* text)
1007 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1013 gtk_dialog_run (dialog);
1014 gtk_widget_destroy (GTK_WIDGET (dialog));
1018 show_ok (GtkWindow *parent_window, const gchar* text)
1020 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1026 gtk_dialog_run (dialog);
1027 gtk_widget_destroy (GTK_WIDGET (dialog));