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