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.
620 on_response (GtkDialog *wizard_dialog,
624 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
625 enable_buttons (self);
627 gboolean prevent_response = FALSE;
629 /* Warn about unsaved changes: */
630 /* TODO: Actually detect whether changes were made. */
631 if (response_id == GTK_RESPONSE_CANCEL) {
632 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
635 GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
636 _("imum_nc_wizard_confirm_lose_changes") ));
638 const gint dialog_response = gtk_dialog_run (dialog);
639 gtk_widget_destroy (GTK_WIDGET (dialog));
641 if (dialog_response != GTK_RESPONSE_OK)
642 prevent_response = TRUE;
644 /* Check for invalid input: */
645 else if (!check_data (self)) {
646 prevent_response = TRUE;
649 if (prevent_response) {
650 /* This is a nasty hack. murrayc. */
651 /* Don't let the dialog close */
652 g_signal_stop_emission_by_name (wizard_dialog, "response");
657 if (response_id == GTK_RESPONSE_OK) {
658 /* Try to save the changes: */
659 const gboolean saved = save_configuration (self);
661 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
663 show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
668 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
670 /* Create the notebook to be used by the GtkDialog base class:
671 * Each page of the notebook will be a page of the wizard: */
672 GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
675 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
677 /* Get the account manager object,
678 * so we can check for existing accounts,
679 * and create new accounts: */
680 self->account_manager = modest_runtime_get_account_mgr ();
681 g_assert (self->account_manager);
682 g_object_ref (self->account_manager);
684 /* Create the common pages,
686 self->page_account_details = create_page_account_details (self);
687 self->page_user_details = create_page_user_details (self);
688 self->page_incoming = create_page_incoming (self);
689 self->page_outgoing = create_page_outgoing (self);
691 /* Add the notebook pages: */
692 gtk_notebook_append_page (notebook, self->page_account_details,
693 gtk_label_new (_("mcen_ti_account_settings_account")));
694 gtk_notebook_append_page (notebook, self->page_user_details,
695 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
696 gtk_notebook_append_page (notebook, self->page_incoming,
697 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
698 gtk_notebook_append_page (notebook, self->page_outgoing,
699 gtk_label_new (_("mcen_ti_advsetup_sending")));
701 GtkDialog *dialog = GTK_DIALOG (self);
702 gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
703 gtk_widget_show (GTK_WIDGET (notebook));
705 /* Add the buttons: */
706 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
707 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
709 /* Connect to the dialog's response signal: */
710 /* We use connect-before
711 * so we can stop the signal emission,
712 * to stop the default signal handler from closing the dialog.
714 g_signal_connect (G_OBJECT (self), "response",
715 G_CALLBACK (on_response), self);
718 ModestAccountSettingsDialog*
719 modest_account_settings_dialog_new (void)
721 return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
724 /** Update the UI with the stored account details, so they can be edited.
725 * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
727 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
732 /* Save the account name so we can refer to it later: */
733 if (dialog->account_name)
734 g_free (dialog->account_name);
735 dialog->account_name = g_strdup (account_name);
738 /* Get the account data for this account name: */
739 ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager,
742 g_printerr ("modest: failed to get account data for %s\n", account_name);
746 /* Save the account title so we can refer to it if the user changes it: */
747 if (dialog->original_account_title)
748 g_free (dialog->original_account_title);
749 dialog->original_account_title = g_strdup (account_data->display_name);
752 if (!(account_data->store_account)) {
753 g_printerr ("modest: account has no stores: %s\n", account_name);
757 /* Show the account data in the widgets: */
759 /* Note that we never show the non-display name in the UI.
760 * (Though the display name defaults to the non-display name at the start.) */
761 gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
762 account_data->display_name ? account_data->display_name : "");
764 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name),
765 account_data->fullname ? account_data->fullname : "");
766 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email),
767 account_data->email ? account_data->email : "");
769 const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
770 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
771 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
774 ModestServerAccountData *incoming_account = account_data->store_account;
775 if (incoming_account) {
776 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
777 incoming_account->username ? incoming_account->username : "");
778 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password),
779 incoming_account->password ? incoming_account->password : "");
781 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver),
782 incoming_account->hostname ? incoming_account->hostname : "");
784 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(incoming_account);
785 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth),
786 secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
788 update_incoming_server_title (dialog, incoming_account->proto);
789 update_incoming_server_security_choices (dialog, incoming_account->proto);
791 const ModestProtocol security = modest_server_account_data_get_option_security (incoming_account);
792 easysetup_serversecurity_combo_box_set_active_serversecurity (
793 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
797 ModestProtocol proto;
805 ModestServerAccountData *outgoing_account = account_data->transport_account;
806 if (outgoing_account) {
807 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver),
808 outgoing_account->hostname ? outgoing_account->hostname : "");
810 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username),
811 outgoing_account->username ? outgoing_account->username : "");
812 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password),
813 outgoing_account->password ? outgoing_account->password : "");
815 /* Get the secure-auth setting: */
816 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(outgoing_account);
817 easysetup_secureauth_combo_box_set_active_secureauth (
818 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
819 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
821 easysetup_serversecurity_combo_box_fill (
822 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
824 /* Get the security setting: */
825 const ModestProtocol security = modest_server_account_data_get_option_security (outgoing_account);
826 easysetup_serversecurity_combo_box_set_active_serversecurity (
827 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
829 /* TODO: set port. */
832 /* account_data->is_enabled, */
833 /*account_data->is_default, */
835 /* account_data->store_account->proto */
837 modest_account_mgr_free_account_data (dialog->account_manager, account_data);
841 save_configuration (ModestAccountSettingsDialog *dialog)
843 g_assert (dialog->account_name);
845 const gchar* account_name = dialog->account_name;
847 /* Set the account data from the widgets: */
848 const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
849 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
850 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
854 const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
855 test = modest_account_mgr_set_string (dialog->account_manager, account_name,
856 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
860 const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
861 test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
862 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
867 gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
868 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
869 g_assert (incoming_account_name);
871 const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
872 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
873 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
877 const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
878 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
879 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
883 const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
884 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
885 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
889 const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active
890 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))
891 ? MODEST_PROTOCOL_AUTH_PASSWORD
892 : MODEST_PROTOCOL_AUTH_NONE;
893 modest_server_account_set_option_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
895 const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
896 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
897 modest_server_account_set_option_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
899 g_free (incoming_account_name);
902 gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
903 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
904 g_assert (outgoing_account_name);
906 hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
907 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
908 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
912 username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
913 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
914 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
918 password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
919 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
920 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
924 const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
925 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
926 modest_server_account_set_option_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
928 const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
929 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
930 modest_server_account_set_option_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);
932 g_free (outgoing_account_name);
935 /* Set the changed account title last, to simplify the previous code: */
936 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
937 if ((!account_title) || (strlen(account_title) == 0))
938 return FALSE; /* Should be prevented already anyway. */
940 if (strcmp(account_title, account_name) != 0) {
941 /* Change the title: */
942 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
943 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
951 static gboolean entry_is_empty (GtkWidget *entry)
956 const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
957 if ((!text) || (strlen(text) == 0))
964 enable_buttons (ModestAccountSettingsDialog *self)
966 gboolean enable_ok = TRUE;
968 /* The account details title is mandatory: */
969 if (entry_is_empty(self->entry_account_title))
972 /* The user details username is mandatory: */
973 if (entry_is_empty(self->entry_user_username))
976 /* The user details email address is mandatory: */
977 if (enable_ok && entry_is_empty (self->entry_user_email))
980 /* The custom incoming server is mandatory: */
981 if (entry_is_empty(self->entry_incomingserver))
984 /* Enable the buttons,
985 * identifying them via their associated response codes:
987 GtkDialog *dialog_base = GTK_DIALOG (self);
988 gtk_dialog_set_response_sensitive (dialog_base,
994 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
996 GObjectClass *object_class = G_OBJECT_CLASS (klass);
997 g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1000 object_class->get_property = modest_account_settings_dialog_get_property;
1001 object_class->set_property = modest_account_settings_dialog_set_property;
1002 object_class->dispose = modest_account_settings_dialog_dispose;
1003 object_class->finalize = modest_account_settings_dialog_finalize;
1007 show_error (GtkWindow *parent_window, const gchar* text)
1009 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1015 gtk_dialog_run (dialog);
1016 gtk_widget_destroy (GTK_WIDGET (dialog));
1020 show_ok (GtkWindow *parent_window, const gchar* text)
1022 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1028 gtk_dialog_run (dialog);
1029 gtk_widget_destroy (GTK_WIDGET (dialog));