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         return TRUE;
616 }
617 /*
618  */
619 static void 
620 on_response (GtkDialog *wizard_dialog,
621         gint response_id,
622         gpointer user_data)
623 {
624         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
625         enable_buttons (self);
626         
627         gboolean prevent_response = FALSE;
628         
629         /* Warn about unsaved changes: */
630         /* TODO: Actually detect whether changes were made. */
631         if (response_id == GTK_RESPONSE_CANCEL) {
632                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
633                 (GtkDialogFlags)0,
634                  GTK_MESSAGE_INFO,
635                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
636                  _("imum_nc_wizard_confirm_lose_changes") ));
637                  
638                  const gint dialog_response = gtk_dialog_run (dialog);
639                  gtk_widget_destroy (GTK_WIDGET (dialog));
640                  
641                 if (dialog_response != GTK_RESPONSE_OK)
642                         prevent_response = TRUE;
643         }
644         /* Check for invalid input: */
645         else if (!check_data (self)) {
646                 prevent_response = TRUE;
647         }
648                 
649         if (prevent_response) {
650                 /* This is a nasty hack. murrayc. */
651                 /* Don't let the dialog close */
652         g_signal_stop_emission_by_name (wizard_dialog, "response");
653                 return; 
654         }
655                 
656                 
657         if (response_id == GTK_RESPONSE_OK) {
658                 /* Try to save the changes: */  
659                 const gboolean saved = save_configuration (self);
660                 if (saved)
661                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
662                 else
663                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
664         }
665 }
666
667 static void
668 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
669 {
670         /* Create the notebook to be used by the GtkDialog base class:
671          * Each page of the notebook will be a page of the wizard: */
672         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
673
674     
675     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
676         
677         /* Get the account manager object, 
678          * so we can check for existing accounts,
679          * and create new accounts: */
680         self->account_manager = modest_runtime_get_account_mgr ();
681         g_assert (self->account_manager);
682         g_object_ref (self->account_manager);
683         
684     /* Create the common pages, 
685      */
686         self->page_account_details = create_page_account_details (self);
687         self->page_user_details = create_page_user_details (self);
688         self->page_incoming = create_page_incoming (self);
689         self->page_outgoing = create_page_outgoing (self);
690         
691         /* Add the notebook pages: */
692         gtk_notebook_append_page (notebook, self->page_account_details, 
693                 gtk_label_new (_("mcen_ti_account_settings_account")));
694         gtk_notebook_append_page (notebook, self->page_user_details, 
695                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
696         gtk_notebook_append_page (notebook, self->page_incoming,
697                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
698         gtk_notebook_append_page (notebook, self->page_outgoing,
699                 gtk_label_new (_("mcen_ti_advsetup_sending")));
700                 
701         GtkDialog *dialog = GTK_DIALOG (self);
702         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
703         gtk_widget_show (GTK_WIDGET (notebook));
704         
705     /* Add the buttons: */
706     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
707     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
708     
709     /* Connect to the dialog's response signal: */
710     /* We use connect-before 
711      * so we can stop the signal emission, 
712      * to stop the default signal handler from closing the dialog.
713      */
714     g_signal_connect (G_OBJECT (self), "response",
715             G_CALLBACK (on_response), self);       
716 }
717
718 ModestAccountSettingsDialog*
719 modest_account_settings_dialog_new (void)
720 {
721         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
722 }
723
724 /** Update the UI with the stored account details, so they can be edited.
725  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
726  */
727 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
728 {
729         if (!account_name)
730                 return;
731                 
732         /* Save the account name so we can refer to it later: */
733         if (dialog->account_name)
734                 g_free (dialog->account_name);
735         dialog->account_name = g_strdup (account_name);
736         
737                 
738         /* Get the account data for this account name: */
739         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
740                 account_name);
741         if (!account_data) {
742                 g_printerr ("modest: failed to get account data for %s\n", account_name);
743                 return;
744         }
745         
746         /* Save the account title so we can refer to it if the user changes it: */
747         if (dialog->original_account_title)
748                 g_free (dialog->original_account_title);
749         dialog->original_account_title = g_strdup (account_data->display_name);
750         
751
752         if (!(account_data->store_account)) {
753                 g_printerr ("modest: account has no stores: %s\n", account_name);
754                 return;
755         }
756                 
757         /* Show the account data in the widgets: */
758         
759         /* Note that we never show the non-display name in the UI.
760          * (Though the display name defaults to the non-display name at the start.) */
761         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
762                 account_data->display_name ? account_data->display_name : "");
763                 
764         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
765                 account_data->fullname ? account_data->fullname : "");
766         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
767                 account_data->email ? account_data->email : "");
768                 
769         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
770                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
771         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
772                 
773                 
774         ModestServerAccountData *incoming_account = account_data->store_account;
775         if (incoming_account) {
776                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
777                         incoming_account->username ? incoming_account->username : "");
778                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
779                         incoming_account->password ? incoming_account->password : "");
780                         
781                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
782                         incoming_account->hostname ? incoming_account->hostname : "");
783                         
784                 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(incoming_account);
785                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
786                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
787                         
788                 update_incoming_server_title (dialog, incoming_account->proto);
789                 update_incoming_server_security_choices (dialog, incoming_account->proto);
790                 
791                 const ModestProtocol security = modest_server_account_data_get_option_security (incoming_account);
792                 easysetup_serversecurity_combo_box_set_active_serversecurity (
793                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
794                 
795                 /* TODO:
796         gchar            *uri;
797         ModestProtocol    proto;
798         gchar            *password;
799         time_t            last_updated;
800         GSList           *options;
801         */
802         
803         }
804         
805         ModestServerAccountData *outgoing_account = account_data->transport_account;
806         if (outgoing_account) {
807                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
808                         outgoing_account->hostname ? outgoing_account->hostname : "");
809                 
810                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
811                         outgoing_account->username ? outgoing_account->username : "");
812                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
813                         outgoing_account->password ? outgoing_account->password : "");
814                 
815                 /* Get the secure-auth setting: */
816                 const ModestProtocol secure_auth = modest_server_account_data_get_option_secure_auth(outgoing_account);
817                 easysetup_secureauth_combo_box_set_active_secureauth (
818                         EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
819                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
820                 
821                 easysetup_serversecurity_combo_box_fill (
822                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
823                 
824                 /* Get the security setting: */
825                 const ModestProtocol security = modest_server_account_data_get_option_security (outgoing_account);
826                 easysetup_serversecurity_combo_box_set_active_serversecurity (
827                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
828                 
829                 /* TODO: set port. */
830         }
831         
832         /* account_data->is_enabled,  */
833         /*account_data->is_default,  */
834
835         /* account_data->store_account->proto */
836
837         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
838 }
839
840 static gboolean
841 save_configuration (ModestAccountSettingsDialog *dialog)
842 {
843         g_assert (dialog->account_name);
844         
845         const gchar* account_name = dialog->account_name;
846                 
847         /* Set the account data from the widgets: */
848         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
849         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
850                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
851         if (!test)
852                 return FALSE;
853                 
854         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
855         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
856                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
857         if (!test)
858                 return FALSE;
859         
860         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
861         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
862                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
863         if (!test)
864                 return FALSE;
865                         
866         /* Incoming: */
867         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
868                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
869         g_assert (incoming_account_name);
870         
871         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
872         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
873                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
874         if (!test)
875                 return FALSE;
876                                 
877         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
878         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
879                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
880         if (!test)
881                 return FALSE;
882                                 
883         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
884         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
885                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
886         if (!test)
887                 return FALSE;
888                         
889         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
890                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
891                         ? MODEST_PROTOCOL_AUTH_PASSWORD
892                         : MODEST_PROTOCOL_AUTH_NONE;
893         modest_server_account_set_option_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
894                         
895         const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
896                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
897         modest_server_account_set_option_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
898         
899         g_free (incoming_account_name);
900         
901         /* Outgoing: */
902         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
903                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
904         g_assert (outgoing_account_name);
905         
906         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
907         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
908                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
909         if (!test)
910                 return FALSE;
911                 
912         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
913         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
914                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
915         if (!test)
916                 return FALSE;
917                 
918         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
919         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
920                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
921         if (!test)
922                 return FALSE;
923         
924         const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
925                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
926         modest_server_account_set_option_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
927         
928         const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
929                 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
930         modest_server_account_set_option_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);        
931                 
932         g_free (outgoing_account_name);
933         
934         
935         /* Set the changed account title last, to simplify the previous code: */
936         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
937         if ((!account_title) || (strlen(account_title) == 0))
938                 return FALSE; /* Should be prevented already anyway. */
939                 
940         if (strcmp(account_title, account_name) != 0) {
941                 /* Change the title: */
942                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
943                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
944                 if (!test)
945                         return FALSE;
946         }
947         
948         return TRUE;
949 }
950
951 static gboolean entry_is_empty (GtkWidget *entry)
952 {
953         if (!entry)
954                 return FALSE;
955                 
956         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
957         if ((!text) || (strlen(text) == 0))
958                 return TRUE;
959         else
960                 return FALSE;
961 }
962
963 static void
964 enable_buttons (ModestAccountSettingsDialog *self)
965 {
966         gboolean enable_ok = TRUE;
967         
968         /* The account details title is mandatory: */
969         if (entry_is_empty(self->entry_account_title))
970                         enable_ok = FALSE;
971
972         /* The user details username is mandatory: */
973         if (entry_is_empty(self->entry_user_username))
974                 enable_ok = FALSE;
975                 
976         /* The user details email address is mandatory: */
977         if (enable_ok && entry_is_empty (self->entry_user_email))
978                 enable_ok = FALSE;
979
980         /* The custom incoming server is mandatory: */
981         if (entry_is_empty(self->entry_incomingserver))
982                 enable_ok = FALSE;
983                         
984         /* Enable the buttons, 
985          * identifying them via their associated response codes:
986          */
987         GtkDialog *dialog_base = GTK_DIALOG (self);
988     gtk_dialog_set_response_sensitive (dialog_base,
989                                        GTK_RESPONSE_OK,
990                                        enable_ok);
991 }
992
993 static void
994 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
995 {
996         GObjectClass *object_class = G_OBJECT_CLASS (klass);
997         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
998
999
1000         object_class->get_property = modest_account_settings_dialog_get_property;
1001         object_class->set_property = modest_account_settings_dialog_set_property;
1002         object_class->dispose = modest_account_settings_dialog_dispose;
1003         object_class->finalize = modest_account_settings_dialog_finalize;
1004 }
1005  
1006 static void
1007 show_error (GtkWindow *parent_window, const gchar* text)
1008 {
1009         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1010                 (GtkDialogFlags)0,
1011                  GTK_MESSAGE_ERROR,
1012                  GTK_BUTTONS_OK,
1013                  text ));
1014                  
1015                  gtk_dialog_run (dialog);
1016                  gtk_widget_destroy (GTK_WIDGET (dialog));
1017 }
1018
1019 static void
1020 show_ok (GtkWindow *parent_window, const gchar* text)
1021 {
1022         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1023                 (GtkDialogFlags)0,
1024                  GTK_MESSAGE_INFO,
1025                  GTK_BUTTONS_OK,
1026                  text ));
1027                  
1028                  gtk_dialog_run (dialog);
1029                  gtk_widget_destroy (GTK_WIDGET (dialog));
1030 }
1031
1032
1033