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