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->original_account_name)
90 g_free (self->original_account_name);
92 if (self->account_manager)
93 g_object_unref (G_OBJECT (self->account_manager));
95 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
99 show_error (GtkWindow *parent_window, const gchar* text);
102 show_ok (GtkWindow *parent_window, const gchar* text);
106 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
108 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
110 enable_buttons(self);
114 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
116 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
118 enable_buttons(self);
121 /** This is a convenience function to create a caption containing a mandatory widget.
122 * When the widget is edited, the enable_buttons() vfunc will be called.
124 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
129 HildonCaptionStatus flag)
131 GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
133 /* The translated strings seem to already contain the *,
134 * but this code can be used if that is not true in future.
137 /* Add a * character to indicate mandatory fields,
138 * as specified in our "Email UI Specification": */
139 if (flag == HILDON_CAPTION_MANDATORY) {
140 gchar* title = g_strdup_printf("%s*", value);
141 caption = hildon_caption_new (group, title, control, icon, flag);
145 caption = hildon_caption_new (group, value, control, icon, flag);
148 /* Connect to the appropriate changed signal for the widget,
149 * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
151 if (GTK_IS_ENTRY (control)) {
152 g_signal_connect (G_OBJECT (control), "changed",
153 G_CALLBACK (on_caption_entry_changed), self);
156 else if (GTK_IS_COMBO_BOX (control)) {
157 g_signal_connect (G_OBJECT (control), "changed",
158 G_CALLBACK (on_caption_combobox_changed), self);
165 create_page_account_details (ModestAccountSettingsDialog *self)
167 GtkWidget *box = gtk_vbox_new (FALSE, 2);
168 GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
169 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
170 gtk_widget_show (label);
172 /* Create a size group to be used by all captions.
173 * Note that HildonCaption does not create a default size group if we do not specify one.
174 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
175 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
177 /* The description widgets: */
178 self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
179 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"),
180 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
181 gtk_widget_show (self->entry_account_title);
182 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
183 gtk_widget_show (caption);
185 /* Prevent the use of some characters in the account title,
186 * as required by our UI specification: */
187 GList *list_prevent = NULL;
188 list_prevent = g_list_append (list_prevent, "\\");
189 list_prevent = g_list_append (list_prevent, "/");
190 list_prevent = g_list_append (list_prevent, ":");
191 list_prevent = g_list_append (list_prevent, "*");
192 list_prevent = g_list_append (list_prevent, "?");
193 list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
194 list_prevent = g_list_append (list_prevent, "“");
195 list_prevent = g_list_append (list_prevent, "<");
196 list_prevent = g_list_append (list_prevent, ">");
197 list_prevent = g_list_append (list_prevent, "|");
198 list_prevent = g_list_append (list_prevent, "^");
199 easysetup_validating_entry_set_unallowed_characters (
200 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
201 g_list_free (list_prevent);
203 /* Set max length as in the UI spec:
204 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
205 gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
207 /* TODO: The Retrieve and Limit Retrieve combo boxes. */
209 /* The leave-messages widgets: */
210 if(!self->checkbox_leave_messages)
211 self->checkbox_leave_messages =
212 gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
213 gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
214 gtk_widget_show (self->checkbox_leave_messages);
217 gtk_widget_show (GTK_WIDGET (box));
219 return GTK_WIDGET (box);
223 on_button_signature (GtkButton *button, gpointer user_data)
229 create_page_user_details (ModestAccountSettingsDialog *self)
231 GtkWidget *box = gtk_vbox_new (FALSE, 2);
233 /* Create a size group to be used by all captions.
234 * Note that HildonCaption does not create a default size group if we do not specify one.
235 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
236 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
238 /* The name widgets: */
239 self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
240 /* Set max length as in the UI spec:
241 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
242 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
243 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
244 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
245 gtk_widget_show (self->entry_user_name);
246 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
247 gtk_widget_show (caption);
249 /* Prevent the use of some characters in the name,
250 * as required by our UI specification: */
251 GList *list_prevent = NULL;
252 list_prevent = g_list_append (list_prevent, "<");
253 list_prevent = g_list_append (list_prevent, ">");
254 easysetup_validating_entry_set_unallowed_characters (
255 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
256 g_list_free (list_prevent);
258 /* The username widgets: */
259 self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
260 caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
261 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
262 gtk_widget_show (self->entry_user_username);
263 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
264 gtk_widget_show (caption);
266 /* Prevent the use of some characters in the username,
267 * as required by our UI specification: */
268 easysetup_validating_entry_set_unallowed_characters_whitespace (
269 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
271 /* Set max length as in the UI spec:
272 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
273 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
275 /* The password widgets: */
276 self->entry_user_password = gtk_entry_new ();
277 gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
278 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
279 caption = create_caption_new_with_asterix (self, sizegroup,
280 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
281 gtk_widget_show (self->entry_user_password);
282 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
283 gtk_widget_show (caption);
285 /* The email address widgets: */
286 self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
287 caption = create_caption_new_with_asterix (self, sizegroup,
288 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
289 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
290 gtk_widget_show (self->entry_user_email);
291 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
292 gtk_widget_show (caption);
294 /* Set max length as in the UI spec:
295 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
296 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
299 /* Signature button: */
300 if (!self->button_signature)
301 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
302 caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"),
303 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
304 gtk_widget_show (self->button_signature);
305 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
306 gtk_widget_show (caption);
308 g_signal_connect (G_OBJECT (self->button_signature), "clicked",
309 G_CALLBACK (on_button_signature), self);
311 gtk_widget_show (GTK_WIDGET (box));
313 return GTK_WIDGET (box);
316 /** Change the caption title for the incoming server,
317 * as specified in the UI spec:
319 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
322 (protocol == MODEST_PROTOCOL_STORE_POP ?
323 _("mail_fi_emailtype_pop3") :
324 _("mail_fi_emailtype_imap") );
327 /* Note that this produces a compiler warning,
328 * because the compiler does not know that the translated string will have a %s in it.
329 * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
330 gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
331 g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
332 g_free(incomingserver_title);
335 /** Change the caption title for the incoming server,
336 * as specified in the UI spec:
338 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
340 /* Fill the combo with appropriately titled choices for POP or IMAP. */
341 /* The choices are the same, but the titles are different, as in the UI spec. */
342 easysetup_serversecurity_combo_box_fill (
343 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
346 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
348 GtkWidget *box = gtk_vbox_new (FALSE, 2);
350 /* Create a size group to be used by all captions.
351 * Note that HildonCaption does not create a default size group if we do not specify one.
352 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
353 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
355 /* The incoming server widgets: */
356 if(!self->entry_incomingserver)
357 self->entry_incomingserver = gtk_entry_new ();
359 if (self->caption_incoming)
360 gtk_widget_destroy (self->caption_incoming);
362 /* The caption title will be updated in update_incoming_server_title().
363 * so this default text will never be seen: */
364 /* (Note: Changing the title seems pointless. murrayc) */
365 self->caption_incoming = create_caption_new_with_asterix (self, sizegroup,
366 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
367 gtk_widget_show (self->entry_incomingserver);
368 gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
369 gtk_widget_show (self->caption_incoming);
371 /* The secure connection widgets: */
372 /* This will be filled by update_incoming_server_security_choices(). */
373 if (!self->combo_incoming_security)
374 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
375 GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
376 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
377 gtk_widget_show (self->combo_incoming_security);
378 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
379 gtk_widget_show (caption);
381 /* The port widgets: */
382 /* TODO: There are various rules about this in the UI spec. */
383 if (!self->entry_incoming_port)
384 self->entry_incoming_port = GTK_WIDGET (gtk_entry_new ());
385 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
386 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
387 gtk_widget_show (self->entry_incoming_port);
388 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
389 gtk_widget_show (caption);
391 /* The secure authentication widgets: */
392 if(!self->checkbox_incoming_auth)
393 self->checkbox_incoming_auth =
394 gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
395 gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
396 gtk_widget_show (self->checkbox_incoming_auth);
398 gtk_widget_show (GTK_WIDGET (box));
400 return GTK_WIDGET (box);
404 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
406 GtkWidget *widget = GTK_WIDGET (user_data);
408 /* Enable the widget only if the toggle button is active: */
409 const gboolean enable = gtk_toggle_button_get_active (togglebutton);
410 gtk_widget_set_sensitive (widget, enable);
413 /* Make the sensitivity of a widget depend on a toggle button.
416 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
418 g_signal_connect (G_OBJECT (button), "toggled",
419 G_CALLBACK (on_toggle_button_changed), widget);
421 /* Set the starting sensitivity: */
422 on_toggle_button_changed (button, widget);
426 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
428 /* Destroy the window when it is closed: */
429 gtk_widget_destroy (GTK_WIDGET (window));
433 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
436 ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
438 /* Show the window: */
439 /* TODO: Retrieve the chosen settings,
440 * so we can supply them when creating the connection somehow.
442 GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
443 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
444 g_signal_connect (G_OBJECT (window), "hide",
445 G_CALLBACK (on_smtp_servers_window_hide), self);
446 gtk_widget_show (window);
450 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
452 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
454 ModestProtocol protocol_security =
455 easysetup_secureauth_combo_box_get_active_secureauth (
456 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
457 const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
459 gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
460 gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
463 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
465 GtkWidget *box = gtk_vbox_new (FALSE, 2);
467 /* Create a size group to be used by all captions.
468 * Note that HildonCaption does not create a default size group if we do not specify one.
469 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
470 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
472 /* The outgoing server widgets: */
473 if (!self->entry_outgoingserver)
474 self->entry_outgoingserver = gtk_entry_new ();
475 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
476 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
477 gtk_widget_show (self->entry_outgoingserver);
478 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
479 gtk_widget_show (caption);
481 /* The secure authentication widgets: */
482 if (!self->combo_outgoing_auth)
483 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
484 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
485 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
486 gtk_widget_show (self->combo_outgoing_auth);
487 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
488 gtk_widget_show (caption);
490 /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
491 g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
493 /* The username widgets: */
494 self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
495 self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
496 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
497 gtk_widget_show (self->entry_outgoing_username);
498 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
499 gtk_widget_show (self->caption_outgoing_username);
501 /* Prevent the use of some characters in the username,
502 * as required by our UI specification: */
503 easysetup_validating_entry_set_unallowed_characters_whitespace (
504 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
506 /* Set max length as in the UI spec:
507 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
508 gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
510 /* The password widgets: */
511 self->entry_outgoing_password = gtk_entry_new ();
512 gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
513 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
514 self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup,
515 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
516 gtk_widget_show (self->entry_outgoing_password);
517 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
518 gtk_widget_show (self->caption_outgoing_password);
520 /* The secure connection widgets: */
521 /* This will be filled and set with easysetup_serversecurity_combo_box_fill()
522 * and easysetup_serversecurity_combo_box_set_active_serversecurity().
524 if (!self->combo_outgoing_security)
525 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
526 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
527 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
528 gtk_widget_show (self->combo_outgoing_security);
529 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
530 gtk_widget_show (caption);
532 /* The port widgets: */
533 if (!self->entry_outgoing_port)
534 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
535 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
536 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
537 gtk_widget_show (self->entry_outgoing_port);
538 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
539 gtk_widget_show (caption);
541 /* connection-specific checkbox: */
542 if (!self->checkbox_outgoing_smtp_specific) {
543 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
544 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific),
547 gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
548 gtk_widget_show (self->checkbox_outgoing_smtp_specific);
550 /* Connection-specific SMTP-Severs Edit button: */
551 if (!self->button_outgoing_smtp_servers)
552 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
553 caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"),
554 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
555 gtk_widget_show (self->button_outgoing_smtp_servers);
556 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
557 gtk_widget_show (caption);
559 /* Only enable the button when the checkbox is checked: */
560 enable_widget_for_togglebutton (self->button_outgoing_smtp_servers,
561 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
563 g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
564 G_CALLBACK (on_button_outgoing_smtp_servers), self);
567 gtk_widget_show (GTK_WIDGET (box));
569 return GTK_WIDGET (box);
573 check_data (ModestAccountSettingsDialog *self)
575 /* Check that the title is not already in use: */
576 const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
577 if ((!account_name) || (strlen(account_name) == 0))
578 return FALSE; /* Should be prevented already anyway. */
580 if (strcmp(account_name, self->original_account_name) != 0) {
581 /* Check the changed title: */
582 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (self->account_manager,
586 /* Warn the user via a dialog: */
587 show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
593 /* Check that the email address is valud: */
594 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
595 if ((!email_address) || (strlen(email_address) == 0))
598 if (!modest_text_utils_validate_email_address (email_address)) {
599 /* Warn the user via a dialog: */
600 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
602 /* Return focus to the email address entry: */
603 gtk_widget_grab_focus (self->entry_user_email);
608 /* TODO: The UI Spec wants us to check that the servernames are valid,
609 * but does not specify how.
615 on_response (GtkDialog *wizard_dialog,
619 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
620 enable_buttons (self);
622 /* TODO: Prevent the OK response if the data is invalid. */
624 gboolean prevent_response = FALSE;
626 /* Warn about unsaved changes: */
627 /* TODO: Actually detect whether changes were made. */
628 if (response_id == GTK_RESPONSE_CANCEL) {
629 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
632 GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
633 _("imum_nc_wizard_confirm_lose_changes") ));
635 const gint dialog_response = gtk_dialog_run (dialog);
636 gtk_widget_destroy (GTK_WIDGET (dialog));
638 if (dialog_response != GTK_RESPONSE_OK)
639 prevent_response = TRUE;
641 /* Check for invalid input: */
642 else if (!check_data (self)) {
643 prevent_response = TRUE;
646 if (prevent_response) {
647 /* This is a nasty hack. murrayc. */
648 /* Don't let the dialog close */
649 g_signal_stop_emission_by_name (wizard_dialog, "response");
654 if (response_id == GTK_RESPONSE_OK) {
655 /* Try to save the changes: */
656 const gboolean saved = save_configuration (self);
658 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
660 show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
665 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
667 /* Create the notebook to be used by the GtkDialog base class:
668 * Each page of the notebook will be a page of the wizard: */
669 GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
672 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
674 /* Get the account manager object,
675 * so we can check for existing accounts,
676 * and create new accounts: */
677 self->account_manager = modest_runtime_get_account_mgr ();
678 g_assert (self->account_manager);
679 g_object_ref (self->account_manager);
681 /* Create the common pages,
683 self->page_account_details = create_page_account_details (self);
684 self->page_user_details = create_page_user_details (self);
685 self->page_incoming = create_page_incoming (self);
686 self->page_outgoing = create_page_outgoing (self);
688 /* Add the notebook pages: */
689 gtk_notebook_append_page (notebook, self->page_account_details,
690 gtk_label_new (_("mcen_ti_account_settings_account")));
691 gtk_notebook_append_page (notebook, self->page_user_details,
692 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
693 gtk_notebook_append_page (notebook, self->page_incoming,
694 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
695 gtk_notebook_append_page (notebook, self->page_outgoing,
696 gtk_label_new (_("mcen_ti_advsetup_sending")));
698 GtkDialog *dialog = GTK_DIALOG (self);
699 gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
700 gtk_widget_show (GTK_WIDGET (notebook));
702 /* Add the buttons: */
703 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
704 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
706 /* Connect to the dialog's response signal: */
707 /* We use connect-before
708 * so we can stop the signal emission,
709 * to stop the default signal handler from closing the dialog.
711 g_signal_connect (G_OBJECT (self), "response",
712 G_CALLBACK (on_response), self);
715 ModestAccountSettingsDialog*
716 modest_account_settings_dialog_new (void)
718 return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
721 /** Update the UI with the stored account details, so they can be edited.
722 * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
724 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
729 /* Save the account name so we can refer to it if the user changes it: */
730 if (dialog->original_account_name)
731 g_free (dialog->original_account_name);
732 dialog->original_account_name = g_strdup (account_name);
734 /* Get the account data for this account name: */
735 ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager,
738 g_printerr ("modest: failed to get account data for %s\n", account_name);
742 if (!(account_data->store_account)) {
743 g_printerr ("modest: account has no stores: %s\n", account_name);
747 /* Show the account data in the widgets: */
749 /* Note that we never show the non-display name in the UI.
750 * (Though the display name defaults to the non-display name at the start.) */
751 gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
752 account_data->display_name ? account_data->display_name : "");
754 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name),
755 account_data->fullname ? account_data->fullname : "");
756 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email),
757 account_data->email ? account_data->email : "");
759 const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
760 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
761 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
764 ModestServerAccountData *incoming_account = account_data->store_account;
765 if (incoming_account) {
766 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
767 incoming_account->username ? incoming_account->username : "");
768 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password),
769 incoming_account->password ? incoming_account->password : "");
771 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver),
772 incoming_account->hostname ? incoming_account->hostname : "");
774 easysetup_serversecurity_combo_box_set_active_serversecurity (
775 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), incoming_account->proto);
776 update_incoming_server_title (dialog, incoming_account->proto);
777 update_incoming_server_security_choices (dialog, incoming_account->proto);
781 ModestProtocol proto;
789 ModestServerAccountData *outgoing_account = account_data->transport_account;
790 if (outgoing_account) {
791 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver),
792 outgoing_account->hostname ? outgoing_account->hostname : "");
794 /* TODO: Dim these if secure authentication is None, as per the UI spec: */
795 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username),
796 outgoing_account->username ? outgoing_account->username : "");
797 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password),
798 outgoing_account->password ? outgoing_account->password : "");
800 /* TODO: How do we get the auth setting from the server account struct?: */
801 /* This seems to be in ->options, with hard-coded option names.
802 * This will need new API in ModestAccountMgr. */
803 easysetup_secureauth_combo_box_set_active_secureauth (
804 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), MODEST_PROTOCOL_AUTH_NONE);
805 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
807 easysetup_serversecurity_combo_box_fill (
808 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
812 /* TODO: set port. */
815 /* TODO: account_data->display_name */
816 /* account_data->is_enabled, */
817 /*account_data->is_default, */
819 /* account_data->store_account->proto */
821 modest_account_mgr_free_account_data (dialog->account_manager, account_data);
825 save_configuration (ModestAccountSettingsDialog *dialog)
827 g_assert (dialog->original_account_name);
829 const gchar* account_name = dialog->original_account_name;
831 /* Set the account data from the widgets: */
832 const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
833 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
834 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
838 const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
839 test = modest_account_mgr_set_string (dialog->account_manager, account_name,
840 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
844 /* TODO: Change name: */
845 /* Possibly the account name may never change, but that should be hidden,
846 * and the display name may change, defaulting to the account name.
849 const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
850 test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
851 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
856 gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
857 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
858 g_assert (incoming_account_name);
860 const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
861 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
862 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
866 const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
867 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
868 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
872 const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
873 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
874 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
878 /* TODO: How can we set these in the server account?:
879 ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth))
880 ? MODEST_PROTOCOL_AUTH_PASSWORD
881 : MODEST_PROTOCOL_AUTH_NONE;
883 ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
884 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
888 g_free (incoming_account_name);
891 gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
892 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
893 g_assert (outgoing_account_name);
895 hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
896 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
897 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
901 username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
902 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
903 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
907 password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
908 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
909 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
913 /* TODO: How do we set these in the account data?:
914 ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
915 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
917 ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
918 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
921 g_free (outgoing_account_name);
924 /* Set the changed account title last, to simplify the previous code: */
925 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
926 if ((!account_title) || (strlen(account_title) == 0))
927 return FALSE; /* Should be prevented already anyway. */
929 if (strcmp(account_title, account_name) != 0) {
930 /* Change the title: */
931 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
932 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
940 static gboolean entry_is_empty (GtkWidget *entry)
945 const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
946 if ((!text) || (strlen(text) == 0))
953 enable_buttons (ModestAccountSettingsDialog *self)
955 gboolean enable_ok = TRUE;
957 /* The account details title is mandatory: */
958 if (entry_is_empty(self->entry_account_title))
961 /* The user details username is mandatory: */
962 if (entry_is_empty(self->entry_user_username))
965 /* The user details email address is mandatory: */
966 if (enable_ok && entry_is_empty (self->entry_user_email))
969 /* The custom incoming server is mandatory: */
970 if (entry_is_empty(self->entry_incomingserver))
973 /* Enable the buttons,
974 * identifying them via their associated response codes:
976 GtkDialog *dialog_base = GTK_DIALOG (self);
977 gtk_dialog_set_response_sensitive (dialog_base,
983 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
985 GObjectClass *object_class = G_OBJECT_CLASS (klass);
986 g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
989 object_class->get_property = modest_account_settings_dialog_get_property;
990 object_class->set_property = modest_account_settings_dialog_set_property;
991 object_class->dispose = modest_account_settings_dialog_dispose;
992 object_class->finalize = modest_account_settings_dialog_finalize;
996 show_error (GtkWindow *parent_window, const gchar* text)
998 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1004 gtk_dialog_run (dialog);
1005 gtk_widget_destroy (GTK_WIDGET (dialog));
1009 show_ok (GtkWindow *parent_window, const gchar* text)
1011 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1017 gtk_dialog_run (dialog);
1018 gtk_widget_destroy (GTK_WIDGET (dialog));