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);
108 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
111 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
115 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
117 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
119 enable_buttons(self);
123 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
125 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
127 enable_buttons(self);
130 /** This is a convenience function to create a caption containing a mandatory widget.
131 * When the widget is edited, the enable_buttons() vfunc will be called.
133 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
138 HildonCaptionStatus flag)
140 GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
142 /* The translated strings seem to already contain the *,
143 * but this code can be used if that is not true in future.
146 /* Add a * character to indicate mandatory fields,
147 * as specified in our "Email UI Specification": */
148 if (flag == HILDON_CAPTION_MANDATORY) {
149 gchar* title = g_strdup_printf("%s*", value);
150 caption = hildon_caption_new (group, title, control, icon, flag);
154 caption = hildon_caption_new (group, value, control, icon, flag);
157 /* Connect to the appropriate changed signal for the widget,
158 * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
160 if (GTK_IS_ENTRY (control)) {
161 g_signal_connect (G_OBJECT (control), "changed",
162 G_CALLBACK (on_caption_entry_changed), self);
165 else if (GTK_IS_COMBO_BOX (control)) {
166 g_signal_connect (G_OBJECT (control), "changed",
167 G_CALLBACK (on_caption_combobox_changed), self);
174 create_page_account_details (ModestAccountSettingsDialog *self)
176 GtkWidget *box = gtk_vbox_new (FALSE, 2);
177 GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
178 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
179 gtk_widget_show (label);
181 /* Create a size group to be used by all captions.
182 * Note that HildonCaption does not create a default size group if we do not specify one.
183 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
184 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
186 /* The description widgets: */
187 self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
188 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"),
189 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
190 gtk_widget_show (self->entry_account_title);
191 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
192 gtk_widget_show (caption);
194 /* Prevent the use of some characters in the account title,
195 * as required by our UI specification: */
196 GList *list_prevent = NULL;
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 list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
203 list_prevent = g_list_append (list_prevent, "“");
204 list_prevent = g_list_append (list_prevent, "<");
205 list_prevent = g_list_append (list_prevent, ">");
206 list_prevent = g_list_append (list_prevent, "|");
207 list_prevent = g_list_append (list_prevent, "^");
208 easysetup_validating_entry_set_unallowed_characters (
209 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
210 g_list_free (list_prevent);
212 /* Set max length as in the UI spec:
213 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
214 gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
216 /* TODO: The Retrieve and Limit Retrieve combo boxes. */
218 /* The leave-messages widgets: */
219 if(!self->checkbox_leave_messages)
220 self->checkbox_leave_messages =
221 gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
222 gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
223 gtk_widget_show (self->checkbox_leave_messages);
226 gtk_widget_show (GTK_WIDGET (box));
228 return GTK_WIDGET (box);
232 on_button_signature (GtkButton *button, gpointer user_data)
238 create_page_user_details (ModestAccountSettingsDialog *self)
240 GtkWidget *box = gtk_vbox_new (FALSE, 2);
242 /* Create a size group to be used by all captions.
243 * Note that HildonCaption does not create a default size group if we do not specify one.
244 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
245 GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
247 /* The name widgets: */
248 self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
249 /* Set max length as in the UI spec:
250 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
251 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
252 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
253 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
254 gtk_widget_show (self->entry_user_name);
255 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
256 gtk_widget_show (caption);
258 /* Prevent the use of some characters in the name,
259 * as required by our UI specification: */
260 GList *list_prevent = NULL;
261 list_prevent = g_list_append (list_prevent, "<");
262 list_prevent = g_list_append (list_prevent, ">");
263 easysetup_validating_entry_set_unallowed_characters (
264 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
265 g_list_free (list_prevent);
267 /* The username widgets: */
268 self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
269 caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
270 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
271 gtk_widget_show (self->entry_user_username);
272 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
273 gtk_widget_show (caption);
275 /* Prevent the use of some characters in the username,
276 * as required by our UI specification: */
277 easysetup_validating_entry_set_unallowed_characters_whitespace (
278 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
280 /* Set max length as in the UI spec:
281 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
282 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
284 /* The password widgets: */
285 self->entry_user_password = gtk_entry_new ();
286 gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
287 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
288 caption = create_caption_new_with_asterix (self, sizegroup,
289 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
290 gtk_widget_show (self->entry_user_password);
291 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
292 gtk_widget_show (caption);
294 /* The email address widgets: */
295 self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
296 caption = create_caption_new_with_asterix (self, sizegroup,
297 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
298 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
299 gtk_widget_show (self->entry_user_email);
300 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
301 gtk_widget_show (caption);
303 /* Set max length as in the UI spec:
304 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
305 gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
308 /* Signature button: */
309 if (!self->button_signature)
310 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
311 caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"),
312 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
313 gtk_widget_show (self->button_signature);
314 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
315 gtk_widget_show (caption);
317 g_signal_connect (G_OBJECT (self->button_signature), "clicked",
318 G_CALLBACK (on_button_signature), self);
320 gtk_widget_show (GTK_WIDGET (box));
322 return GTK_WIDGET (box);
325 /** Change the caption title for the incoming server,
326 * as specified in the UI spec:
328 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
331 (protocol == MODEST_PROTOCOL_STORE_POP ?
332 _("mail_fi_emailtype_pop3") :
333 _("mail_fi_emailtype_imap") );
336 /* Note that this produces a compiler warning,
337 * because the compiler does not know that the translated string will have a %s in it.
338 * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
339 gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
340 g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
341 g_free(incomingserver_title);
344 /** Change the caption title for the incoming server,
345 * as specified in the UI spec:
347 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
349 /* Fill the combo with appropriately titled choices for POP or IMAP. */
350 /* The choices are the same, but the titles are different, as in the UI spec. */
351 easysetup_serversecurity_combo_box_fill (
352 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
355 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
357 GtkWidget *box = gtk_vbox_new (FALSE, 2);
359 /* Create a size group to be used by all captions.
360 * Note that HildonCaption does not create a default size group if we do not specify one.
361 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
362 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
364 /* The incoming server widgets: */
365 if(!self->entry_incomingserver)
366 self->entry_incomingserver = gtk_entry_new ();
368 if (self->caption_incoming)
369 gtk_widget_destroy (self->caption_incoming);
371 /* The caption title will be updated in update_incoming_server_title().
372 * so this default text will never be seen: */
373 /* (Note: Changing the title seems pointless. murrayc) */
374 self->caption_incoming = create_caption_new_with_asterix (self, sizegroup,
375 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
376 gtk_widget_show (self->entry_incomingserver);
377 gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
378 gtk_widget_show (self->caption_incoming);
380 /* The secure connection widgets: */
381 /* This will be filled by update_incoming_server_security_choices(). */
382 if (!self->combo_incoming_security)
383 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
384 GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
385 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
386 gtk_widget_show (self->combo_incoming_security);
387 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
388 gtk_widget_show (caption);
390 /* Show a default port number when the security method changes, as per the UI spec: */
391 g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
394 /* The port widgets: */
395 /* TODO: There are various rules about this in the UI spec. */
396 if (!self->entry_incoming_port)
397 self->entry_incoming_port = GTK_WIDGET (gtk_entry_new ());
398 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
399 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
400 gtk_widget_show (self->entry_incoming_port);
401 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
402 gtk_widget_show (caption);
404 /* The secure authentication widgets: */
405 if(!self->checkbox_incoming_auth)
406 self->checkbox_incoming_auth =
407 gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
408 gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
409 gtk_widget_show (self->checkbox_incoming_auth);
411 gtk_widget_show (GTK_WIDGET (box));
413 return GTK_WIDGET (box);
417 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
419 GtkWidget *widget = GTK_WIDGET (user_data);
421 /* Enable the widget only if the toggle button is active: */
422 const gboolean enable = gtk_toggle_button_get_active (togglebutton);
423 gtk_widget_set_sensitive (widget, enable);
426 /* Make the sensitivity of a widget depend on a toggle button.
429 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
431 g_signal_connect (G_OBJECT (button), "toggled",
432 G_CALLBACK (on_toggle_button_changed), widget);
434 /* Set the starting sensitivity: */
435 on_toggle_button_changed (button, widget);
439 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
441 /* Destroy the window when it is closed: */
442 gtk_widget_destroy (GTK_WIDGET (window));
446 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
449 ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
451 /* Show the window: */
452 /* TODO: Retrieve the chosen settings,
453 * so we can supply them when creating the connection somehow.
455 GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
456 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
457 g_signal_connect (G_OBJECT (window), "hide",
458 G_CALLBACK (on_smtp_servers_window_hide), self);
459 gtk_widget_show (window);
463 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
465 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
467 ModestProtocol protocol_security =
468 easysetup_secureauth_combo_box_get_active_secureauth (
469 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
470 const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
472 gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
473 gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
477 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
479 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
481 const gint port_number =
482 easysetup_serversecurity_combo_box_get_active_serversecurity_port (
483 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
485 if(port_number != 0) {
486 gchar* str = g_strdup_printf ("%d", port_number);
487 gtk_entry_set_text (GTK_ENTRY (self->entry_outgoing_port), str);
493 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
495 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
497 const gint port_number =
498 easysetup_serversecurity_combo_box_get_active_serversecurity_port (
499 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
501 if(port_number != 0) {
502 gchar* str = g_strdup_printf ("%d", port_number);
503 gtk_entry_set_text (GTK_ENTRY (self->entry_incoming_port), str);
509 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
511 GtkWidget *box = gtk_vbox_new (FALSE, 2);
513 /* Create a size group to be used by all captions.
514 * Note that HildonCaption does not create a default size group if we do not specify one.
515 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
516 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
518 /* The outgoing server widgets: */
519 if (!self->entry_outgoingserver)
520 self->entry_outgoingserver = gtk_entry_new ();
521 GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup,
522 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
523 gtk_widget_show (self->entry_outgoingserver);
524 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
525 gtk_widget_show (caption);
527 /* The secure authentication widgets: */
528 if (!self->combo_outgoing_auth)
529 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
530 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
531 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
532 gtk_widget_show (self->combo_outgoing_auth);
533 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
534 gtk_widget_show (caption);
536 /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
537 g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
539 /* The username widgets: */
540 self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
541 self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"),
542 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
543 gtk_widget_show (self->entry_outgoing_username);
544 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
545 gtk_widget_show (self->caption_outgoing_username);
547 /* Prevent the use of some characters in the username,
548 * as required by our UI specification: */
549 easysetup_validating_entry_set_unallowed_characters_whitespace (
550 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
552 /* Set max length as in the UI spec:
553 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
554 gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
556 /* The password widgets: */
557 self->entry_outgoing_password = gtk_entry_new ();
558 gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
559 /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
560 self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup,
561 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
562 gtk_widget_show (self->entry_outgoing_password);
563 gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
564 gtk_widget_show (self->caption_outgoing_password);
566 /* The secure connection widgets: */
567 /* This will be filled and set with easysetup_serversecurity_combo_box_fill()
568 * and easysetup_serversecurity_combo_box_set_active_serversecurity().
570 if (!self->combo_outgoing_security)
571 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
572 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
573 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
574 gtk_widget_show (self->combo_outgoing_security);
575 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
576 gtk_widget_show (caption);
578 /* Show a default port number when the security method changes, as per the UI spec: */
579 g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
581 /* The port widgets: */
582 if (!self->entry_outgoing_port)
583 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
584 caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"),
585 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
586 gtk_widget_show (self->entry_outgoing_port);
587 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
588 gtk_widget_show (caption);
590 /* connection-specific checkbox: */
591 if (!self->checkbox_outgoing_smtp_specific) {
592 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
593 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific),
596 gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
597 gtk_widget_show (self->checkbox_outgoing_smtp_specific);
599 /* Connection-specific SMTP-Severs Edit button: */
600 if (!self->button_outgoing_smtp_servers)
601 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
602 caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"),
603 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
604 gtk_widget_show (self->button_outgoing_smtp_servers);
605 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
606 gtk_widget_show (caption);
608 /* Only enable the button when the checkbox is checked: */
609 enable_widget_for_togglebutton (self->button_outgoing_smtp_servers,
610 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
612 g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
613 G_CALLBACK (on_button_outgoing_smtp_servers), self);
616 gtk_widget_show (GTK_WIDGET (box));
618 return GTK_WIDGET (box);
622 check_data (ModestAccountSettingsDialog *self)
624 /* Check that the title is not already in use: */
625 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
626 if ((!account_title) || (strlen(account_title) == 0))
627 return FALSE; /* Should be prevented already anyway. */
629 if (strcmp(account_title, self->original_account_title) != 0) {
630 /* Check the changed title: */
631 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (self->account_manager,
635 /* Warn the user via a dialog: */
636 show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
642 /* Check that the email address is valud: */
643 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
644 if ((!email_address) || (strlen(email_address) == 0))
647 if (!modest_text_utils_validate_email_address (email_address)) {
648 /* Warn the user via a dialog: */
649 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
651 /* Return focus to the email address entry: */
652 gtk_widget_grab_focus (self->entry_user_email);
657 /* TODO: The UI Spec wants us to check that the servernames are valid,
658 * but does not specify how.
666 on_response (GtkDialog *wizard_dialog,
670 ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
671 enable_buttons (self);
673 gboolean prevent_response = FALSE;
675 /* Warn about unsaved changes: */
676 /* TODO: Actually detect whether changes were made. */
677 if (response_id == GTK_RESPONSE_CANCEL) {
678 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
681 GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
682 _("imum_nc_wizard_confirm_lose_changes") ));
684 const gint dialog_response = gtk_dialog_run (dialog);
685 gtk_widget_destroy (GTK_WIDGET (dialog));
687 if (dialog_response != GTK_RESPONSE_OK)
688 prevent_response = TRUE;
690 /* Check for invalid input: */
691 else if (!check_data (self)) {
692 prevent_response = TRUE;
695 if (prevent_response) {
696 /* This is a nasty hack. murrayc. */
697 /* Don't let the dialog close */
698 g_signal_stop_emission_by_name (wizard_dialog, "response");
703 if (response_id == GTK_RESPONSE_OK) {
704 /* Try to save the changes: */
705 const gboolean saved = save_configuration (self);
707 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
709 show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
714 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
716 /* Create the notebook to be used by the GtkDialog base class:
717 * Each page of the notebook will be a page of the wizard: */
718 GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
721 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
723 /* Get the account manager object,
724 * so we can check for existing accounts,
725 * and create new accounts: */
726 self->account_manager = modest_runtime_get_account_mgr ();
727 g_assert (self->account_manager);
728 g_object_ref (self->account_manager);
730 /* Create the common pages,
732 self->page_account_details = create_page_account_details (self);
733 self->page_user_details = create_page_user_details (self);
734 self->page_incoming = create_page_incoming (self);
735 self->page_outgoing = create_page_outgoing (self);
737 /* Add the notebook pages: */
738 gtk_notebook_append_page (notebook, self->page_account_details,
739 gtk_label_new (_("mcen_ti_account_settings_account")));
740 gtk_notebook_append_page (notebook, self->page_user_details,
741 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
742 gtk_notebook_append_page (notebook, self->page_incoming,
743 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
744 gtk_notebook_append_page (notebook, self->page_outgoing,
745 gtk_label_new (_("mcen_ti_advsetup_sending")));
747 GtkDialog *dialog = GTK_DIALOG (self);
748 gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
749 gtk_widget_show (GTK_WIDGET (notebook));
751 /* Add the buttons: */
752 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
753 gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
755 /* Connect to the dialog's response signal: */
756 /* We use connect-before
757 * so we can stop the signal emission,
758 * to stop the default signal handler from closing the dialog.
760 g_signal_connect (G_OBJECT (self), "response",
761 G_CALLBACK (on_response), self);
764 ModestAccountSettingsDialog*
765 modest_account_settings_dialog_new (void)
767 return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
770 /** Update the UI with the stored account details, so they can be edited.
771 * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
773 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
778 /* Save the account name so we can refer to it later: */
779 if (dialog->account_name)
780 g_free (dialog->account_name);
781 dialog->account_name = g_strdup (account_name);
784 /* Get the account data for this account name: */
785 ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager,
788 g_printerr ("modest: failed to get account data for %s\n", account_name);
792 /* Save the account title so we can refer to it if the user changes it: */
793 if (dialog->original_account_title)
794 g_free (dialog->original_account_title);
795 dialog->original_account_title = g_strdup (account_data->display_name);
798 if (!(account_data->store_account)) {
799 g_printerr ("modest: account has no stores: %s\n", account_name);
803 /* Show the account data in the widgets: */
805 /* Note that we never show the non-display name in the UI.
806 * (Though the display name defaults to the non-display name at the start.) */
807 gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
808 account_data->display_name ? account_data->display_name : "");
810 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name),
811 account_data->fullname ? account_data->fullname : "");
812 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email),
813 account_data->email ? account_data->email : "");
815 const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
816 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
817 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
819 ModestServerAccountData *incoming_account = account_data->store_account;
820 if (incoming_account) {
821 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
822 incoming_account->username ? incoming_account->username : "");
823 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password),
824 incoming_account->password ? incoming_account->password : "");
826 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver),
827 incoming_account->hostname ? incoming_account->hostname : "");
829 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(incoming_account);
830 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth),
831 secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
833 update_incoming_server_title (dialog, incoming_account->proto);
834 update_incoming_server_security_choices (dialog, incoming_account->proto);
836 const ModestProtocol security = modest_server_account_data_get_option_security (incoming_account);
837 easysetup_serversecurity_combo_box_set_active_serversecurity (
838 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
840 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
841 MODEST_ACCOUNT_PORT, TRUE /* server account */);
842 gchar *port_str = g_strdup_printf ("%d", port_num);
843 gtk_entry_set_text (GTK_ENTRY (dialog->entry_incoming_port), port_str);
848 ModestProtocol proto;
856 ModestServerAccountData *outgoing_account = account_data->transport_account;
857 if (outgoing_account) {
858 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver),
859 outgoing_account->hostname ? outgoing_account->hostname : "");
861 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username),
862 outgoing_account->username ? outgoing_account->username : "");
863 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password),
864 outgoing_account->password ? outgoing_account->password : "");
866 /* Get the secure-auth setting: */
867 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(outgoing_account);
868 easysetup_secureauth_combo_box_set_active_secureauth (
869 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
870 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
872 easysetup_serversecurity_combo_box_fill (
873 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
875 /* Get the security setting: */
876 const ModestProtocol security = modest_server_account_data_get_option_security (outgoing_account);
877 easysetup_serversecurity_combo_box_set_active_serversecurity (
878 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
880 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
881 MODEST_ACCOUNT_PORT, TRUE /* server account */);
882 gchar *port_str = g_strdup_printf ("%d", port_num);
883 gtk_entry_set_text (GTK_ENTRY (dialog->entry_outgoing_port), port_str);
887 /* account_data->is_enabled, */
888 /*account_data->is_default, */
890 /* account_data->store_account->proto */
892 modest_account_mgr_free_account_data (dialog->account_manager, account_data);
896 save_configuration (ModestAccountSettingsDialog *dialog)
898 g_assert (dialog->account_name);
900 const gchar* account_name = dialog->account_name;
902 /* Set the account data from the widgets: */
903 const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
904 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
905 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
909 const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
910 test = modest_account_mgr_set_string (dialog->account_manager, account_name,
911 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
915 const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
916 test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
917 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
922 gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
923 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
924 g_assert (incoming_account_name);
926 const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
927 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
928 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
932 const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
933 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
934 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
938 const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
939 test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
940 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
944 const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active
945 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))
946 ? MODEST_PROTOCOL_AUTH_PASSWORD
947 : MODEST_PROTOCOL_AUTH_NONE;
948 modest_server_account_set_option_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
950 const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
951 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
952 modest_server_account_set_option_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
955 const gchar* port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incoming_port));
958 port_num = atoi (port_str);
959 modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
960 MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
962 g_free (incoming_account_name);
965 gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
966 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
967 g_assert (outgoing_account_name);
969 hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
970 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
971 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
975 username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
976 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
977 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
981 password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
982 test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
983 MODEST_ACCOUNT_PASSWORD, password, TRUE /* server account */);
987 const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
988 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
989 modest_server_account_set_option_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
991 const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
992 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
993 modest_server_account_set_option_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);
996 port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_port));
999 port_num = atoi (port_str);
1000 modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1001 MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1003 g_free (outgoing_account_name);
1006 /* Set the changed account title last, to simplify the previous code: */
1007 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1008 if ((!account_title) || (strlen(account_title) == 0))
1009 return FALSE; /* Should be prevented already anyway. */
1011 if (strcmp(account_title, account_name) != 0) {
1012 /* Change the title: */
1013 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1014 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1022 static gboolean entry_is_empty (GtkWidget *entry)
1027 const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1028 if ((!text) || (strlen(text) == 0))
1035 enable_buttons (ModestAccountSettingsDialog *self)
1037 gboolean enable_ok = TRUE;
1039 /* The account details title is mandatory: */
1040 if (entry_is_empty(self->entry_account_title))
1043 /* The user details username is mandatory: */
1044 if (entry_is_empty(self->entry_user_username))
1047 /* The user details email address is mandatory: */
1048 if (enable_ok && entry_is_empty (self->entry_user_email))
1051 /* The custom incoming server is mandatory: */
1052 if (entry_is_empty(self->entry_incomingserver))
1055 /* Enable the buttons,
1056 * identifying them via their associated response codes:
1058 GtkDialog *dialog_base = GTK_DIALOG (self);
1059 gtk_dialog_set_response_sensitive (dialog_base,
1065 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1067 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1068 g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1071 object_class->get_property = modest_account_settings_dialog_get_property;
1072 object_class->set_property = modest_account_settings_dialog_set_property;
1073 object_class->dispose = modest_account_settings_dialog_dispose;
1074 object_class->finalize = modest_account_settings_dialog_finalize;
1078 show_error (GtkWindow *parent_window, const gchar* text)
1080 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1086 gtk_dialog_run (dialog);
1087 gtk_widget_destroy (GTK_WIDGET (dialog));
1091 show_ok (GtkWindow *parent_window, const gchar* text)
1093 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1099 gtk_dialog_run (dialog);
1100 gtk_widget_destroy (GTK_WIDGET (dialog));