9d8e4330e35fec9c27b7698e4ecd9abaeb35251f
[modest] / src / hildon2 / modest-default-account-settings-dialog.c
1 /* Copyright (c) 2008, 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 <glib/gi18n.h>
32 #include <gtk/gtknotebook.h>
33 #include <gtk/gtkvbox.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkcombobox.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkcheckbutton.h>
39 #include <gtk/gtkmessagedialog.h>
40 #include <gtk/gtkstock.h>
41 #include "modest-hildon-includes.h"
42 #include "modest-default-account-settings-dialog.h"
43 #include "modest-account-mgr.h"
44 #include "widgets/modest-secureauth-combo-box.h"
45 #include "widgets/modest-validating-entry.h"
46 #include "widgets/modest-retrieve-combo-box.h"
47 #include "widgets/modest-limit-retrieve-combo-box.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
51 #include <modest-server-account-settings.h>
52 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
53 #include "modest-connection-specific-smtp-window.h"
54 #include "modest-signature-editor-dialog.h"
55 #include <modest-utils.h>
56 #include <modest-defs.h>
57 #include "modest-maemo-utils.h"
58 #include "modest-maemo-security-options-view.h"
59 #include "widgets/modest-ui-constants.h"
60 #include <tny-account.h>
61 #include <tny-status.h>
62
63 #include <gconf/gconf-client.h>
64 #include <string.h> /* For strlen(). */
65
66 /* Include config.h so that _() works: */
67 #ifdef HAVE_CONFIG_H
68 #include <config.h>
69 #endif
70
71 #define PORT_MIN 1
72 #define PORT_MAX 65535
73
74 static void modest_account_settings_dialog_init (gpointer g, gpointer iface_data);
75
76 G_DEFINE_TYPE_EXTENDED (ModestDefaultAccountSettingsDialog, 
77                         modest_default_account_settings_dialog, 
78                         GTK_TYPE_DIALOG,
79                         0, 
80                         G_IMPLEMENT_INTERFACE (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, 
81                                                modest_account_settings_dialog_init));
82
83 #define MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
84         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_DEFAULT_ACCOUNT_SETTINGS_DIALOG, ModestDefaultAccountSettingsDialogPrivate))
85
86 typedef struct _ModestDefaultAccountSettingsDialogPrivate ModestDefaultAccountSettingsDialogPrivate;
87
88 struct _ModestDefaultAccountSettingsDialogPrivate
89 {
90         /* Used by derived widgets to query existing accounts,
91          * and to create new accounts: */
92         ModestAccountMgr *account_manager;
93         ModestAccountSettings *settings;
94         
95         gboolean modified;
96         gchar * account_name; /* This may not change. It is not user visible. */
97         ModestProtocolType incoming_protocol; /* This may not change. */
98         ModestProtocolType outgoing_protocol; /* This may not change. */
99         gchar * original_account_title;
100
101         ModestProtocolType protocol_authentication_incoming;
102         
103         GtkNotebook *notebook;
104         
105         GtkWidget *page_account_details;
106         GtkWidget *entry_account_title;
107         GtkWidget *combo_retrieve;
108         GtkWidget *combo_limit_retrieve;
109         GtkWidget *caption_leave_messages;
110         GtkWidget *checkbox_leave_messages;
111         
112         GtkWidget *page_user_details;
113         GtkWidget *entry_user_name;
114         GtkWidget *entry_user_username;
115         GtkWidget *entry_user_password;
116         GtkWidget *entry_user_email;
117 /*      GtkWidget *entry_incoming_port; */
118         GtkWidget *button_signature;
119         
120         GtkWidget *page_complete_easysetup;
121         
122         GtkWidget *page_incoming;
123         GtkWidget *caption_incoming;
124         GtkWidget *entry_incomingserver;
125 /*      GtkWidget *combo_incoming_security; */
126 /*      GtkWidget *checkbox_incoming_auth; */
127
128         GtkWidget *page_outgoing;
129         GtkWidget *entry_outgoingserver;
130 /*      GtkWidget *caption_outgoing_username; */
131 /*      GtkWidget *entry_outgoing_username; */
132 /*      GtkWidget *caption_outgoing_password; */
133 /*      GtkWidget *entry_outgoing_password; */
134 /*      GtkWidget *combo_outgoing_security; */
135 /*      GtkWidget *combo_outgoing_auth; */
136 /*      GtkWidget *entry_outgoing_port; */
137         GtkWidget *checkbox_outgoing_smtp_specific;
138         GtkWidget *button_outgoing_smtp_servers;
139         
140         GtkWidget *signature_dialog;
141
142         GtkWidget *incoming_security;
143         GtkWidget *outgoing_security;
144 };
145
146 static void
147 enable_buttons (ModestDefaultAccountSettingsDialog *self);
148
149 static gboolean
150 save_configuration (ModestDefaultAccountSettingsDialog *dialog);
151
152 static const gchar * null_means_empty (const gchar * str);
153
154 static const gchar *
155 null_means_empty (const gchar * str)
156 {
157         return str ? str : "";
158 }
159
160 static void
161 modest_default_account_settings_dialog_dispose (GObject *object)
162 {
163         if (G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->dispose)
164                 G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->dispose (object);
165 }
166
167 static void
168 modest_default_account_settings_dialog_finalize (GObject *object)
169 {
170         ModestDefaultAccountSettingsDialog *self;
171         ModestDefaultAccountSettingsDialogPrivate *priv;
172
173         self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (object);
174         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
175         
176         if (priv->account_name)
177                 g_free (priv->account_name);
178                 
179         if (priv->original_account_title)
180                 g_free (priv->original_account_title);
181                 
182         if (priv->account_manager)
183                 g_object_unref (G_OBJECT (priv->account_manager));
184                 
185         if (priv->signature_dialog)
186                 gtk_widget_destroy (priv->signature_dialog);
187
188         if (priv->settings) {
189                 g_object_unref (priv->settings);
190                 priv->settings = NULL;
191         }
192         
193         G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->finalize (object);
194 }
195
196 static void 
197 set_modified (ModestDefaultAccountSettingsDialog *self, gboolean modified)
198 {
199         ModestDefaultAccountSettingsDialogPrivate *priv;
200
201         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
202         priv->modified = modified;
203 }
204
205 static void
206 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
207 {
208         set_modified (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data), TRUE);
209 }
210
211 static void
212 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
213 {
214         set_modified (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data), TRUE);
215 }
216
217 static void
218 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
219 {
220         set_modified (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data), TRUE);
221 }
222
223 static void
224 on_modified_number_editor_changed (HildonNumberEditor *number_editor, gint new_value, gpointer user_data)
225 {
226         set_modified (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data), TRUE);
227 }
228
229 static void       
230 on_number_editor_notify (HildonNumberEditor *editor, GParamSpec *arg1, gpointer user_data)
231 {
232         ModestDefaultAccountSettingsDialog *dialog = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
233         gint value = hildon_number_editor_get_value (editor);
234
235         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, value > 0);
236 }
237
238 /* Set a modified boolean whenever the widget is changed, 
239  * so we can check for it later.
240  */
241 static void
242 connect_for_modified (ModestDefaultAccountSettingsDialog *self, GtkWidget *widget)
243 {
244         if (HILDON_IS_NUMBER_EDITOR (widget)) {
245                 g_signal_connect (G_OBJECT (widget), "notify::value",
246                         G_CALLBACK (on_modified_number_editor_changed), self);
247                 g_signal_connect (G_OBJECT (widget), "notify", G_CALLBACK (on_number_editor_notify), self);
248         }
249         else if (GTK_IS_ENTRY (widget)) {
250                 g_signal_connect (G_OBJECT (widget), "changed",
251                         G_CALLBACK (on_modified_entry_changed), self);
252         } else if (GTK_IS_COMBO_BOX (widget)) {
253                 g_signal_connect (G_OBJECT (widget), "changed",
254                         G_CALLBACK (on_modified_combobox_changed), self);       
255         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
256                 g_signal_connect (G_OBJECT (widget), "toggled",
257                         G_CALLBACK (on_modified_checkbox_toggled), self);
258         }
259 }
260
261 static void
262 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
263 {
264         ModestDefaultAccountSettingsDialog *self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
265         g_assert(self);
266         enable_buttons(self);
267 }
268
269 static void
270 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
271 {
272         ModestDefaultAccountSettingsDialog *self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
273         g_assert(self);
274         enable_buttons(self);
275 }
276
277 /** This is a convenience function to create a caption containing a mandatory widget.
278  * When the widget is edited, the enable_buttons() vfunc will be called.
279  */
280 static GtkWidget* 
281 create_caption_new_with_asterisk(ModestDefaultAccountSettingsDialog *self,
282         GtkSizeGroup *group,
283         const gchar *value,
284         GtkWidget *control,
285         GtkWidget *icon,
286         HildonCaptionStatus flag)
287 {
288         GtkWidget *caption = NULL;
289   
290         /* Note: Previously, the translated strings already contained the "*",
291          * Comment out this code if they do again.
292          */
293         /* Add a * character to indicate mandatory fields,
294          * as specified in our "Email UI Specification": */
295         if (flag == HILDON_CAPTION_MANDATORY) {
296                 gchar* title = g_strdup_printf("%s*", value);
297                 caption = hildon_caption_new (group, title, control, icon, flag);       
298                 g_free(title);
299         }       
300         else
301                 caption = hildon_caption_new (group, value, control, icon, flag);
302
303         /* Connect to the appropriate changed signal for the widget, 
304          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
305          */
306         if (GTK_IS_ENTRY (control)) {
307                 g_signal_connect (G_OBJECT (control), "changed",
308                 G_CALLBACK (on_caption_entry_changed), self);
309                 
310         }
311         else if (GTK_IS_COMBO_BOX (control)) {
312                 g_signal_connect (G_OBJECT (control), "changed",
313                 G_CALLBACK (on_caption_combobox_changed), self);
314         }
315          
316         return caption;
317 }
318
319 static void
320 on_entry_invalid_account_title_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
321 {
322         gchar *tmp, *msg;
323                         
324         tmp = g_strndup (account_title_forbidden_chars, ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH);
325         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
326
327         modest_platform_information_banner (GTK_WIDGET (self), NULL, msg);
328
329         g_free (msg);
330         g_free (tmp);
331 }
332
333 static void
334 on_entry_invalid_fullname_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
335 {
336         gchar *tmp, *msg;
337                         
338         tmp = g_strndup (user_name_forbidden_chars, USER_NAME_FORBIDDEN_CHARS_LENGTH);
339         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
340
341         modest_platform_information_banner (GTK_WIDGET (self), NULL, msg);
342
343         g_free (msg);
344         g_free (tmp);
345 }
346
347
348 static void
349 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
350 {
351         modest_platform_information_banner (GTK_WIDGET (self), NULL, 
352                                             _CS("ckdg_ib_maximum_characters_reached"));
353 }
354
355 static GtkWidget*
356 create_page_account_details (ModestDefaultAccountSettingsDialog *self)
357 {
358         ModestDefaultAccountSettingsDialogPrivate *priv;
359         GtkWidget *box;
360         GtkAdjustment *focus_adjustment = NULL;
361         GtkSizeGroup* sizegroup;
362         GtkWidget *scrollwin;
363
364         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
365         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
366         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
367         scrollwin = gtk_scrolled_window_new (NULL, NULL);
368         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
369                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
370            
371         /* The description widgets: */  
372         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
373         /* Do use auto-capitalization: */
374         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_account_title), 
375                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
376         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
377                 priv->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
378         gtk_widget_show (priv->entry_account_title);
379         connect_for_modified (self, priv->entry_account_title);
380         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
381         gtk_widget_show (caption);
382         
383         /* Prevent the use of some characters in the account title, 
384          * as required by our UI specification: */
385         GList *list_prevent = NULL;
386         list_prevent = g_list_append (list_prevent, "\\");
387         list_prevent = g_list_append (list_prevent, "/");
388         list_prevent = g_list_append (list_prevent, ":");
389         list_prevent = g_list_append (list_prevent, "*");
390         list_prevent = g_list_append (list_prevent, "?");
391         list_prevent = g_list_append (list_prevent, "\"");
392         list_prevent = g_list_append (list_prevent, "<"); 
393         list_prevent = g_list_append (list_prevent, ">"); 
394         list_prevent = g_list_append (list_prevent, "|");
395         list_prevent = g_list_append (list_prevent, "^");       
396         modest_validating_entry_set_unallowed_characters (
397                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
398         g_list_free (list_prevent);
399         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
400                                          on_entry_invalid_account_title_character, self);
401         
402         /* Set max length as in the UI spec:
403          * The UI spec seems to want us to show a dialog if we hit the maximum. */
404         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
405         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title), 
406                 on_entry_max, self);
407         
408         /* The retrieve combobox: */
409         priv->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
410         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
411                 priv->combo_retrieve, NULL, HILDON_CAPTION_MANDATORY);
412         gtk_widget_show (priv->combo_retrieve);
413         connect_for_modified (self, priv->combo_retrieve);
414         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
415         gtk_widget_show (caption);
416         
417         /* The limit-retrieve combobox: */
418         priv->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
419         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
420                 priv->combo_limit_retrieve, NULL, HILDON_CAPTION_MANDATORY);
421         gtk_widget_show (priv->combo_limit_retrieve);
422         connect_for_modified (self, priv->combo_limit_retrieve);
423         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
424         gtk_widget_show (caption);
425
426         /* The leave-messages widgets: */
427         if(!priv->checkbox_leave_messages)
428                 priv->checkbox_leave_messages = gtk_check_button_new ();
429         if (!priv->caption_leave_messages) {
430                 priv->caption_leave_messages = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
431                         priv->checkbox_leave_messages, NULL, HILDON_CAPTION_MANDATORY);
432         }
433                         
434         gtk_widget_show (priv->checkbox_leave_messages);
435         connect_for_modified (self, priv->checkbox_leave_messages);
436         gtk_box_pack_start (GTK_BOX (box), priv->caption_leave_messages, FALSE, FALSE, MODEST_MARGIN_HALF);
437         gtk_widget_show (priv->caption_leave_messages);
438
439         g_object_unref (sizegroup);
440         
441         gtk_widget_show (GTK_WIDGET (box));
442         
443         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollwin), box);
444         gtk_widget_show (scrollwin);
445
446         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
447         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment); 
448         
449         return GTK_WIDGET (scrollwin);
450 }
451
452 static gchar*
453 get_entered_account_title (ModestDefaultAccountSettingsDialog *dialog)
454 {
455         ModestDefaultAccountSettingsDialogPrivate *priv;
456         const gchar* account_title;
457
458         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
459         account_title = gtk_entry_get_text (GTK_ENTRY (priv->entry_account_title));
460
461         if (!account_title || (strlen (account_title) == 0))
462                 return NULL;
463         else {
464                 /* Strip it of whitespace at the start and end: */
465                 gchar *result = g_strdup (account_title);
466                 result = g_strstrip (result);
467                 
468                 if (!result)
469                         return NULL;
470                         
471                 if (strlen (result) == 0) {
472                         g_free (result);
473                         return NULL;    
474                 }
475                 
476                 return result;
477         }
478 }
479
480
481 static void
482 on_button_signature (GtkButton *button, gpointer user_data)
483 {
484         ModestDefaultAccountSettingsDialog *self;
485         gint response;
486         ModestDefaultAccountSettingsDialogPrivate *priv;
487
488         self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
489         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
490
491         /* Create the window, if necessary: */
492         if (!(priv->signature_dialog)) {
493                 priv->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ());
494         
495                 gboolean use_signature = modest_account_settings_get_use_signature (priv->settings);
496                 const gchar *signature = modest_account_settings_get_signature(priv->settings);
497                 gchar* account_title = get_entered_account_title (self);
498                 modest_signature_editor_dialog_set_settings (
499                         MODEST_SIGNATURE_EDITOR_DIALOG (priv->signature_dialog), 
500                         use_signature, signature, account_title);
501
502                 g_free (account_title);
503                 account_title = NULL;
504                 signature = NULL;
505         }
506
507         /* Show the window: */  
508         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
509                                      GTK_WINDOW (priv->signature_dialog));
510
511         response = gtk_dialog_run (GTK_DIALOG (priv->signature_dialog));
512         gtk_widget_hide (priv->signature_dialog);
513         if (response != GTK_RESPONSE_OK) {
514                 /* Destroy the widget now, and its data: */
515                 gtk_widget_destroy (priv->signature_dialog);
516                 priv->signature_dialog = NULL;
517         } else {
518                 /* Mark modified, so we use the dialog's data later: */
519                 priv->modified = TRUE;  
520         }
521 }
522
523 static GtkWidget*
524 create_page_user_details (ModestDefaultAccountSettingsDialog *self)
525 {
526         ModestDefaultAccountSettingsDialogPrivate *priv;
527         GtkWidget *box;
528         GtkAdjustment *focus_adjustment = NULL;
529         GtkSizeGroup* sizegroup;
530         GtkWidget *scrollwin;
531
532         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
533
534         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
535         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
536         scrollwin = gtk_scrolled_window_new (NULL, NULL);
537         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
538                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
539          
540         /* The name widgets: */
541         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
542
543         /* Auto-capitalization is the default, so let's turn it off: */
544         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
545         /* Set max length as in the UI spec:
546          * The UI spec seems to want us to show a dialog if we hit the maximum. */
547         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
548         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
549                 on_entry_max, self);
550         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
551                 _("mcen_li_emailsetup_name"), priv->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
552         gtk_widget_show (priv->entry_user_name);
553         connect_for_modified (self, priv->entry_user_name);
554         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
555         gtk_widget_show (caption);
556
557
558         /* Prevent the use of some characters in the name, 
559          * as required by our UI specification: */
560         GList *list_prevent = NULL;
561         list_prevent = g_list_append (list_prevent, "<");
562         list_prevent = g_list_append (list_prevent, ">");
563         modest_validating_entry_set_unallowed_characters (
564                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
565         g_list_free (list_prevent);
566         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
567                                          on_entry_invalid_fullname_character, self);
568         
569         /* The username widgets: */     
570         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
571         /* Auto-capitalization is the default, so let's turn it off: */
572         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
573         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
574                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
575         gtk_widget_show (priv->entry_user_username);
576         connect_for_modified (self, priv->entry_user_username);
577         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
578         gtk_widget_show (caption);
579         
580         /* Prevent the use of some characters in the username, 
581          * as required by our UI specification: */
582         modest_validating_entry_set_unallowed_characters_whitespace (
583                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
584         modest_validating_entry_set_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
585                                           modest_utils_on_entry_invalid_character, 
586                                           self);
587         
588         /* Set max length as in the UI spec:
589          * The UI spec seems to want us to show a dialog if we hit the maximum. */
590         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
591         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
592                 on_entry_max, self);
593         
594         /* The password widgets: */     
595         priv->entry_user_password = gtk_entry_new ();
596         /* Auto-capitalization is the default, so let's turn it off: */
597         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
598                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
599         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
600         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
601         caption = create_caption_new_with_asterisk (self, sizegroup, 
602                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
603         gtk_widget_show (priv->entry_user_password);
604         connect_for_modified (self, priv->entry_user_password);
605         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
606         gtk_widget_show (caption);
607         
608         /* The email address widgets: */        
609         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
610         /* Auto-capitalization is the default, so let's turn it off: */
611         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
612         caption = create_caption_new_with_asterisk (self, sizegroup, 
613                 _("mcen_li_emailsetup_email_address"), priv->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
614         gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
615         gtk_widget_show (priv->entry_user_email);
616         connect_for_modified (self, priv->entry_user_email);
617         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
618         gtk_widget_show (caption);
619         
620         /* Set max length as in the UI spec:
621          * The UI spec seems to want us to show a dialog if we hit the maximum. */
622         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
623         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
624                 on_entry_max, self);
625         
626         /* Signature button: */
627         if (!priv->button_signature)
628                 priv->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
629         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
630                 priv->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
631         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
632         gtk_widget_show (priv->button_signature);
633         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
634         gtk_widget_show (caption);
635
636         g_object_unref (sizegroup);
637                 
638         g_signal_connect (G_OBJECT (priv->button_signature), "clicked",
639                 G_CALLBACK (on_button_signature), self);
640                 
641         gtk_widget_show (GTK_WIDGET (box));
642         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollwin), box);
643         gtk_widget_show (scrollwin);
644
645         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
646         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment); 
647         
648         return GTK_WIDGET (scrollwin);
649 }
650
651 /* Change the caption title for the incoming server */
652 static void
653 update_incoming_server_title (ModestDefaultAccountSettingsDialog *self,
654                               ModestProtocolType protocol_type)
655 {
656         ModestProtocolRegistry *protocol_registry;
657         ModestProtocol *protocol;
658         const gchar *protocol_display_name;
659         gchar* incomingserver_title;
660         gchar *with_asterisk;
661         ModestDefaultAccountSettingsDialogPrivate *priv;
662
663         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
664
665         protocol_registry = modest_runtime_get_protocol_registry ();
666         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
667         protocol_display_name = modest_protocol_get_display_name (protocol);
668         incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), 
669                                                protocol_display_name);
670         
671         /* This is a mandatory field, so add a *. This is usually done by
672          * create_caption_new_with_asterisk() but we can't use that here. */
673         with_asterisk = g_strconcat (incomingserver_title, "*", NULL);
674         g_free (incomingserver_title);
675         
676         g_object_set (G_OBJECT (priv->caption_incoming), "label", with_asterisk, NULL);
677         g_free (with_asterisk);
678 }
679
680 static GtkWidget* 
681 create_page_incoming (ModestDefaultAccountSettingsDialog *self)
682 {
683         ModestDefaultAccountSettingsDialogPrivate *priv;
684         GtkWidget *box;
685         GtkSizeGroup *sizegroup;
686
687         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
688
689         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE); 
690         /* Create a size group to be used by all captions.
691          * Note that HildonCaption does not create a default size group if we do not specify one.
692          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
693         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
694          
695         /* The incoming server widgets: */
696         if(!priv->entry_incomingserver)
697                 priv->entry_incomingserver = gtk_entry_new ();
698         /* Auto-capitalization is the default, so let's turn it off: */
699         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
700
701         if (priv->caption_incoming)
702                 gtk_widget_destroy (priv->caption_incoming);
703            
704         /* The caption title will be updated in update_incoming_server_title().
705          * so this default text will never be seen: */
706         /* (Note: Changing the title seems pointless. murrayc) */
707         priv->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
708                 "Incoming Server", priv->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
709         gtk_widget_show (priv->entry_incomingserver);
710         connect_for_modified (self, priv->entry_incomingserver);
711         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
712         gtk_widget_show (priv->caption_incoming);
713
714         /* Incoming security widgets */
715         priv->incoming_security = 
716                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
717                                                         TRUE, sizegroup);
718         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security, 
719                             FALSE, FALSE, MODEST_MARGIN_HALF);
720
721         gtk_widget_show (priv->incoming_security);
722
723         g_object_unref (sizegroup);     
724         gtk_widget_show (GTK_WIDGET (box));
725         
726         return GTK_WIDGET (box);
727 }
728
729 static void
730 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
731 {
732         GtkWidget *widget = GTK_WIDGET (user_data);
733         
734         /* Enable the widget only if the toggle button is active: */
735         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
736         gtk_widget_set_sensitive (widget, enable);
737 }
738
739 /* Make the sensitivity of a widget depend on a toggle button.
740  */
741 static void
742 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
743 {
744         g_signal_connect (G_OBJECT (button), "toggled",
745                 G_CALLBACK (on_toggle_button_changed), widget);
746         
747         /* Set the starting sensitivity: */
748         on_toggle_button_changed (button, widget);
749 }
750
751 static void
752 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
753 {
754         ModestDefaultAccountSettingsDialog * self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
755         ModestConnectionSpecificSmtpWindow *smtp_win;
756         ModestDefaultAccountSettingsDialogPrivate *priv;
757
758         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
759
760         /* Create the window if necessary: */
761         smtp_win = modest_connection_specific_smtp_window_new ();
762         modest_connection_specific_smtp_window_fill_with_connections (smtp_win, priv->account_manager);
763
764         /* Show the window: */  
765         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (smtp_win));
766         gtk_widget_show (GTK_WIDGET (smtp_win));
767         priv->modified = TRUE;
768 }
769
770 static void
771 on_missing_mandatory_data (ModestSecurityOptionsView *security_view,
772                            gboolean missing,
773                            gpointer user_data)
774 {
775         /* Disable the OK button */
776         gtk_dialog_set_response_sensitive (GTK_DIALOG (user_data),
777                                            GTK_RESPONSE_OK,
778                                            !missing);
779 }
780
781 static GtkWidget* 
782 create_page_outgoing (ModestDefaultAccountSettingsDialog *self)
783 {
784         ModestDefaultAccountSettingsDialogPrivate *priv;
785         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
786         GtkAdjustment *focus_adjustment = NULL;
787
788         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
789         
790         /* Put it all in a scrolled window, so that all widgets can be 
791          * accessed even when the on-screen keyboard is visible: */
792         GtkWidget *scrollwin = gtk_scrolled_window_new(NULL, NULL);
793         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin), 
794                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
795         
796         /* Create a size group to be used by all captions.
797          * Note that HildonCaption does not create a default size group if we do not specify one.
798          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
799         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
800          
801         /* The outgoing server widgets: */
802         if (!priv->entry_outgoingserver)
803                 priv->entry_outgoingserver = gtk_entry_new ();
804         /* Auto-capitalization is the default, so let's turn it off: */
805         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
806         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
807                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
808         gtk_widget_show (priv->entry_outgoingserver);
809         connect_for_modified (self, priv->entry_outgoingserver);
810         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
811         gtk_widget_show (caption);
812
813         /* Outgoing security widgets */
814         priv->outgoing_security = 
815                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
816                                                         TRUE, sizegroup);
817         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security, 
818                             FALSE, FALSE, MODEST_MARGIN_HALF);
819         gtk_widget_show (priv->outgoing_security);
820         g_signal_connect (priv->outgoing_security, "missing-mandatory-data",
821                           G_CALLBACK (on_missing_mandatory_data), self);
822
823         GtkWidget *separator = gtk_hseparator_new ();
824         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
825         gtk_widget_show (separator);
826         
827         /* connection-specific checkbox: */
828         if (!priv->checkbox_outgoing_smtp_specific) {
829                 priv->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
830                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
831                         FALSE);
832         }
833         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
834                 priv->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
835         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
836         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
837         gtk_widget_show (caption);
838         connect_for_modified (self, priv->checkbox_outgoing_smtp_specific);
839         
840         /* Connection-specific SMTP-Severs Edit button: */
841         if (!priv->button_outgoing_smtp_servers)
842                 priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
843         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
844                 priv->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
845         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
846         gtk_widget_show (priv->button_outgoing_smtp_servers);
847         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
848         gtk_widget_show (caption);
849         
850         /* Only enable the button when the checkbox is checked: */
851         enable_widget_for_togglebutton (priv->button_outgoing_smtp_servers, 
852                 GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific));
853
854         g_object_unref (sizegroup);
855                 
856         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
857                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
858                 
859         gtk_widget_show (GTK_WIDGET (box));
860         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollwin), box);
861         gtk_widget_show(scrollwin);
862
863         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
864         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment);
865         
866         return GTK_WIDGET (scrollwin);
867 }
868         
869 static gboolean
870 check_data (ModestDefaultAccountSettingsDialog *self)
871 {
872         gchar* account_title;
873         const gchar* email_address; 
874         const gchar* hostname;
875         const gchar* hostname2;
876         ModestDefaultAccountSettingsDialogPrivate *priv;
877
878         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
879
880         /* Check that the title is not already in use: */
881         account_title = get_entered_account_title (self);
882         if (!account_title)
883                 return FALSE; /* Should be prevented already anyway. */
884                 
885         if (strcmp(account_title, priv->original_account_title) != 0) {
886                 gboolean name_in_use; 
887
888                 /* Check the changed title: */
889                 name_in_use = modest_account_mgr_account_with_display_name_exists (priv->account_manager,
890                                                                                    account_title);
891                 
892                 if (name_in_use) {
893                         /* Warn the user via a dialog: */
894                         modest_platform_information_banner(NULL, NULL, _("mail_ib_account_name_already_existing"));
895                         
896                         g_free (account_title);
897                         return FALSE;
898                 }
899         }
900         
901         g_free (account_title);
902         account_title  = NULL;
903
904         /* Check that the email address is valid: */
905         email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
906         if ((!email_address) || (strlen(email_address) == 0)) {
907                 return FALSE;
908         }
909                         
910         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
911                 /* Warn the user via a dialog: */
912                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_email"));
913                                          
914                 /* Return focus to the email address entry: */
915                 gtk_widget_grab_focus (priv->entry_user_email);
916                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
917                 return FALSE;
918         }
919
920         /* make sure the domain name for the incoming server is valid */
921         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
922         if ((!hostname) || (strlen(hostname) == 0)) {
923                 return FALSE;
924         }
925         
926         if (!modest_text_utils_validate_domain_name (hostname)) {
927                 /* Warn the user via a dialog: */
928                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
929                                          
930                 /* Return focus to the email address entry: */
931                 gtk_widget_grab_focus (priv->entry_incomingserver);
932                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_incomingserver), 0, -1);
933                 return FALSE;
934         }
935
936         /* make sure the domain name for the outgoing server is valid */
937         hostname2 = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
938         if ((!hostname2) || (strlen(hostname2) == 0)) {
939                 return FALSE;
940         }
941         
942         if (!modest_text_utils_validate_domain_name (hostname2)) {
943                 /* Warn the user via a dialog: */
944                 modest_platform_information_banner (priv->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
945
946                 /* Return focus to the email address entry: */
947                 gtk_widget_grab_focus (priv->entry_outgoingserver);
948                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
949                 return FALSE;
950         }
951         
952         return TRUE;
953 }
954
955 static void 
956 on_response (GtkDialog *wizard_dialog,
957              gint response_id,
958              gpointer user_data)
959 {
960         ModestDefaultAccountSettingsDialog *self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
961         ModestDefaultAccountSettingsDialogPrivate *priv;
962         gboolean prevent_response = FALSE, sec_changed;
963         ModestSecurityOptionsView *incoming_sec, *outgoing_sec;
964
965         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
966         enable_buttons (self);
967
968         /* Check if security widgets changed */
969         incoming_sec = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
970         outgoing_sec = MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security);
971         sec_changed =
972                 modest_security_options_view_changed (incoming_sec, priv->settings) ||
973                 modest_security_options_view_changed (outgoing_sec, priv->settings);
974
975         /* Warn about unsaved changes: */
976         if (response_id == GTK_RESPONSE_CANCEL && (priv->modified || sec_changed)) {
977                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
978                         _("imum_nc_wizard_confirm_lose_changes")));
979                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
980                  
981                  const gint dialog_response = gtk_dialog_run (dialog);
982                  gtk_widget_destroy (GTK_WIDGET (dialog));
983                  
984                 if (dialog_response != GTK_RESPONSE_OK)
985                         prevent_response = TRUE;
986         }
987         /* Check for invalid input: */
988         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
989                 prevent_response = TRUE;
990         }
991                 
992         if (prevent_response) {
993                 /* This is a nasty hack. murrayc. */
994                 /* Don't let the dialog close */
995                 g_signal_stop_emission_by_name (wizard_dialog, "response");
996                 return; 
997         } else {
998                 modest_tny_account_store_set_send_mail_blocked (modest_runtime_get_account_store (), FALSE);
999         }
1000                 
1001         if (response_id == GTK_RESPONSE_OK) {
1002                 /* Try to save the changes if modified (NB #59251): */
1003                 if (priv->modified || sec_changed) {
1004                         if (save_configuration (self)) {
1005                                 /* Do not show the account-saved dialog if we are just saving this 
1006                                  * temporarily, because from the user's point of view it will not 
1007                                  * really be saved (saved + enabled) until later
1008                                  */
1009                                 if (modest_account_settings_get_account_name (priv->settings) != NULL) {
1010                                         ModestServerAccountSettings *store_settings;
1011                                         ModestServerAccountSettings *transport_settings;
1012                                         const gchar *store_account_name;
1013                                         const gchar *transport_account_name;
1014
1015
1016                                         store_settings = modest_account_settings_get_store_settings (priv->settings);
1017                                         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1018                                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
1019                                         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
1020                                         
1021                                         if (store_account_name) {
1022                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1023                                                                                           store_account_name);
1024                                         }
1025                                         if (transport_account_name) {
1026                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1027                                                                                           transport_account_name);
1028                                         }
1029                                         g_object_unref (store_settings);
1030                                         g_object_unref (transport_settings);
1031                                         
1032                                         modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1033                                 }
1034                         } else {
1035                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1036                         }
1037                 }
1038         }
1039 }
1040
1041 static void
1042 modest_default_account_settings_dialog_init (ModestDefaultAccountSettingsDialog *self)
1043 {
1044         ModestDefaultAccountSettingsDialogPrivate *priv;
1045
1046         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(self);
1047
1048         priv->incoming_security = NULL;
1049         priv->outgoing_security = NULL;
1050
1051         /* Create the notebook to be used by the GtkDialog base class:
1052          * Each page of the notebook will be a page of the wizard: */
1053         priv->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1054         priv->settings = modest_account_settings_new ();
1055
1056         /* Get the account manager object, 
1057          * so we can check for existing accounts,
1058          * and create new accounts: */
1059         priv->account_manager = modest_runtime_get_account_mgr ();
1060         g_assert (priv->account_manager);
1061         g_object_ref (priv->account_manager);
1062         
1063         priv->protocol_authentication_incoming = MODEST_PROTOCOLS_AUTH_PASSWORD;
1064
1065     /* Create the common pages, 
1066      */
1067         priv->page_account_details = create_page_account_details (self);
1068         priv->page_user_details = create_page_user_details (self);
1069         priv->page_incoming = create_page_incoming (self);
1070         priv->page_outgoing = create_page_outgoing (self);
1071         
1072         /* Add the notebook pages: */
1073         gtk_notebook_append_page (priv->notebook, priv->page_account_details, 
1074                 gtk_label_new (_("mcen_ti_account_settings_account")));
1075         gtk_notebook_append_page (priv->notebook, priv->page_user_details, 
1076                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1077         gtk_notebook_append_page (priv->notebook, priv->page_incoming,
1078                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1079         gtk_notebook_append_page (priv->notebook, priv->page_outgoing,
1080                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1081                 
1082         GtkDialog *dialog = GTK_DIALOG (self);
1083         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (priv->notebook));
1084         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1085         gtk_widget_show (GTK_WIDGET (priv->notebook));
1086         
1087     /* Add the buttons: */
1088     gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
1089     gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
1090     
1091     /* Connect to the dialog's response signal: */
1092     /* We use connect-before 
1093      * so we can stop the signal emission, 
1094      * to stop the default signal handler from closing the dialog.
1095      */
1096     g_signal_connect (G_OBJECT (self), "response",
1097             G_CALLBACK (on_response), self); 
1098             
1099     priv->modified = FALSE;
1100
1101     /* When this window is shown, hibernation should not be possible, 
1102          * because there is no sensible way to save the state: */
1103     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1104         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1105
1106     /* Prevent sending mails while editing an account, to avoid hangs on unprotected locks
1107      * while sending messages causes an error dialog and we have a lock */
1108     modest_tny_account_store_set_send_mail_blocked (modest_runtime_get_account_store (), TRUE);
1109
1110     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings",
1111                                     modest_maemo_utils_get_osso_context());
1112 }
1113
1114 ModestAccountSettingsDialog*
1115 modest_default_account_settings_dialog_new (void)
1116 {
1117         return g_object_new (MODEST_TYPE_DEFAULT_ACCOUNT_SETTINGS_DIALOG, NULL);
1118 }
1119
1120 /** Update the UI with the stored account details, so they can be edited.
1121  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1122  */
1123 static void 
1124 modest_default_account_settings_dialog_load_settings (ModestAccountSettingsDialog *dialog, 
1125                                                       ModestAccountSettings *settings)
1126 {
1127         ModestServerAccountSettings *incoming_account;
1128         ModestServerAccountSettings *outgoing_account;
1129         ModestProtocolRegistry *protocol_registry;
1130         const gchar *account_name, *server_account_name;
1131         ModestDefaultAccountSettingsDialogPrivate *priv;
1132         gint page_num;
1133         gboolean username_known;
1134
1135         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
1136         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1137
1138         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1139         protocol_registry = modest_runtime_get_protocol_registry ();
1140
1141         incoming_account = modest_account_settings_get_store_settings (settings);
1142         outgoing_account = modest_account_settings_get_transport_settings (settings);
1143
1144         account_name = modest_account_settings_get_account_name (settings);
1145                 
1146         /* Save the account name so we can refer to it later: */
1147         if (priv->account_name)
1148                 g_free (priv->account_name);
1149         priv->account_name = g_strdup (account_name);
1150
1151         if (priv->settings)
1152                 g_object_unref (priv->settings);
1153         priv->settings = g_object_ref (settings);
1154         
1155         /* Save the account title so we can refer to it if the user changes it: */
1156         if (priv->original_account_title)
1157                 g_free (priv->original_account_title);
1158         priv->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1159         
1160         /* Show the account data in the widgets: */
1161         
1162         /* Note that we never show the non-display name in the UI.
1163          * (Though the display name defaults to the non-display name at the start.) */
1164         gtk_entry_set_text( GTK_ENTRY (priv->entry_account_title),
1165                             null_means_empty (modest_account_settings_get_display_name (settings)));
1166         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_name), 
1167                             null_means_empty (modest_account_settings_get_fullname (settings)));
1168         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_email), 
1169                             null_means_empty (modest_account_settings_get_email_address (settings)));
1170         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), 
1171                                                             modest_account_settings_get_retrieve_type (settings));
1172         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1173                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve), 
1174                 modest_account_settings_get_retrieve_limit (settings));
1175         
1176         
1177         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages), 
1178                                       modest_account_settings_get_leave_messages_on_server (settings));
1179         
1180
1181         if (incoming_account) {
1182                 const gchar *username, *password, *hostname, *proto_str, *account_title;
1183                 gchar *proto_name, *title;
1184                 ModestProtocolType incoming_protocol;
1185
1186                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), modest_server_account_settings_get_protocol (incoming_account));
1187                 
1188                 if (!modest_protocol_registry_protocol_type_has_leave_on_server (protocol_registry,
1189                                                                                  modest_server_account_settings_get_protocol (incoming_account))) {
1190                         gtk_widget_hide (priv->caption_leave_messages);
1191                 } else {
1192                         gtk_widget_show (priv->caption_leave_messages);
1193                 }
1194
1195                 /* Remember this for later: */
1196                 incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1197                 
1198                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1199                 username = modest_server_account_settings_get_username (incoming_account);
1200                 password = modest_server_account_settings_get_password (incoming_account);
1201                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_username),
1202                                     null_means_empty (username));
1203                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_password), 
1204                                     null_means_empty (password));
1205                         
1206                 gtk_entry_set_text( GTK_ENTRY (priv->entry_incomingserver), 
1207                                     null_means_empty (hostname));
1208
1209                 /* Load security settings */
1210                 modest_security_options_view_load_settings (
1211                             MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security), 
1212                             settings);
1213                 gtk_widget_show (priv->incoming_security);
1214                                         
1215                 /* Update the incoming label */
1216                 update_incoming_server_title (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (dialog), 
1217                                               incoming_protocol);
1218                 
1219                 /* Set window title according to account */
1220                 proto_str = modest_protocol_get_display_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, incoming_protocol));
1221                 proto_name = g_utf8_strup (proto_str, -1);
1222                 account_title = modest_account_settings_get_display_name(settings);
1223                 title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1224                 
1225                 gtk_window_set_title (GTK_WINDOW (dialog), title);
1226
1227                 g_free (proto_name);
1228                 g_free (title);
1229                 g_object_unref (incoming_account);
1230         }
1231         
1232         outgoing_account = modest_account_settings_get_transport_settings (settings);
1233         if (outgoing_account) {
1234                 const gchar *hostname;
1235                 const gchar *username;
1236                 const gchar *password;
1237                 ModestProtocolType outgoing_protocol;
1238
1239                 /* Remember this for later: */
1240                 outgoing_protocol = 
1241                         modest_server_account_settings_get_protocol (outgoing_account);
1242
1243                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1244                 username = modest_server_account_settings_get_username (outgoing_account);
1245                 password = modest_server_account_settings_get_password (outgoing_account);
1246                 gtk_entry_set_text( GTK_ENTRY (priv->entry_outgoingserver), 
1247                                     null_means_empty (hostname));
1248
1249                 /* Load security settings */
1250                 modest_security_options_view_load_settings (
1251                             MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security), 
1252                             settings);
1253                 gtk_widget_show (priv->outgoing_security);
1254
1255                 const gboolean has_specific = 
1256                         modest_account_settings_get_use_connection_specific_smtp (settings);
1257                 gtk_toggle_button_set_active (
1258                         GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
1259                         has_specific);
1260                 g_object_unref (outgoing_account);
1261         }
1262
1263         /* Switch to user page */
1264         page_num = gtk_notebook_page_num (priv->notebook,priv->page_user_details);
1265         gtk_notebook_set_current_page (priv->notebook, page_num);
1266
1267         /* Check if we allow changes or not */
1268         server_account_name = modest_server_account_settings_get_account_name (incoming_account);
1269         username_known = 
1270                 modest_account_mgr_get_server_account_username_has_succeeded (priv->account_manager,
1271                                                                               server_account_name);
1272         gtk_widget_set_sensitive (priv->entry_user_username, !username_known);
1273         gtk_widget_set_sensitive (priv->entry_incomingserver, !username_known);
1274         modest_security_options_view_enable_changes (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
1275                                                      !username_known);
1276
1277
1278         /* Unset the modified flag so we can detect changes later: */
1279         priv->modified = FALSE;
1280 }
1281
1282 static gboolean
1283 save_configuration (ModestDefaultAccountSettingsDialog *dialog)
1284 {
1285         const gchar* user_fullname;
1286         const gchar* emailaddress;
1287         ModestServerAccountSettings *store_settings;
1288         ModestServerAccountSettings *transport_settings;
1289         ModestAccountRetrieveType retrieve_type;
1290         gint retrieve_limit;
1291         gboolean leave_on_server;
1292         const gchar* hostname;
1293         const gchar* username;
1294         const gchar* password;
1295         gchar* account_title;
1296         ModestDefaultAccountSettingsDialogPrivate *priv;
1297         const gchar* account_name;
1298
1299         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1300         account_name = priv->account_name;
1301
1302         /* Set the account data from the widgets: */
1303         user_fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
1304         modest_account_settings_set_fullname (priv->settings, user_fullname);
1305         
1306         emailaddress = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1307         modest_account_settings_set_email_address (priv->settings, emailaddress);
1308                 
1309         /* Signature: */
1310         if (priv->signature_dialog) {
1311                 gboolean use_signature = FALSE;
1312                 gchar *signature;
1313                 signature = modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (priv->signature_dialog),
1314                                                                          &use_signature);
1315         
1316                 modest_account_settings_set_use_signature (priv->settings, use_signature);
1317                 modest_account_settings_set_signature (priv->settings, signature);
1318         }
1319         
1320         retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1321                 MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve));
1322         modest_account_settings_set_retrieve_type (priv->settings, retrieve_type);
1323         
1324         retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1325                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve));
1326         modest_account_settings_set_retrieve_limit (priv->settings, retrieve_limit);
1327         
1328         leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages));
1329         modest_account_settings_set_leave_messages_on_server (priv->settings, leave_on_server); 
1330
1331         store_settings = modest_account_settings_get_store_settings (priv->settings);
1332                         
1333         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
1334         modest_server_account_settings_set_hostname (store_settings, hostname);
1335                                 
1336         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1337         modest_server_account_settings_set_username (store_settings, username);
1338         
1339         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1340         modest_server_account_settings_set_password (store_settings, password);
1341
1342         /* Save security settings */
1343         modest_security_options_view_save_settings (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security), 
1344                                                     priv->settings);
1345
1346         g_object_unref (store_settings);
1347         
1348         /* Outgoing: */
1349         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1350         
1351         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
1352         modest_server_account_settings_set_hostname (transport_settings, hostname);
1353                         
1354         /* Save security settings */
1355         modest_security_options_view_save_settings (
1356             MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security), 
1357             priv->settings);
1358         
1359         /* Set the changed account title last, to simplify the previous code: */
1360         account_title = get_entered_account_title (dialog);
1361         if (!account_title)
1362                 return FALSE; /* Should be prevented already anyway. */
1363                 
1364 /*      if (strcmp (account_title, account_name) != 0) { */
1365         modest_account_settings_set_display_name (priv->settings, account_title);
1366 /*      } */
1367         g_free (account_title);
1368         account_title = NULL;
1369         
1370         /* Save connection-specific SMTP server accounts: */
1371         modest_account_settings_set_use_connection_specific_smtp 
1372                 (priv->settings, 
1373                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
1374
1375         /* this configuration is not persistent, we should not save */
1376         if (account_name != NULL)
1377                 modest_account_mgr_save_account_settings (priv->account_manager, priv->settings);
1378
1379         return TRUE;
1380 }
1381
1382 static gboolean entry_is_empty (GtkWidget *entry)
1383 {
1384         if (!entry)
1385                 return FALSE;
1386                 
1387         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1388         if ((!text) || (strlen(text) == 0))
1389                 return TRUE;
1390         else {
1391                 /* Strip it of whitespace at the start and end: */
1392                 gchar *stripped = g_strdup (text);
1393                 stripped = g_strstrip (stripped);
1394                 
1395                 if (!stripped)
1396                         return TRUE;
1397                         
1398                 const gboolean result = (strlen (stripped) == 0);
1399                 
1400                 g_free (stripped);
1401                 return result;
1402         }
1403 }
1404
1405 static void
1406 enable_buttons (ModestDefaultAccountSettingsDialog *self)
1407 {
1408         gboolean enable_ok = TRUE;
1409         ModestProtocolRegistry *protocol_registry;
1410         ModestDefaultAccountSettingsDialogPrivate *priv;
1411
1412         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
1413         
1414         /* The account details title is mandatory: */
1415         if (entry_is_empty(priv->entry_account_title))
1416                 enable_ok = FALSE;
1417
1418         /* The user details username is mandatory: */
1419         if (enable_ok && entry_is_empty(priv->entry_user_username))
1420                 enable_ok = FALSE;
1421                 
1422         /* The user details email address is mandatory: */
1423         if (enable_ok && entry_is_empty (priv->entry_user_email))
1424                 enable_ok = FALSE;
1425
1426         /* The custom incoming server is mandatory: */
1427         if (enable_ok && entry_is_empty(priv->entry_incomingserver))
1428                 enable_ok = FALSE;
1429
1430         /* The custom outgoing server is mandatory: */
1431         if (enable_ok && entry_is_empty(priv->entry_outgoingserver))
1432                 enable_ok = FALSE;
1433
1434         protocol_registry = modest_runtime_get_protocol_registry ();
1435                         
1436         /* Enable the buttons, 
1437          * identifying them via their associated response codes:
1438          */
1439         GtkDialog *dialog_base = GTK_DIALOG (self);
1440         gtk_dialog_set_response_sensitive (dialog_base,
1441                                            GTK_RESPONSE_OK,
1442                                            enable_ok);
1443 }
1444
1445 static void
1446 modest_default_account_settings_dialog_class_init (ModestDefaultAccountSettingsDialogClass *klass)
1447 {
1448         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1449         g_type_class_add_private (klass, sizeof (ModestDefaultAccountSettingsDialogPrivate));
1450
1451         object_class->dispose = modest_default_account_settings_dialog_dispose;
1452         object_class->finalize = modest_default_account_settings_dialog_finalize;
1453 }
1454
1455 static void 
1456 modest_account_settings_dialog_init (gpointer g_iface, gpointer iface_data)
1457 {
1458         ModestAccountSettingsDialogClass *iface = (ModestAccountSettingsDialogClass *) g_iface;
1459
1460         iface->load_settings = modest_default_account_settings_dialog_load_settings;
1461 }