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