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