Attempt to commit. See ChangeLog2
[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->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 static void
99 show_error (GtkWindow *parent_window, const gchar* text);
100
101 static void
102 show_ok (GtkWindow *parent_window, const gchar* text);
103
104
105 static void
106 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
107 {
108         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
109         g_assert(self);
110         enable_buttons(self);
111 }
112
113 static void
114 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
115 {
116         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
117         g_assert(self);
118         enable_buttons(self);
119 }
120
121 /** This is a convenience function to create a caption containing a mandatory widget.
122  * When the widget is edited, the enable_buttons() vfunc will be called.
123  */
124 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
125         GtkSizeGroup *group,
126         const gchar *value,
127         GtkWidget *control,
128         GtkWidget *icon,
129         HildonCaptionStatus flag)
130 {
131   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
132   
133 /* The translated strings seem to already contain the *,
134  * but this code can be used if that is not true in future.
135  */
136 #if 0
137         /* Add a * character to indicate mandatory fields,
138          * as specified in our "Email UI Specification": */
139         if (flag == HILDON_CAPTION_MANDATORY) {
140                 gchar* title = g_strdup_printf("%s*", value);
141                 caption = hildon_caption_new (group, title, control, icon, flag);       
142                 g_free(title);
143         }       
144         else
145                 caption = hildon_caption_new (group, value, control, icon, flag);
146 #endif
147
148         /* Connect to the appropriate changed signal for the widget, 
149          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
150          */
151         if (GTK_IS_ENTRY (control)) {
152                 g_signal_connect (G_OBJECT (control), "changed",
153                 G_CALLBACK (on_caption_entry_changed), self);
154                 
155         }
156         else if (GTK_IS_COMBO_BOX (control)) {
157                 g_signal_connect (G_OBJECT (control), "changed",
158                 G_CALLBACK (on_caption_combobox_changed), self);
159         }
160          
161         return caption;
162 }
163
164 static GtkWidget*
165 create_page_account_details (ModestAccountSettingsDialog *self)
166 {
167         GtkWidget *box = gtk_vbox_new (FALSE, 2);
168         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
169         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
170         gtk_widget_show (label);
171         
172         /* Create a size group to be used by all captions.
173          * Note that HildonCaption does not create a default size group if we do not specify one.
174          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
175         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
176            
177         /* The description widgets: */  
178         self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
179         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
180                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
181         gtk_widget_show (self->entry_account_title);
182         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
183         gtk_widget_show (caption);
184         
185         /* Prevent the use of some characters in the account title, 
186          * as required by our UI specification: */
187         GList *list_prevent = NULL;
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, "?");
193         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
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         list_prevent = g_list_append (list_prevent, "^");       
199         easysetup_validating_entry_set_unallowed_characters (
200                 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
201         g_list_free (list_prevent);
202         
203         /* Set max length as in the UI spec:
204          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
205         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
206         
207         /* TODO: The Retrieve and Limit Retrieve combo boxes. */
208         
209         /* The leave-messages widgets: */
210         if(!self->checkbox_leave_messages)
211                 self->checkbox_leave_messages = 
212                         gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
213         gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
214         gtk_widget_show (self->checkbox_leave_messages);
215         
216         
217         gtk_widget_show (GTK_WIDGET (box));
218         
219         return GTK_WIDGET (box);
220 }
221
222 static void
223 on_button_signature (GtkButton *button, gpointer user_data)
224 {
225         
226 }
227
228 static GtkWidget*
229 create_page_user_details (ModestAccountSettingsDialog *self)
230 {
231         GtkWidget *box = gtk_vbox_new (FALSE, 2);
232         
233         /* Create a size group to be used by all captions.
234          * Note that HildonCaption does not create a default size group if we do not specify one.
235          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
236         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
237          
238         /* The name widgets: */
239         self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
240         /* Set max length as in the UI spec:
241          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
242         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
243         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
244                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
245         gtk_widget_show (self->entry_user_name);
246         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
247         gtk_widget_show (caption);
248         
249         /* Prevent the use of some characters in the name, 
250          * as required by our UI specification: */
251         GList *list_prevent = NULL;
252         list_prevent = g_list_append (list_prevent, "<");
253         list_prevent = g_list_append (list_prevent, ">");
254         easysetup_validating_entry_set_unallowed_characters (
255                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
256         g_list_free (list_prevent);
257         
258         /* The username widgets: */     
259         self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
260         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
261                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
262         gtk_widget_show (self->entry_user_username);
263         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
264         gtk_widget_show (caption);
265         
266         /* Prevent the use of some characters in the username, 
267          * as required by our UI specification: */
268         easysetup_validating_entry_set_unallowed_characters_whitespace (
269                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
270         
271         /* Set max length as in the UI spec:
272          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
273         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
274         
275         /* The password widgets: */     
276         self->entry_user_password = gtk_entry_new ();
277         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
278         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
279         caption = create_caption_new_with_asterix (self, sizegroup, 
280                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
281         gtk_widget_show (self->entry_user_password);
282         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
283         gtk_widget_show (caption);
284         
285         /* The email address widgets: */        
286         self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
287         caption = create_caption_new_with_asterix (self, sizegroup, 
288                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
289         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
290         gtk_widget_show (self->entry_user_email);
291         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
292         gtk_widget_show (caption);
293         
294         /* Set max length as in the UI spec:
295          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
296         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
297         
298         
299         /* Signature button: */
300         if (!self->button_signature)
301                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
302         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
303                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
304         gtk_widget_show (self->button_signature);
305         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
306         gtk_widget_show (caption);
307                 
308         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
309                 G_CALLBACK (on_button_signature), self);
310                 
311         gtk_widget_show (GTK_WIDGET (box));
312         
313         return GTK_WIDGET (box);
314 }
315
316 /** Change the caption title for the incoming server, 
317  * as specified in the UI spec:
318  */
319 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
320 {
321         const gchar* type = 
322                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
323                         _("mail_fi_emailtype_pop3") : 
324                         _("mail_fi_emailtype_imap") );
325                         
326                 
327         /* Note that this produces a compiler warning, 
328          * because the compiler does not know that the translated string will have a %s in it.
329          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
330         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
331         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
332         g_free(incomingserver_title);
333 }
334
335 /** Change the caption title for the incoming server, 
336  * as specified in the UI spec:
337  */
338 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
339 {
340         /* Fill the combo with appropriately titled choices for POP or IMAP. */
341         /* The choices are the same, but the titles are different, as in the UI spec. */
342         easysetup_serversecurity_combo_box_fill (
343                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
344 }
345            
346 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
347 {
348         GtkWidget *box = gtk_vbox_new (FALSE, 2);
349         
350         /* Create a size group to be used by all captions.
351          * Note that HildonCaption does not create a default size group if we do not specify one.
352          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
353         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
354          
355         /* The incoming server widgets: */
356         if(!self->entry_incomingserver)
357                 self->entry_incomingserver = gtk_entry_new ();
358
359         if (self->caption_incoming)
360           gtk_widget_destroy (self->caption_incoming);
361            
362         /* The caption title will be updated in update_incoming_server_title().
363          * so this default text will never be seen: */
364         /* (Note: Changing the title seems pointless. murrayc) */
365         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
366                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
367         gtk_widget_show (self->entry_incomingserver);
368         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
369         gtk_widget_show (self->caption_incoming);
370         
371         /* The secure connection widgets: */
372         /* This will be filled by update_incoming_server_security_choices(). */
373         if (!self->combo_incoming_security)
374                 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
375         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
376                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
377         gtk_widget_show (self->combo_incoming_security);
378         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
379         gtk_widget_show (caption);
380         
381         /* The port widgets: */
382         /* TODO: There are various rules about this in the UI spec. */
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 static gboolean
573 check_data (ModestAccountSettingsDialog *self)
574 {
575         /* Check that the title is not already in use: */
576         const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
577         if ((!account_name) || (strlen(account_name) == 0))
578                 return FALSE; /* Should be prevented already anyway. */
579                 
580         if (strcmp(account_name, self->original_account_name) != 0) {
581                 /* Check the changed title: */
582                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
583                         account_name);
584         
585                 if (name_in_use) {
586                         /* Warn the user via a dialog: */
587                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
588                 
589                         return FALSE;
590                 }
591         }
592
593         /* Check that the email address is valud: */
594         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
595         if ((!email_address) || (strlen(email_address) == 0))
596                 return FALSE;
597                         
598         if (!modest_text_utils_validate_email_address (email_address)) {
599                 /* Warn the user via a dialog: */
600                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
601                                          
602         /* Return focus to the email address entry: */
603         gtk_widget_grab_focus (self->entry_user_email);
604         
605                 return FALSE;
606         }
607         
608         /* TODO: The UI Spec wants us to check that the servernames are valid, 
609          * but does not specify how.
610          */
611 }
612 /*
613  */
614 static void 
615 on_response (GtkDialog *wizard_dialog,
616         gint response_id,
617         gpointer user_data)
618 {
619         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
620         enable_buttons (self);
621         
622         /* TODO: Prevent the OK response if the data is invalid. */
623         
624         gboolean prevent_response = FALSE;
625         
626         /* Warn about unsaved changes: */
627         /* TODO: Actually detect whether changes were made. */
628         if (response_id == GTK_RESPONSE_CANCEL) {
629                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
630                 (GtkDialogFlags)0,
631                  GTK_MESSAGE_INFO,
632                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
633                  _("imum_nc_wizard_confirm_lose_changes") ));
634                  
635                  const gint dialog_response = gtk_dialog_run (dialog);
636                  gtk_widget_destroy (GTK_WIDGET (dialog));
637                  
638                 if (dialog_response != GTK_RESPONSE_OK)
639                         prevent_response = TRUE;
640         }
641         /* Check for invalid input: */
642         else if (!check_data (self)) {
643                 prevent_response = TRUE;
644         }
645                 
646         if (prevent_response) {
647                 /* This is a nasty hack. murrayc. */
648                 /* Don't let the dialog close */
649         g_signal_stop_emission_by_name (wizard_dialog, "response");
650                 return; 
651         }
652                 
653                 
654         if (response_id == GTK_RESPONSE_OK) {
655                 /* Try to save the changes: */  
656                 const gboolean saved = save_configuration (self);
657                 if (saved)
658                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
659                 else
660                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
661         }
662 }
663
664 static void
665 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
666 {
667         /* Create the notebook to be used by the GtkDialog base class:
668          * Each page of the notebook will be a page of the wizard: */
669         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
670
671     
672     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
673         
674         /* Get the account manager object, 
675          * so we can check for existing accounts,
676          * and create new accounts: */
677         self->account_manager = modest_runtime_get_account_mgr ();
678         g_assert (self->account_manager);
679         g_object_ref (self->account_manager);
680         
681     /* Create the common pages, 
682      */
683         self->page_account_details = create_page_account_details (self);
684         self->page_user_details = create_page_user_details (self);
685         self->page_incoming = create_page_incoming (self);
686         self->page_outgoing = create_page_outgoing (self);
687         
688         /* Add the notebook pages: */
689         gtk_notebook_append_page (notebook, self->page_account_details, 
690                 gtk_label_new (_("mcen_ti_account_settings_account")));
691         gtk_notebook_append_page (notebook, self->page_user_details, 
692                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
693         gtk_notebook_append_page (notebook, self->page_incoming,
694                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
695         gtk_notebook_append_page (notebook, self->page_outgoing,
696                 gtk_label_new (_("mcen_ti_advsetup_sending")));
697                 
698         GtkDialog *dialog = GTK_DIALOG (self);
699         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
700         gtk_widget_show (GTK_WIDGET (notebook));
701         
702     /* Add the buttons: */
703     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
704     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
705     
706     /* Connect to the dialog's response signal: */
707     /* We use connect-before 
708      * so we can stop the signal emission, 
709      * to stop the default signal handler from closing the dialog.
710      */
711     g_signal_connect (G_OBJECT (self), "response",
712             G_CALLBACK (on_response), self);       
713 }
714
715 ModestAccountSettingsDialog*
716 modest_account_settings_dialog_new (void)
717 {
718         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
719 }
720
721 /** Update the UI with the stored account details, so they can be edited.
722  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
723  */
724 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
725 {
726         if (!account_name)
727                 return;
728                 
729         /* Save the account name so we can refer to it if the user changes it: */
730         if (dialog->original_account_name)
731                 g_free (dialog->original_account_name);
732         dialog->original_account_name = g_strdup (account_name);
733         
734         /* Get the account data for this account name: */
735         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
736                 account_name);
737         if (!account_data) {
738                 g_printerr ("modest: failed to get account data for %s\n", account_name);
739                 return;
740         }
741
742         if (!(account_data->store_account)) {
743                 g_printerr ("modest: account has no stores: %s\n", account_name);
744                 return;
745         }
746                 
747         /* Show the account data in the widgets: */
748         
749         /* Note that we never show the non-display name in the UI.
750          * (Though the display name defaults to the non-display name at the start.) */
751         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
752                 account_data->display_name ? account_data->display_name : "");
753                 
754         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
755                 account_data->fullname ? account_data->fullname : "");
756         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
757                 account_data->email ? account_data->email : "");
758                 
759         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
760                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
761         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
762                 
763                 
764         ModestServerAccountData *incoming_account = account_data->store_account;
765         if (incoming_account) {
766                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
767                         incoming_account->username ? incoming_account->username : "");
768                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
769                         incoming_account->password ? incoming_account->password : "");
770                         
771                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
772                         incoming_account->hostname ? incoming_account->hostname : "");
773                         
774                 easysetup_serversecurity_combo_box_set_active_serversecurity (
775                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), incoming_account->proto);
776                 update_incoming_server_title (dialog, incoming_account->proto);
777                 update_incoming_server_security_choices (dialog, incoming_account->proto);
778                 
779                 /* TODO:
780         gchar            *uri;
781         ModestProtocol    proto;
782         gchar            *password;
783         time_t            last_updated;
784         GSList           *options;
785         */
786         
787         }
788         
789         ModestServerAccountData *outgoing_account = account_data->transport_account;
790         if (outgoing_account) {
791                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
792                         outgoing_account->hostname ? outgoing_account->hostname : "");
793                 
794                 /* TODO: Dim these if secure authentication is None, as per the UI spec: */
795                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
796                         outgoing_account->username ? outgoing_account->username : "");
797                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
798                         outgoing_account->password ? outgoing_account->password : "");
799                 
800                 /* TODO: How do we get the auth setting from the server account struct?: */
801                 /* This seems to be in ->options, with hard-coded option names.
802                  * This will need new API in ModestAccountMgr. */
803                 easysetup_secureauth_combo_box_set_active_secureauth (
804                         EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), MODEST_PROTOCOL_AUTH_NONE);
805                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
806                 
807                 easysetup_serversecurity_combo_box_fill (
808                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
809                 
810                 
811                 
812                 /* TODO: set port. */
813         }
814         
815         /* TODO: account_data->display_name */
816         /* account_data->is_enabled,  */
817         /*account_data->is_default,  */
818
819         /* account_data->store_account->proto */
820
821         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
822 }
823
824 static gboolean
825 save_configuration (ModestAccountSettingsDialog *dialog)
826 {
827         g_assert (dialog->original_account_name);
828         
829         const gchar* account_name = dialog->original_account_name;
830                 
831         /* Set the account data from the widgets: */
832         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
833         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
834                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
835         if (!test)
836                 return FALSE;
837                 
838         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
839         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
840                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
841         if (!test)
842                 return FALSE;
843                                 
844         /* TODO: Change name: */
845         /* Possibly the account name may never change, but that should be hidden, 
846          * and the display name may change, defaulting to the account name.
847          */
848         
849         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
850         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
851                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
852         if (!test)
853                 return FALSE;
854                         
855         /* Incoming: */
856         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
857                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
858         g_assert (incoming_account_name);
859         
860         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
861         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
862                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
863         if (!test)
864                 return FALSE;
865                                 
866         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
867         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
868                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
869         if (!test)
870                 return FALSE;
871                                 
872         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
873         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
874                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
875         if (!test)
876                 return FALSE;
877                         
878         /* TODO: How can we set these in the server account?:   
879         ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
880                         ? MODEST_PROTOCOL_AUTH_PASSWORD
881                         : MODEST_PROTOCOL_AUTH_NONE;
882                         
883         ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
884                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
885         
886         */
887         
888         g_free (incoming_account_name);
889         
890         /* Outgoing: */
891         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
892                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
893         g_assert (outgoing_account_name);
894         
895         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
896         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
897                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
898         if (!test)
899                 return FALSE;
900                 
901         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
902         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
903                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
904         if (!test)
905                 return FALSE;
906                 
907         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
908         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
909                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
910         if (!test)
911                 return FALSE;
912         
913         /* TODO: How do we set these in the account data?:
914         ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
915                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
916         
917         ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
918                 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
919          */
920                 
921         g_free (outgoing_account_name);
922         
923         
924         /* Set the changed account title last, to simplify the previous code: */
925         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
926         if ((!account_title) || (strlen(account_title) == 0))
927                 return FALSE; /* Should be prevented already anyway. */
928                 
929         if (strcmp(account_title, account_name) != 0) {
930                 /* Change the title: */
931                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
932                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
933                 if (!test)
934                         return FALSE;
935         }
936         
937         return TRUE;
938 }
939
940 static gboolean entry_is_empty (GtkWidget *entry)
941 {
942         if (!entry)
943                 return FALSE;
944                 
945         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
946         if ((!text) || (strlen(text) == 0))
947                 return TRUE;
948         else
949                 return FALSE;
950 }
951
952 static void
953 enable_buttons (ModestAccountSettingsDialog *self)
954 {
955         gboolean enable_ok = TRUE;
956         
957         /* The account details title is mandatory: */
958         if (entry_is_empty(self->entry_account_title))
959                         enable_ok = FALSE;
960
961         /* The user details username is mandatory: */
962         if (entry_is_empty(self->entry_user_username))
963                 enable_ok = FALSE;
964                 
965         /* The user details email address is mandatory: */
966         if (enable_ok && entry_is_empty (self->entry_user_email))
967                 enable_ok = FALSE;
968
969         /* The custom incoming server is mandatory: */
970         if (entry_is_empty(self->entry_incomingserver))
971                 enable_ok = FALSE;
972                         
973         /* Enable the buttons, 
974          * identifying them via their associated response codes:
975          */
976         GtkDialog *dialog_base = GTK_DIALOG (self);
977     gtk_dialog_set_response_sensitive (dialog_base,
978                                        GTK_RESPONSE_OK,
979                                        enable_ok);
980 }
981
982 static void
983 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
984 {
985         GObjectClass *object_class = G_OBJECT_CLASS (klass);
986         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
987
988
989         object_class->get_property = modest_account_settings_dialog_get_property;
990         object_class->set_property = modest_account_settings_dialog_set_property;
991         object_class->dispose = modest_account_settings_dialog_dispose;
992         object_class->finalize = modest_account_settings_dialog_finalize;
993 }
994  
995 static void
996 show_error (GtkWindow *parent_window, const gchar* text)
997 {
998         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
999                 (GtkDialogFlags)0,
1000                  GTK_MESSAGE_ERROR,
1001                  GTK_BUTTONS_OK,
1002                  text ));
1003                  
1004                  gtk_dialog_run (dialog);
1005                  gtk_widget_destroy (GTK_WIDGET (dialog));
1006 }
1007
1008 static void
1009 show_ok (GtkWindow *parent_window, const gchar* text)
1010 {
1011         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1012                 (GtkDialogFlags)0,
1013                  GTK_MESSAGE_INFO,
1014                  GTK_BUTTONS_OK,
1015                  text ));
1016                  
1017                  gtk_dialog_run (dialog);
1018                  gtk_widget_destroy (GTK_WIDGET (dialog));
1019 }
1020
1021
1022