Use GTK+ single includes
[modest] / src / gnome / modest-default-account-settings-dialog.c
1 /* Copyright (c) 2006, 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 "widgets/modest-account-settings-dialog.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtk.h>
34
35 #include "widgets/modest-serversecurity-combo-box.h"
36 #include "widgets/modest-secureauth-combo-box.h"
37 #include "widgets/modest-validating-entry.h"
38 #include "widgets/modest-retrieve-combo-box.h"
39 #include "widgets/modest-limit-retrieve-combo-box.h"
40 #include "modest-defs.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-runtime.h" /* For modest_runtime_get_account_mgr(). */
45 #include "modest-protocol-registry.h"
46 #include <modest-utils.h>
47 #include <modest-platform.h>
48 #include "widgets/modest-ui-constants.h"
49 #include "widgets/modest-default-account-settings-dialog.h"
50 #include <tny-account.h>
51 #include <tny-status.h>
52 #include <tny-simple-list.h>
53
54 #include <gconf/gconf-client.h>
55 #include <string.h> /* For strlen(). */
56
57 /* Include config.h so that _() works: */
58 #ifdef HAVE_CONFIG_H
59 #include <config.h>
60 #endif
61
62 #define PORT_MIN 1
63 #define PORT_MAX 65535
64
65 static void modest_account_settings_dialog_init (gpointer g, gpointer iface_data);
66
67 G_DEFINE_TYPE_EXTENDED (ModestDefaultAccountSettingsDialog, 
68                         modest_default_account_settings_dialog, 
69                         GTK_TYPE_DIALOG,
70                         0, 
71                         G_IMPLEMENT_INTERFACE (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, 
72                                                modest_account_settings_dialog_init));
73
74 #define MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
75         (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
76                                       MODEST_TYPE_DEFAULT_ACCOUNT_SETTINGS_DIALOG, \
77                                       ModestDefaultAccountSettingsDialogPrivate))
78
79 typedef struct _ModestDefaultAccountSettingsDialogPrivate ModestDefaultAccountSettingsDialogPrivate;
80
81 struct _ModestDefaultAccountSettingsDialogPrivate
82 {
83         /* Used by derived widgets to query existing accounts,
84          * and to create new accounts: */
85         ModestAccountMgr *account_manager;
86         ModestAccountSettings *settings;
87         
88         gboolean modified;
89         gchar * account_name; /* This may not change. It is not user visible. */
90         ModestProtocolType incoming_protocol; /* This may not change. */
91         ModestProtocolType outgoing_protocol; /* This may not change. */
92         gchar * original_account_title;
93
94         ModestProtocolType protocol_authentication_incoming;
95         
96         GtkNotebook *notebook;
97         
98         GtkWidget *page_account_details;
99         GtkWidget *entry_account_title;
100         GtkWidget *combo_retrieve;
101         GtkWidget *combo_limit_retrieve;
102         GtkWidget *caption_leave_messages;
103         GtkWidget *checkbox_leave_messages;
104         
105         GtkWidget *page_user_details;
106         GtkWidget *entry_user_name;
107         GtkWidget *entry_user_username;
108         GtkWidget *entry_user_password;
109         GtkWidget *entry_user_email;
110         GtkWidget *entry_incoming_port;
111         GtkWidget *button_signature;
112         
113         GtkWidget *page_complete_easysetup;
114         
115         GtkWidget *page_incoming;
116         GtkWidget *caption_incoming;
117         GtkWidget *entry_incomingserver;
118         GtkWidget *combo_incoming_security;
119         GtkWidget *checkbox_incoming_auth;
120
121         GtkWidget *page_outgoing;
122         GtkWidget *entry_outgoingserver;
123         GtkWidget *caption_outgoing_username;
124         GtkWidget *entry_outgoing_username;
125         GtkWidget *caption_outgoing_password;
126         GtkWidget *entry_outgoing_password;
127         GtkWidget *combo_outgoing_security;
128         GtkWidget *combo_outgoing_auth;
129         GtkWidget *entry_outgoing_port;
130         GtkWidget *checkbox_outgoing_smtp_specific;
131         GtkWidget *button_outgoing_smtp_servers;
132         
133         GtkWidget *signature_dialog;
134 };
135
136 static void enable_buttons (ModestAccountSettingsDialog *self);
137 static gboolean save_configuration (ModestAccountSettingsDialog *dialog);
138 static void modest_default_account_settings_dialog_load_settings (ModestAccountSettingsDialog *dialog, 
139                                                                   ModestAccountSettings *settings);
140
141 static const gchar *
142 null_means_empty (const gchar * str)
143 {
144         return str ? str : "";
145 }
146
147
148 static void
149 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
150                                                                                                                         GValue *value, GParamSpec *pspec)
151 {
152         switch (property_id) {
153         default:
154                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
155         }
156 }
157
158 static void
159 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
160                                                                                                                         const GValue *value, GParamSpec *pspec)
161 {
162         switch (property_id) {
163         default:
164                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
165         }
166 }
167
168 static void
169 modest_account_settings_dialog_dispose (GObject *object)
170 {
171         if (G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->dispose)
172                 G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->dispose (object);
173 }
174
175 static void
176 modest_account_settings_dialog_finalize (GObject *object)
177 {
178         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
179         ModestDefaultAccountSettingsDialogPrivate *priv;
180
181         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
182         
183         if (priv->account_name)
184                 g_free (priv->account_name);
185                 
186         if (priv->original_account_title)
187                 g_free (priv->original_account_title);
188                 
189         if (priv->account_manager)
190                 g_object_unref (G_OBJECT (priv->account_manager));
191                 
192         if (priv->signature_dialog)
193                 gtk_widget_destroy (priv->signature_dialog);
194
195         if (priv->settings) {
196                 g_object_unref (priv->settings);
197                 priv->settings = NULL;
198         }
199         
200         G_OBJECT_CLASS (modest_default_account_settings_dialog_parent_class)->finalize (object);
201 }
202
203 static void
204 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
205
206 static void
207 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
208
209 static void
210 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
211 {
212         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
213         ModestDefaultAccountSettingsDialogPrivate *priv;
214
215         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
216         priv->modified = TRUE;
217 }
218
219 static void
220 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
221 {
222         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
223         ModestDefaultAccountSettingsDialogPrivate *priv;
224
225         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
226         priv->modified = TRUE;
227 }
228
229 static void
230 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
231 {
232         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
233         ModestDefaultAccountSettingsDialogPrivate *priv;
234
235         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
236         priv->modified = TRUE;
237 }
238
239 static void
240 on_modified_spin_button_changed (GtkSpinButton *spin_button, gpointer user_data)
241 {
242         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
243         ModestDefaultAccountSettingsDialogPrivate *priv;
244
245         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
246         gint value = gtk_spin_button_get_value_as_int (spin_button);
247
248         gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, value > 0);
249         priv->modified = TRUE;
250 }
251
252 /* Set a modified boolean whenever the widget is changed, 
253  * so we can check for it later.
254  */
255 static void
256 connect_for_modified (ModestDefaultAccountSettingsDialog *self, 
257                       GtkWidget *widget)
258 {
259         if (GTK_SPIN_BUTTON (widget)) {
260                 g_signal_connect (G_OBJECT (widget), "value_changed",
261                         G_CALLBACK (on_modified_spin_button_changed), self);
262         }
263         else if (GTK_IS_ENTRY (widget)) {
264                 g_signal_connect (G_OBJECT (widget), "changed",
265                         G_CALLBACK (on_modified_entry_changed), self);
266         } else if (GTK_IS_COMBO_BOX (widget)) {
267                 g_signal_connect (G_OBJECT (widget), "changed",
268                         G_CALLBACK (on_modified_combobox_changed), self);       
269         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
270                 g_signal_connect (G_OBJECT (widget), "toggled",
271                         G_CALLBACK (on_modified_checkbox_toggled), self);
272         }
273 }
274
275 static void
276 on_field_entry_changed (GtkEditable *editable, gpointer user_data)
277 {
278         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
279         g_assert(self);
280         enable_buttons(self);
281 }
282
283 static void
284 on_field_combobox_changed (GtkComboBox *widget, gpointer user_data)
285 {
286         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
287         g_assert(self);
288         enable_buttons(self);
289 }
290
291 /** This is a convenience function to create a field containing a mandatory widget.
292  * When the widget is edited, the enable_buttons() vfunc will be called.
293  */
294 static GtkWidget* 
295 create_field(ModestDefaultAccountSettingsDialog *self,
296              GtkSizeGroup *group,
297              const gchar *value,
298              GtkWidget *control,
299              GtkWidget *icon,
300              gboolean mandatory)
301 {
302         GtkWidget *hbox;
303         gchar *title;
304         GtkWidget *label;
305
306         hbox = gtk_hbox_new (FALSE, 12);
307         if (mandatory)
308                 title = g_strdup_printf("%s*:", value);
309         else
310                 title = g_strdup_printf ("%s:", value);
311         label = gtk_label_new (title);
312         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
313         g_free (title);
314
315         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
316         gtk_box_pack_start (GTK_BOX (hbox), control, TRUE, TRUE, 0);
317         gtk_size_group_add_widget (group, label);
318   
319         /* Connect to the appropriate changed signal for the widget, 
320          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
321          */
322         if (GTK_IS_ENTRY (control)) {
323                 g_signal_connect (G_OBJECT (control), "changed",
324                 G_CALLBACK (on_field_entry_changed), self);
325                 
326         }
327         else if (GTK_IS_COMBO_BOX (control)) {
328                 g_signal_connect (G_OBJECT (control), "changed",
329                 G_CALLBACK (on_field_combobox_changed), self);
330         }
331         gtk_widget_show_all (hbox);
332          
333         return hbox;
334 }
335
336 static void
337 on_entry_invalid_account_title_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
338 {
339         gchar *tmp, *msg;
340                         
341         tmp = g_strndup (account_title_forbidden_chars, ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH);
342         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
343
344         modest_platform_information_banner (GTK_WIDGET (self), NULL, msg);
345
346         g_free (msg);
347         g_free (tmp);
348 }
349
350 static void
351 on_entry_invalid_fullname_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
352 {
353         gchar *tmp, *msg;
354                         
355         tmp = g_strndup (user_name_forbidden_chars, USER_NAME_FORBIDDEN_CHARS_LENGTH);
356         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
357
358         modest_platform_information_banner(GTK_WIDGET (self), NULL, msg);
359
360         g_free (msg);
361         g_free (tmp);
362 }
363
364
365 static void
366 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
367 {
368         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
369                                          _CS("ckdg_ib_maximum_characters_reached"));
370 }
371
372 static GtkWidget*
373 create_page_account_details (ModestDefaultAccountSettingsDialog *self)
374 {
375         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
376         GtkWidget *alignment;   
377         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
378         ModestDefaultAccountSettingsDialogPrivate *priv;
379
380         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
381
382         /* The description widgets: */  
383         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
384         GtkWidget *field = create_field (self, sizegroup, _("mcen_fi_account_title"), 
385                 priv->entry_account_title, NULL, TRUE);
386         gtk_widget_show (priv->entry_account_title);
387         connect_for_modified (self, priv->entry_account_title);
388         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
389         gtk_widget_show (field);
390         
391         /* Prevent the use of some characters in the account title, 
392          * as required by our UI specification: */
393         GList *list_prevent = NULL;
394         list_prevent = g_list_append (list_prevent, "\\");
395         list_prevent = g_list_append (list_prevent, "/");
396         list_prevent = g_list_append (list_prevent, ":");
397         list_prevent = g_list_append (list_prevent, "*");
398         list_prevent = g_list_append (list_prevent, "?");
399         list_prevent = g_list_append (list_prevent, "\"");
400         list_prevent = g_list_append (list_prevent, "<"); 
401         list_prevent = g_list_append (list_prevent, ">"); 
402         list_prevent = g_list_append (list_prevent, "|");
403         list_prevent = g_list_append (list_prevent, "^");       
404         modest_validating_entry_set_unallowed_characters (
405                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
406         g_list_free (list_prevent);
407         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
408                                          on_entry_invalid_account_title_character, self);
409         
410         /* Set max length as in the UI spec:
411          * The UI spec seems to want us to show a dialog if we hit the maximum. */
412         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
413         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title), 
414                 on_entry_max, self);
415         
416         /* The retrieve combobox: */
417         priv->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
418         field = create_field (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
419                               priv->combo_retrieve, NULL, TRUE);
420         gtk_widget_show (priv->combo_retrieve);
421         connect_for_modified (self, priv->combo_retrieve);
422         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
423         gtk_widget_show (field);
424         
425         /* The limit-retrieve combobox: */
426         priv->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
427         field = create_field (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
428                               priv->combo_limit_retrieve, NULL, TRUE);
429         gtk_widget_show (priv->combo_limit_retrieve);
430         connect_for_modified (self, priv->combo_limit_retrieve);
431         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
432         gtk_widget_show (field);
433
434         /* The leave-messages widgets: */
435         if(!priv->checkbox_leave_messages)
436                 priv->checkbox_leave_messages = gtk_check_button_new ();
437         if (!priv->caption_leave_messages) {
438                 priv->caption_leave_messages = create_field (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
439                                                              priv->checkbox_leave_messages, NULL, TRUE);
440         }
441                         
442         gtk_widget_show (priv->checkbox_leave_messages);
443         connect_for_modified (self, priv->checkbox_leave_messages);
444         gtk_box_pack_start (GTK_BOX (box), priv->caption_leave_messages, FALSE, FALSE, MODEST_MARGIN_HALF);
445         gtk_widget_show (priv->caption_leave_messages);
446         
447         gtk_widget_show (GTK_WIDGET (box));
448
449         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
450         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
451         gtk_container_add (GTK_CONTAINER (alignment), box);
452         gtk_widget_show (alignment);
453         
454         return GTK_WIDGET (alignment);
455 }
456
457 static gchar*
458 get_entered_account_title (ModestAccountSettingsDialog *dialog)
459 {
460         ModestDefaultAccountSettingsDialogPrivate *priv;
461
462         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
463
464         const gchar* account_title = 
465                 gtk_entry_get_text (GTK_ENTRY (priv->entry_account_title));
466         if (!account_title || (strlen (account_title) == 0))
467                 return NULL;
468         else {
469                 /* Strip it of whitespace at the start and end: */
470                 gchar *result = g_strdup (account_title);
471                 result = g_strstrip (result);
472                 
473                 if (!result)
474                         return NULL;
475                         
476                 if (strlen (result) == 0) {
477                         g_free (result);
478                         return NULL;    
479                 }
480                 
481                 return result;
482         }
483 }
484
485
486 static void
487 on_button_signature (GtkButton *button, gpointer user_data)
488 {
489 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
490         
491 /*      /\* Create the window, if necessary: *\/ */
492 /*      if (!(priv->signature_dialog)) { */
493 /*              priv->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ()); */
494         
495 /*              gboolean use_signature = modest_account_settings_get_use_signature (priv->settings); */
496 /*              const gchar *signature = modest_account_settings_get_signature(priv->settings); */
497 /*              gchar* account_title = get_entered_account_title (self); */
498 /*              modest_signature_editor_dialog_set_settings ( */
499 /*                      MODEST_SIGNATURE_EDITOR_DIALOG (priv->signature_dialog),  */
500 /*                      use_signature, signature, account_title); */
501
502 /*              g_free (account_title); */
503 /*              account_title = NULL; */
504 /*              signature = NULL; */
505 /*      } */
506
507 /*      /\* Show the window: *\/         */
508 /*      gtk_window_set_transient_for (GTK_WINDOW (priv->signature_dialog), GTK_WINDOW (self)); */
509 /*      gtk_window_set_modal (GTK_WINDOW (priv->signature_dialog), TRUE); */
510 /*     const gint response = gtk_dialog_run (GTK_DIALOG (priv->signature_dialog)); */
511 /*     gtk_widget_hide (priv->signature_dialog); */
512 /*     if (response != GTK_RESPONSE_OK) { */
513 /*      /\* Destroy the widget now, and its data: *\/ */
514 /*      gtk_widget_destroy (priv->signature_dialog); */
515 /*      priv->signature_dialog = NULL; */
516 /*     } */
517 /*     else { */
518 /*      /\* Mark modified, so we use the dialog's data later: *\/ */
519 /*      priv->modified = TRUE;   */
520 /*     } */
521 }
522
523 static GtkWidget*
524 create_page_user_details (ModestDefaultAccountSettingsDialog *self)
525 {
526         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
527         GtkWidget *alignment;   
528         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
529         ModestDefaultAccountSettingsDialogPrivate *priv;
530
531         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
532          
533         /* The name widgets: */
534         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
535
536         /* Set max length as in the UI spec:
537          * The UI spec seems to want us to show a dialog if we hit the maximum. */
538         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
539         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
540                 on_entry_max, self);
541         GtkWidget *field = create_field (self, sizegroup, 
542                                          _("mcen_li_emailsetup_name"), priv->entry_user_name, NULL, FALSE);
543         gtk_widget_show (priv->entry_user_name);
544         connect_for_modified (self, priv->entry_user_name);
545         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
546         gtk_widget_show (field);
547
548
549         /* Prevent the use of some characters in the name, 
550          * as required by our UI specification: */
551         GList *list_prevent = NULL;
552         list_prevent = g_list_append (list_prevent, "<");
553         list_prevent = g_list_append (list_prevent, ">");
554         modest_validating_entry_set_unallowed_characters (
555                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
556         g_list_free (list_prevent);
557         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
558                                          on_entry_invalid_fullname_character, self);
559         
560         /* The username widgets: */     
561         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
562         field = create_field (self, sizegroup, _("mail_fi_username"), 
563                                 priv->entry_user_username, NULL, TRUE);
564         gtk_widget_show (priv->entry_user_username);
565         connect_for_modified (self, priv->entry_user_username);
566         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
567         gtk_widget_show (field);
568         
569         /* Prevent the use of some characters in the username, 
570          * as required by our UI specification: */
571         modest_validating_entry_set_unallowed_characters_whitespace (
572                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
573         
574         /* Set max length as in the UI spec:
575          * The UI spec seems to want us to show a dialog if we hit the maximum. */
576         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
577         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
578                 on_entry_max, self);
579         
580         /* The password widgets: */     
581         priv->entry_user_password = gtk_entry_new ();
582         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
583         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
584         field = create_field (self, sizegroup, 
585                               _("mail_fi_password"), priv->entry_user_password, NULL, FALSE);
586         gtk_widget_show (priv->entry_user_password);
587         connect_for_modified (self, priv->entry_user_password);
588         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
589         gtk_widget_show (field);
590         
591         /* The email address widgets: */        
592         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
593         field = create_field (self, sizegroup, 
594                               _("mcen_li_emailsetup_email_address"), priv->entry_user_email, NULL, TRUE);
595         gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
596         gtk_widget_show (priv->entry_user_email);
597         connect_for_modified (self, priv->entry_user_email);
598         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
599         gtk_widget_show (field);
600         
601         /* Set max length as in the UI spec:
602          * The UI spec seems to want us to show a dialog if we hit the maximum. */
603         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
604         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
605                 on_entry_max, self);
606         
607         /* Signature button: */
608         if (!priv->button_signature)
609                 priv->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
610         field = create_field (self, sizegroup, _("mcen_fi_email_signature"), 
611                 priv->button_signature, NULL, FALSE);
612         gtk_widget_show (priv->button_signature);
613         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
614         gtk_widget_show (field);
615                 
616         g_signal_connect (G_OBJECT (priv->button_signature), "clicked",
617                 G_CALLBACK (on_button_signature), self);
618                 
619         gtk_widget_show (GTK_WIDGET (box));
620
621         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
622         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
623         gtk_container_add (GTK_CONTAINER (alignment), box);
624         gtk_widget_show (alignment);
625         
626         return GTK_WIDGET (alignment);
627 }
628
629 /** Change the field title for the incoming server, 
630  * as specified in the UI spec:
631  */
632 static void 
633 update_incoming_server_title (ModestAccountSettingsDialog *self, 
634                               ModestProtocolType protocol)
635 {
636         ModestDefaultAccountSettingsDialogPrivate *priv;
637
638         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
639
640         const gchar* type = 
641                 (protocol == MODEST_PROTOCOLS_STORE_POP ? 
642                         _("mail_fi_emailtype_pop3") : 
643                         _("mail_fi_emailtype_imap") );
644                         
645                 
646         /* Note that this produces a compiler warning, 
647          * because the compiler does not know that the translated string will have a %s in it.
648          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
649         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
650         
651         /* This is a mandatory field, so add a *. This is usually done by 
652          * create_field() but we can't use that here. */
653         gchar *with_asterisk = g_strconcat (incomingserver_title, "*", NULL);
654         g_free (incomingserver_title);
655         
656         g_object_set (G_OBJECT (priv->caption_incoming), "label", with_asterisk, NULL);
657         g_free(with_asterisk);
658 }
659
660 /** Change the field title for the incoming server, 
661  * as specified in the UI spec:
662  */
663 static void 
664 update_incoming_server_security_choices (ModestAccountSettingsDialog *self, 
665                                          ModestProtocolType protocol)
666 {
667         ModestDefaultAccountSettingsDialogPrivate *priv;
668
669         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
670
671         /* Fill the combo with appropriately titled choices for POP or IMAP. */
672         /* The choices are the same, but the titles are different, as in the UI spec. */
673         modest_serversecurity_combo_box_fill (
674                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security), protocol);
675 }
676            
677 static GtkWidget* 
678 create_page_incoming (ModestDefaultAccountSettingsDialog *self)
679 {
680         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
681         GtkWidget *alignment;
682         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
683         ModestDefaultAccountSettingsDialogPrivate *priv;
684
685         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
686          
687         /* The incoming server widgets: */
688         if(!priv->entry_incomingserver)
689                 priv->entry_incomingserver = gtk_entry_new ();
690
691         if (priv->caption_incoming)
692           gtk_widget_destroy (priv->caption_incoming);
693            
694         /* The field title will be updated in update_incoming_server_title().
695          * so this default text will never be seen: */
696         /* (Note: Changing the title seems pointless. murrayc) */
697         priv->caption_incoming = create_field (self, sizegroup, 
698                                              "Incoming Server", priv->entry_incomingserver, NULL, TRUE);
699         gtk_widget_show (priv->entry_incomingserver);
700         connect_for_modified (self, priv->entry_incomingserver);
701         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
702         gtk_widget_show (priv->caption_incoming);
703         
704         /* The secure connection widgets: */
705         /* This will be filled by update_incoming_server_security_choices(). */
706         if (!priv->combo_incoming_security)
707                 priv->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
708         GtkWidget *field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
709                                          priv->combo_incoming_security, NULL, FALSE);
710         gtk_widget_show (priv->combo_incoming_security);
711         connect_for_modified (self, priv->combo_incoming_security);
712         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
713         gtk_widget_show (field);
714         
715         /* Show a default port number when the security method changes, as per the UI spec: */
716         g_signal_connect (G_OBJECT (priv->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
717         
718         
719         /* The port widgets: */
720         if (!priv->entry_incoming_port)
721                 priv->entry_incoming_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble) PORT_MIN, (gdouble) PORT_MAX, 1.0));
722         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
723                 priv->entry_incoming_port, NULL, FALSE);
724         gtk_widget_show (priv->entry_incoming_port);
725         connect_for_modified (self, priv->entry_incoming_port);
726         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
727         gtk_widget_show (field);
728         
729         /* The secure authentication widgets: */
730         if(!priv->checkbox_incoming_auth)
731                 priv->checkbox_incoming_auth = gtk_check_button_new ();
732         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
733                               priv->checkbox_incoming_auth, NULL, FALSE);
734         gtk_widget_show (priv->checkbox_incoming_auth);
735         connect_for_modified (self, priv->checkbox_incoming_auth);
736         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
737         gtk_widget_show (field);
738         
739         gtk_widget_show (GTK_WIDGET (box));
740         
741         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
742         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
743         gtk_container_add (GTK_CONTAINER (alignment), box);
744         gtk_widget_show (alignment);
745         
746         return GTK_WIDGET (alignment);
747 }
748
749 static void
750 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
751 {
752         GtkWidget *widget = GTK_WIDGET (user_data);
753         
754         /* Enable the widget only if the toggle button is active: */
755         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
756         gtk_widget_set_sensitive (widget, enable);
757 }
758
759 /* Make the sensitivity of a widget depend on a toggle button.
760  */
761 static void
762 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
763 {
764         g_signal_connect (G_OBJECT (button), "toggled",
765                 G_CALLBACK (on_toggle_button_changed), widget);
766         
767         /* Set the starting sensitivity: */
768         on_toggle_button_changed (button, widget);
769 }
770
771 static void
772 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
773 {
774 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
775         
776 /*      /\* Create the window if necessary: *\/ */
777 /*      if (!(priv->specific_window)) { */
778 /*              priv->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ()); */
779 /*              modest_connection_specific_smtp_window_fill_with_connections ( */
780 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (priv->specific_window), priv->account_manager); */
781 /*      } */
782
783 /*      /\* Show the window: *\/         */
784 /*      gtk_window_set_transient_for (GTK_WINDOW (priv->specific_window), GTK_WINDOW (self)); */
785 /*      gtk_window_set_modal (GTK_WINDOW (priv->specific_window), TRUE); */
786 /*      gtk_widget_show (priv->specific_window); */
787 /*      priv->modified = TRUE; */
788 }
789
790 static void
791 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
792 {
793         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
794         ModestDefaultAccountSettingsDialogPrivate *priv;
795
796         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
797         
798         ModestProtocolType protocol_security = 
799                 modest_secureauth_combo_box_get_active_secureauth (
800                         MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
801         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOLS_AUTH_NONE;
802         
803         gtk_widget_set_sensitive (priv->caption_outgoing_username, secureauth_used);
804         gtk_widget_set_sensitive (priv->caption_outgoing_password, secureauth_used);
805 }
806
807 static void
808 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
809 {
810         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
811         ModestDefaultAccountSettingsDialogPrivate *priv;
812
813         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
814         
815         const gint port_number = 
816                 modest_serversecurity_combo_box_get_active_serversecurity_port (
817                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
818
819         if(port_number != 0) {
820                 gtk_spin_button_set_value (
821                         GTK_SPIN_BUTTON (priv->entry_outgoing_port), (gdouble) port_number);
822         }               
823 }
824
825 static void
826 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
827 {
828         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
829         ModestDefaultAccountSettingsDialogPrivate *priv;
830
831         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
832
833         const gint port_number = 
834                 modest_serversecurity_combo_box_get_active_serversecurity_port (
835                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security));
836
837         if(port_number != 0) {
838                 gtk_spin_button_set_value (
839                         GTK_SPIN_BUTTON (priv->entry_incoming_port), (gdouble) port_number);
840         }               
841 }
842
843
844 static GtkWidget* 
845 create_page_outgoing (ModestDefaultAccountSettingsDialog *self)
846 {
847         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
848         GtkWidget *alignment;
849         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
850
851         ModestDefaultAccountSettingsDialogPrivate *priv;
852
853         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
854          
855         /* The outgoing server widgets: */
856         if (!priv->entry_outgoingserver)
857                 priv->entry_outgoingserver = gtk_entry_new ();
858         GtkWidget *field = create_field (self, sizegroup, 
859                                          _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, FALSE);
860         gtk_widget_show (priv->entry_outgoingserver);
861         connect_for_modified (self, priv->entry_outgoingserver);
862         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
863         gtk_widget_show (field);
864         
865         /* The secure authentication widgets: */
866         if (!priv->combo_outgoing_auth)
867                 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
868         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
869                 priv->combo_outgoing_auth, NULL, FALSE);
870         gtk_widget_show (priv->combo_outgoing_auth);
871         connect_for_modified (self, priv->combo_outgoing_auth);
872         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
873         gtk_widget_show (field);
874         
875         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
876         g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
877         
878         /* The username widgets: */     
879         priv->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
880         priv->caption_outgoing_username = create_field (self, sizegroup, _("mail_fi_username"), 
881                                                       priv->entry_outgoing_username, NULL, TRUE);
882         gtk_widget_show (priv->entry_outgoing_username);
883         connect_for_modified (self, priv->entry_outgoing_username);
884         gtk_box_pack_start (GTK_BOX (box), priv->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
885         gtk_widget_show (priv->caption_outgoing_username);
886         
887         /* Prevent the use of some characters in the username, 
888          * as required by our UI specification: */
889         modest_validating_entry_set_unallowed_characters_whitespace (
890                 MODEST_VALIDATING_ENTRY (priv->entry_outgoing_username));
891         
892         /* Set max length as in the UI spec:
893          * The UI spec seems to want us to show a dialog if we hit the maximum. */
894         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_outgoing_username), 64);
895         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_outgoing_username), 
896                 on_entry_max, self);
897                 
898         /* The password widgets: */     
899         priv->entry_outgoing_password = gtk_entry_new ();
900         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_outgoing_password), FALSE);
901         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_outgoing_password), '*'); */
902         priv->caption_outgoing_password = create_field (self, sizegroup, 
903                                                       _("mail_fi_password"), priv->entry_outgoing_password, NULL, FALSE);
904         gtk_widget_show (priv->entry_outgoing_password);
905         connect_for_modified (self, priv->entry_outgoing_password);
906         gtk_box_pack_start (GTK_BOX (box), priv->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
907         gtk_widget_show (priv->caption_outgoing_password);
908         
909         /* The secure connection widgets: */
910         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
911          * and modest_serversecurity_combo_box_set_active_serversecurity().
912          */
913         if (!priv->combo_outgoing_security)
914                 
915                 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
916         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
917                               priv->combo_outgoing_security, NULL, FALSE);
918         gtk_widget_show (priv->combo_outgoing_security);
919         connect_for_modified (self, priv->combo_outgoing_security);
920         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
921         gtk_widget_show (field);
922         
923         /* Show a default port number when the security method changes, as per the UI spec: */
924         g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
925         
926         /* The port widgets: */
927         if (!priv->entry_outgoing_port)
928                 priv->entry_outgoing_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble)PORT_MIN, (gdouble) PORT_MAX, 1.0));
929         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
930                               priv->entry_outgoing_port, NULL, FALSE);
931         gtk_widget_show (priv->entry_outgoing_port);
932         connect_for_modified (self, priv->entry_outgoing_port);
933         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
934         gtk_widget_show (field);
935         
936         GtkWidget *separator = gtk_hseparator_new ();
937         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
938         gtk_widget_show (separator);
939         
940         /* connection-specific checkbox: */
941         if (!priv->checkbox_outgoing_smtp_specific) {
942                 priv->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
943                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
944                         FALSE);
945         }
946         field = create_field (self, sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
947                               priv->checkbox_outgoing_smtp_specific, NULL, FALSE);
948         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
949         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
950         gtk_widget_show (field);
951         connect_for_modified (self, priv->checkbox_outgoing_smtp_specific);
952         
953         /* Connection-specific SMTP-Severs Edit button: */
954         if (!priv->button_outgoing_smtp_servers)
955                 priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
956         field = create_field (self, sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
957                               priv->button_outgoing_smtp_servers, NULL, FALSE);
958         gtk_widget_show (priv->button_outgoing_smtp_servers);
959         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
960         gtk_widget_show (field);
961         
962         /* Only enable the button when the checkbox is checked: */
963         enable_widget_for_togglebutton (priv->button_outgoing_smtp_servers, 
964                 GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific));
965                 
966         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
967                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
968                 
969         gtk_widget_show (GTK_WIDGET (box));
970         
971         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
972         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
973         gtk_container_add (GTK_CONTAINER (alignment), box);
974         gtk_widget_show (alignment);
975         
976         return GTK_WIDGET (alignment);
977 }
978         
979 static gboolean
980 check_data (ModestAccountSettingsDialog *self)
981 {
982         ModestDefaultAccountSettingsDialogPrivate *priv;
983
984         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
985
986         /* Check that the title is not already in use: */
987         gchar* account_title = get_entered_account_title (self);
988         if (!account_title)
989                 return FALSE; /* Should be prevented already anyway. */
990                 
991         if (g_strcmp0 (account_title, priv->original_account_title) != 0) {
992                 /* Check the changed title: */
993                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (priv->account_manager,
994                         account_title);
995         
996                 if (name_in_use) {
997                         /* Warn the user via a dialog: */
998                         modest_platform_information_banner(NULL, NULL, _("mail_ib_account_name_already_existing"));
999                 
1000                 g_free (account_title);
1001                         return FALSE;
1002                 }
1003         }
1004         
1005         g_free (account_title);
1006         account_title  = NULL;
1007
1008         /* Check that the email address is valid: */
1009         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1010         if ((!email_address) || (strlen(email_address) == 0)) {
1011                 return FALSE;
1012         }
1013                         
1014         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1015                 /* Warn the user via a dialog: */
1016                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_email"));
1017                                          
1018                 /* Return focus to the email address entry: */
1019                 gtk_widget_grab_focus (priv->entry_user_email);
1020                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1021                 return FALSE;
1022         }
1023
1024         /* make sure the domain name for the incoming server is valid */
1025         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
1026         if ((!hostname) || (strlen(hostname) == 0)) {
1027                 return FALSE;
1028         }
1029         
1030         if (!modest_text_utils_validate_domain_name (hostname)) {
1031                 /* Warn the user via a dialog: */
1032                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
1033                                          
1034                 /* Return focus to the email address entry: */
1035                 gtk_widget_grab_focus (priv->entry_incomingserver);
1036                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_incomingserver), 0, -1);
1037                 return FALSE;
1038         }
1039
1040         /* make sure the domain name for the outgoing server is valid */
1041         const gchar* hostname2 = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
1042         if ((!hostname2) || (strlen(hostname2) == 0)) {
1043                 return FALSE;
1044         }
1045         
1046         if (!modest_text_utils_validate_domain_name (hostname2)) {
1047                 /* Warn the user via a dialog: */
1048                 modest_platform_information_banner (priv->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
1049
1050                 /* Return focus to the email address entry: */
1051                 gtk_widget_grab_focus (priv->entry_outgoingserver);
1052                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
1053                 return FALSE;
1054         }
1055         
1056         /* Find a suitable authentication method when secure authentication is desired */
1057
1058         const gint port_num = gtk_spin_button_get_value_as_int (
1059                         GTK_SPIN_BUTTON (priv->entry_incoming_port));
1060         const gchar* username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1061
1062         /*
1063         const ModestProtocolType protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1064                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security));
1065         */
1066         /* If we use an encrypted protocol then there is no need to encrypt the password */
1067         /* I don't think this is a good assumption. It overrides the user's request. murrayc: 
1068          *  if (!modest_protocol_info_is_secure(protocol_security_incoming)) */
1069         if (TRUE)
1070         {
1071                 if (gtk_toggle_button_get_active (
1072                                 GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth))) {
1073                         GError *error = NULL;
1074
1075                         GList *list_auth_methods = 
1076                                 modest_utils_get_supported_secure_authentication_methods (priv->incoming_protocol, 
1077                                         hostname, port_num, username, GTK_WINDOW (self), &error);
1078                         if (list_auth_methods) {
1079                                 /* Use the first supported method.
1080                                  * TODO: Should we prioritize them, to prefer a particular one? */
1081                                 GList* method;
1082                                 for (method = list_auth_methods; method != NULL; method = g_list_next(method))
1083                                 {
1084                                         ModestProtocolType proto = (ModestProtocolType)(GPOINTER_TO_INT(method->data));
1085                                         // Allow secure methods, e.g MD5 only
1086                                         if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (), proto))
1087                                         {
1088                                                 priv->protocol_authentication_incoming = proto;
1089                                                 break;
1090                                         }
1091                                 }
1092                                 g_list_free (list_auth_methods);
1093                         }
1094
1095                         if (list_auth_methods == NULL || 
1096                             !modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
1097                                                                                priv->protocol_authentication_incoming)) {
1098                                 if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
1099                                                 error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
1100                                         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
1101                                                                          _("Could not discover supported secure authentication methods."));
1102
1103                                 if(error != NULL)
1104                                         g_error_free(error);
1105                                         
1106                                 /* This is a nasty hack. jschmid. */
1107                                 /* Don't let the dialog close */
1108                                 /*g_signal_stop_emission_by_name (dialog, "response");*/
1109                                 return FALSE;
1110                         }
1111                 }
1112         }
1113         
1114         return TRUE;
1115 }
1116 /*
1117  */
1118 static void 
1119 on_response (GtkDialog *wizard_dialog,
1120         gint response_id,
1121         gpointer user_data)
1122 {
1123         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
1124         ModestDefaultAccountSettingsDialogPrivate *priv;
1125
1126         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
1127
1128         enable_buttons (self);
1129         
1130         gboolean prevent_response = FALSE;
1131
1132         /* Warn about unsaved changes: */
1133         if (response_id == GTK_RESPONSE_CANCEL && priv->modified) {
1134                 GtkDialog *dialog = GTK_DIALOG (modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
1135                                                                                          _("imum_nc_wizard_confirm_lose_changes")));
1136                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1137                 
1138                 const gint dialog_response = gtk_dialog_run (dialog);
1139                 gtk_widget_destroy (GTK_WIDGET (dialog));
1140                 
1141                 if (dialog_response != GTK_RESPONSE_OK)
1142                         prevent_response = TRUE;
1143         }
1144         /* Check for invalid input: */
1145         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
1146                 prevent_response = TRUE;
1147         }
1148                 
1149         if (prevent_response) {
1150                 /* This is a nasty hack. murrayc. */
1151                 /* Don't let the dialog close */
1152                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1153                 return; 
1154         }
1155                 
1156         if (response_id == GTK_RESPONSE_OK) {
1157                 /* Try to save the changes if modified (NB #59251): */
1158                 if (priv->modified)
1159                 {
1160                         const gboolean saved = save_configuration (self);
1161                         if (saved) {
1162                                 /* Do not show the account-saved dialog if we are just saving this 
1163                                  * temporarily, because from the user's point of view it will not 
1164                                  * really be saved (saved + enabled) until later
1165                                  */
1166                                 if (modest_account_settings_get_account_name (priv->settings) != NULL) {
1167                                         ModestServerAccountSettings *store_settings;
1168                                         ModestServerAccountSettings *transport_settings;
1169                                         const gchar *store_account_name;
1170                                         const gchar *transport_account_name;
1171
1172
1173                                         store_settings = modest_account_settings_get_store_settings (priv->settings);
1174                                         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1175                                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
1176                                         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
1177                                         
1178                                         if (store_account_name) {
1179                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1180                                                                                           store_account_name);
1181                                         }
1182                                         if (transport_account_name) {
1183                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1184                                                                                           transport_account_name);
1185                                         }
1186                                         g_object_unref (store_settings);
1187                                         g_object_unref (transport_settings);
1188
1189                                         modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1190                                 }
1191                         } else {
1192                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1193                         }
1194                 }
1195         }
1196 }
1197
1198 static void 
1199 modest_account_settings_dialog_init (gpointer g, gpointer iface_data)
1200 {
1201         ModestAccountSettingsDialogClass *iface = (ModestAccountSettingsDialogClass *) g;
1202
1203         iface->load_settings = modest_default_account_settings_dialog_load_settings;
1204 }
1205
1206 static void
1207 modest_default_account_settings_dialog_init (ModestDefaultAccountSettingsDialog *self)
1208 {
1209         ModestDefaultAccountSettingsDialogPrivate *priv;
1210
1211         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
1212
1213         /* Create the notebook to be used by the GtkDialog base class:
1214          * Each page of the notebook will be a page of the wizard: */
1215         priv->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1216         priv->settings = modest_account_settings_new ();
1217
1218         /* Get the account manager object, 
1219          * so we can check for existing accounts,
1220          * and create new accounts: */
1221         priv->account_manager = modest_runtime_get_account_mgr ();
1222         g_assert (priv->account_manager);
1223         g_object_ref (priv->account_manager);
1224         
1225         priv->protocol_authentication_incoming = MODEST_PROTOCOLS_AUTH_PASSWORD;
1226
1227     /* Create the common pages, 
1228      */
1229         priv->page_account_details = create_page_account_details (self);
1230         priv->page_user_details = create_page_user_details (self);
1231         priv->page_incoming = create_page_incoming (self);
1232         priv->page_outgoing = create_page_outgoing (self);
1233         
1234         /* Add the notebook pages: */
1235         gtk_notebook_append_page (priv->notebook, priv->page_account_details, 
1236                 gtk_label_new (_("mcen_ti_account_settings_account")));
1237         gtk_notebook_append_page (priv->notebook, priv->page_user_details, 
1238                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1239         gtk_notebook_append_page (priv->notebook, priv->page_incoming,
1240                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1241         gtk_notebook_append_page (priv->notebook, priv->page_outgoing,
1242                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1243                 
1244         GtkDialog *dialog = GTK_DIALOG (self);
1245         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (priv->notebook));
1246         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1247         gtk_widget_show (GTK_WIDGET (priv->notebook));
1248         
1249     /* Add the buttons: */
1250         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1251         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_SAVE, GTK_RESPONSE_OK);
1252     
1253     /* Connect to the dialog's response signal: */
1254     /* We use connect-before 
1255      * so we can stop the signal emission, 
1256      * to stop the default signal handler from closing the dialog.
1257      */
1258     g_signal_connect (G_OBJECT (self), "response",
1259             G_CALLBACK (on_response), self); 
1260             
1261     priv->modified = FALSE;
1262
1263     /* When this window is shown, hibernation should not be possible, 
1264          * because there is no sensible way to save the state: */
1265     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1266         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1267
1268     gtk_window_set_default_size (GTK_WINDOW (self), 600, 400);
1269
1270 /*     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings", */
1271 /*                                  modest_maemo_utils_get_osso_context()); */
1272 }
1273
1274 ModestAccountSettingsDialog*
1275 modest_default_account_settings_dialog_new (void)
1276 {
1277         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
1278 }
1279
1280 /** Update the UI with the stored account details, so they can be edited.
1281  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1282  */
1283 static void 
1284 modest_default_account_settings_dialog_load_settings (ModestAccountSettingsDialog *dialog, 
1285                                                       ModestAccountSettings *settings)
1286 {
1287         ModestServerAccountSettings *incoming_account;
1288         ModestServerAccountSettings *outgoing_account;
1289         const gchar *account_name;
1290
1291         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1292         ModestDefaultAccountSettingsDialogPrivate *priv;
1293
1294         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1295
1296         incoming_account = modest_account_settings_get_store_settings (settings);
1297         outgoing_account = modest_account_settings_get_transport_settings (settings);
1298
1299         account_name = modest_account_settings_get_account_name (settings);
1300                 
1301         /* Save the account name so we can refer to it later: */
1302         if (priv->account_name)
1303                 g_free (priv->account_name);
1304         priv->account_name = g_strdup (account_name);
1305
1306         if (priv->settings)
1307                 g_object_unref (priv->settings);
1308         priv->settings = g_object_ref (settings);
1309         
1310         /* Save the account title so we can refer to it if the user changes it: */
1311         if (priv->original_account_title)
1312                 g_free (priv->original_account_title);
1313         priv->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1314         
1315         /* Show the account data in the widgets: */
1316         
1317         /* Note that we never show the non-display name in the UI.
1318          * (Though the display name defaults to the non-display name at the start.) */
1319         gtk_entry_set_text( GTK_ENTRY (priv->entry_account_title),
1320                             null_means_empty (modest_account_settings_get_display_name (settings)));
1321         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_name), 
1322                             null_means_empty (modest_account_settings_get_fullname (settings)));
1323         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_email), 
1324                             null_means_empty (modest_account_settings_get_email_address (settings)));
1325         modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), 
1326                                         modest_server_account_settings_get_protocol (incoming_account));
1327         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), 
1328                                                             modest_account_settings_get_retrieve_type (settings));
1329         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1330                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve), 
1331                 modest_account_settings_get_retrieve_limit (settings));
1332         
1333         
1334         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages), 
1335                                       modest_account_settings_get_leave_messages_on_server (settings));
1336         
1337         /* Only show the leave-on-server checkbox for POP, 
1338          * as per the UI spec: */
1339         if (modest_server_account_settings_get_protocol (incoming_account) != MODEST_PROTOCOLS_STORE_POP) {
1340                 gtk_widget_hide (priv->caption_leave_messages);
1341         } else {
1342                 gtk_widget_show (priv->caption_leave_messages);
1343         }
1344         
1345         update_incoming_server_security_choices (dialog, modest_server_account_settings_get_protocol (incoming_account));
1346         if (incoming_account) {
1347                 const gchar *username;
1348                 const gchar *password;
1349                 const gchar *hostname;
1350                 /* Remember this for later: */
1351                 priv->incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1352                 
1353                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1354                 username = modest_server_account_settings_get_username (incoming_account);
1355                 password = modest_server_account_settings_get_password (incoming_account);
1356                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_username),
1357                                     null_means_empty (username));
1358                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_password), 
1359                                     null_means_empty (password));
1360                         
1361                 gtk_entry_set_text( GTK_ENTRY (priv->entry_incomingserver), 
1362                                     null_means_empty (hostname));
1363                         
1364                 /* The UI spec says:
1365                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1366          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1367                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1368          */                                                                                                              
1369                 modest_serversecurity_combo_box_set_active_serversecurity (
1370                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security), 
1371                         modest_server_account_settings_get_security_protocol (incoming_account));
1372                 
1373                 /* Check if we have
1374                  - a secure protocol
1375                  OR
1376                  - use encrypted passwords
1377                 */
1378                 const ModestProtocolType secure_auth = modest_server_account_settings_get_auth_protocol (incoming_account);
1379                 priv->protocol_authentication_incoming = (secure_auth != MODEST_PROTOCOLS_AUTH_NONE)?
1380                         secure_auth:MODEST_PROTOCOLS_AUTH_PASSWORD;
1381                 if (modest_protocol_registry_protocol_type_is_secure(modest_runtime_get_protocol_registry (),
1382                                                                      secure_auth))
1383                 {
1384                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth), 
1385                                                      TRUE);
1386                 }
1387                 else
1388                 {
1389                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth), 
1390                                                      FALSE);
1391                 };
1392                                         
1393                 update_incoming_server_title (dialog, priv->incoming_protocol);
1394                 
1395                 const gint port_num = modest_server_account_settings_get_port (incoming_account);
1396                 if (port_num == 0) {
1397                         /* Show the appropriate port number: */
1398                         on_combo_incoming_security_changed (
1399                                 GTK_COMBO_BOX (priv->combo_incoming_security), dialog);
1400                 } else {
1401                         /* Keep the user-entered port-number,
1402                          * or the already-appropriate automatic port number: */
1403                         gtk_spin_button_set_value (
1404                                 GTK_SPIN_BUTTON (priv->entry_incoming_port), (gdouble) port_num);
1405                 }
1406                 g_object_unref (incoming_account);
1407         }
1408         
1409         outgoing_account = modest_account_settings_get_transport_settings (settings);
1410         if (outgoing_account) {
1411                 const gchar *hostname;
1412                 const gchar *username;
1413                 const gchar *password;
1414
1415                 /* Remember this for later: */
1416                 priv->outgoing_protocol = 
1417                         modest_server_account_settings_get_protocol (outgoing_account);
1418
1419                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1420                 username = modest_server_account_settings_get_username (outgoing_account);
1421                 password = modest_server_account_settings_get_password (outgoing_account);
1422                 gtk_entry_set_text( GTK_ENTRY (priv->entry_outgoingserver), 
1423                                     null_means_empty (hostname));
1424                 
1425                 gtk_entry_set_text( GTK_ENTRY (priv->entry_outgoing_username), 
1426                                     null_means_empty (username));
1427                 gtk_entry_set_text( GTK_ENTRY (priv->entry_outgoing_password), 
1428                                     null_means_empty (password));
1429                 
1430                 /* Get the secure-auth setting: */
1431                 const ModestProtocolType secure_auth = modest_server_account_settings_get_auth_protocol (outgoing_account);
1432                 modest_secureauth_combo_box_set_active_secureauth (
1433                         MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), secure_auth);
1434                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (priv->combo_outgoing_auth), dialog);
1435                 
1436                 modest_serversecurity_combo_box_fill (
1437                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), 
1438                         priv->outgoing_protocol);
1439                 
1440                 /* Get the security setting: */
1441                 const ModestProtocolType security = 
1442                         modest_server_account_settings_get_security_protocol (outgoing_account);
1443                 modest_serversecurity_combo_box_set_active_serversecurity (
1444                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), security);
1445                 
1446                 const gint port_num = modest_server_account_settings_get_port (outgoing_account);
1447                 if (port_num == 0) {
1448                         /* Show the appropriate port number: */
1449                         on_combo_outgoing_security_changed (
1450                                 GTK_COMBO_BOX (priv->combo_outgoing_security), dialog);
1451                 }
1452                 else {
1453                         /* Keep the user-entered port-number,
1454                          * or the already-appropriate automatic port number: */
1455                         gtk_spin_button_set_value (
1456                                 GTK_SPIN_BUTTON (priv->entry_outgoing_port), (gdouble) port_num);
1457                 }
1458                 
1459                 const gboolean has_specific = 
1460                         modest_account_settings_get_use_connection_specific_smtp (settings);
1461                 gtk_toggle_button_set_active (
1462                         GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
1463                         has_specific);
1464                 g_object_unref (outgoing_account);
1465         }
1466
1467         /* Set window title according to account: */
1468         /* TODO: Is this the correct way to find a human-readable name for
1469          * the protocol used? */
1470         ModestProtocol *proto = 
1471                 modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), 
1472                                                                priv->incoming_protocol);
1473
1474         const gchar* proto_str = modest_protocol_get_display_name (proto);
1475         gchar *proto_name = g_utf8_strup(proto_str, -1);
1476         const gchar *account_title = modest_account_settings_get_display_name(settings);
1477
1478         gchar *title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1479         g_free (proto_name);
1480
1481         gtk_window_set_title (GTK_WINDOW (dialog), title);
1482         g_free (title);
1483
1484         /* account_data->is_enabled,  */
1485         /*account_data->is_default,  */
1486
1487         /* Unset the modified flag so we can detect changes later: */
1488         priv->modified = FALSE;
1489 }
1490
1491 /** Show the User Info tab.
1492  */
1493 void 
1494 modest_account_settings_dialog_switch_to_user_info (ModestAccountSettingsDialog *dialog)
1495 {
1496         ModestDefaultAccountSettingsDialogPrivate *priv;
1497
1498         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1499
1500         const gint page_num = gtk_notebook_page_num (priv->notebook, priv->page_user_details);
1501         if (page_num == -1) {
1502                 g_warning ("%s: notebook page not found.\n", __FUNCTION__);     
1503         }
1504                 
1505         /* Ensure that the widget is visible so that gtk_notebook_set_current_page() works: */
1506         /* TODO: even this hack (recommened by the GTK+ documentation) doesn't seem to work. */
1507         
1508         gtk_widget_show (priv->page_user_details);
1509         gtk_widget_show (GTK_WIDGET (priv->notebook));
1510         gtk_widget_show (GTK_WIDGET (dialog));
1511         gtk_notebook_set_current_page (priv->notebook, page_num);
1512 }
1513
1514 static gboolean
1515 save_configuration (ModestAccountSettingsDialog *dialog)
1516 {
1517         ModestServerAccountSettings *store_settings;
1518         ModestServerAccountSettings *transport_settings;
1519         ModestDefaultAccountSettingsDialogPrivate *priv;
1520
1521         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1522                 
1523         /* Set the account data from the widgets: */
1524         const gchar* account_name = priv->account_name;
1525         const gchar* user_fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
1526         modest_account_settings_set_fullname (priv->settings, user_fullname);
1527         
1528         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1529         modest_account_settings_set_email_address (priv->settings, emailaddress);
1530                 
1531 /*      /\* Signature: *\/ */
1532 /*      if (priv->signature_dialog) { */
1533 /*              gboolean use_signature = FALSE; */
1534 /*              gchar *signature =  */
1535 /*                      modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (priv->signature_dialog), */
1536 /*                                                                   &use_signature); */
1537         
1538 /*              modest_account_settings_set_use_signature (priv->settings, use_signature); */
1539 /*              modest_account_settings_set_signature (priv->settings, signature); */
1540 /*      } */
1541         
1542         ModestAccountRetrieveType retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1543                 MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve));
1544         modest_account_settings_set_retrieve_type (priv->settings, retrieve_type);
1545         
1546         gint retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1547                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve));
1548         modest_account_settings_set_retrieve_limit (priv->settings, retrieve_limit);
1549         
1550         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages));
1551         modest_account_settings_set_leave_messages_on_server (priv->settings, leave_on_server); 
1552
1553         store_settings = modest_account_settings_get_store_settings (priv->settings);
1554                         
1555         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
1556         modest_server_account_settings_set_hostname (store_settings, hostname);
1557                                 
1558         const gchar* username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1559         modest_server_account_settings_set_username (store_settings, username);
1560         
1561         const gchar* password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1562         modest_server_account_settings_set_password (store_settings, password);
1563                         
1564         /* port: */
1565         gint port_num = gtk_spin_button_get_value_as_int (
1566                         GTK_SPIN_BUTTON (priv->entry_incoming_port));
1567         modest_server_account_settings_set_port (store_settings, port_num);
1568                         
1569         /* The UI spec says:
1570          * If secure authentication is unchecked, allow sending username and password also as plain text.
1571          * If secure authentication is checked, require one of the secure 
1572          * methods during connection: SSL, TLS, CRAM-MD5 etc. 
1573          */
1574         
1575         const ModestProtocolType protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1576                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security));
1577         modest_server_account_settings_set_security_protocol (store_settings, 
1578                                                               protocol_security_incoming);      
1579         modest_server_account_settings_set_auth_protocol (store_settings, 
1580                                                           priv->protocol_authentication_incoming);
1581
1582         g_object_unref (store_settings);
1583         
1584         /* Outgoing: */
1585         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1586         
1587         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
1588         modest_server_account_settings_set_hostname (transport_settings, hostname);
1589                 
1590         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoing_username));
1591         modest_server_account_settings_set_username (transport_settings, username);
1592                 
1593         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoing_password));
1594         modest_server_account_settings_set_password (transport_settings, password);
1595         
1596         const ModestProtocolType protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1597                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
1598         modest_server_account_settings_set_security_protocol (transport_settings, 
1599                                                               protocol_security_outgoing);
1600         
1601         const ModestProtocolType protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1602                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
1603         modest_server_account_settings_set_auth_protocol (transport_settings, protocol_authentication_outgoing);        
1604         
1605         /* port: */
1606         port_num = gtk_spin_button_get_value_as_int (
1607                         GTK_SPIN_BUTTON (priv->entry_outgoing_port));
1608         modest_server_account_settings_set_port (transport_settings, port_num);
1609         g_object_unref (transport_settings);
1610         
1611         
1612         /* Set the changed account title last, to simplify the previous code: */
1613         gchar* account_title = get_entered_account_title (dialog);
1614         if (!account_title)
1615                 return FALSE; /* Should be prevented already anyway. */
1616                 
1617 /*      if (strcmp (account_title, account_name) != 0) { */
1618         modest_account_settings_set_display_name (priv->settings, account_title);
1619 /*      } */
1620         g_free (account_title);
1621         account_title = NULL;
1622         
1623         /* Save connection-specific SMTP server accounts: */
1624         modest_account_settings_set_use_connection_specific_smtp 
1625                 (priv->settings, 
1626                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
1627
1628         /* this configuration is not persistent, we should not save */
1629         if (account_name != NULL)
1630                 modest_account_mgr_save_account_settings (priv->account_manager, priv->settings);
1631
1632 /*      if (priv->specific_window) { */
1633 /*              return modest_connection_specific_smtp_window_save_server_accounts ( */
1634 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (priv->specific_window)); */
1635 /*      } else { */
1636                 return TRUE;
1637 /*      } */
1638
1639 }
1640
1641 static gboolean entry_is_empty (GtkWidget *entry)
1642 {
1643         if (!entry)
1644                 return FALSE;
1645                 
1646         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1647         if ((!text) || (strlen(text) == 0))
1648                 return TRUE;
1649         else {
1650                 /* Strip it of whitespace at the start and end: */
1651                 gchar *stripped = g_strdup (text);
1652                 stripped = g_strstrip (stripped);
1653                 
1654                 if (!stripped)
1655                         return TRUE;
1656                         
1657                 const gboolean result = (strlen (stripped) == 0);
1658                 
1659                 g_free (stripped);
1660                 return result;
1661         }
1662 }
1663
1664 static void
1665 enable_buttons (ModestAccountSettingsDialog *self)
1666 {
1667         gboolean enable_ok = TRUE;
1668         ModestProtocolType outgoing_auth_protocol;
1669         ModestDefaultAccountSettingsDialogPrivate *priv;
1670
1671         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);;
1672         
1673         /* The account details title is mandatory: */
1674         if (entry_is_empty(priv->entry_account_title))
1675                 enable_ok = FALSE;
1676
1677         /* The user details username is mandatory: */
1678         if (enable_ok && entry_is_empty(priv->entry_user_username))
1679                 enable_ok = FALSE;
1680                 
1681         /* The user details email address is mandatory: */
1682         if (enable_ok && entry_is_empty (priv->entry_user_email))
1683                 enable_ok = FALSE;
1684
1685         /* The custom incoming server is mandatory: */
1686         if (enable_ok && entry_is_empty(priv->entry_incomingserver))
1687                 enable_ok = FALSE;
1688
1689         /* The custom incoming server is mandatory: */
1690         if (enable_ok && entry_is_empty(priv->entry_outgoingserver))
1691                 enable_ok = FALSE;
1692
1693         /* Outgoing username is mandatory if outgoing auth is secure */
1694         if (priv->combo_outgoing_auth) {
1695                 outgoing_auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
1696                         MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
1697                 if (enable_ok && 
1698                     outgoing_auth_protocol != MODEST_PROTOCOLS_AUTH_NONE &&
1699                     entry_is_empty (priv->entry_outgoing_username))
1700                         enable_ok = FALSE;
1701         }
1702                         
1703         /* Enable the buttons, 
1704          * identifying them via their associated response codes:
1705          */
1706         GtkDialog *dialog_base = GTK_DIALOG (self);
1707         gtk_dialog_set_response_sensitive (dialog_base,
1708                                            GTK_RESPONSE_OK,
1709                                            enable_ok);
1710 }
1711
1712 void
1713 modest_account_settings_dialog_check_allow_changes (ModestAccountSettingsDialog *self)
1714 {
1715         ModestServerAccountSettings *incoming_settings;
1716         const gchar *server_account_name;
1717         gboolean username_known;
1718         ModestDefaultAccountSettingsDialogPrivate *priv;
1719
1720         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);;
1721
1722         if (!G_IS_OBJECT (priv->settings))
1723                 return;
1724
1725         incoming_settings = modest_account_settings_get_store_settings (priv->settings);
1726         server_account_name = modest_server_account_settings_get_account_name (incoming_settings);
1727
1728         username_known = modest_account_mgr_get_server_account_username_has_succeeded (priv->account_manager, 
1729                                                                                        server_account_name);
1730
1731         /* Enable or disable widgets */
1732         gtk_widget_set_sensitive (priv->entry_user_username, !username_known);
1733         gtk_widget_set_sensitive (priv->entry_incomingserver, !username_known);
1734         gtk_widget_set_sensitive (priv->entry_incoming_port, !username_known);
1735         gtk_widget_set_sensitive (priv->combo_incoming_security, !username_known);
1736 }
1737
1738 void
1739 modest_account_settings_dialog_save_password (ModestAccountSettingsDialog *dialog)
1740 {
1741         ModestDefaultAccountSettingsDialogPrivate *priv;
1742
1743         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
1744
1745         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1746         priv->modified = TRUE;
1747 }
1748
1749 static void
1750 modest_default_account_settings_dialog_class_init (ModestDefaultAccountSettingsDialogClass *klass)
1751 {
1752         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1753         g_type_class_add_private (klass, sizeof (ModestDefaultAccountSettingsDialogPrivate));
1754
1755         object_class->get_property = modest_account_settings_dialog_get_property;
1756         object_class->set_property = modest_account_settings_dialog_set_property;
1757         object_class->dispose = modest_account_settings_dialog_dispose;
1758         object_class->finalize = modest_account_settings_dialog_finalize;
1759 }