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