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