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