* Fixes NB#91689. fixes a wrong check for ASCII
[modest] / src / maemo / 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                                      GTK_WINDOW (self));
512
513         response = gtk_dialog_run (GTK_DIALOG (priv->signature_dialog));
514         gtk_widget_hide (priv->signature_dialog);
515         if (response != GTK_RESPONSE_OK) {
516                 /* Destroy the widget now, and its data: */
517                 gtk_widget_destroy (priv->signature_dialog);
518                 priv->signature_dialog = NULL;
519         } else {
520                 /* Mark modified, so we use the dialog's data later: */
521                 priv->modified = TRUE;  
522         }
523 }
524
525 static GtkWidget*
526 create_page_user_details (ModestDefaultAccountSettingsDialog *self)
527 {
528         ModestDefaultAccountSettingsDialogPrivate *priv;
529         GtkWidget *box;
530         GtkAdjustment *focus_adjustment = NULL;
531         GtkSizeGroup* sizegroup;
532         GtkWidget *scrollwin;
533
534         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
535
536         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
537         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
538         scrollwin = gtk_scrolled_window_new (NULL, NULL);
539         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
540                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
541          
542         /* The name widgets: */
543         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
544
545         /* Auto-capitalization is the default, so let's turn it off: */
546         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
547         /* Set max length as in the UI spec:
548          * The UI spec seems to want us to show a dialog if we hit the maximum. */
549         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
550         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
551                 on_entry_max, self);
552         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
553                 _("mcen_li_emailsetup_name"), priv->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
554         gtk_widget_show (priv->entry_user_name);
555         connect_for_modified (self, priv->entry_user_name);
556         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
557         gtk_widget_show (caption);
558
559
560         /* Prevent the use of some characters in the name, 
561          * as required by our UI specification: */
562         GList *list_prevent = NULL;
563         list_prevent = g_list_append (list_prevent, "<");
564         list_prevent = g_list_append (list_prevent, ">");
565         modest_validating_entry_set_unallowed_characters (
566                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
567         g_list_free (list_prevent);
568         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
569                                          on_entry_invalid_fullname_character, self);
570         
571         /* The username widgets: */     
572         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
573         /* Auto-capitalization is the default, so let's turn it off: */
574         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
575         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
576                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
577         gtk_widget_show (priv->entry_user_username);
578         connect_for_modified (self, priv->entry_user_username);
579         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
580         gtk_widget_show (caption);
581         
582         /* Prevent the use of some characters in the username, 
583          * as required by our UI specification: */
584         modest_validating_entry_set_unallowed_characters_whitespace (
585                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
586         modest_validating_entry_set_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
587                                           modest_utils_on_entry_invalid_character, 
588                                           self);
589         
590         /* Set max length as in the UI spec:
591          * The UI spec seems to want us to show a dialog if we hit the maximum. */
592         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
593         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
594                 on_entry_max, self);
595         
596         /* The password widgets: */     
597         priv->entry_user_password = gtk_entry_new ();
598         /* Auto-capitalization is the default, so let's turn it off: */
599         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
600                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
601         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
602         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
603         caption = create_caption_new_with_asterisk (self, sizegroup, 
604                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
605         gtk_widget_show (priv->entry_user_password);
606         connect_for_modified (self, priv->entry_user_password);
607         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
608         gtk_widget_show (caption);
609         
610         /* The email address widgets: */        
611         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
612         /* Auto-capitalization is the default, so let's turn it off: */
613         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
614         caption = create_caption_new_with_asterisk (self, sizegroup, 
615                 _("mcen_li_emailsetup_email_address"), priv->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
616         gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
617         gtk_widget_show (priv->entry_user_email);
618         connect_for_modified (self, priv->entry_user_email);
619         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
620         gtk_widget_show (caption);
621         
622         /* Set max length as in the UI spec:
623          * The UI spec seems to want us to show a dialog if we hit the maximum. */
624         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
625         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
626                 on_entry_max, self);
627         
628         /* Signature button: */
629         if (!priv->button_signature)
630                 priv->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
631         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
632                 priv->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
633         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
634         gtk_widget_show (priv->button_signature);
635         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
636         gtk_widget_show (caption);
637
638         g_object_unref (sizegroup);
639                 
640         g_signal_connect (G_OBJECT (priv->button_signature), "clicked",
641                 G_CALLBACK (on_button_signature), self);
642                 
643         gtk_widget_show (GTK_WIDGET (box));
644         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollwin), box);
645         gtk_widget_show (scrollwin);
646
647         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
648         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment); 
649         
650         return GTK_WIDGET (scrollwin);
651 }
652
653 /* Change the caption title for the incoming server */
654 static void
655 update_incoming_server_title (ModestDefaultAccountSettingsDialog *self,
656                               ModestProtocolType protocol_type)
657 {
658         ModestProtocolRegistry *protocol_registry;
659         ModestProtocol *protocol;
660         const gchar *protocol_display_name;
661         gchar* incomingserver_title;
662         gchar *with_asterisk;
663         ModestDefaultAccountSettingsDialogPrivate *priv;
664
665         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
666
667         protocol_registry = modest_runtime_get_protocol_registry ();
668         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
669         protocol_display_name = modest_protocol_get_display_name (protocol);
670         incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), 
671                                                protocol_display_name);
672         
673         /* This is a mandatory field, so add a *. This is usually done by
674          * create_caption_new_with_asterisk() but we can't use that here. */
675         with_asterisk = g_strconcat (incomingserver_title, "*", NULL);
676         g_free (incomingserver_title);
677         
678         g_object_set (G_OBJECT (priv->caption_incoming), "label", with_asterisk, NULL);
679         g_free (with_asterisk);
680 }
681
682 /** Change the caption title for the incoming server, 
683  * as specified in the UI spec:
684  */
685 /* static void  */
686 /* update_incoming_server_security_choices (ModestDefaultAccountSettingsDialog *self,  */
687 /*                                       ModestProtocolType protocol) */
688 /* { */
689 /*      /\* Fill the combo with appropriately titled choices for POP or IMAP. *\/ */
690 /*      /\* The choices are the same, but the titles are different, as in the UI spec. *\/ */
691 /*      modest_serversecurity_combo_box_fill ( */
692 /*              MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security), protocol); */
693 /* } */
694            
695 static GtkWidget* 
696 create_page_incoming (ModestDefaultAccountSettingsDialog *self)
697 {
698         ModestDefaultAccountSettingsDialogPrivate *priv;
699         GtkWidget *box;
700         GtkSizeGroup *sizegroup;
701
702         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
703
704         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE); 
705         /* Create a size group to be used by all captions.
706          * Note that HildonCaption does not create a default size group if we do not specify one.
707          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
708         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
709          
710         /* The incoming server widgets: */
711         if(!priv->entry_incomingserver)
712                 priv->entry_incomingserver = gtk_entry_new ();
713         /* Auto-capitalization is the default, so let's turn it off: */
714         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
715
716         if (priv->caption_incoming)
717                 gtk_widget_destroy (priv->caption_incoming);
718            
719         /* The caption title will be updated in update_incoming_server_title().
720          * so this default text will never be seen: */
721         /* (Note: Changing the title seems pointless. murrayc) */
722         priv->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
723                 "Incoming Server", priv->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
724         gtk_widget_show (priv->entry_incomingserver);
725         connect_for_modified (self, priv->entry_incomingserver);
726         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
727         gtk_widget_show (priv->caption_incoming);
728
729         /* Incoming security widgets */
730         priv->incoming_security = 
731                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
732                                                         TRUE, sizegroup);
733         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security, 
734                             FALSE, FALSE, MODEST_MARGIN_HALF);
735
736         gtk_widget_show (priv->incoming_security);
737
738         g_object_unref (sizegroup);     
739         gtk_widget_show (GTK_WIDGET (box));
740         
741         return GTK_WIDGET (box);
742 }
743
744 static void
745 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
746 {
747         GtkWidget *widget = GTK_WIDGET (user_data);
748         
749         /* Enable the widget only if the toggle button is active: */
750         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
751         gtk_widget_set_sensitive (widget, enable);
752 }
753
754 /* Make the sensitivity of a widget depend on a toggle button.
755  */
756 static void
757 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
758 {
759         g_signal_connect (G_OBJECT (button), "toggled",
760                 G_CALLBACK (on_toggle_button_changed), widget);
761         
762         /* Set the starting sensitivity: */
763         on_toggle_button_changed (button, widget);
764 }
765
766 static void
767 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
768 {
769         ModestDefaultAccountSettingsDialog * self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data);
770         ModestConnectionSpecificSmtpWindow *smtp_win;
771         ModestDefaultAccountSettingsDialogPrivate *priv;
772
773         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
774
775         /* Create the window if necessary: */
776         smtp_win = modest_connection_specific_smtp_window_new ();
777         modest_connection_specific_smtp_window_fill_with_connections (smtp_win, priv->account_manager);
778
779         /* Show the window: */  
780         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
781                                      GTK_WINDOW (smtp_win), GTK_WINDOW (self));
782         gtk_widget_show (GTK_WIDGET (smtp_win));
783         priv->modified = TRUE;
784 }
785
786 /* static void */
787 /* on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data) */
788 /* { */
789 /*      ModestDefaultAccountSettingsDialog *self; */
790 /*      ModestProtocolRegistry *protocol_registry; */
791 /*      ModestProtocolType protocol_security;    */
792 /*      gboolean secureauth_used; */
793
794 /*      self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data); */
795 /*      protocol_registry = modest_runtime_get_protocol_registry (); */
796         
797 /*      protocol_security =  */
798 /*              modest_secureauth_combo_box_get_active_secureauth ( */
799 /*                      MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth)); */
800 /*      secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry, protocol_security); */
801         
802 /*      gtk_widget_set_sensitive (priv->caption_outgoing_username, secureauth_used); */
803 /*      gtk_widget_set_sensitive (priv->caption_outgoing_password, secureauth_used); */
804 /* } */
805
806 /* static void */
807 /* on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data) */
808 /* { */
809 /*      ModestDefaultAccountSettingsDialog *self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (user_data); */
810         
811 /*      const gint port_number =  */
812 /*              modest_serversecurity_combo_box_get_active_serversecurity_port ( */
813 /*                      MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security)); */
814
815 /*      if(port_number != 0) { */
816 /*              hildon_number_editor_set_value ( */
817 /*                      HILDON_NUMBER_EDITOR (priv->entry_outgoing_port), port_number); */
818 /*      }                */
819 /* } */
820
821 static void
822 on_missing_mandatory_data (ModestSecurityOptionsView *security_view,
823                            gboolean missing,
824                            gpointer user_data)
825 {
826         /* Disable the OK button */
827         gtk_dialog_set_response_sensitive (GTK_DIALOG (user_data),
828                                            GTK_RESPONSE_OK,
829                                            !missing);
830 }
831
832 static GtkWidget* 
833 create_page_outgoing (ModestDefaultAccountSettingsDialog *self)
834 {
835         ModestDefaultAccountSettingsDialogPrivate *priv;
836         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
837         GtkAdjustment *focus_adjustment = NULL;
838
839         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
840         
841         /* Put it all in a scrolled window, so that all widgets can be 
842          * accessed even when the on-screen keyboard is visible: */
843         GtkWidget *scrollwin = gtk_scrolled_window_new(NULL, NULL);
844         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin), 
845                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
846         
847         /* Create a size group to be used by all captions.
848          * Note that HildonCaption does not create a default size group if we do not specify one.
849          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
850         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
851          
852         /* The outgoing server widgets: */
853         if (!priv->entry_outgoingserver)
854                 priv->entry_outgoingserver = gtk_entry_new ();
855         /* Auto-capitalization is the default, so let's turn it off: */
856         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
857         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
858                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
859         gtk_widget_show (priv->entry_outgoingserver);
860         connect_for_modified (self, priv->entry_outgoingserver);
861         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
862         gtk_widget_show (caption);
863
864         /* Outgoing security widgets */
865         priv->outgoing_security = 
866                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
867                                                         TRUE, sizegroup);
868         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security, 
869                             FALSE, FALSE, MODEST_MARGIN_HALF);
870         gtk_widget_show (priv->outgoing_security);
871         g_signal_connect (priv->outgoing_security, "missing-mandatory-data",
872                           G_CALLBACK (on_missing_mandatory_data), self);
873
874         GtkWidget *separator = gtk_hseparator_new ();
875         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
876         gtk_widget_show (separator);
877         
878         /* connection-specific checkbox: */
879         if (!priv->checkbox_outgoing_smtp_specific) {
880                 priv->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
881                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
882                         FALSE);
883         }
884         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
885                 priv->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
886         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
887         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
888         gtk_widget_show (caption);
889         connect_for_modified (self, priv->checkbox_outgoing_smtp_specific);
890         
891         /* Connection-specific SMTP-Severs Edit button: */
892         if (!priv->button_outgoing_smtp_servers)
893                 priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
894         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
895                 priv->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
896         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
897         gtk_widget_show (priv->button_outgoing_smtp_servers);
898         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
899         gtk_widget_show (caption);
900         
901         /* Only enable the button when the checkbox is checked: */
902         enable_widget_for_togglebutton (priv->button_outgoing_smtp_servers, 
903                 GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific));
904
905         g_object_unref (sizegroup);
906                 
907         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
908                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
909                 
910         gtk_widget_show (GTK_WIDGET (box));
911         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollwin), box);
912         gtk_widget_show(scrollwin);
913
914         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
915         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment);
916         
917         return GTK_WIDGET (scrollwin);
918 }
919         
920 static gboolean
921 check_data (ModestDefaultAccountSettingsDialog *self)
922 {
923         gchar* account_title;
924         const gchar* email_address; 
925         const gchar* hostname;
926         const gchar* hostname2;
927         ModestDefaultAccountSettingsDialogPrivate *priv;
928
929         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
930
931         /* Check that the title is not already in use: */
932         account_title = get_entered_account_title (self);
933         if (!account_title)
934                 return FALSE; /* Should be prevented already anyway. */
935                 
936         if (strcmp(account_title, priv->original_account_title) != 0) {
937                 gboolean name_in_use; 
938
939                 /* Check the changed title: */
940                 name_in_use = modest_account_mgr_account_with_display_name_exists (priv->account_manager,
941                                                                                    account_title);
942                 
943                 if (name_in_use) {
944                         /* Warn the user via a dialog: */
945                         modest_platform_information_banner(NULL, NULL, _("mail_ib_account_name_already_existing"));
946                         
947                         g_free (account_title);
948                         return FALSE;
949                 }
950         }
951         
952         g_free (account_title);
953         account_title  = NULL;
954
955         /* Check that the email address is valid: */
956         email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
957         if ((!email_address) || (strlen(email_address) == 0)) {
958                 return FALSE;
959         }
960                         
961         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
962                 /* Warn the user via a dialog: */
963                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_email"));
964                                          
965                 /* Return focus to the email address entry: */
966                 gtk_widget_grab_focus (priv->entry_user_email);
967                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
968                 return FALSE;
969         }
970
971         /* make sure the domain name for the incoming server is valid */
972         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
973         if ((!hostname) || (strlen(hostname) == 0)) {
974                 return FALSE;
975         }
976         
977         if (!modest_text_utils_validate_domain_name (hostname)) {
978                 /* Warn the user via a dialog: */
979                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
980                                          
981                 /* Return focus to the email address entry: */
982                 gtk_widget_grab_focus (priv->entry_incomingserver);
983                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_incomingserver), 0, -1);
984                 return FALSE;
985         }
986
987         /* make sure the domain name for the outgoing server is valid */
988         hostname2 = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
989         if ((!hostname2) || (strlen(hostname2) == 0)) {
990                 return FALSE;
991         }
992         
993         if (!modest_text_utils_validate_domain_name (hostname2)) {
994                 /* Warn the user via a dialog: */
995                 modest_platform_information_banner (priv->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
996
997                 /* Return focus to the email address entry: */
998                 gtk_widget_grab_focus (priv->entry_outgoingserver);
999                 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
1000                 return FALSE;
1001         }
1002         
1003 /*      /\* Find a suitable authentication method when secure authentication is desired *\/ */
1004 /*      port_num = hildon_number_editor_get_value ( */
1005 /*              HILDON_NUMBER_EDITOR (priv->entry_incoming_port)); */
1006 /*      username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)); */
1007
1008 /*      protocol_registry = modest_runtime_get_protocol_registry (); */
1009
1010 /*      protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity ( */
1011 /*              MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security)); */
1012 /*      if (!modest_protocol_registry_protocol_type_is_secure(protocol_registry, protocol_security_incoming)) */
1013 /*      { */
1014 /*              if (gtk_toggle_button_get_active ( */
1015 /*                              GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth))) { */
1016 /*                      GError *error = NULL; */
1017 /*                      GList *list_auth_methods; */
1018
1019 /*                      list_auth_methods =  */
1020 /*                              modest_utils_get_supported_secure_authentication_methods (priv->incoming_protocol,  */
1021 /*                                                                                        hostname, port_num, username, GTK_WINDOW (self), &error); */
1022 /*                      if (list_auth_methods) { */
1023 /*                              GList* method; */
1024
1025 /*                              /\* Use the first supported method. */
1026 /*                               * TODO: Should we prioritize them, to prefer a particular one? *\/ */
1027 /*                              for (method = list_auth_methods; method != NULL; method = g_list_next(method)) */
1028 /*                              { */
1029 /*                                      ModestProtocolType proto = (ModestProtocolType)(GPOINTER_TO_INT(method->data)); */
1030 /*                                      // Allow secure methods, e.g MD5 only */
1031 /*                                      if (modest_protocol_registry_protocol_type_is_secure(protocol_registry, proto)) */
1032 /*                                      { */
1033 /*                                              priv->protocol_authentication_incoming = proto; */
1034 /*                                              break; */
1035 /*                                      } */
1036 /*                              } */
1037 /*                              g_list_free (list_auth_methods); */
1038 /*                      } */
1039
1040 /*                      if (list_auth_methods == NULL ||  */
1041 /*                          !modest_protocol_registry_protocol_type_is_secure(protocol_registry, priv->protocol_authentication_incoming)) */
1042 /*                      { */
1043 /*                              if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() || */
1044 /*                                              error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED) */
1045 /*                                      modest_platform_information_banner(GTK_WIDGET (self), NULL,  */
1046 /*                                                                     _("mcen_ib_unableto_discover_auth_methods")); */
1047
1048 /*                              if(error != NULL) */
1049 /*                                      g_error_free(error); */
1050                                         
1051 /*                              /\* This is a nasty hack. jschmid. *\/ */
1052 /*                              /\* Don't let the dialog close *\/ */
1053 /*                              /\*g_signal_stop_emission_by_name (dialog, "response");*\/ */
1054 /*                              return FALSE; */
1055 /*                      } */
1056 /*              } */
1057 /*      } */
1058         
1059         return TRUE;
1060 }
1061
1062 static void 
1063 on_response (GtkDialog *wizard_dialog,
1064              gint response_id,
1065              gpointer user_data)
1066 {
1067         ModestDefaultAccountSettingsDialog *self = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
1068         ModestDefaultAccountSettingsDialogPrivate *priv;
1069         gboolean prevent_response = FALSE, sec_changed;
1070         ModestSecurityOptionsView *incoming_sec, *outgoing_sec;
1071
1072         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
1073         enable_buttons (self);
1074
1075         /* Check if security widgets changed */
1076         incoming_sec = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
1077         outgoing_sec = MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security);
1078         sec_changed =
1079                 modest_security_options_view_changed (incoming_sec, priv->settings) ||
1080                 modest_security_options_view_changed (outgoing_sec, priv->settings);
1081
1082         /* Warn about unsaved changes: */
1083         if (response_id == GTK_RESPONSE_CANCEL && (priv->modified || sec_changed)) {
1084                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
1085                         _("imum_nc_wizard_confirm_lose_changes")));
1086                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1087                  
1088                  const gint dialog_response = gtk_dialog_run (dialog);
1089                  gtk_widget_destroy (GTK_WIDGET (dialog));
1090                  
1091                 if (dialog_response != GTK_RESPONSE_OK)
1092                         prevent_response = TRUE;
1093         }
1094         /* Check for invalid input: */
1095         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
1096                 prevent_response = TRUE;
1097         }
1098                 
1099         if (prevent_response) {
1100                 /* This is a nasty hack. murrayc. */
1101                 /* Don't let the dialog close */
1102                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1103                 return; 
1104         } else {
1105                 modest_tny_account_store_set_send_mail_blocked (modest_runtime_get_account_store (), FALSE);
1106         }
1107                 
1108         if (response_id == GTK_RESPONSE_OK) {
1109                 /* Try to save the changes if modified (NB #59251): */
1110                 if (priv->modified || sec_changed) {
1111                         if (save_configuration (self)) {
1112                                 /* Do not show the account-saved dialog if we are just saving this 
1113                                  * temporarily, because from the user's point of view it will not 
1114                                  * really be saved (saved + enabled) until later
1115                                  */
1116                                 if (modest_account_settings_get_account_name (priv->settings) != NULL) {
1117                                         ModestServerAccountSettings *store_settings;
1118                                         ModestServerAccountSettings *transport_settings;
1119                                         const gchar *store_account_name;
1120                                         const gchar *transport_account_name;
1121
1122
1123                                         store_settings = modest_account_settings_get_store_settings (priv->settings);
1124                                         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1125                                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
1126                                         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
1127                                         
1128                                         if (store_account_name) {
1129                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1130                                                                                           store_account_name);
1131                                         }
1132                                         if (transport_account_name) {
1133                                                 modest_account_mgr_notify_account_update (priv->account_manager, 
1134                                                                                           transport_account_name);
1135                                         }
1136                                         g_object_unref (store_settings);
1137                                         g_object_unref (transport_settings);
1138                                         
1139                                         modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1140                                 }
1141                         } else {
1142                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1143                         }
1144                 }
1145         }
1146 }
1147
1148 static void
1149 modest_default_account_settings_dialog_init (ModestDefaultAccountSettingsDialog *self)
1150 {
1151         ModestDefaultAccountSettingsDialogPrivate *priv;
1152
1153         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(self);
1154
1155         priv->incoming_security = NULL;
1156         priv->outgoing_security = NULL;
1157
1158         /* Create the notebook to be used by the GtkDialog base class:
1159          * Each page of the notebook will be a page of the wizard: */
1160         priv->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1161         priv->settings = modest_account_settings_new ();
1162
1163         /* Get the account manager object, 
1164          * so we can check for existing accounts,
1165          * and create new accounts: */
1166         priv->account_manager = modest_runtime_get_account_mgr ();
1167         g_assert (priv->account_manager);
1168         g_object_ref (priv->account_manager);
1169         
1170         priv->protocol_authentication_incoming = MODEST_PROTOCOLS_AUTH_PASSWORD;
1171
1172     /* Create the common pages, 
1173      */
1174         priv->page_account_details = create_page_account_details (self);
1175         priv->page_user_details = create_page_user_details (self);
1176         priv->page_incoming = create_page_incoming (self);
1177         priv->page_outgoing = create_page_outgoing (self);
1178         
1179         /* Add the notebook pages: */
1180         gtk_notebook_append_page (priv->notebook, priv->page_account_details, 
1181                 gtk_label_new (_("mcen_ti_account_settings_account")));
1182         gtk_notebook_append_page (priv->notebook, priv->page_user_details, 
1183                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1184         gtk_notebook_append_page (priv->notebook, priv->page_incoming,
1185                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1186         gtk_notebook_append_page (priv->notebook, priv->page_outgoing,
1187                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1188                 
1189         GtkDialog *dialog = GTK_DIALOG (self);
1190         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (priv->notebook));
1191         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1192         gtk_widget_show (GTK_WIDGET (priv->notebook));
1193         
1194     /* Add the buttons: */
1195     gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
1196     gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
1197     
1198     /* Connect to the dialog's response signal: */
1199     /* We use connect-before 
1200      * so we can stop the signal emission, 
1201      * to stop the default signal handler from closing the dialog.
1202      */
1203     g_signal_connect (G_OBJECT (self), "response",
1204             G_CALLBACK (on_response), self); 
1205             
1206     priv->modified = FALSE;
1207
1208     /* When this window is shown, hibernation should not be possible, 
1209          * because there is no sensible way to save the state: */
1210     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1211         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1212
1213     /* Prevent sending mails while editing an account, to avoid hangs on unprotected locks
1214      * while sending messages causes an error dialog and we have a lock */
1215     modest_tny_account_store_set_send_mail_blocked (modest_runtime_get_account_store (), TRUE);
1216
1217     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings",
1218                                     modest_maemo_utils_get_osso_context());
1219 }
1220
1221 ModestAccountSettingsDialog*
1222 modest_default_account_settings_dialog_new (void)
1223 {
1224         return g_object_new (MODEST_TYPE_DEFAULT_ACCOUNT_SETTINGS_DIALOG, NULL);
1225 }
1226
1227 /** Update the UI with the stored account details, so they can be edited.
1228  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1229  */
1230 static void 
1231 modest_default_account_settings_dialog_load_settings (ModestAccountSettingsDialog *dialog, 
1232                                                       ModestAccountSettings *settings)
1233 {
1234         ModestServerAccountSettings *incoming_account;
1235         ModestServerAccountSettings *outgoing_account;
1236         ModestProtocolRegistry *protocol_registry;
1237         const gchar *account_name, *server_account_name;
1238         ModestDefaultAccountSettingsDialogPrivate *priv;
1239         gint page_num;
1240         gboolean username_known;
1241
1242         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
1243         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1244
1245         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1246         protocol_registry = modest_runtime_get_protocol_registry ();
1247
1248         incoming_account = modest_account_settings_get_store_settings (settings);
1249         outgoing_account = modest_account_settings_get_transport_settings (settings);
1250
1251         account_name = modest_account_settings_get_account_name (settings);
1252                 
1253         /* Save the account name so we can refer to it later: */
1254         if (priv->account_name)
1255                 g_free (priv->account_name);
1256         priv->account_name = g_strdup (account_name);
1257
1258         if (priv->settings)
1259                 g_object_unref (priv->settings);
1260         priv->settings = g_object_ref (settings);
1261         
1262         /* Save the account title so we can refer to it if the user changes it: */
1263         if (priv->original_account_title)
1264                 g_free (priv->original_account_title);
1265         priv->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1266         
1267         /* Show the account data in the widgets: */
1268         
1269         /* Note that we never show the non-display name in the UI.
1270          * (Though the display name defaults to the non-display name at the start.) */
1271         gtk_entry_set_text( GTK_ENTRY (priv->entry_account_title),
1272                             null_means_empty (modest_account_settings_get_display_name (settings)));
1273         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_name), 
1274                             null_means_empty (modest_account_settings_get_fullname (settings)));
1275         gtk_entry_set_text( GTK_ENTRY (priv->entry_user_email), 
1276                             null_means_empty (modest_account_settings_get_email_address (settings)));
1277         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), 
1278                                                             modest_account_settings_get_retrieve_type (settings));
1279         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1280                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve), 
1281                 modest_account_settings_get_retrieve_limit (settings));
1282         
1283         
1284         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages), 
1285                                       modest_account_settings_get_leave_messages_on_server (settings));
1286         
1287
1288         if (incoming_account) {
1289                 const gchar *username, *password, *hostname, *proto_str, *account_title;
1290                 gchar *proto_name, *title;
1291                 ModestProtocolType incoming_protocol;
1292
1293                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve), modest_server_account_settings_get_protocol (incoming_account));
1294                 
1295                 if (!modest_protocol_registry_protocol_type_has_leave_on_server (protocol_registry,
1296                                                                                  modest_server_account_settings_get_protocol (incoming_account))) {
1297                         gtk_widget_hide (priv->caption_leave_messages);
1298                 } else {
1299                         gtk_widget_show (priv->caption_leave_messages);
1300                 }
1301
1302                 /* Remember this for later: */
1303                 incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1304                 
1305                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1306                 username = modest_server_account_settings_get_username (incoming_account);
1307                 password = modest_server_account_settings_get_password (incoming_account);
1308                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_username),
1309                                     null_means_empty (username));
1310                 gtk_entry_set_text( GTK_ENTRY (priv->entry_user_password), 
1311                                     null_means_empty (password));
1312                         
1313                 gtk_entry_set_text( GTK_ENTRY (priv->entry_incomingserver), 
1314                                     null_means_empty (hostname));
1315
1316                 /* Load security settings */
1317                 modest_security_options_view_load_settings (
1318                             MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security), 
1319                             settings);
1320                 gtk_widget_show (priv->incoming_security);
1321                                         
1322                 /* Update the incoming label */
1323                 update_incoming_server_title (MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG (dialog), 
1324                                               incoming_protocol);
1325                 
1326                 /* Set window title according to account */
1327                 proto_str = modest_protocol_get_display_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, incoming_protocol));
1328                 proto_name = g_utf8_strup (proto_str, -1);
1329                 account_title = modest_account_settings_get_display_name(settings);
1330                 title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1331                 
1332                 gtk_window_set_title (GTK_WINDOW (dialog), title);
1333
1334                 g_free (proto_name);
1335                 g_free (title);
1336                 g_object_unref (incoming_account);
1337         }
1338         
1339         outgoing_account = modest_account_settings_get_transport_settings (settings);
1340         if (outgoing_account) {
1341                 const gchar *hostname;
1342                 const gchar *username;
1343                 const gchar *password;
1344                 ModestProtocolType outgoing_protocol;
1345
1346                 /* Remember this for later: */
1347                 outgoing_protocol = 
1348                         modest_server_account_settings_get_protocol (outgoing_account);
1349
1350                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1351                 username = modest_server_account_settings_get_username (outgoing_account);
1352                 password = modest_server_account_settings_get_password (outgoing_account);
1353                 gtk_entry_set_text( GTK_ENTRY (priv->entry_outgoingserver), 
1354                                     null_means_empty (hostname));
1355
1356                 /* Load security settings */
1357                 modest_security_options_view_load_settings (
1358                             MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security), 
1359                             settings);
1360                 gtk_widget_show (priv->outgoing_security);
1361
1362                 const gboolean has_specific = 
1363                         modest_account_settings_get_use_connection_specific_smtp (settings);
1364                 gtk_toggle_button_set_active (
1365                         GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
1366                         has_specific);
1367                 g_object_unref (outgoing_account);
1368         }
1369
1370         /* Switch to user page */
1371         page_num = gtk_notebook_page_num (priv->notebook,priv->page_user_details);
1372         gtk_notebook_set_current_page (priv->notebook, page_num);
1373
1374         /* Check if we allow changes or not */
1375         server_account_name = modest_server_account_settings_get_account_name (incoming_account);
1376         username_known = 
1377                 modest_account_mgr_get_server_account_username_has_succeeded (priv->account_manager,
1378                                                                               server_account_name);
1379         gtk_widget_set_sensitive (priv->entry_user_username, !username_known);
1380         gtk_widget_set_sensitive (priv->entry_incomingserver, !username_known);
1381         modest_security_options_view_enable_changes (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
1382                                                      !username_known);
1383
1384
1385         /* Unset the modified flag so we can detect changes later: */
1386         priv->modified = FALSE;
1387 }
1388
1389 static gboolean
1390 save_configuration (ModestDefaultAccountSettingsDialog *dialog)
1391 {
1392         const gchar* user_fullname;
1393         const gchar* emailaddress;
1394         ModestServerAccountSettings *store_settings;
1395         ModestServerAccountSettings *transport_settings;
1396         ModestAccountRetrieveType retrieve_type;
1397         gint retrieve_limit;
1398         gboolean leave_on_server;
1399         const gchar* hostname;
1400         const gchar* username;
1401         const gchar* password;
1402         gchar* account_title;
1403         ModestDefaultAccountSettingsDialogPrivate *priv;
1404         const gchar* account_name;
1405
1406         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (dialog);
1407         account_name = priv->account_name;
1408
1409         /* Set the account data from the widgets: */
1410         user_fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
1411         modest_account_settings_set_fullname (priv->settings, user_fullname);
1412         
1413         emailaddress = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1414         modest_account_settings_set_email_address (priv->settings, emailaddress);
1415                 
1416         /* Signature: */
1417         if (priv->signature_dialog) {
1418                 gboolean use_signature = FALSE;
1419                 gchar *signature;
1420                 signature = modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (priv->signature_dialog),
1421                                                                          &use_signature);
1422         
1423                 modest_account_settings_set_use_signature (priv->settings, use_signature);
1424                 modest_account_settings_set_signature (priv->settings, signature);
1425         }
1426         
1427         retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1428                 MODEST_RETRIEVE_COMBO_BOX (priv->combo_retrieve));
1429         modest_account_settings_set_retrieve_type (priv->settings, retrieve_type);
1430         
1431         retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1432                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (priv->combo_limit_retrieve));
1433         modest_account_settings_set_retrieve_limit (priv->settings, retrieve_limit);
1434         
1435         leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->checkbox_leave_messages));
1436         modest_account_settings_set_leave_messages_on_server (priv->settings, leave_on_server); 
1437
1438         store_settings = modest_account_settings_get_store_settings (priv->settings);
1439                         
1440         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver));
1441         modest_server_account_settings_set_hostname (store_settings, hostname);
1442                                 
1443         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1444         modest_server_account_settings_set_username (store_settings, username);
1445         
1446         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1447         modest_server_account_settings_set_password (store_settings, password);
1448
1449         /* Save security settings */
1450         modest_security_options_view_save_settings (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security), 
1451                                                     priv->settings);
1452
1453         g_object_unref (store_settings);
1454         
1455         /* Outgoing: */
1456         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1457         
1458         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
1459         modest_server_account_settings_set_hostname (transport_settings, hostname);
1460                         
1461         /* Save security settings */
1462         modest_security_options_view_save_settings (
1463             MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security), 
1464             priv->settings);
1465         
1466         /* Set the changed account title last, to simplify the previous code: */
1467         account_title = get_entered_account_title (dialog);
1468         if (!account_title)
1469                 return FALSE; /* Should be prevented already anyway. */
1470                 
1471 /*      if (strcmp (account_title, account_name) != 0) { */
1472         modest_account_settings_set_display_name (priv->settings, account_title);
1473 /*      } */
1474         g_free (account_title);
1475         account_title = NULL;
1476         
1477         /* Save connection-specific SMTP server accounts: */
1478         modest_account_settings_set_use_connection_specific_smtp 
1479                 (priv->settings, 
1480                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
1481
1482         /* this configuration is not persistent, we should not save */
1483         if (account_name != NULL)
1484                 modest_account_mgr_save_account_settings (priv->account_manager, priv->settings);
1485
1486         return TRUE;
1487 }
1488
1489 static gboolean entry_is_empty (GtkWidget *entry)
1490 {
1491         if (!entry)
1492                 return FALSE;
1493                 
1494         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1495         if ((!text) || (strlen(text) == 0))
1496                 return TRUE;
1497         else {
1498                 /* Strip it of whitespace at the start and end: */
1499                 gchar *stripped = g_strdup (text);
1500                 stripped = g_strstrip (stripped);
1501                 
1502                 if (!stripped)
1503                         return TRUE;
1504                         
1505                 const gboolean result = (strlen (stripped) == 0);
1506                 
1507                 g_free (stripped);
1508                 return result;
1509         }
1510 }
1511
1512 static void
1513 enable_buttons (ModestDefaultAccountSettingsDialog *self)
1514 {
1515         gboolean enable_ok = TRUE;
1516         ModestProtocolRegistry *protocol_registry;
1517         ModestDefaultAccountSettingsDialogPrivate *priv;
1518
1519         priv = MODEST_DEFAULT_ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE (self);
1520         
1521         /* The account details title is mandatory: */
1522         if (entry_is_empty(priv->entry_account_title))
1523                 enable_ok = FALSE;
1524
1525         /* The user details username is mandatory: */
1526         if (enable_ok && entry_is_empty(priv->entry_user_username))
1527                 enable_ok = FALSE;
1528                 
1529         /* The user details email address is mandatory: */
1530         if (enable_ok && entry_is_empty (priv->entry_user_email))
1531                 enable_ok = FALSE;
1532
1533         /* The custom incoming server is mandatory: */
1534         if (enable_ok && entry_is_empty(priv->entry_incomingserver))
1535                 enable_ok = FALSE;
1536
1537         /* The custom outgoing server is mandatory: */
1538         if (enable_ok && entry_is_empty(priv->entry_outgoingserver))
1539                 enable_ok = FALSE;
1540
1541         protocol_registry = modest_runtime_get_protocol_registry ();
1542                         
1543         /* Enable the buttons, 
1544          * identifying them via their associated response codes:
1545          */
1546         GtkDialog *dialog_base = GTK_DIALOG (self);
1547         gtk_dialog_set_response_sensitive (dialog_base,
1548                                            GTK_RESPONSE_OK,
1549                                            enable_ok);
1550 }
1551
1552 static void
1553 modest_default_account_settings_dialog_class_init (ModestDefaultAccountSettingsDialogClass *klass)
1554 {
1555         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1556         g_type_class_add_private (klass, sizeof (ModestDefaultAccountSettingsDialogPrivate));
1557
1558         object_class->dispose = modest_default_account_settings_dialog_dispose;
1559         object_class->finalize = modest_default_account_settings_dialog_finalize;
1560 }
1561
1562 static void 
1563 modest_account_settings_dialog_init (gpointer g_iface, gpointer iface_data)
1564 {
1565         ModestAccountSettingsDialogClass *iface = (ModestAccountSettingsDialogClass *) g_iface;
1566
1567         iface->load_settings = modest_default_account_settings_dialog_load_settings;
1568 }