Fix in the reference to dimming manager in hildon2 window children
[modest] / src / gnome / modest-account-settings-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 #include "widgets/modest-account-settings-dialog.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkstock.h>
42
43 #include "widgets/modest-serversecurity-combo-box.h"
44 #include "widgets/modest-secureauth-combo-box.h"
45 #include "widgets/modest-validating-entry.h"
46 #include "widgets/modest-retrieve-combo-box.h"
47 #include "widgets/modest-limit-retrieve-combo-box.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
51 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
52 #include "modest-protocol-info.h"
53 /* #include "maemo/modest-connection-specific-smtp-window.h" */
54 /* #include "maemo/modest-signature-editor-dialog.h" */
55 #include <modest-utils.h>
56 #include <modest-platform.h>
57 /* #include "maemo/modest-maemo-utils.h" */
58 #include "widgets/modest-ui-constants.h"
59 #include <tny-account.h>
60 #include <tny-status.h>
61
62 #include <gconf/gconf-client.h>
63 #include <string.h> /* For strlen(). */
64
65 /* Include config.h so that _() works: */
66 #ifdef HAVE_CONFIG_H
67 #include <config.h>
68 #endif
69
70 #define PORT_MIN 1
71 #define PORT_MAX 65535
72
73 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
74
75 static void
76 enable_buttons (ModestAccountSettingsDialog *self);
77
78 static gboolean
79 save_configuration (ModestAccountSettingsDialog *dialog);
80
81 static const gchar * null_means_empty (const gchar * str);
82
83 static const gchar *
84 null_means_empty (const gchar * str)
85 {
86         return str ? str : "";
87 }
88
89
90 static void
91 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
92                                                                                                                         GValue *value, GParamSpec *pspec)
93 {
94         switch (property_id) {
95         default:
96                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97         }
98 }
99
100 static void
101 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
102                                                                                                                         const GValue *value, GParamSpec *pspec)
103 {
104         switch (property_id) {
105         default:
106                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107         }
108 }
109
110 static void
111 modest_account_settings_dialog_dispose (GObject *object)
112 {
113         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
114                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
115 }
116
117 static void
118 modest_account_settings_dialog_finalize (GObject *object)
119 {
120         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
121         
122         if (self->account_name)
123                 g_free (self->account_name);
124                 
125         if (self->original_account_title)
126                 g_free (self->original_account_title);
127                 
128         if (self->account_manager)
129                 g_object_unref (G_OBJECT (self->account_manager));
130                 
131         if (self->signature_dialog)
132                 gtk_widget_destroy (self->signature_dialog);
133
134         if (self->settings) {
135                 g_object_unref (self->settings);
136                 self->settings = NULL;
137         }
138         
139         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
140 }
141
142 static void
143 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
144
145 static void
146 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
147
148 static void
149 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
150 {
151         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
152         self->modified = TRUE;
153 }
154
155 static void
156 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
157 {
158         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
159         self->modified = TRUE;
160 }
161
162 static void
163 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
164 {
165         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
166         self->modified = TRUE;
167 }
168
169 static void
170 on_modified_spin_button_changed (GtkSpinButton *spin_button, gpointer user_data)
171 {
172         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
173         gint value = gtk_spin_button_get_value_as_int (spin_button);
174
175         gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, value > 0);
176         self->modified = TRUE;
177 }
178
179 /* Set a modified boolean whenever the widget is changed, 
180  * so we can check for it later.
181  */
182 static void
183 connect_for_modified (ModestAccountSettingsDialog *self, GtkWidget *widget)
184 {
185         if (GTK_SPIN_BUTTON (widget)) {
186                 g_signal_connect (G_OBJECT (widget), "value_changed",
187                         G_CALLBACK (on_modified_spin_button_changed), self);
188         }
189         else if (GTK_IS_ENTRY (widget)) {
190                 g_signal_connect (G_OBJECT (widget), "changed",
191                         G_CALLBACK (on_modified_entry_changed), self);
192         } else if (GTK_IS_COMBO_BOX (widget)) {
193                 g_signal_connect (G_OBJECT (widget), "changed",
194                         G_CALLBACK (on_modified_combobox_changed), self);       
195         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
196                 g_signal_connect (G_OBJECT (widget), "toggled",
197                         G_CALLBACK (on_modified_checkbox_toggled), self);
198         }
199 }
200
201 static void
202 on_field_entry_changed (GtkEditable *editable, gpointer user_data)
203 {
204         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
205         g_assert(self);
206         enable_buttons(self);
207 }
208
209 static void
210 on_field_combobox_changed (GtkComboBox *widget, gpointer user_data)
211 {
212         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
213         g_assert(self);
214         enable_buttons(self);
215 }
216
217 /** This is a convenience function to create a field containing a mandatory widget.
218  * When the widget is edited, the enable_buttons() vfunc will be called.
219  */
220 static GtkWidget* create_field(ModestAccountSettingsDialog *self,
221                                GtkSizeGroup *group,
222                                const gchar *value,
223                                GtkWidget *control,
224                                GtkWidget *icon,
225                                gboolean mandatory)
226 {
227         GtkWidget *hbox;
228         gchar *title;
229         GtkWidget *label;
230
231         hbox = gtk_hbox_new (FALSE, 12);
232         if (mandatory)
233                 title = g_strdup_printf("%s*:", value);
234         else
235                 title = g_strdup_printf ("%s:", value);
236         label = gtk_label_new (title);
237         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
238         g_free (title);
239
240         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
241         gtk_box_pack_start (GTK_BOX (hbox), control, TRUE, TRUE, 0);
242         gtk_size_group_add_widget (group, label);
243   
244         /* Connect to the appropriate changed signal for the widget, 
245          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
246          */
247         if (GTK_IS_ENTRY (control)) {
248                 g_signal_connect (G_OBJECT (control), "changed",
249                 G_CALLBACK (on_field_entry_changed), self);
250                 
251         }
252         else if (GTK_IS_COMBO_BOX (control)) {
253                 g_signal_connect (G_OBJECT (control), "changed",
254                 G_CALLBACK (on_field_combobox_changed), self);
255         }
256         gtk_widget_show_all (hbox);
257          
258         return hbox;
259 }
260
261 static void
262 on_entry_invalid_account_title_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
263 {
264         gchar *tmp, *msg;
265                         
266         tmp = g_strndup (account_title_forbidden_chars, ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH);
267         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
268
269         modest_platform_information_banner (GTK_WIDGET (self), NULL, msg);
270
271         g_free (msg);
272         g_free (tmp);
273 }
274
275 static void
276 on_entry_invalid_fullname_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
277 {
278         gchar *tmp, *msg;
279                         
280         tmp = g_strndup (user_name_forbidden_chars, USER_NAME_FORBIDDEN_CHARS_LENGTH);
281         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
282
283         modest_platform_information_banner(GTK_WIDGET (self), NULL, msg);
284
285         g_free (msg);
286         g_free (tmp);
287 }
288
289
290 static void
291 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
292 {
293         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
294                                          _CS("ckdg_ib_maximum_characters_reached"));
295 }
296
297 static GtkWidget*
298 create_page_account_details (ModestAccountSettingsDialog *self)
299 {
300         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
301         GtkAdjustment *focus_adjustment = NULL;
302         GtkWidget *alignment;
303         
304         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
305         /* The description widgets: */  
306         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
307         GtkWidget *field = create_field (self, sizegroup, _("mcen_fi_account_title"), 
308                 self->entry_account_title, NULL, TRUE);
309         gtk_widget_show (self->entry_account_title);
310         connect_for_modified (self, self->entry_account_title);
311         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
312         gtk_widget_show (field);
313         
314         /* Prevent the use of some characters in the account title, 
315          * as required by our UI specification: */
316         GList *list_prevent = NULL;
317         list_prevent = g_list_append (list_prevent, "\\");
318         list_prevent = g_list_append (list_prevent, "/");
319         list_prevent = g_list_append (list_prevent, ":");
320         list_prevent = g_list_append (list_prevent, "*");
321         list_prevent = g_list_append (list_prevent, "?");
322         list_prevent = g_list_append (list_prevent, "\"");
323         list_prevent = g_list_append (list_prevent, "<"); 
324         list_prevent = g_list_append (list_prevent, ">"); 
325         list_prevent = g_list_append (list_prevent, "|");
326         list_prevent = g_list_append (list_prevent, "^");       
327         modest_validating_entry_set_unallowed_characters (
328                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
329         g_list_free (list_prevent);
330         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_account_title),
331                                          on_entry_invalid_account_title_character, self);
332         
333         /* Set max length as in the UI spec:
334          * The UI spec seems to want us to show a dialog if we hit the maximum. */
335         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
336         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
337                 on_entry_max, self);
338         
339         /* The retrieve combobox: */
340         self->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
341         field = create_field (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
342                               self->combo_retrieve, NULL, TRUE);
343         gtk_widget_show (self->combo_retrieve);
344         connect_for_modified (self, self->combo_retrieve);
345         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
346         gtk_widget_show (field);
347         
348         /* The limit-retrieve combobox: */
349         self->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
350         field = create_field (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
351                               self->combo_limit_retrieve, NULL, TRUE);
352         gtk_widget_show (self->combo_limit_retrieve);
353         connect_for_modified (self, self->combo_limit_retrieve);
354         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
355         gtk_widget_show (field);
356
357         /* The leave-messages widgets: */
358         if(!self->checkbox_leave_messages)
359                 self->checkbox_leave_messages = gtk_check_button_new ();
360         if (!self->caption_leave_messages) {
361                 self->caption_leave_messages = create_field (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
362                                                              self->checkbox_leave_messages, NULL, TRUE);
363         }
364                         
365         gtk_widget_show (self->checkbox_leave_messages);
366         connect_for_modified (self, self->checkbox_leave_messages);
367         gtk_box_pack_start (GTK_BOX (box), self->caption_leave_messages, FALSE, FALSE, MODEST_MARGIN_HALF);
368         gtk_widget_show (self->caption_leave_messages);
369         
370         gtk_widget_show (GTK_WIDGET (box));
371
372         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
373         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
374         gtk_container_add (GTK_CONTAINER (alignment), box);
375         gtk_widget_show (alignment);
376         
377         return GTK_WIDGET (alignment);
378 }
379
380 static gchar*
381 get_entered_account_title (ModestAccountSettingsDialog *dialog)
382 {
383         const gchar* account_title = 
384                 gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
385         if (!account_title || (strlen (account_title) == 0))
386                 return NULL;
387         else {
388                 /* Strip it of whitespace at the start and end: */
389                 gchar *result = g_strdup (account_title);
390                 result = g_strstrip (result);
391                 
392                 if (!result)
393                         return NULL;
394                         
395                 if (strlen (result) == 0) {
396                         g_free (result);
397                         return NULL;    
398                 }
399                 
400                 return result;
401         }
402 }
403
404
405 static void
406 on_button_signature (GtkButton *button, gpointer user_data)
407 {
408 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
409         
410 /*      /\* Create the window, if necessary: *\/ */
411 /*      if (!(self->signature_dialog)) { */
412 /*              self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ()); */
413         
414 /*              gboolean use_signature = modest_account_settings_get_use_signature (self->settings); */
415 /*              const gchar *signature = modest_account_settings_get_signature(self->settings); */
416 /*              gchar* account_title = get_entered_account_title (self); */
417 /*              modest_signature_editor_dialog_set_settings ( */
418 /*                      MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog),  */
419 /*                      use_signature, signature, account_title); */
420
421 /*              g_free (account_title); */
422 /*              account_title = NULL; */
423 /*              signature = NULL; */
424 /*      } */
425
426 /*      /\* Show the window: *\/         */
427 /*      gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self)); */
428 /*      gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE); */
429 /*     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog)); */
430 /*     gtk_widget_hide (self->signature_dialog); */
431 /*     if (response != GTK_RESPONSE_OK) { */
432 /*      /\* Destroy the widget now, and its data: *\/ */
433 /*      gtk_widget_destroy (self->signature_dialog); */
434 /*      self->signature_dialog = NULL; */
435 /*     } */
436 /*     else { */
437 /*      /\* Mark modified, so we use the dialog's data later: *\/ */
438 /*      self->modified = TRUE;   */
439 /*     } */
440 }
441
442 static GtkWidget*
443 create_page_user_details (ModestAccountSettingsDialog *self)
444 {
445         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
446         GtkAdjustment *focus_adjustment = NULL;
447         GtkWidget *alignment;
448         
449         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
450          
451         /* The name widgets: */
452         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
453
454         /* Set max length as in the UI spec:
455          * The UI spec seems to want us to show a dialog if we hit the maximum. */
456         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
457         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
458                 on_entry_max, self);
459         GtkWidget *field = create_field (self, sizegroup, 
460                                          _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, FALSE);
461         gtk_widget_show (self->entry_user_name);
462         connect_for_modified (self, self->entry_user_name);
463         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
464         gtk_widget_show (field);
465
466
467         /* Prevent the use of some characters in the name, 
468          * as required by our UI specification: */
469         GList *list_prevent = NULL;
470         list_prevent = g_list_append (list_prevent, "<");
471         list_prevent = g_list_append (list_prevent, ">");
472         modest_validating_entry_set_unallowed_characters (
473                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
474         g_list_free (list_prevent);
475         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_user_name),
476                                          on_entry_invalid_fullname_character, self);
477         
478         /* The username widgets: */     
479         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
480         field = create_field (self, sizegroup, _("mail_fi_username"), 
481                                 self->entry_user_username, NULL, TRUE);
482         gtk_widget_show (self->entry_user_username);
483         connect_for_modified (self, self->entry_user_username);
484         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
485         gtk_widget_show (field);
486         
487         /* Prevent the use of some characters in the username, 
488          * as required by our UI specification: */
489         modest_validating_entry_set_unallowed_characters_whitespace (
490                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
491         
492         /* Set max length as in the UI spec:
493          * The UI spec seems to want us to show a dialog if we hit the maximum. */
494         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
495         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
496                 on_entry_max, self);
497         
498         /* The password widgets: */     
499         self->entry_user_password = gtk_entry_new ();
500         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
501         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
502         field = create_field (self, sizegroup, 
503                               _("mail_fi_password"), self->entry_user_password, NULL, FALSE);
504         gtk_widget_show (self->entry_user_password);
505         connect_for_modified (self, self->entry_user_password);
506         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
507         gtk_widget_show (field);
508         
509         /* The email address widgets: */        
510         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
511         field = create_field (self, sizegroup, 
512                               _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, TRUE);
513         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
514         gtk_widget_show (self->entry_user_email);
515         connect_for_modified (self, self->entry_user_email);
516         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
517         gtk_widget_show (field);
518         
519         /* Set max length as in the UI spec:
520          * The UI spec seems to want us to show a dialog if we hit the maximum. */
521         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
522         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
523                 on_entry_max, self);
524         
525         /* Signature button: */
526         if (!self->button_signature)
527                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
528         field = create_field (self, sizegroup, _("mcen_fi_email_signature"), 
529                 self->button_signature, NULL, FALSE);
530         gtk_widget_show (self->button_signature);
531         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
532         gtk_widget_show (field);
533                 
534         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
535                 G_CALLBACK (on_button_signature), self);
536                 
537         gtk_widget_show (GTK_WIDGET (box));
538
539         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
540         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
541         gtk_container_add (GTK_CONTAINER (alignment), box);
542         gtk_widget_show (alignment);
543         
544         return GTK_WIDGET (alignment);
545 }
546
547 /** Change the field title for the incoming server, 
548  * as specified in the UI spec:
549  */
550 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
551 {
552         const gchar* type = 
553                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
554                         _("mail_fi_emailtype_pop3") : 
555                         _("mail_fi_emailtype_imap") );
556                         
557                 
558         /* Note that this produces a compiler warning, 
559          * because the compiler does not know that the translated string will have a %s in it.
560          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
561         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
562         
563         /* This is a mandatory field, so add a *. This is usually done by 
564          * create_field() but we can't use that here. */
565         gchar *with_asterisk = g_strconcat (incomingserver_title, "*", NULL);
566         g_free (incomingserver_title);
567         
568         g_object_set (G_OBJECT (self->caption_incoming), "label", with_asterisk, NULL);
569         g_free(with_asterisk);
570 }
571
572 /** Change the field title for the incoming server, 
573  * as specified in the UI spec:
574  */
575 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
576 {
577         /* Fill the combo with appropriately titled choices for POP or IMAP. */
578         /* The choices are the same, but the titles are different, as in the UI spec. */
579         modest_serversecurity_combo_box_fill (
580                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
581 }
582            
583 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
584 {
585         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
586         GtkWidget *alignment;
587         
588         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
589          
590         /* The incoming server widgets: */
591         if(!self->entry_incomingserver)
592                 self->entry_incomingserver = gtk_entry_new ();
593
594         if (self->caption_incoming)
595           gtk_widget_destroy (self->caption_incoming);
596            
597         /* The field title will be updated in update_incoming_server_title().
598          * so this default text will never be seen: */
599         /* (Note: Changing the title seems pointless. murrayc) */
600         self->caption_incoming = create_field (self, sizegroup, 
601                                              "Incoming Server", self->entry_incomingserver, NULL, TRUE);
602         gtk_widget_show (self->entry_incomingserver);
603         connect_for_modified (self, self->entry_incomingserver);
604         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
605         gtk_widget_show (self->caption_incoming);
606         
607         /* The secure connection widgets: */
608         /* This will be filled by update_incoming_server_security_choices(). */
609         if (!self->combo_incoming_security)
610                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
611         GtkWidget *field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
612                                          self->combo_incoming_security, NULL, FALSE);
613         gtk_widget_show (self->combo_incoming_security);
614         connect_for_modified (self, self->combo_incoming_security);
615         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
616         gtk_widget_show (field);
617         
618         /* Show a default port number when the security method changes, as per the UI spec: */
619         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
620         
621         
622         /* The port widgets: */
623         if (!self->entry_incoming_port)
624                 self->entry_incoming_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble) PORT_MIN, (gdouble) PORT_MAX, 1.0));
625         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
626                 self->entry_incoming_port, NULL, FALSE);
627         gtk_widget_show (self->entry_incoming_port);
628         connect_for_modified (self, self->entry_incoming_port);
629         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
630         gtk_widget_show (field);
631         
632         /* The secure authentication widgets: */
633         if(!self->checkbox_incoming_auth)
634                 self->checkbox_incoming_auth = gtk_check_button_new ();
635         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
636                               self->checkbox_incoming_auth, NULL, FALSE);
637         gtk_widget_show (self->checkbox_incoming_auth);
638         connect_for_modified (self, self->checkbox_incoming_auth);
639         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
640         gtk_widget_show (field);
641         
642         gtk_widget_show (GTK_WIDGET (box));
643         
644         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
645         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
646         gtk_container_add (GTK_CONTAINER (alignment), box);
647         gtk_widget_show (alignment);
648         
649         return GTK_WIDGET (alignment);
650 }
651
652 static void
653 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
654 {
655         GtkWidget *widget = GTK_WIDGET (user_data);
656         
657         /* Enable the widget only if the toggle button is active: */
658         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
659         gtk_widget_set_sensitive (widget, enable);
660 }
661
662 /* Make the sensitivity of a widget depend on a toggle button.
663  */
664 static void
665 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
666 {
667         g_signal_connect (G_OBJECT (button), "toggled",
668                 G_CALLBACK (on_toggle_button_changed), widget);
669         
670         /* Set the starting sensitivity: */
671         on_toggle_button_changed (button, widget);
672 }
673
674 static void
675 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
676 {
677 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
678         
679 /*      /\* Create the window if necessary: *\/ */
680 /*      if (!(self->specific_window)) { */
681 /*              self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ()); */
682 /*              modest_connection_specific_smtp_window_fill_with_connections ( */
683 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager); */
684 /*      } */
685
686 /*      /\* Show the window: *\/         */
687 /*      gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self)); */
688 /*      gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE); */
689 /*      gtk_widget_show (self->specific_window); */
690 /*      self->modified = TRUE; */
691 }
692
693 static void
694 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
695 {
696         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
697         
698         ModestAuthProtocol protocol_security = 
699                 modest_secureauth_combo_box_get_active_secureauth (
700                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
701         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
702         
703         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
704         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
705 }
706
707 static void
708 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
709 {
710         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
711         
712         const gint port_number = 
713                 modest_serversecurity_combo_box_get_active_serversecurity_port (
714                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
715
716         if(port_number != 0) {
717                 gtk_spin_button_set_value (
718                         GTK_SPIN_BUTTON (self->entry_outgoing_port), (gdouble) port_number);
719         }               
720 }
721
722 static void
723 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
724 {
725         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
726         
727         const gint port_number = 
728                 modest_serversecurity_combo_box_get_active_serversecurity_port (
729                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
730
731         if(port_number != 0) {
732                 gtk_spin_button_set_value (
733                         GTK_SPIN_BUTTON (self->entry_incoming_port), (gdouble) port_number);
734         }               
735 }
736
737
738 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
739 {
740         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
741         GtkAdjustment *focus_adjustment = NULL;
742         GtkWidget *alignment;
743         
744         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
745          
746         /* The outgoing server widgets: */
747         if (!self->entry_outgoingserver)
748                 self->entry_outgoingserver = gtk_entry_new ();
749         GtkWidget *field = create_field (self, sizegroup, 
750                                          _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, FALSE);
751         gtk_widget_show (self->entry_outgoingserver);
752         connect_for_modified (self, self->entry_outgoingserver);
753         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
754         gtk_widget_show (field);
755         
756         /* The secure authentication widgets: */
757         if (!self->combo_outgoing_auth)
758                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
759         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
760                 self->combo_outgoing_auth, NULL, FALSE);
761         gtk_widget_show (self->combo_outgoing_auth);
762         connect_for_modified (self, self->combo_outgoing_auth);
763         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
764         gtk_widget_show (field);
765         
766         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
767         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
768         
769         /* The username widgets: */     
770         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
771         self->caption_outgoing_username = create_field (self, sizegroup, _("mail_fi_username"), 
772                                                       self->entry_outgoing_username, NULL, TRUE);
773         gtk_widget_show (self->entry_outgoing_username);
774         connect_for_modified (self, self->entry_outgoing_username);
775         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
776         gtk_widget_show (self->caption_outgoing_username);
777         
778         /* Prevent the use of some characters in the username, 
779          * as required by our UI specification: */
780         modest_validating_entry_set_unallowed_characters_whitespace (
781                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
782         
783         /* Set max length as in the UI spec:
784          * The UI spec seems to want us to show a dialog if we hit the maximum. */
785         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
786         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
787                 on_entry_max, self);
788                 
789         /* The password widgets: */     
790         self->entry_outgoing_password = gtk_entry_new ();
791         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
792         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
793         self->caption_outgoing_password = create_field (self, sizegroup, 
794                                                       _("mail_fi_password"), self->entry_outgoing_password, NULL, FALSE);
795         gtk_widget_show (self->entry_outgoing_password);
796         connect_for_modified (self, self->entry_outgoing_password);
797         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
798         gtk_widget_show (self->caption_outgoing_password);
799         
800         /* The secure connection widgets: */
801         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
802          * and modest_serversecurity_combo_box_set_active_serversecurity().
803          */
804         if (!self->combo_outgoing_security)
805                 
806                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
807         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
808                               self->combo_outgoing_security, NULL, FALSE);
809         gtk_widget_show (self->combo_outgoing_security);
810         connect_for_modified (self, self->combo_outgoing_security);
811         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
812         gtk_widget_show (field);
813         
814         /* Show a default port number when the security method changes, as per the UI spec: */
815         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
816         
817         /* The port widgets: */
818         if (!self->entry_outgoing_port)
819                 self->entry_outgoing_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble)PORT_MIN, (gdouble) PORT_MAX, 1.0));
820         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
821                               self->entry_outgoing_port, NULL, FALSE);
822         gtk_widget_show (self->entry_outgoing_port);
823         connect_for_modified (self, self->entry_outgoing_port);
824         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
825         gtk_widget_show (field);
826         
827         GtkWidget *separator = gtk_hseparator_new ();
828         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
829         gtk_widget_show (separator);
830         
831         /* connection-specific checkbox: */
832         if (!self->checkbox_outgoing_smtp_specific) {
833                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
834                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
835                         FALSE);
836         }
837         field = create_field (self, sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
838                               self->checkbox_outgoing_smtp_specific, NULL, FALSE);
839         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
840         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
841         gtk_widget_show (field);
842         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
843         
844         /* Connection-specific SMTP-Severs Edit button: */
845         if (!self->button_outgoing_smtp_servers)
846                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
847         field = create_field (self, sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
848                               self->button_outgoing_smtp_servers, NULL, FALSE);
849         gtk_widget_show (self->button_outgoing_smtp_servers);
850         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
851         gtk_widget_show (field);
852         
853         /* Only enable the button when the checkbox is checked: */
854         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
855                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
856                 
857         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
858                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
859                 
860         gtk_widget_show (GTK_WIDGET (box));
861         
862         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
863         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
864         gtk_container_add (GTK_CONTAINER (alignment), box);
865         gtk_widget_show (alignment);
866         
867         return GTK_WIDGET (alignment);
868 }
869         
870 static gboolean
871 check_data (ModestAccountSettingsDialog *self)
872 {
873         /* Check that the title is not already in use: */
874         gchar* account_title = get_entered_account_title (self);
875         if (!account_title)
876                 return FALSE; /* Should be prevented already anyway. */
877                 
878         if (strcmp(account_title, self->original_account_title) != 0) {
879                 /* Check the changed title: */
880                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
881                         account_title);
882         
883                 if (name_in_use) {
884                         /* Warn the user via a dialog: */
885                         modest_platform_information_banner(NULL, NULL, _("mail_ib_account_name_already_existing"));
886                 
887                 g_free (account_title);
888                         return FALSE;
889                 }
890         }
891         
892         g_free (account_title);
893         account_title  = NULL;
894
895         /* Check that the email address is valid: */
896         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
897         if ((!email_address) || (strlen(email_address) == 0)) {
898                 return FALSE;
899         }
900                         
901         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
902                 /* Warn the user via a dialog: */
903                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_email"));
904                                          
905                 /* Return focus to the email address entry: */
906                 gtk_widget_grab_focus (self->entry_user_email);
907                 gtk_editable_select_region (GTK_EDITABLE (self->entry_user_email), 0, -1);
908                 return FALSE;
909         }
910
911         /* make sure the domain name for the incoming server is valid */
912         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver));
913         if ((!hostname) || (strlen(hostname) == 0)) {
914                 return FALSE;
915         }
916         
917         if (!modest_text_utils_validate_domain_name (hostname)) {
918                 /* Warn the user via a dialog: */
919                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
920                                          
921                 /* Return focus to the email address entry: */
922                 gtk_widget_grab_focus (self->entry_incomingserver);
923                 gtk_editable_select_region (GTK_EDITABLE (self->entry_incomingserver), 0, -1);
924                 return FALSE;
925         }
926
927         /* make sure the domain name for the outgoing server is valid */
928         const gchar* hostname2 = gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver));
929         if ((!hostname2) || (strlen(hostname2) == 0)) {
930                 return FALSE;
931         }
932         
933         if (!modest_text_utils_validate_domain_name (hostname2)) {
934                 /* Warn the user via a dialog: */
935                 modest_platform_information_banner (self->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
936
937                 /* Return focus to the email address entry: */
938                 gtk_widget_grab_focus (self->entry_outgoingserver);
939                 gtk_editable_select_region (GTK_EDITABLE (self->entry_outgoingserver), 0, -1);
940                 return FALSE;
941         }
942         
943         /* Find a suitable authentication method when secure authentication is desired */
944
945         const gint port_num = gtk_spin_button_get_value_as_int (
946                         GTK_SPIN_BUTTON (self->entry_incoming_port));
947         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
948
949         /*
950         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
951                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
952         */
953         /* If we use an encrypted protocol then there is no need to encrypt the password */
954         /* I don't think this is a good assumption. It overrides the user's request. murrayc: 
955          *  if (!modest_protocol_info_is_secure(protocol_security_incoming)) */
956         if (TRUE)
957         {
958                 if (gtk_toggle_button_get_active (
959                                 GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth))) {
960                         GError *error = NULL;
961
962                         GList *list_auth_methods = 
963                                 modest_utils_get_supported_secure_authentication_methods (self->incoming_protocol, 
964                                         hostname, port_num, username, GTK_WINDOW (self), &error);
965                         if (list_auth_methods) {
966                                 /* Use the first supported method.
967                                  * TODO: Should we prioritize them, to prefer a particular one? */
968                                 GList* method;
969                                 for (method = list_auth_methods; method != NULL; method = g_list_next(method))
970                                 {
971                                         ModestAuthProtocol proto = (ModestAuthProtocol)(GPOINTER_TO_INT(method->data));
972                                         // Allow secure methods, e.g MD5 only
973                                         if (modest_protocol_info_auth_is_secure(proto))
974                                         {
975                                                 self->protocol_authentication_incoming = proto;
976                                                 break;
977                                         }
978                                 }
979                                 g_list_free (list_auth_methods);
980                         }
981
982                         if (list_auth_methods == NULL || 
983                                         !modest_protocol_info_auth_is_secure(self->protocol_authentication_incoming))
984                         {
985                                 if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
986                                                 error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
987                                         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
988                                                                          _("Could not discover supported secure authentication methods."));
989
990                                 if(error != NULL)
991                                         g_error_free(error);
992                                         
993                                 /* This is a nasty hack. jschmid. */
994                                 /* Don't let the dialog close */
995                                 /*g_signal_stop_emission_by_name (dialog, "response");*/
996                                 return FALSE;
997                         }
998                 }
999         }
1000         
1001         return TRUE;
1002 }
1003 /*
1004  */
1005 static void 
1006 on_response (GtkDialog *wizard_dialog,
1007         gint response_id,
1008         gpointer user_data)
1009 {
1010         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
1011         enable_buttons (self);
1012         
1013         gboolean prevent_response = FALSE;
1014
1015         /* Warn about unsaved changes: */
1016         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
1017                 GtkDialog *dialog = GTK_DIALOG (modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
1018                                                                                          _("imum_nc_wizard_confirm_lose_changes")));
1019                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1020                 
1021                 const gint dialog_response = gtk_dialog_run (dialog);
1022                 gtk_widget_destroy (GTK_WIDGET (dialog));
1023                 
1024                 if (dialog_response != GTK_RESPONSE_OK)
1025                         prevent_response = TRUE;
1026         }
1027         /* Check for invalid input: */
1028         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
1029                 prevent_response = TRUE;
1030         }
1031                 
1032         if (prevent_response) {
1033                 /* This is a nasty hack. murrayc. */
1034                 /* Don't let the dialog close */
1035                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1036                 return; 
1037         }
1038                 
1039         if (response_id == GTK_RESPONSE_OK) {
1040                 /* Try to save the changes if modified (NB #59251): */
1041                 if (self->modified)
1042                 {
1043                         const gboolean saved = save_configuration (self);
1044                         if (saved) {
1045                                 /* Do not show the account-saved dialog if we are just saving this 
1046                                  * temporarily, because from the user's point of view it will not 
1047                                  * really be saved (saved + enabled) until later
1048                                  */
1049                                 if (modest_account_settings_get_account_name (self->settings) != NULL) {
1050                                         ModestServerAccountSettings *store_settings;
1051                                         ModestServerAccountSettings *transport_settings;
1052                                         const gchar *store_account_name;
1053                                         const gchar *transport_account_name;
1054
1055
1056                                         store_settings = modest_account_settings_get_store_settings (self->settings);
1057                                         transport_settings = modest_account_settings_get_transport_settings (self->settings);
1058                                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
1059                                         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
1060                                         
1061                                         if (store_account_name) {
1062                                                 modest_account_mgr_notify_account_update (self->account_manager, 
1063                                                                                           store_account_name);
1064                                         }
1065                                         if (transport_account_name) {
1066                                                 modest_account_mgr_notify_account_update (self->account_manager, 
1067                                                                                           transport_account_name);
1068                                         }
1069                                         g_object_unref (store_settings);
1070                                         g_object_unref (transport_settings);
1071
1072                                         modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1073                                 }
1074                         } else {
1075                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1076                         }
1077                 }
1078         }
1079 }
1080
1081 static void
1082 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
1083 {
1084         /* Create the notebook to be used by the GtkDialog base class:
1085          * Each page of the notebook will be a page of the wizard: */
1086         self->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1087         self->settings = modest_account_settings_new ();
1088
1089         /* Get the account manager object, 
1090          * so we can check for existing accounts,
1091          * and create new accounts: */
1092         self->account_manager = modest_runtime_get_account_mgr ();
1093         g_assert (self->account_manager);
1094         g_object_ref (self->account_manager);
1095         
1096         self->protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1097
1098     /* Create the common pages, 
1099      */
1100         self->page_account_details = create_page_account_details (self);
1101         self->page_user_details = create_page_user_details (self);
1102         self->page_incoming = create_page_incoming (self);
1103         self->page_outgoing = create_page_outgoing (self);
1104         
1105         /* Add the notebook pages: */
1106         gtk_notebook_append_page (self->notebook, self->page_account_details, 
1107                 gtk_label_new (_("mcen_ti_account_settings_account")));
1108         gtk_notebook_append_page (self->notebook, self->page_user_details, 
1109                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1110         gtk_notebook_append_page (self->notebook, self->page_incoming,
1111                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1112         gtk_notebook_append_page (self->notebook, self->page_outgoing,
1113                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1114                 
1115         GtkDialog *dialog = GTK_DIALOG (self);
1116         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (self->notebook));
1117         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1118         gtk_widget_show (GTK_WIDGET (self->notebook));
1119         
1120     /* Add the buttons: */
1121         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1122         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_SAVE, GTK_RESPONSE_OK);
1123     
1124     /* Connect to the dialog's response signal: */
1125     /* We use connect-before 
1126      * so we can stop the signal emission, 
1127      * to stop the default signal handler from closing the dialog.
1128      */
1129     g_signal_connect (G_OBJECT (self), "response",
1130             G_CALLBACK (on_response), self); 
1131             
1132     self->modified = FALSE;
1133
1134     /* When this window is shown, hibernation should not be possible, 
1135          * because there is no sensible way to save the state: */
1136     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1137         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1138
1139     gtk_window_set_default_size (GTK_WINDOW (self), 600, 400);
1140
1141 /*     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings", */
1142 /*                                  modest_maemo_utils_get_osso_context()); */
1143 }
1144
1145 ModestAccountSettingsDialog*
1146 modest_account_settings_dialog_new (void)
1147 {
1148         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
1149 }
1150
1151 /** Update the UI with the stored account details, so they can be edited.
1152  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1153  */
1154 void modest_account_settings_dialog_set_account (ModestAccountSettingsDialog *dialog, ModestAccountSettings *settings)
1155 {
1156         ModestServerAccountSettings *incoming_account;
1157         ModestServerAccountSettings *outgoing_account;
1158         const gchar *account_name;
1159
1160         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1161
1162         incoming_account = modest_account_settings_get_store_settings (settings);
1163         outgoing_account = modest_account_settings_get_transport_settings (settings);
1164
1165         account_name = modest_account_settings_get_account_name (settings);
1166                 
1167         /* Save the account name so we can refer to it later: */
1168         if (dialog->account_name)
1169                 g_free (dialog->account_name);
1170         dialog->account_name = g_strdup (account_name);
1171
1172         if (dialog->settings)
1173                 g_object_unref (dialog->settings);
1174         dialog->settings = g_object_ref (settings);
1175         
1176         /* Save the account title so we can refer to it if the user changes it: */
1177         if (dialog->original_account_title)
1178                 g_free (dialog->original_account_title);
1179         dialog->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1180         
1181         /* Show the account data in the widgets: */
1182         
1183         /* Note that we never show the non-display name in the UI.
1184          * (Though the display name defaults to the non-display name at the start.) */
1185         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1186                             null_means_empty (modest_account_settings_get_display_name (settings)));
1187         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1188                             null_means_empty (modest_account_settings_get_fullname (settings)));
1189         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1190                             null_means_empty (modest_account_settings_get_email_address (settings)));
1191         modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1192                                         modest_server_account_settings_get_protocol (incoming_account));
1193         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1194                                                             modest_account_settings_get_retrieve_type (settings));
1195         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1196                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), 
1197                 modest_account_settings_get_retrieve_limit (settings));
1198         
1199         
1200         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), 
1201                                       modest_account_settings_get_leave_messages_on_server (settings));
1202         
1203         /* Only show the leave-on-server checkbox for POP, 
1204          * as per the UI spec: */
1205         if (modest_server_account_settings_get_protocol (incoming_account) != MODEST_PROTOCOL_STORE_POP) {
1206                 gtk_widget_hide (dialog->caption_leave_messages);
1207         } else {
1208                 gtk_widget_show (dialog->caption_leave_messages);
1209         }
1210         
1211         update_incoming_server_security_choices (dialog, modest_server_account_settings_get_protocol (incoming_account));
1212         if (incoming_account) {
1213                 const gchar *username;
1214                 const gchar *password;
1215                 const gchar *hostname;
1216                 /* Remember this for later: */
1217                 dialog->incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1218                 
1219                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1220                 username = modest_server_account_settings_get_username (incoming_account);
1221                 password = modest_server_account_settings_get_password (incoming_account);
1222                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1223                                     null_means_empty (username));
1224                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1225                                     null_means_empty (password));
1226                         
1227                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1228                                     null_means_empty (hostname));
1229                         
1230                 /* The UI spec says:
1231                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1232          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1233                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1234          */                                                                                                              
1235                 modest_serversecurity_combo_box_set_active_serversecurity (
1236                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), 
1237                         modest_server_account_settings_get_security (incoming_account));
1238                 
1239                 /* Check if we have
1240                  - a secure protocol
1241                  OR
1242                  - use encrypted passwords
1243                 */
1244                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (incoming_account);
1245                 dialog->protocol_authentication_incoming = (secure_auth != MODEST_PROTOCOL_AUTH_NONE)?
1246                         secure_auth:MODEST_PROTOCOL_AUTH_PASSWORD;
1247                 if (modest_protocol_info_auth_is_secure(secure_auth))
1248                 {
1249                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1250                                                      TRUE);
1251                 }
1252                 else
1253                 {
1254                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1255                                                      FALSE);
1256                 };
1257                                         
1258                 update_incoming_server_title (dialog, dialog->incoming_protocol);
1259                 
1260                 const gint port_num = modest_server_account_settings_get_port (incoming_account);
1261                 if (port_num == 0) {
1262                         /* Show the appropriate port number: */
1263                         on_combo_incoming_security_changed (
1264                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1265                 } else {
1266                         /* Keep the user-entered port-number,
1267                          * or the already-appropriate automatic port number: */
1268                         gtk_spin_button_set_value (
1269                                 GTK_SPIN_BUTTON (dialog->entry_incoming_port), (gdouble) port_num);
1270                 }
1271                 g_object_unref (incoming_account);
1272         }
1273         
1274         outgoing_account = modest_account_settings_get_transport_settings (settings);
1275         if (outgoing_account) {
1276                 const gchar *hostname;
1277                 const gchar *username;
1278                 const gchar *password;
1279
1280                 /* Remember this for later: */
1281                 dialog->outgoing_protocol = 
1282                         modest_server_account_settings_get_protocol (outgoing_account);
1283
1284                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1285                 username = modest_server_account_settings_get_username (outgoing_account);
1286                 password = modest_server_account_settings_get_password (outgoing_account);
1287                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1288                                     null_means_empty (hostname));
1289                 
1290                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1291                                     null_means_empty (username));
1292                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1293                                     null_means_empty (password));
1294                 
1295                 /* Get the secure-auth setting: */
1296                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (outgoing_account);
1297                 modest_secureauth_combo_box_set_active_secureauth (
1298                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1299                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1300                 
1301                 modest_serversecurity_combo_box_fill (
1302                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), 
1303                         dialog->outgoing_protocol);
1304                 
1305                 /* Get the security setting: */
1306                 const ModestConnectionProtocol security = modest_server_account_settings_get_security (outgoing_account);
1307                 modest_serversecurity_combo_box_set_active_serversecurity (
1308                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1309                 
1310                 const gint port_num = modest_server_account_settings_get_port (outgoing_account);
1311                 if (port_num == 0) {
1312                         /* Show the appropriate port number: */
1313                         on_combo_outgoing_security_changed (
1314                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1315                 }
1316                 else {
1317                         /* Keep the user-entered port-number,
1318                          * or the already-appropriate automatic port number: */
1319                         gtk_spin_button_set_value (
1320                                 GTK_SPIN_BUTTON (dialog->entry_outgoing_port), (gdouble) port_num);
1321                 }
1322                 
1323                 const gboolean has_specific = 
1324                         modest_account_settings_get_use_connection_specific_smtp (settings);
1325                 gtk_toggle_button_set_active (
1326                         GTK_TOGGLE_BUTTON (dialog->checkbox_outgoing_smtp_specific), 
1327                         has_specific);
1328                 g_object_unref (outgoing_account);
1329         }
1330
1331         /* Set window title according to account: */
1332         /* TODO: Is this the correct way to find a human-readable name for
1333          * the protocol used? */
1334         const gchar* proto_str = modest_protocol_info_get_transport_store_protocol_name (dialog->incoming_protocol);
1335         gchar *proto_name = g_utf8_strup(proto_str, -1);
1336         const gchar *account_title = modest_account_settings_get_display_name(settings);
1337
1338         gchar *title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1339         g_free (proto_name);
1340
1341         gtk_window_set_title (GTK_WINDOW (dialog), title);
1342         g_free (title);
1343
1344         /* account_data->is_enabled,  */
1345         /*account_data->is_default,  */
1346
1347         /* Unset the modified flag so we can detect changes later: */
1348         dialog->modified = FALSE;
1349 }
1350
1351 /** Show the User Info tab.
1352  */
1353 void modest_account_settings_dialog_switch_to_user_info (ModestAccountSettingsDialog *dialog)
1354 {
1355         const gint page_num = gtk_notebook_page_num (dialog->notebook, dialog->page_user_details);
1356         if (page_num == -1) {
1357                 g_warning ("%s: notebook page not found.\n", __FUNCTION__);     
1358         }
1359                 
1360         /* Ensure that the widget is visible so that gtk_notebook_set_current_page() works: */
1361         /* TODO: even this hack (recommened by the GTK+ documentation) doesn't seem to work. */
1362         
1363         gtk_widget_show (dialog->page_user_details);
1364         gtk_widget_show (GTK_WIDGET (dialog->notebook));
1365         gtk_widget_show (GTK_WIDGET (dialog));
1366         gtk_notebook_set_current_page (dialog->notebook, page_num);
1367 }
1368
1369 static gboolean
1370 save_configuration (ModestAccountSettingsDialog *dialog)
1371 {
1372         const gchar* account_name = dialog->account_name;
1373         ModestServerAccountSettings *store_settings;
1374         ModestServerAccountSettings *transport_settings;
1375                 
1376         /* Set the account data from the widgets: */
1377         const gchar* user_fullname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1378         modest_account_settings_set_fullname (dialog->settings, user_fullname);
1379         
1380         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1381         modest_account_settings_set_email_address (dialog->settings, emailaddress);
1382                 
1383 /*      /\* Signature: *\/ */
1384 /*      if (dialog->signature_dialog) { */
1385 /*              gboolean use_signature = FALSE; */
1386 /*              gchar *signature =  */
1387 /*                      modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog), */
1388 /*                                                                   &use_signature); */
1389         
1390 /*              modest_account_settings_set_use_signature (dialog->settings, use_signature); */
1391 /*              modest_account_settings_set_signature (dialog->settings, signature); */
1392 /*      } */
1393         
1394         ModestAccountRetrieveType retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1395                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1396         modest_account_settings_set_retrieve_type (dialog->settings, retrieve_type);
1397         
1398         gint retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1399                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1400         modest_account_settings_set_retrieve_limit (dialog->settings, retrieve_limit);
1401         
1402         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1403         modest_account_settings_set_leave_messages_on_server (dialog->settings, leave_on_server); 
1404
1405         store_settings = modest_account_settings_get_store_settings (dialog->settings);
1406                         
1407         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1408         modest_server_account_settings_set_hostname (store_settings, hostname);
1409                                 
1410         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1411         modest_server_account_settings_set_username (store_settings, username);
1412         
1413         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1414         modest_server_account_settings_set_password (store_settings, password);
1415                         
1416         /* port: */
1417         gint port_num = gtk_spin_button_get_value_as_int (
1418                         GTK_SPIN_BUTTON (dialog->entry_incoming_port));
1419         modest_server_account_settings_set_port (store_settings, port_num);
1420                         
1421         /* The UI spec says:
1422          * If secure authentication is unchecked, allow sending username and password also as plain text.
1423          * If secure authentication is checked, require one of the secure 
1424          * methods during connection: SSL, TLS, CRAM-MD5 etc. 
1425          */
1426         
1427         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1428                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1429         modest_server_account_settings_set_security (store_settings, protocol_security_incoming);       
1430         modest_server_account_settings_set_auth_protocol (store_settings, dialog->protocol_authentication_incoming);
1431
1432         g_object_unref (store_settings);
1433         
1434         /* Outgoing: */
1435         transport_settings = modest_account_settings_get_transport_settings (dialog->settings);
1436         
1437         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1438         modest_server_account_settings_set_hostname (transport_settings, hostname);
1439                 
1440         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1441         modest_server_account_settings_set_username (transport_settings, username);
1442                 
1443         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1444         modest_server_account_settings_set_password (transport_settings, password);
1445         
1446         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1447                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1448         modest_server_account_settings_set_security (transport_settings, protocol_security_outgoing);
1449         
1450         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1451                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1452         modest_server_account_settings_set_auth_protocol (transport_settings, protocol_authentication_outgoing);        
1453         
1454         /* port: */
1455         port_num = gtk_spin_button_get_value_as_int (
1456                         GTK_SPIN_BUTTON (dialog->entry_outgoing_port));
1457         modest_server_account_settings_set_port (transport_settings, port_num);
1458         g_object_unref (transport_settings);
1459         
1460         
1461         /* Set the changed account title last, to simplify the previous code: */
1462         gchar* account_title = get_entered_account_title (dialog);
1463         if (!account_title)
1464                 return FALSE; /* Should be prevented already anyway. */
1465                 
1466 /*      if (strcmp (account_title, account_name) != 0) { */
1467         modest_account_settings_set_display_name (dialog->settings, account_title);
1468 /*      } */
1469         g_free (account_title);
1470         account_title = NULL;
1471         
1472         /* Save connection-specific SMTP server accounts: */
1473         modest_account_settings_set_use_connection_specific_smtp 
1474                 (dialog->settings, 
1475                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbox_outgoing_smtp_specific)));
1476
1477         /* this configuration is not persistent, we should not save */
1478         if (account_name != NULL)
1479                 modest_account_mgr_save_account_settings (dialog->account_manager, dialog->settings);
1480
1481 /*      if (dialog->specific_window) { */
1482 /*              return modest_connection_specific_smtp_window_save_server_accounts ( */
1483 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window)); */
1484 /*      } else { */
1485                 return TRUE;
1486 /*      } */
1487
1488 }
1489
1490 static gboolean entry_is_empty (GtkWidget *entry)
1491 {
1492         if (!entry)
1493                 return FALSE;
1494                 
1495         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1496         if ((!text) || (strlen(text) == 0))
1497                 return TRUE;
1498         else {
1499                 /* Strip it of whitespace at the start and end: */
1500                 gchar *stripped = g_strdup (text);
1501                 stripped = g_strstrip (stripped);
1502                 
1503                 if (!stripped)
1504                         return TRUE;
1505                         
1506                 const gboolean result = (strlen (stripped) == 0);
1507                 
1508                 g_free (stripped);
1509                 return result;
1510         }
1511 }
1512
1513 static void
1514 enable_buttons (ModestAccountSettingsDialog *self)
1515 {
1516         gboolean enable_ok = TRUE;
1517         ModestAuthProtocol outgoing_auth_protocol;
1518         
1519         /* The account details title is mandatory: */
1520         if (entry_is_empty(self->entry_account_title))
1521                 enable_ok = FALSE;
1522
1523         /* The user details username is mandatory: */
1524         if (enable_ok && entry_is_empty(self->entry_user_username))
1525                 enable_ok = FALSE;
1526                 
1527         /* The user details email address is mandatory: */
1528         if (enable_ok && entry_is_empty (self->entry_user_email))
1529                 enable_ok = FALSE;
1530
1531         /* The custom incoming server is mandatory: */
1532         if (enable_ok && entry_is_empty(self->entry_incomingserver))
1533                 enable_ok = FALSE;
1534
1535         /* The custom incoming server is mandatory: */
1536         if (enable_ok && entry_is_empty(self->entry_outgoingserver))
1537                 enable_ok = FALSE;
1538
1539         /* Outgoing username is mandatory if outgoing auth is secure */
1540         if (self->combo_outgoing_auth) {
1541                 outgoing_auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
1542                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1543                 if (enable_ok && 
1544                     outgoing_auth_protocol != MODEST_PROTOCOL_AUTH_NONE &&
1545                     entry_is_empty (self->entry_outgoing_username))
1546                         enable_ok = FALSE;
1547         }
1548                         
1549         /* Enable the buttons, 
1550          * identifying them via their associated response codes:
1551          */
1552         GtkDialog *dialog_base = GTK_DIALOG (self);
1553         gtk_dialog_set_response_sensitive (dialog_base,
1554                                            GTK_RESPONSE_OK,
1555                                            enable_ok);
1556 }
1557
1558 void
1559 modest_account_settings_dialog_check_allow_changes (ModestAccountSettingsDialog *self)
1560 {
1561         ModestServerAccountSettings *incoming_settings;
1562         const gchar *server_account_name;
1563         gboolean username_known;
1564
1565         if (!G_IS_OBJECT (self->settings))
1566                 return;
1567
1568         incoming_settings = modest_account_settings_get_store_settings (self->settings);
1569         server_account_name = modest_server_account_settings_get_account_name (incoming_settings);
1570
1571         username_known = modest_account_mgr_get_server_account_username_has_succeeded (self->account_manager, 
1572                                                                                        server_account_name);
1573
1574         /* Enable or disable widgets */
1575         gtk_widget_set_sensitive (self->entry_user_username, !username_known);
1576         gtk_widget_set_sensitive (self->entry_incomingserver, !username_known);
1577         gtk_widget_set_sensitive (self->entry_incoming_port, !username_known);
1578         gtk_widget_set_sensitive (self->combo_incoming_security, !username_known);
1579 }
1580
1581 void
1582 modest_account_settings_dialog_save_password (ModestAccountSettingsDialog *dialog)
1583 {
1584         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
1585
1586         dialog->modified = TRUE;
1587 }
1588
1589 static void
1590 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1591 {
1592         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1593
1594         object_class->get_property = modest_account_settings_dialog_get_property;
1595         object_class->set_property = modest_account_settings_dialog_set_property;
1596         object_class->dispose = modest_account_settings_dialog_dispose;
1597         object_class->finalize = modest_account_settings_dialog_finalize;
1598 }