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