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