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