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