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