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