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