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