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