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