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