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