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