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