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