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