2007-07-10 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         /* Create a size group to be used by all captions.
702          * Note that HildonCaption does not create a default size group if we do not specify one.
703          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
704         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
705          
706         /* The outgoing server widgets: */
707         if (!self->entry_outgoingserver)
708                 self->entry_outgoingserver = gtk_entry_new ();
709         /* Auto-capitalization is the default, so let's turn it off: */
710         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
711         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
712                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
713         gtk_widget_show (self->entry_outgoingserver);
714         connect_for_modified (self, self->entry_outgoingserver);
715         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
716         gtk_widget_show (caption);
717         
718         /* The secure authentication widgets: */
719         if (!self->combo_outgoing_auth)
720                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
721         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
722                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
723         gtk_widget_show (self->combo_outgoing_auth);
724         connect_for_modified (self, self->combo_outgoing_auth);
725         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
726         gtk_widget_show (caption);
727         
728         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
729         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
730         
731         /* The username widgets: */     
732         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
733         /* Auto-capitalization is the default, so let's turn it off: */
734         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_username), HILDON_GTK_INPUT_MODE_FULL);
735         self->caption_outgoing_username = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
736                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
737         gtk_widget_show (self->entry_outgoing_username);
738         connect_for_modified (self, self->entry_outgoing_username);
739         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
740         gtk_widget_show (self->caption_outgoing_username);
741         
742         /* Prevent the use of some characters in the username, 
743          * as required by our UI specification: */
744         modest_validating_entry_set_unallowed_characters_whitespace (
745                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
746         
747         /* Set max length as in the UI spec:
748          * The UI spec seems to want us to show a dialog if we hit the maximum. */
749         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
750         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
751                 on_entry_max, self);
752                 
753         /* The password widgets: */     
754         self->entry_outgoing_password = gtk_entry_new ();
755         /* Auto-capitalization is the default, so let's turn it off: */
756         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_password), 
757                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
758         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
759         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
760         self->caption_outgoing_password = create_caption_new_with_asterisk (self, sizegroup, 
761                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
762         gtk_widget_show (self->entry_outgoing_password);
763         connect_for_modified (self, self->entry_outgoing_password);
764         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
765         gtk_widget_show (self->caption_outgoing_password);
766         
767         /* The secure connection widgets: */
768         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
769          * and modest_serversecurity_combo_box_set_active_serversecurity().
770          */
771         if (!self->combo_outgoing_security)
772                 
773                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
774         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
775                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
776         gtk_widget_show (self->combo_outgoing_security);
777         connect_for_modified (self, self->combo_outgoing_security);
778         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
779         gtk_widget_show (caption);
780         
781         /* Show a default port number when the security method changes, as per the UI spec: */
782         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
783         
784         /* The port widgets: */
785         if (!self->entry_outgoing_port)
786                 self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
787         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
788                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
789         gtk_widget_show (self->entry_outgoing_port);
790         connect_for_modified (self, self->entry_outgoing_port);
791         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
792         gtk_widget_show (caption);
793         
794         GtkWidget *separator = gtk_hseparator_new ();
795         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
796         gtk_widget_show (separator);
797         
798         /* connection-specific checkbox: */
799         if (!self->checkbox_outgoing_smtp_specific) {
800                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
801                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
802                         FALSE);
803         }
804         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
805                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
806         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
807         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
808         gtk_widget_show (caption);
809         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
810         
811         /* Connection-specific SMTP-Severs Edit button: */
812         if (!self->button_outgoing_smtp_servers)
813                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
814         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
815                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
816         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
817         gtk_widget_show (self->button_outgoing_smtp_servers);
818         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
819         gtk_widget_show (caption);
820         
821         /* Only enable the button when the checkbox is checked: */
822         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
823                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
824                 
825         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
826                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
827                 
828         gtk_widget_show (GTK_WIDGET (box));
829         
830         return GTK_WIDGET (box);
831 }
832
833 static gboolean
834 check_data (ModestAccountSettingsDialog *self)
835 {
836         /* Check that the title is not already in use: */
837         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
838         if ((!account_title) || (strlen(account_title) == 0))
839                 return FALSE; /* Should be prevented already anyway. */
840                 
841         if (strcmp(account_title, self->original_account_title) != 0) {
842                 /* Check the changed title: */
843                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
844                         account_title);
845         
846                 if (name_in_use) {
847                         /* Warn the user via a dialog: */
848                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
849                 
850                         return FALSE;
851                 }
852         }
853
854         /* Check that the email address is valud: */
855         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
856         if ((!email_address) || (strlen(email_address) == 0))
857                 return FALSE;
858                         
859         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
860                 /* Warn the user via a dialog: */
861                 /*show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));*/
862                 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
863                                          
864                 /* Return focus to the email address entry: */
865                 gtk_widget_grab_focus (self->entry_user_email);
866                 gtk_editable_select_region (GTK_EDITABLE (self->entry_user_email), 0, -1);
867                 return FALSE;
868         }
869
870         /* Find a suitable authentication method when secure authentication is desired */
871         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver));
872         gint port_num = hildon_number_editor_get_value (
873                         HILDON_NUMBER_EDITOR (self->entry_incoming_port));
874         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
875
876         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
877                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
878
879         /* If we use an encrypted protocol then there is no need to encrypt the password */
880         if (!modest_protocol_info_is_secure(protocol_security_incoming))
881         {
882                 if (gtk_toggle_button_get_active (
883                                 GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth))) {
884                         GError *error = NULL;
885                         GList *list_auth_methods = 
886                                 modest_maemo_utils_get_supported_secure_authentication_methods (self->incoming_protocol, 
887                                         hostname, port_num, username, GTK_WINDOW (self), &error);
888                         if (list_auth_methods) {
889                                 /* Use the first supported method.
890                                  * TODO: Should we prioritize them, to prefer a particular one? */
891                                 GList* method;
892                                 for (method = list_auth_methods; method != NULL; method = g_list_next(method))
893                                 {
894                                         ModestAuthProtocol proto = (ModestAuthProtocol)(GPOINTER_TO_INT(list_auth_methods->data));
895                                         // Allow secure methods, e.g MD5 only
896                                         if (modest_protocol_info_auth_is_secure(proto))
897                                         {
898                                                 self->protocol_authentication_incoming = proto;
899                                                 break;
900                                         }
901                                 }
902                                 g_list_free (list_auth_methods);
903                         }
904
905                         if (list_auth_methods == NULL || 
906                                         !modest_protocol_info_auth_is_secure(self->protocol_authentication_incoming))
907                         {
908                                 if(error == NULL || error->domain != modest_maemo_utils_get_supported_secure_authentication_error_quark() ||
909                                                 error->code != MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
910                                 {
911                                         show_error (GTK_WIDGET (self), _("Could not discover supported secure authentication methods."));
912                                 }
913
914                                 if(error != NULL)
915                                         g_error_free(error);
916                                         
917                                 /* This is a nasty hack. jschmid. */
918                                 /* Don't let the dialog close */
919                                 /*g_signal_stop_emission_by_name (dialog, "response");*/
920                                 return FALSE;
921                         }
922                 }
923         }
924
925         /* TODO: The UI Spec wants us to check that the servernames are valid, 
926          * but does not specify how.
927          */
928          
929         return TRUE;
930 }
931 /*
932  */
933 static void 
934 on_response (GtkDialog *wizard_dialog,
935         gint response_id,
936         gpointer user_data)
937 {
938         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
939         enable_buttons (self);
940         
941         gboolean prevent_response = FALSE;
942
943         /* Warn about unsaved changes: */
944         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
945                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
946                         _("imum_nc_wizard_confirm_lose_changes")));
947                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
948                  
949                  const gint dialog_response = gtk_dialog_run (dialog);
950                  gtk_widget_destroy (GTK_WIDGET (dialog));
951                  
952                 if (dialog_response != GTK_RESPONSE_OK)
953                         prevent_response = TRUE;
954         }
955         /* Check for invalid input: */
956         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
957                 prevent_response = TRUE;
958         }
959                 
960         if (prevent_response) {
961                 /* This is a nasty hack. murrayc. */
962                 /* Don't let the dialog close */
963                 g_signal_stop_emission_by_name (wizard_dialog, "response");
964                 return; 
965         }
966                 
967         if (response_id == GTK_RESPONSE_OK) {
968                 /* Try to save the changes if modified (NB #59251): */
969                 if (self->modified)
970                 {
971                         const gboolean saved = save_configuration (self);
972                         if (saved) {
973                                 /* Do not show the account-saved dialog if we are just saving this 
974                                  * temporarily, because from the user's point of view it will not 
975                                  * really be saved (saved + enabled) until later.
976                                  */
977                                 const gboolean enabled = 
978                                         modest_account_mgr_get_enabled (self->account_manager, self->account_name);
979                                 if (enabled)
980                                         show_error (GTK_WIDGET (self), _("mcen_ib_advsetup_settings_saved"));
981                         }
982                         else
983                                 show_error (GTK_WIDGET (self), _("mail_ib_setting_failed"));
984                 }
985         }
986 }
987
988 static void
989 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
990 {
991         /* Create the notebook to be used by the GtkDialog base class:
992          * Each page of the notebook will be a page of the wizard: */
993         self->notebook = GTK_NOTEBOOK (gtk_notebook_new());
994
995         /* Get the account manager object, 
996          * so we can check for existing accounts,
997          * and create new accounts: */
998         self->account_manager = modest_runtime_get_account_mgr ();
999         g_assert (self->account_manager);
1000         g_object_ref (self->account_manager);
1001         
1002         self->protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1003
1004     /* Create the common pages, 
1005      */
1006         self->page_account_details = create_page_account_details (self);
1007         self->page_user_details = create_page_user_details (self);
1008         self->page_incoming = create_page_incoming (self);
1009         self->page_outgoing = create_page_outgoing (self);
1010         
1011         /* Add the notebook pages: */
1012         gtk_notebook_append_page (self->notebook, self->page_account_details, 
1013                 gtk_label_new (_("mcen_ti_account_settings_account")));
1014         gtk_notebook_append_page (self->notebook, self->page_user_details, 
1015                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1016         gtk_notebook_append_page (self->notebook, self->page_incoming,
1017                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1018         gtk_notebook_append_page (self->notebook, self->page_outgoing,
1019                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1020                 
1021         GtkDialog *dialog = GTK_DIALOG (self);
1022         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (self->notebook));
1023         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1024         gtk_widget_show (GTK_WIDGET (self->notebook));
1025         
1026     /* Add the buttons: */
1027     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
1028     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1029     
1030     /* Connect to the dialog's response signal: */
1031     /* We use connect-before 
1032      * so we can stop the signal emission, 
1033      * to stop the default signal handler from closing the dialog.
1034      */
1035     g_signal_connect (G_OBJECT (self), "response",
1036             G_CALLBACK (on_response), self); 
1037             
1038     self->modified = FALSE;
1039     
1040     /* When this window is shown, hibernation should not be possible, 
1041          * because there is no sensible way to save the state: */
1042     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1043         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1044 }
1045
1046 ModestAccountSettingsDialog*
1047 modest_account_settings_dialog_new (void)
1048 {
1049         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
1050 }
1051
1052 /** Update the UI with the stored account details, so they can be edited.
1053  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1054  */
1055 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
1056 {
1057         if (!account_name)
1058                 return;
1059                 
1060         /* Save the account name so we can refer to it later: */
1061         if (dialog->account_name)
1062                 g_free (dialog->account_name);
1063         dialog->account_name = g_strdup (account_name);
1064         
1065                 
1066         /* Get the account data for this account name: */
1067         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
1068                 account_name);
1069         if (!account_data) {
1070                 g_printerr ("modest: failed to get account data for %s\n", account_name);
1071                 return;
1072         }
1073         
1074         /* Save the account title so we can refer to it if the user changes it: */
1075         if (dialog->original_account_title)
1076                 g_free (dialog->original_account_title);
1077         dialog->original_account_title = g_strdup (account_data->display_name);
1078         
1079
1080         if (!(account_data->store_account)) {
1081                 g_printerr ("modest: account has no stores: %s\n", account_name);
1082                 return;
1083         }
1084                 
1085         /* Show the account data in the widgets: */
1086         
1087         /* Note that we never show the non-display name in the UI.
1088          * (Though the display name defaults to the non-display name at the start.) */
1089         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1090                 account_data->display_name ? account_data->display_name : "");
1091                 
1092         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1093                 account_data->fullname ? account_data->fullname : "");
1094         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1095                 account_data->email ? account_data->email : "");
1096                 
1097         ModestServerAccountData *incoming_account = account_data->store_account;
1098                 
1099         if (incoming_account)
1100                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
1101         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
1102                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
1103         if (!retrieve) {
1104                 /* Default to something, though no default is specified in the UI spec: */
1105                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
1106         }
1107         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
1108         g_free (retrieve);
1109         
1110         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
1111                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
1112         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
1113         
1114         
1115         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
1116                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
1117         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
1118         
1119         /* Only show the leave-on-server checkbox for POP, 
1120          * as per the UI spec: */
1121         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
1122                 gtk_widget_hide (dialog->caption_leave_messages);
1123         } else {
1124                 gtk_widget_show (dialog->caption_leave_messages);
1125         }
1126         
1127         update_incoming_server_security_choices (dialog, incoming_account->proto);
1128         if (incoming_account) {
1129                 /* Remember this for later: */
1130                 dialog->incoming_protocol = incoming_account->proto;
1131                 
1132                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1133                         incoming_account->username ? incoming_account->username : "");
1134                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1135                         incoming_account->password ? incoming_account->password : "");
1136                         
1137                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1138                         incoming_account->hostname ? incoming_account->hostname : "");
1139                         
1140                 /* The UI spec says:
1141                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1142          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1143                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1144          */                                                                                                              
1145                 const ModestConnectionProtocol security = modest_server_account_get_security (
1146                         dialog->account_manager, incoming_account->account_name);
1147                 modest_serversecurity_combo_box_set_active_serversecurity (
1148                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1149                 
1150                 /* Check if we have
1151                  - a secure protocol
1152                  OR
1153                  - use encrypted passwords
1154                 */
1155                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1156                         dialog->account_manager, incoming_account->account_name);
1157                 dialog->protocol_authentication_incoming = secure_auth;
1158                 if (modest_protocol_info_is_secure(security) || 
1159                                 modest_protocol_info_auth_is_secure(secure_auth))
1160                 {
1161                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1162                                                                                                                                          TRUE);
1163                 }
1164                 else
1165                 {
1166                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1167                                                                                                                                          FALSE);
1168                 };
1169                                         
1170                 update_incoming_server_title (dialog, incoming_account->proto);
1171                 
1172                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1173                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1174                         
1175                 if (port_num == 0) {
1176                         /* Show the appropriate port number: */
1177                         on_combo_incoming_security_changed (
1178                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1179                 } else {
1180                         /* Keep the user-entered port-number,
1181                          * or the already-appropriate automatic port number: */
1182                         hildon_number_editor_set_value (
1183                                 HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1184                 }
1185         }
1186         
1187         ModestServerAccountData *outgoing_account = account_data->transport_account;
1188         if (outgoing_account) {
1189                 /* Remember this for later: */
1190                 dialog->outgoing_protocol = outgoing_account->proto;
1191                 
1192                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1193                         outgoing_account->hostname ? outgoing_account->hostname : "");
1194                 
1195                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1196                         outgoing_account->username ? outgoing_account->username : "");
1197                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1198                         outgoing_account->password ? outgoing_account->password : "");
1199                 
1200                 /* Get the secure-auth setting: */
1201                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1202                         dialog->account_manager, outgoing_account->account_name);
1203                 modest_secureauth_combo_box_set_active_secureauth (
1204                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1205                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1206                 
1207                 modest_serversecurity_combo_box_fill (
1208                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1209                 
1210                 /* Get the security setting: */
1211                 const ModestConnectionProtocol security = modest_server_account_get_security (
1212                         dialog->account_manager, outgoing_account->account_name);
1213                 modest_serversecurity_combo_box_set_active_serversecurity (
1214                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1215                 
1216                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1217                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1218                 if (port_num == 0) {
1219                         /* Show the appropriate port number: */
1220                         on_combo_outgoing_security_changed (
1221                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1222                 }
1223                 else {
1224                         /* Keep the user-entered port-number,
1225                          * or the already-appropriate automatic port number: */
1226                         hildon_number_editor_set_value (
1227                                 HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1228                 }
1229                 
1230                 const gboolean has_specific = 
1231                         modest_account_mgr_get_has_connection_specific_smtp (
1232                                 dialog->account_manager, 
1233                                 account_name);
1234                 gtk_toggle_button_set_active (
1235                         GTK_TOGGLE_BUTTON (dialog->checkbox_outgoing_smtp_specific), 
1236                         has_specific);
1237         }
1238
1239         /* Set window title according to account: */
1240         /* TODO: Is this the correct way to find a human-readable name for
1241          * the protocol used? */
1242         const gchar* proto_str = modest_protocol_info_get_transport_store_protocol_name (dialog->incoming_protocol);
1243         gchar *proto_name = g_utf8_strup(proto_str, -1);
1244         gchar *account_title = modest_account_mgr_get_display_name(dialog->account_manager, account_name);
1245
1246         gchar *title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1247         g_free (proto_name);
1248         g_free (account_title);
1249
1250         gtk_window_set_title (GTK_WINDOW (dialog), title);
1251         g_free (title);
1252
1253         /* account_data->is_enabled,  */
1254         /*account_data->is_default,  */
1255
1256         /* account_data->store_account->proto */
1257
1258         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1259         
1260         /* Unset the modified flag so we can detect changes later: */
1261         dialog->modified = FALSE;
1262 }
1263
1264 /** Show the User Info tab.
1265  */
1266 void modest_account_settings_dialog_switch_to_user_info (ModestAccountSettingsDialog *dialog)
1267 {
1268         const gint page_num = gtk_notebook_page_num (dialog->notebook, dialog->page_user_details);
1269         if (page_num == -1) {
1270                 g_warning ("%s: notebook page not found.\n", __FUNCTION__);     
1271         }
1272                 
1273         /* Ensure that the widget is visible so that gtk_notebook_set_current_page() works: */
1274         /* TODO: even this hack (recommened by the GTK+ documentation) doesn't seem to work. */
1275         
1276         gtk_widget_show (dialog->page_user_details);
1277         gtk_widget_show (GTK_WIDGET (dialog->notebook));
1278         gtk_widget_show (GTK_WIDGET (dialog));
1279         gtk_notebook_set_current_page (dialog->notebook, page_num);
1280 }
1281
1282 static gboolean
1283 save_configuration (ModestAccountSettingsDialog *dialog)
1284 {
1285         g_assert (dialog->account_name);
1286         
1287         const gchar* account_name = dialog->account_name;
1288                 
1289         /* Set the account data from the widgets: */
1290         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1291         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1292                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1293         if (!test)
1294                 return FALSE;
1295                 
1296         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1297         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1298                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1299         if (!test)
1300                 return FALSE;
1301                 
1302         /* Signature: */
1303         if (dialog->signature_dialog) {
1304                 gboolean use_signature = FALSE;
1305         gchar *signature = modest_signature_editor_dialog_get_settings (
1306                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1307                 &use_signature);
1308         
1309         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1310                 signature, use_signature);
1311         g_free (signature);
1312     }
1313         
1314         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1315                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1316         modest_account_mgr_set_string (dialog->account_manager, account_name,
1317                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1318         g_free (retrieve);
1319         
1320         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1321                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1322         modest_account_mgr_set_int (dialog->account_manager, account_name,
1323                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1324         
1325         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1326         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1327                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1328         if (!test)
1329                 return FALSE;
1330                         
1331         /* Incoming: */
1332         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1333                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1334         g_assert (incoming_account_name);
1335         
1336         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1337         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1338                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1339         if (!test)
1340                 return FALSE;
1341                                 
1342         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1343         modest_server_account_set_username (dialog->account_manager, incoming_account_name, username);
1344         
1345         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1346         modest_server_account_set_password (dialog->account_manager, incoming_account_name, password);
1347                         
1348         /* port: */
1349         gint port_num = hildon_number_editor_get_value (
1350                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1351         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1352                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1353                         
1354         /* The UI spec says:
1355          * If secure authentication is unchecked, allow sending username and password also as plain text.
1356          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1357          */
1358         
1359         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1360                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1361         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1362         
1363         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, dialog->protocol_authentication_incoming);
1364         
1365                 
1366         g_free (incoming_account_name);
1367         
1368         /* Outgoing: */
1369         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1370                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1371         g_assert (outgoing_account_name);
1372         
1373         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1374         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1375                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1376         if (!test)
1377                 return FALSE;
1378                 
1379         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1380         modest_server_account_set_username (dialog->account_manager, outgoing_account_name,
1381                 username);
1382                 
1383         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1384         modest_server_account_set_password (dialog->account_manager, outgoing_account_name,
1385                 password);
1386         
1387         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1388                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1389         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1390         
1391         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1392                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1393         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1394         
1395         /* port: */
1396         port_num = hildon_number_editor_get_value (
1397                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1398         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1399                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1400                         
1401         g_free (outgoing_account_name);
1402         
1403         
1404         /* Set the changed account title last, to simplify the previous code: */
1405         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1406         if ((!account_title) || (strlen(account_title) == 0))
1407                 return FALSE; /* Should be prevented already anyway. */
1408                 
1409         if (strcmp(account_title, account_name) != 0) {
1410                 /* Change the title: */
1411                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1412                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1413                 if (!test)
1414                         return FALSE;
1415         }
1416         
1417         /* Save connection-specific SMTP server accounts: */
1418         if (dialog->specific_window) {
1419                 return modest_connection_specific_smtp_window_save_server_accounts (
1420                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1421         }
1422         else
1423                 return TRUE;
1424 }
1425
1426 static gboolean entry_is_empty (GtkWidget *entry)
1427 {
1428         if (!entry)
1429                 return FALSE;
1430                 
1431         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1432         if ((!text) || (strlen(text) == 0))
1433                 return TRUE;
1434         else
1435                 return FALSE;
1436 }
1437
1438 static void
1439 enable_buttons (ModestAccountSettingsDialog *self)
1440 {
1441         gboolean enable_ok = TRUE;
1442         
1443         /* The account details title is mandatory: */
1444         if (entry_is_empty(self->entry_account_title))
1445                         enable_ok = FALSE;
1446
1447         /* The user details username is mandatory: */
1448         if (entry_is_empty(self->entry_user_username))
1449                 enable_ok = FALSE;
1450                 
1451         /* The user details email address is mandatory: */
1452         if (enable_ok && entry_is_empty (self->entry_user_email))
1453                 enable_ok = FALSE;
1454
1455         /* The custom incoming server is mandatory: */
1456         if (entry_is_empty(self->entry_incomingserver))
1457                 enable_ok = FALSE;
1458                         
1459         /* Enable the buttons, 
1460          * identifying them via their associated response codes:
1461          */
1462         GtkDialog *dialog_base = GTK_DIALOG (self);
1463     gtk_dialog_set_response_sensitive (dialog_base,
1464                                        GTK_RESPONSE_OK,
1465                                        enable_ok);
1466 }
1467
1468 static void
1469 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1470 {
1471         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1472         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1473
1474
1475         object_class->get_property = modest_account_settings_dialog_get_property;
1476         object_class->set_property = modest_account_settings_dialog_set_property;
1477         object_class->dispose = modest_account_settings_dialog_dispose;
1478         object_class->finalize = modest_account_settings_dialog_finalize;
1479 }
1480  
1481 static void
1482 show_error (GtkWidget *parent_widget, const gchar* text)
1483 {
1484         hildon_banner_show_information(parent_widget, NULL, text);
1485         
1486 #if 0
1487         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_widget, text);
1488         /*
1489           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1490           (GtkDialogFlags)0,
1491           GTK_MESSAGE_ERROR,
1492           GTK_BUTTONS_OK,
1493           text ));
1494         */
1495                  
1496         gtk_dialog_run (dialog);
1497         gtk_widget_destroy (GTK_WIDGET (dialog));
1498 #endif
1499 }