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