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