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