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