2007-04-17 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-account-settings-dialog.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-account-settings-dialog.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <gtk/gtkstock.h>
18 #include <hildon-widgets/hildon-caption.h>
19 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
20 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
21 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
22 #include "maemo/easysetup/modest-easysetup-serversecurity-combo-box.h"
23 #include "maemo/easysetup/modest-easysetup-secureauth-combo-box.h"
24 #include "maemo/easysetup/modest-validating-entry.h"
25 #include "modest-text-utils.h"
26 #include "modest-account-mgr.h"
27 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
28 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
29 #include "maemo/modest-connection-specific-smtp-window.h"
30 #include <gconf/gconf-client.h>
31 #include <string.h> /* For strlen(). */
32
33 /* Include config.h so that _() works: */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
39
40 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
41
42 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
43         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
44
45 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
46
47 struct _ModestAccountSettingsDialogPrivate
48 {
49 };
50
51 static void
52 enable_buttons (ModestAccountSettingsDialog *self);
53
54 static gboolean
55 save_configuration (ModestAccountSettingsDialog *dialog);
56
57 static void
58 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
59                                                                                                                         GValue *value, GParamSpec *pspec)
60 {
61         switch (property_id) {
62         default:
63                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64         }
65 }
66
67 static void
68 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
69                                                                                                                         const GValue *value, GParamSpec *pspec)
70 {
71         switch (property_id) {
72         default:
73                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
74         }
75 }
76
77 static void
78 modest_account_settings_dialog_dispose (GObject *object)
79 {
80         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
81                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
82 }
83
84 static void
85 modest_account_settings_dialog_finalize (GObject *object)
86 {
87         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
88         
89         if (self->account_name)
90                 g_free (self->account_name);
91                 
92         if (self->original_account_title)
93                 g_free (self->original_account_title);
94                 
95         if (self->account_manager)
96                 g_object_unref (G_OBJECT (self->account_manager));
97         
98         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
99 }
100
101 static void
102 show_error (GtkWindow *parent_window, const gchar* text);
103
104 static void
105 show_ok (GtkWindow *parent_window, const gchar* text);
106
107
108 static void
109 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
110 {
111         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
112         g_assert(self);
113         enable_buttons(self);
114 }
115
116 static void
117 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
118 {
119         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
120         g_assert(self);
121         enable_buttons(self);
122 }
123
124 /** This is a convenience function to create a caption containing a mandatory widget.
125  * When the widget is edited, the enable_buttons() vfunc will be called.
126  */
127 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
128         GtkSizeGroup *group,
129         const gchar *value,
130         GtkWidget *control,
131         GtkWidget *icon,
132         HildonCaptionStatus flag)
133 {
134   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
135   
136 /* The translated strings seem to already contain the *,
137  * but this code can be used if that is not true in future.
138  */
139 #if 0
140         /* Add a * character to indicate mandatory fields,
141          * as specified in our "Email UI Specification": */
142         if (flag == HILDON_CAPTION_MANDATORY) {
143                 gchar* title = g_strdup_printf("%s*", value);
144                 caption = hildon_caption_new (group, title, control, icon, flag);       
145                 g_free(title);
146         }       
147         else
148                 caption = hildon_caption_new (group, value, control, icon, flag);
149 #endif
150
151         /* Connect to the appropriate changed signal for the widget, 
152          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
153          */
154         if (GTK_IS_ENTRY (control)) {
155                 g_signal_connect (G_OBJECT (control), "changed",
156                 G_CALLBACK (on_caption_entry_changed), self);
157                 
158         }
159         else if (GTK_IS_COMBO_BOX (control)) {
160                 g_signal_connect (G_OBJECT (control), "changed",
161                 G_CALLBACK (on_caption_combobox_changed), self);
162         }
163          
164         return caption;
165 }
166
167 static GtkWidget*
168 create_page_account_details (ModestAccountSettingsDialog *self)
169 {
170         GtkWidget *box = gtk_vbox_new (FALSE, 2);
171         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
172         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
173         gtk_widget_show (label);
174         
175         /* Create a size group to be used by all captions.
176          * Note that HildonCaption does not create a default size group if we do not specify one.
177          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
178         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
179            
180         /* The description widgets: */  
181         self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
182         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
183                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
184         gtk_widget_show (self->entry_account_title);
185         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
186         gtk_widget_show (caption);
187         
188         /* Prevent the use of some characters in the account title, 
189          * as required by our UI specification: */
190         GList *list_prevent = NULL;
191         list_prevent = g_list_append (list_prevent, "\\");
192         list_prevent = g_list_append (list_prevent, "/");
193         list_prevent = g_list_append (list_prevent, ":");
194         list_prevent = g_list_append (list_prevent, "*");
195         list_prevent = g_list_append (list_prevent, "?");
196         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
197         list_prevent = g_list_append (list_prevent, "“");
198         list_prevent = g_list_append (list_prevent, "<"); 
199         list_prevent = g_list_append (list_prevent, ">"); 
200         list_prevent = g_list_append (list_prevent, "|");
201         list_prevent = g_list_append (list_prevent, "^");       
202         easysetup_validating_entry_set_unallowed_characters (
203                 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
204         g_list_free (list_prevent);
205         
206         /* Set max length as in the UI spec:
207          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
208         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
209         
210         /* TODO: The Retrieve and Limit Retrieve combo boxes. */
211         
212         /* The leave-messages widgets: */
213         if(!self->checkbox_leave_messages)
214                 self->checkbox_leave_messages = 
215                         gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
216         gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
217         gtk_widget_show (self->checkbox_leave_messages);
218         
219         
220         gtk_widget_show (GTK_WIDGET (box));
221         
222         return GTK_WIDGET (box);
223 }
224
225 static void
226 on_button_signature (GtkButton *button, gpointer user_data)
227 {
228         
229 }
230
231 static GtkWidget*
232 create_page_user_details (ModestAccountSettingsDialog *self)
233 {
234         GtkWidget *box = gtk_vbox_new (FALSE, 2);
235         
236         /* Create a size group to be used by all captions.
237          * Note that HildonCaption does not create a default size group if we do not specify one.
238          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
239         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
240          
241         /* The name widgets: */
242         self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
243         /* Set max length as in the UI spec:
244          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
245         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
246         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
247                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
248         gtk_widget_show (self->entry_user_name);
249         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
250         gtk_widget_show (caption);
251         
252         /* Prevent the use of some characters in the name, 
253          * as required by our UI specification: */
254         GList *list_prevent = NULL;
255         list_prevent = g_list_append (list_prevent, "<");
256         list_prevent = g_list_append (list_prevent, ">");
257         easysetup_validating_entry_set_unallowed_characters (
258                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
259         g_list_free (list_prevent);
260         
261         /* The username widgets: */     
262         self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
263         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
264                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
265         gtk_widget_show (self->entry_user_username);
266         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
267         gtk_widget_show (caption);
268         
269         /* Prevent the use of some characters in the username, 
270          * as required by our UI specification: */
271         easysetup_validating_entry_set_unallowed_characters_whitespace (
272                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
273         
274         /* Set max length as in the UI spec:
275          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
276         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
277         
278         /* The password widgets: */     
279         self->entry_user_password = gtk_entry_new ();
280         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
281         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
282         caption = create_caption_new_with_asterix (self, sizegroup, 
283                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
284         gtk_widget_show (self->entry_user_password);
285         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
286         gtk_widget_show (caption);
287         
288         /* The email address widgets: */        
289         self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
290         caption = create_caption_new_with_asterix (self, sizegroup, 
291                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
292         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
293         gtk_widget_show (self->entry_user_email);
294         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
295         gtk_widget_show (caption);
296         
297         /* Set max length as in the UI spec:
298          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
299         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
300         
301         
302         /* Signature button: */
303         if (!self->button_signature)
304                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
305         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
306                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
307         gtk_widget_show (self->button_signature);
308         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
309         gtk_widget_show (caption);
310                 
311         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
312                 G_CALLBACK (on_button_signature), self);
313                 
314         gtk_widget_show (GTK_WIDGET (box));
315         
316         return GTK_WIDGET (box);
317 }
318
319 /** Change the caption title for the incoming server, 
320  * as specified in the UI spec:
321  */
322 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
323 {
324         const gchar* type = 
325                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
326                         _("mail_fi_emailtype_pop3") : 
327                         _("mail_fi_emailtype_imap") );
328                         
329                 
330         /* Note that this produces a compiler warning, 
331          * because the compiler does not know that the translated string will have a %s in it.
332          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
333         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
334         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
335         g_free(incomingserver_title);
336 }
337
338 /** Change the caption title for the incoming server, 
339  * as specified in the UI spec:
340  */
341 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
342 {
343         /* Fill the combo with appropriately titled choices for POP or IMAP. */
344         /* The choices are the same, but the titles are different, as in the UI spec. */
345         easysetup_serversecurity_combo_box_fill (
346                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
347 }
348            
349 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
350 {
351         GtkWidget *box = gtk_vbox_new (FALSE, 2);
352         
353         /* Create a size group to be used by all captions.
354          * Note that HildonCaption does not create a default size group if we do not specify one.
355          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
356         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
357          
358         /* The incoming server widgets: */
359         if(!self->entry_incomingserver)
360                 self->entry_incomingserver = gtk_entry_new ();
361
362         if (self->caption_incoming)
363           gtk_widget_destroy (self->caption_incoming);
364            
365         /* The caption title will be updated in update_incoming_server_title().
366          * so this default text will never be seen: */
367         /* (Note: Changing the title seems pointless. murrayc) */
368         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
369                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
370         gtk_widget_show (self->entry_incomingserver);
371         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
372         gtk_widget_show (self->caption_incoming);
373         
374         /* The secure connection widgets: */
375         /* This will be filled by update_incoming_server_security_choices(). */
376         if (!self->combo_incoming_security)
377                 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
378         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
379                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
380         gtk_widget_show (self->combo_incoming_security);
381         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
382         gtk_widget_show (caption);
383         
384         /* The port widgets: */
385         /* TODO: There are various rules about this in the UI spec. */
386         if (!self->entry_incoming_port)
387                 self->entry_incoming_port = GTK_WIDGET (gtk_entry_new ());
388         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
389                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
390         gtk_widget_show (self->entry_incoming_port);
391         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
392         gtk_widget_show (caption);
393         
394         /* The secure authentication widgets: */
395         if(!self->checkbox_incoming_auth)
396                 self->checkbox_incoming_auth = 
397                         gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
398         gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
399         gtk_widget_show (self->checkbox_incoming_auth);
400         
401         gtk_widget_show (GTK_WIDGET (box));
402         
403         return GTK_WIDGET (box);
404 }
405
406 static void
407 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
408 {
409         GtkWidget *widget = GTK_WIDGET (user_data);
410         
411         /* Enable the widget only if the toggle button is active: */
412         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
413         gtk_widget_set_sensitive (widget, enable);
414 }
415
416 /* Make the sensitivity of a widget depend on a toggle button.
417  */
418 static void
419 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
420 {
421         g_signal_connect (G_OBJECT (button), "toggled",
422                 G_CALLBACK (on_toggle_button_changed), widget);
423         
424         /* Set the starting sensitivity: */
425         on_toggle_button_changed (button, widget);
426 }
427         
428 static void
429 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
430 {
431         /* Destroy the window when it is closed: */
432         gtk_widget_destroy (GTK_WIDGET (window));
433 }
434
435 static void
436 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
437 {
438
439         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
440         
441         /* Show the window: */
442         /* TODO: Retrieve the chosen settings,
443          * so we can supply them when creating the connection somehow.
444          */
445         GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
446         gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
447         g_signal_connect (G_OBJECT (window), "hide",
448                 G_CALLBACK (on_smtp_servers_window_hide), self);
449     gtk_widget_show (window);
450 }
451
452 static void
453 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
454 {
455         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
456         
457         ModestProtocol protocol_security = 
458                 easysetup_secureauth_combo_box_get_active_secureauth (
459                         EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
460         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
461         
462         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
463         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
464 }
465
466 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
467 {
468         GtkWidget *box = gtk_vbox_new (FALSE, 2);
469         
470         /* Create a size group to be used by all captions.
471          * Note that HildonCaption does not create a default size group if we do not specify one.
472          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
473         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
474          
475         /* The outgoing server widgets: */
476         if (!self->entry_outgoingserver)
477                 self->entry_outgoingserver = gtk_entry_new ();
478         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
479                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
480         gtk_widget_show (self->entry_outgoingserver);
481         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
482         gtk_widget_show (caption);
483         
484         /* The secure authentication widgets: */
485         if (!self->combo_outgoing_auth)
486                 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
487         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
488                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
489         gtk_widget_show (self->combo_outgoing_auth);
490         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
491         gtk_widget_show (caption);
492         
493         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
494         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
495         
496         /* The username widgets: */     
497         self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
498         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
499                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
500         gtk_widget_show (self->entry_outgoing_username);
501         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
502         gtk_widget_show (self->caption_outgoing_username);
503         
504         /* Prevent the use of some characters in the username, 
505          * as required by our UI specification: */
506         easysetup_validating_entry_set_unallowed_characters_whitespace (
507                 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
508         
509         /* Set max length as in the UI spec:
510          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
511         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
512         
513         /* The password widgets: */     
514         self->entry_outgoing_password = gtk_entry_new ();
515         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
516         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
517         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
518                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
519         gtk_widget_show (self->entry_outgoing_password);
520         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
521         gtk_widget_show (self->caption_outgoing_password);
522         
523         /* The secure connection widgets: */
524         /* This will be filled and set with easysetup_serversecurity_combo_box_fill() 
525          * and easysetup_serversecurity_combo_box_set_active_serversecurity().
526          */
527         if (!self->combo_outgoing_security)
528                 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
529         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
530                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
531         gtk_widget_show (self->combo_outgoing_security);
532         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
533         gtk_widget_show (caption);      
534         
535         /* The port widgets: */
536         if (!self->entry_outgoing_port)
537                 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
538         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
539                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
540         gtk_widget_show (self->entry_outgoing_port);
541         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
542         gtk_widget_show (caption);
543         
544         /* connection-specific checkbox: */
545         if (!self->checkbox_outgoing_smtp_specific) {
546                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
547                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
548                         FALSE);
549         }
550         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
551         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
552         
553         /* Connection-specific SMTP-Severs Edit button: */
554         if (!self->button_outgoing_smtp_servers)
555                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
556         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
557                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
558         gtk_widget_show (self->button_outgoing_smtp_servers);
559         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
560         gtk_widget_show (caption);
561         
562         /* Only enable the button when the checkbox is checked: */
563         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
564                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
565                 
566         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
567                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
568         
569         
570         gtk_widget_show (GTK_WIDGET (box));
571         
572         return GTK_WIDGET (box);
573 }
574
575 static gboolean
576 check_data (ModestAccountSettingsDialog *self)
577 {
578         /* Check that the title is not already in use: */
579         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
580         if ((!account_title) || (strlen(account_title) == 0))
581                 return FALSE; /* Should be prevented already anyway. */
582                 
583         if (strcmp(account_title, self->original_account_title) != 0) {
584                 /* Check the changed title: */
585                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
586                         account_title);
587         
588                 if (name_in_use) {
589                         /* Warn the user via a dialog: */
590                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
591                 
592                         return FALSE;
593                 }
594         }
595
596         /* Check that the email address is valud: */
597         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
598         if ((!email_address) || (strlen(email_address) == 0))
599                 return FALSE;
600                         
601         if (!modest_text_utils_validate_email_address (email_address)) {
602                 /* Warn the user via a dialog: */
603                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
604                                          
605         /* Return focus to the email address entry: */
606         gtk_widget_grab_focus (self->entry_user_email);
607         
608                 return FALSE;
609         }
610         
611         /* TODO: The UI Spec wants us to check that the servernames are valid, 
612          * but does not specify how.
613          */
614 }
615 /*
616  */
617 static void 
618 on_response (GtkDialog *wizard_dialog,
619         gint response_id,
620         gpointer user_data)
621 {
622         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
623         enable_buttons (self);
624         
625         gboolean prevent_response = FALSE;
626         
627         /* Warn about unsaved changes: */
628         /* TODO: Actually detect whether changes were made. */
629         if (response_id == GTK_RESPONSE_CANCEL) {
630                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
631                 (GtkDialogFlags)0,
632                  GTK_MESSAGE_INFO,
633                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
634                  _("imum_nc_wizard_confirm_lose_changes") ));
635                  
636                  const gint dialog_response = gtk_dialog_run (dialog);
637                  gtk_widget_destroy (GTK_WIDGET (dialog));
638                  
639                 if (dialog_response != GTK_RESPONSE_OK)
640                         prevent_response = TRUE;
641         }
642         /* Check for invalid input: */
643         else if (!check_data (self)) {
644                 prevent_response = TRUE;
645         }
646                 
647         if (prevent_response) {
648                 /* This is a nasty hack. murrayc. */
649                 /* Don't let the dialog close */
650         g_signal_stop_emission_by_name (wizard_dialog, "response");
651                 return; 
652         }
653                 
654                 
655         if (response_id == GTK_RESPONSE_OK) {
656                 /* Try to save the changes: */  
657                 const gboolean saved = save_configuration (self);
658                 if (saved)
659                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
660                 else
661                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
662         }
663 }
664
665 static void
666 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
667 {
668         /* Create the notebook to be used by the GtkDialog base class:
669          * Each page of the notebook will be a page of the wizard: */
670         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
671
672     
673     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
674         
675         /* Get the account manager object, 
676          * so we can check for existing accounts,
677          * and create new accounts: */
678         self->account_manager = modest_runtime_get_account_mgr ();
679         g_assert (self->account_manager);
680         g_object_ref (self->account_manager);
681         
682     /* Create the common pages, 
683      */
684         self->page_account_details = create_page_account_details (self);
685         self->page_user_details = create_page_user_details (self);
686         self->page_incoming = create_page_incoming (self);
687         self->page_outgoing = create_page_outgoing (self);
688         
689         /* Add the notebook pages: */
690         gtk_notebook_append_page (notebook, self->page_account_details, 
691                 gtk_label_new (_("mcen_ti_account_settings_account")));
692         gtk_notebook_append_page (notebook, self->page_user_details, 
693                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
694         gtk_notebook_append_page (notebook, self->page_incoming,
695                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
696         gtk_notebook_append_page (notebook, self->page_outgoing,
697                 gtk_label_new (_("mcen_ti_advsetup_sending")));
698                 
699         GtkDialog *dialog = GTK_DIALOG (self);
700         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
701         gtk_widget_show (GTK_WIDGET (notebook));
702         
703     /* Add the buttons: */
704     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
705     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
706     
707     /* Connect to the dialog's response signal: */
708     /* We use connect-before 
709      * so we can stop the signal emission, 
710      * to stop the default signal handler from closing the dialog.
711      */
712     g_signal_connect (G_OBJECT (self), "response",
713             G_CALLBACK (on_response), self);       
714 }
715
716 ModestAccountSettingsDialog*
717 modest_account_settings_dialog_new (void)
718 {
719         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
720 }
721
722 /** Update the UI with the stored account details, so they can be edited.
723  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
724  */
725 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
726 {
727         if (!account_name)
728                 return;
729                 
730         /* Save the account name so we can refer to it later: */
731         if (dialog->account_name)
732                 g_free (dialog->account_name);
733         dialog->account_name = g_strdup (account_name);
734         
735                 
736         /* Get the account data for this account name: */
737         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
738                 account_name);
739         if (!account_data) {
740                 g_printerr ("modest: failed to get account data for %s\n", account_name);
741                 return;
742         }
743         
744         /* Save the account title so we can refer to it if the user changes it: */
745         if (dialog->original_account_title)
746                 g_free (dialog->original_account_title);
747         dialog->original_account_title = g_strdup (account_data->display_name);
748         
749
750         if (!(account_data->store_account)) {
751                 g_printerr ("modest: account has no stores: %s\n", account_name);
752                 return;
753         }
754                 
755         /* Show the account data in the widgets: */
756         
757         /* Note that we never show the non-display name in the UI.
758          * (Though the display name defaults to the non-display name at the start.) */
759         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
760                 account_data->display_name ? account_data->display_name : "");
761                 
762         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
763                 account_data->fullname ? account_data->fullname : "");
764         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
765                 account_data->email ? account_data->email : "");
766                 
767         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
768                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
769         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
770                 
771                 
772         ModestServerAccountData *incoming_account = account_data->store_account;
773         if (incoming_account) {
774                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
775                         incoming_account->username ? incoming_account->username : "");
776                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
777                         incoming_account->password ? incoming_account->password : "");
778                         
779                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
780                         incoming_account->hostname ? incoming_account->hostname : "");
781                         
782                 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(incoming_account);
783                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
784                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
785                         
786                 update_incoming_server_title (dialog, incoming_account->proto);
787                 update_incoming_server_security_choices (dialog, incoming_account->proto);
788                 
789                 const ModestProtocol security = modest_server_account_data_get_option_security (incoming_account);
790                 easysetup_serversecurity_combo_box_set_active_serversecurity (
791                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
792                 
793                 /* TODO:
794         gchar            *uri;
795         ModestProtocol    proto;
796         gchar            *password;
797         time_t            last_updated;
798         GSList           *options;
799         */
800         
801         }
802         
803         ModestServerAccountData *outgoing_account = account_data->transport_account;
804         if (outgoing_account) {
805                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
806                         outgoing_account->hostname ? outgoing_account->hostname : "");
807                 
808                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
809                         outgoing_account->username ? outgoing_account->username : "");
810                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
811                         outgoing_account->password ? outgoing_account->password : "");
812                 
813                 /* Get the secure-auth setting: */
814                 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(outgoing_account);
815                 easysetup_secureauth_combo_box_set_active_secureauth (
816                         EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
817                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
818                 
819                 easysetup_serversecurity_combo_box_fill (
820                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
821                 
822                 /* Get the security setting: */
823                 const ModestProtocol security = modest_server_account_data_get_option_security (outgoing_account);
824                 easysetup_serversecurity_combo_box_set_active_serversecurity (
825                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
826                 
827                 /* TODO: set port. */
828         }
829         
830         /* account_data->is_enabled,  */
831         /*account_data->is_default,  */
832
833         /* account_data->store_account->proto */
834
835         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
836 }
837
838 static gboolean
839 save_configuration (ModestAccountSettingsDialog *dialog)
840 {
841         g_assert (dialog->account_name);
842         
843         const gchar* account_name = dialog->account_name;
844                 
845         /* Set the account data from the widgets: */
846         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
847         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
848                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
849         if (!test)
850                 return FALSE;
851                 
852         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
853         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
854                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
855         if (!test)
856                 return FALSE;
857         
858         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
859         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
860                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
861         if (!test)
862                 return FALSE;
863                         
864         /* Incoming: */
865         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
866                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
867         g_assert (incoming_account_name);
868         
869         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
870         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
871                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
872         if (!test)
873                 return FALSE;
874                                 
875         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
876         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
877                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
878         if (!test)
879                 return FALSE;
880                                 
881         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
882         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
883                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
884         if (!test)
885                 return FALSE;
886                         
887         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
888                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
889                         ? MODEST_PROTOCOL_AUTH_PASSWORD
890                         : MODEST_PROTOCOL_AUTH_NONE;
891         modest_server_account_set_option_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
892                         
893         const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
894                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
895         modest_server_account_set_option_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
896         
897         g_free (incoming_account_name);
898         
899         /* Outgoing: */
900         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
901                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
902         g_assert (outgoing_account_name);
903         
904         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
905         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
906                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
907         if (!test)
908                 return FALSE;
909                 
910         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
911         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
912                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
913         if (!test)
914                 return FALSE;
915                 
916         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
917         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
918                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
919         if (!test)
920                 return FALSE;
921         
922         const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
923                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
924         modest_server_account_set_option_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
925         
926         const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
927                 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
928         modest_server_account_set_option_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);        
929                 
930         g_free (outgoing_account_name);
931         
932         
933         /* Set the changed account title last, to simplify the previous code: */
934         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
935         if ((!account_title) || (strlen(account_title) == 0))
936                 return FALSE; /* Should be prevented already anyway. */
937                 
938         if (strcmp(account_title, account_name) != 0) {
939                 /* Change the title: */
940                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
941                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
942                 if (!test)
943                         return FALSE;
944         }
945         
946         return TRUE;
947 }
948
949 static gboolean entry_is_empty (GtkWidget *entry)
950 {
951         if (!entry)
952                 return FALSE;
953                 
954         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
955         if ((!text) || (strlen(text) == 0))
956                 return TRUE;
957         else
958                 return FALSE;
959 }
960
961 static void
962 enable_buttons (ModestAccountSettingsDialog *self)
963 {
964         gboolean enable_ok = TRUE;
965         
966         /* The account details title is mandatory: */
967         if (entry_is_empty(self->entry_account_title))
968                         enable_ok = FALSE;
969
970         /* The user details username is mandatory: */
971         if (entry_is_empty(self->entry_user_username))
972                 enable_ok = FALSE;
973                 
974         /* The user details email address is mandatory: */
975         if (enable_ok && entry_is_empty (self->entry_user_email))
976                 enable_ok = FALSE;
977
978         /* The custom incoming server is mandatory: */
979         if (entry_is_empty(self->entry_incomingserver))
980                 enable_ok = FALSE;
981                         
982         /* Enable the buttons, 
983          * identifying them via their associated response codes:
984          */
985         GtkDialog *dialog_base = GTK_DIALOG (self);
986     gtk_dialog_set_response_sensitive (dialog_base,
987                                        GTK_RESPONSE_OK,
988                                        enable_ok);
989 }
990
991 static void
992 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
993 {
994         GObjectClass *object_class = G_OBJECT_CLASS (klass);
995         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
996
997
998         object_class->get_property = modest_account_settings_dialog_get_property;
999         object_class->set_property = modest_account_settings_dialog_set_property;
1000         object_class->dispose = modest_account_settings_dialog_dispose;
1001         object_class->finalize = modest_account_settings_dialog_finalize;
1002 }
1003  
1004 static void
1005 show_error (GtkWindow *parent_window, const gchar* text)
1006 {
1007         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1008                 (GtkDialogFlags)0,
1009                  GTK_MESSAGE_ERROR,
1010                  GTK_BUTTONS_OK,
1011                  text ));
1012                  
1013                  gtk_dialog_run (dialog);
1014                  gtk_widget_destroy (GTK_WIDGET (dialog));
1015 }
1016
1017 static void
1018 show_ok (GtkWindow *parent_window, const gchar* text)
1019 {
1020         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1021                 (GtkDialogFlags)0,
1022                  GTK_MESSAGE_INFO,
1023                  GTK_BUTTONS_OK,
1024                  text ));
1025                  
1026                  gtk_dialog_run (dialog);
1027                  gtk_widget_destroy (GTK_WIDGET (dialog));
1028 }
1029
1030
1031