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