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