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         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
514         
515         /* Show the window: */
516         /* TODO: Retrieve the chosen settings,
517          * so we can supply them when creating the connection somehow.
518          */
519         GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
520         gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (self));
521         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
522     gtk_widget_show (window);
523     
524     g_signal_connect (G_OBJECT (window), "hide",
525         G_CALLBACK (on_smtp_servers_window_hide), self);
526 }
527
528 static void
529 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
530 {
531         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
532         
533         ModestProtocol protocol_security = 
534                 easysetup_secureauth_combo_box_get_active_secureauth (
535                         EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
536         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
537         
538         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
539         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
540 }
541
542 static void
543 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
544 {
545         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
546         
547         const gint port_number = 
548                 easysetup_serversecurity_combo_box_get_active_serversecurity_port (
549                         EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
550
551         if(port_number != 0) {
552                 gchar* str = g_strdup_printf ("%d", port_number);
553                 gtk_entry_set_text (GTK_ENTRY (self->entry_outgoing_port), str);
554                 g_free (str);   
555         }               
556 }
557
558 static void
559 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
560 {
561         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
562         
563         const gint port_number = 
564                 easysetup_serversecurity_combo_box_get_active_serversecurity_port (
565                         EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
566
567         if(port_number != 0) {
568                 gchar* str = g_strdup_printf ("%d", port_number);
569                 gtk_entry_set_text (GTK_ENTRY (self->entry_incoming_port), str);
570                 g_free (str);   
571         }               
572 }
573
574
575 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
576 {
577         GtkWidget *box = gtk_vbox_new (FALSE, 2);
578         
579         /* Create a size group to be used by all captions.
580          * Note that HildonCaption does not create a default size group if we do not specify one.
581          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
582         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
583          
584         /* The outgoing server widgets: */
585         if (!self->entry_outgoingserver)
586                 self->entry_outgoingserver = gtk_entry_new ();
587         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
588                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
589         gtk_widget_show (self->entry_outgoingserver);
590         connect_for_modified (self, self->entry_outgoingserver);
591         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
592         gtk_widget_show (caption);
593         
594         /* The secure authentication widgets: */
595         if (!self->combo_outgoing_auth)
596                 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
597         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
598                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
599         gtk_widget_show (self->combo_outgoing_auth);
600         connect_for_modified (self, self->combo_outgoing_auth);
601         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
602         gtk_widget_show (caption);
603         
604         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
605         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
606         
607         /* The username widgets: */     
608         self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
609         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
610                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
611         gtk_widget_show (self->entry_outgoing_username);
612         connect_for_modified (self, self->entry_outgoing_username);
613         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
614         gtk_widget_show (self->caption_outgoing_username);
615         
616         /* Prevent the use of some characters in the username, 
617          * as required by our UI specification: */
618         easysetup_validating_entry_set_unallowed_characters_whitespace (
619                 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
620         
621         /* Set max length as in the UI spec:
622          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
623         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
624         
625         /* The password widgets: */     
626         self->entry_outgoing_password = gtk_entry_new ();
627         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
628         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
629         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
630                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
631         gtk_widget_show (self->entry_outgoing_password);
632         connect_for_modified (self, self->entry_outgoing_password);
633         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
634         gtk_widget_show (self->caption_outgoing_password);
635         
636         /* The secure connection widgets: */
637         /* This will be filled and set with easysetup_serversecurity_combo_box_fill() 
638          * and easysetup_serversecurity_combo_box_set_active_serversecurity().
639          */
640         if (!self->combo_outgoing_security)
641                 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
642         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
643                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
644         gtk_widget_show (self->combo_outgoing_security);
645         connect_for_modified (self, self->combo_outgoing_security);
646         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
647         gtk_widget_show (caption);
648         
649         /* Show a default port number when the security method changes, as per the UI spec: */
650         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
651         
652         /* The port widgets: */
653         if (!self->entry_outgoing_port)
654                 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
655         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
656                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
657         gtk_widget_show (self->entry_outgoing_port);
658         connect_for_modified (self, self->entry_outgoing_port);
659         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
660         gtk_widget_show (caption);
661         
662         /* connection-specific checkbox: */
663         if (!self->checkbox_outgoing_smtp_specific) {
664                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
665                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
666                         FALSE);
667         }
668         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
669         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
670         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
671         
672         /* Connection-specific SMTP-Severs Edit button: */
673         if (!self->button_outgoing_smtp_servers)
674                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
675         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
676                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
677         gtk_widget_show (self->button_outgoing_smtp_servers);
678         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
679         gtk_widget_show (caption);
680         
681         /* Only enable the button when the checkbox is checked: */
682         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
683                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
684                 
685         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
686                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
687         
688         
689         gtk_widget_show (GTK_WIDGET (box));
690         
691         return GTK_WIDGET (box);
692 }
693
694 static gboolean
695 check_data (ModestAccountSettingsDialog *self)
696 {
697         /* Check that the title is not already in use: */
698         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
699         if ((!account_title) || (strlen(account_title) == 0))
700                 return FALSE; /* Should be prevented already anyway. */
701                 
702         if (strcmp(account_title, self->original_account_title) != 0) {
703                 /* Check the changed title: */
704                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
705                         account_title);
706         
707                 if (name_in_use) {
708                         /* Warn the user via a dialog: */
709                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
710                 
711                         return FALSE;
712                 }
713         }
714
715         /* Check that the email address is valud: */
716         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
717         if ((!email_address) || (strlen(email_address) == 0))
718                 return FALSE;
719                         
720         if (!modest_text_utils_validate_email_address (email_address)) {
721                 /* Warn the user via a dialog: */
722                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
723                                          
724         /* Return focus to the email address entry: */
725         gtk_widget_grab_focus (self->entry_user_email);
726         
727                 return FALSE;
728         }
729         
730         /* TODO: The UI Spec wants us to check that the servernames are valid, 
731          * but does not specify how.
732          */
733          
734         return TRUE;
735 }
736 /*
737  */
738 static void 
739 on_response (GtkDialog *wizard_dialog,
740         gint response_id,
741         gpointer user_data)
742 {
743         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
744         enable_buttons (self);
745         
746         gboolean prevent_response = FALSE;
747         
748         /* Warn about unsaved changes: */
749         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
750                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
751                 (GtkDialogFlags)0,
752                  GTK_MESSAGE_INFO,
753                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
754                  _("imum_nc_wizard_confirm_lose_changes") ));
755                  
756                  const gint dialog_response = gtk_dialog_run (dialog);
757                  gtk_widget_destroy (GTK_WIDGET (dialog));
758                  
759                 if (dialog_response != GTK_RESPONSE_OK)
760                         prevent_response = TRUE;
761         }
762         /* Check for invalid input: */
763         else if (!check_data (self)) {
764                 prevent_response = TRUE;
765         }
766                 
767         if (prevent_response) {
768                 /* This is a nasty hack. murrayc. */
769                 /* Don't let the dialog close */
770         g_signal_stop_emission_by_name (wizard_dialog, "response");
771                 return; 
772         }
773                 
774                 
775         if (response_id == GTK_RESPONSE_OK) {
776                 /* Try to save the changes: */  
777                 const gboolean saved = save_configuration (self);
778                 if (saved)
779                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
780                 else
781                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
782         }
783 }
784
785 static void
786 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
787 {
788         /* Create the notebook to be used by the GtkDialog base class:
789          * Each page of the notebook will be a page of the wizard: */
790         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
791
792     
793     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
794         
795         /* Get the account manager object, 
796          * so we can check for existing accounts,
797          * and create new accounts: */
798         self->account_manager = modest_runtime_get_account_mgr ();
799         g_assert (self->account_manager);
800         g_object_ref (self->account_manager);
801         
802     /* Create the common pages, 
803      */
804         self->page_account_details = create_page_account_details (self);
805         self->page_user_details = create_page_user_details (self);
806         self->page_incoming = create_page_incoming (self);
807         self->page_outgoing = create_page_outgoing (self);
808         
809         /* Add the notebook pages: */
810         gtk_notebook_append_page (notebook, self->page_account_details, 
811                 gtk_label_new (_("mcen_ti_account_settings_account")));
812         gtk_notebook_append_page (notebook, self->page_user_details, 
813                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
814         gtk_notebook_append_page (notebook, self->page_incoming,
815                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
816         gtk_notebook_append_page (notebook, self->page_outgoing,
817                 gtk_label_new (_("mcen_ti_advsetup_sending")));
818                 
819         GtkDialog *dialog = GTK_DIALOG (self);
820         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
821         gtk_widget_show (GTK_WIDGET (notebook));
822         
823     /* Add the buttons: */
824     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
825     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
826     
827     /* Connect to the dialog's response signal: */
828     /* We use connect-before 
829      * so we can stop the signal emission, 
830      * to stop the default signal handler from closing the dialog.
831      */
832     g_signal_connect (G_OBJECT (self), "response",
833             G_CALLBACK (on_response), self); 
834             
835     self->modified = FALSE;      
836 }
837
838 ModestAccountSettingsDialog*
839 modest_account_settings_dialog_new (void)
840 {
841         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
842 }
843
844 /** Update the UI with the stored account details, so they can be edited.
845  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
846  */
847 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
848 {
849         if (!account_name)
850                 return;
851                 
852         /* Save the account name so we can refer to it later: */
853         if (dialog->account_name)
854                 g_free (dialog->account_name);
855         dialog->account_name = g_strdup (account_name);
856         
857                 
858         /* Get the account data for this account name: */
859         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
860                 account_name);
861         if (!account_data) {
862                 g_printerr ("modest: failed to get account data for %s\n", account_name);
863                 return;
864         }
865         
866         /* Save the account title so we can refer to it if the user changes it: */
867         if (dialog->original_account_title)
868                 g_free (dialog->original_account_title);
869         dialog->original_account_title = g_strdup (account_data->display_name);
870         
871
872         if (!(account_data->store_account)) {
873                 g_printerr ("modest: account has no stores: %s\n", account_name);
874                 return;
875         }
876                 
877         /* Show the account data in the widgets: */
878         
879         /* Note that we never show the non-display name in the UI.
880          * (Though the display name defaults to the non-display name at the start.) */
881         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
882                 account_data->display_name ? account_data->display_name : "");
883                 
884         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
885                 account_data->fullname ? account_data->fullname : "");
886         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
887                 account_data->email ? account_data->email : "");
888                 
889         ModestServerAccountData *incoming_account = account_data->store_account;
890                 
891         if (incoming_account)
892                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
893         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
894                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
895         if (!retrieve) {
896                 /* Default to something, though no default is specified in the UI spec: */
897                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
898         }
899         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
900         g_free (retrieve);
901         
902         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
903                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
904         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
905         
906         
907         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
908                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
909         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
910         
911         /* Only show the leave-on-server checkbox for POP, 
912          * as per the UI spec: */
913         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
914                 gtk_widget_hide (dialog->checkbox_leave_messages);
915         } else {
916                 gtk_widget_show (dialog->checkbox_leave_messages);
917         }
918                 
919         if (incoming_account) {
920                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
921                         incoming_account->username ? incoming_account->username : "");
922                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
923                         incoming_account->password ? incoming_account->password : "");
924                         
925                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
926                         incoming_account->hostname ? incoming_account->hostname : "");
927                         
928                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
929                         dialog->account_manager, incoming_account->account_name);
930                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
931                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
932                         
933                 update_incoming_server_title (dialog, incoming_account->proto);
934                 update_incoming_server_security_choices (dialog, incoming_account->proto);
935                 
936                 const ModestProtocol security = modest_server_account_get_security (
937                         dialog->account_manager, incoming_account->account_name);
938                 easysetup_serversecurity_combo_box_set_active_serversecurity (
939                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
940                 
941                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
942                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
943                 gchar *port_str = g_strdup_printf ("%d", port_num);
944                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_incoming_port), port_str);
945                 g_free (port_str);
946         
947                 /* TODO:
948         gchar            *uri;
949         ModestProtocol    proto;
950         gchar            *password;
951         time_t            last_updated;
952         GSList           *options;
953         */
954         
955         }
956         
957         ModestServerAccountData *outgoing_account = account_data->transport_account;
958         if (outgoing_account) {
959                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
960                         outgoing_account->hostname ? outgoing_account->hostname : "");
961                 
962                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
963                         outgoing_account->username ? outgoing_account->username : "");
964                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
965                         outgoing_account->password ? outgoing_account->password : "");
966                 
967                 /* Get the secure-auth setting: */
968                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
969                         dialog->account_manager, outgoing_account->account_name);
970                 easysetup_secureauth_combo_box_set_active_secureauth (
971                         EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
972                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
973                 
974                 easysetup_serversecurity_combo_box_fill (
975                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
976                 
977                 /* Get the security setting: */
978                 const ModestProtocol security = modest_server_account_get_security (
979                         dialog->account_manager, outgoing_account->account_name);
980                 easysetup_serversecurity_combo_box_set_active_serversecurity (
981                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
982                 
983                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
984                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
985                 gchar *port_str = g_strdup_printf ("%d", port_num);
986                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_outgoing_port), port_str);
987                 g_free (port_str);
988         }
989         
990         /* account_data->is_enabled,  */
991         /*account_data->is_default,  */
992
993         /* account_data->store_account->proto */
994
995         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
996         
997         /* Unset the modified flag so we can detect changes later: */
998         dialog->modified = FALSE;
999 }
1000
1001 static gboolean
1002 save_configuration (ModestAccountSettingsDialog *dialog)
1003 {
1004         g_assert (dialog->account_name);
1005         
1006         const gchar* account_name = dialog->account_name;
1007                 
1008         /* Set the account data from the widgets: */
1009         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1010         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1011                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1012         if (!test)
1013                 return FALSE;
1014                 
1015         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1016         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1017                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1018         if (!test)
1019                 return FALSE;
1020         
1021         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1022                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1023         modest_account_mgr_set_string (dialog->account_manager, account_name,
1024                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1025         g_free (retrieve);
1026         
1027         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1028                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1029         modest_account_mgr_set_int (dialog->account_manager, account_name,
1030                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1031         
1032         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1033         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1034                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1035         if (!test)
1036                 return FALSE;
1037                         
1038         /* Incoming: */
1039         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1040                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1041         g_assert (incoming_account_name);
1042         
1043         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1044         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1045                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1046         if (!test)
1047                 return FALSE;
1048                                 
1049         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1050         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1051                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1052         if (!test)
1053                 return FALSE;
1054                                 
1055         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1056         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1057                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1058         if (!test)
1059                 return FALSE;
1060                         
1061         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
1062                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
1063                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1064                         : MODEST_PROTOCOL_AUTH_NONE;
1065         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1066                         
1067         const ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
1068                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1069         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1070         
1071         /* port: */
1072         const gchar* port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incoming_port));
1073         gint port_num = 0;
1074         if (port_str)
1075                 port_num = atoi (port_str);
1076         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1077                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1078                 
1079         g_free (incoming_account_name);
1080         
1081         /* Outgoing: */
1082         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1083                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1084         g_assert (outgoing_account_name);
1085         
1086         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1087         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1088                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1089         if (!test)
1090                 return FALSE;
1091                 
1092         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1093         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1094                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1095         if (!test)
1096                 return FALSE;
1097                 
1098         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1099         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1100                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1101         if (!test)
1102                 return FALSE;
1103         
1104         const ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
1105                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1106         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1107         
1108         const ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
1109                 EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1110         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1111                 
1112         /* port: */
1113         port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_port));
1114         port_num = 0;
1115         if (port_str)
1116                 port_num = atoi (port_str);
1117         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1118                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1119                         
1120         g_free (outgoing_account_name);
1121         
1122         
1123         /* Set the changed account title last, to simplify the previous code: */
1124         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1125         if ((!account_title) || (strlen(account_title) == 0))
1126                 return FALSE; /* Should be prevented already anyway. */
1127                 
1128         if (strcmp(account_title, account_name) != 0) {
1129                 /* Change the title: */
1130                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1131                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1132                 if (!test)
1133                         return FALSE;
1134         }
1135         
1136         return TRUE;
1137 }
1138
1139 static gboolean entry_is_empty (GtkWidget *entry)
1140 {
1141         if (!entry)
1142                 return FALSE;
1143                 
1144         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1145         if ((!text) || (strlen(text) == 0))
1146                 return TRUE;
1147         else
1148                 return FALSE;
1149 }
1150
1151 static void
1152 enable_buttons (ModestAccountSettingsDialog *self)
1153 {
1154         gboolean enable_ok = TRUE;
1155         
1156         /* The account details title is mandatory: */
1157         if (entry_is_empty(self->entry_account_title))
1158                         enable_ok = FALSE;
1159
1160         /* The user details username is mandatory: */
1161         if (entry_is_empty(self->entry_user_username))
1162                 enable_ok = FALSE;
1163                 
1164         /* The user details email address is mandatory: */
1165         if (enable_ok && entry_is_empty (self->entry_user_email))
1166                 enable_ok = FALSE;
1167
1168         /* The custom incoming server is mandatory: */
1169         if (entry_is_empty(self->entry_incomingserver))
1170                 enable_ok = FALSE;
1171                         
1172         /* Enable the buttons, 
1173          * identifying them via their associated response codes:
1174          */
1175         GtkDialog *dialog_base = GTK_DIALOG (self);
1176     gtk_dialog_set_response_sensitive (dialog_base,
1177                                        GTK_RESPONSE_OK,
1178                                        enable_ok);
1179 }
1180
1181 static void
1182 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1183 {
1184         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1185         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1186
1187
1188         object_class->get_property = modest_account_settings_dialog_get_property;
1189         object_class->set_property = modest_account_settings_dialog_set_property;
1190         object_class->dispose = modest_account_settings_dialog_dispose;
1191         object_class->finalize = modest_account_settings_dialog_finalize;
1192 }
1193  
1194 static void
1195 show_error (GtkWindow *parent_window, const gchar* text)
1196 {
1197         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1198                 (GtkDialogFlags)0,
1199                  GTK_MESSAGE_ERROR,
1200                  GTK_BUTTONS_OK,
1201                  text ));
1202                  
1203                  gtk_dialog_run (dialog);
1204                  gtk_widget_destroy (GTK_WIDGET (dialog));
1205 }
1206
1207 static void
1208 show_ok (GtkWindow *parent_window, const gchar* text)
1209 {
1210         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1211                 (GtkDialogFlags)0,
1212                  GTK_MESSAGE_INFO,
1213                  GTK_BUTTONS_OK,
1214                  text ));
1215                  
1216                  gtk_dialog_run (dialog);
1217                  gtk_widget_destroy (GTK_WIDGET (dialog));
1218 }
1219
1220
1221