2007-04-20 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 "widgets/modest-serversecurity-combo-box.h"
20 #include "widgets/modest-secureauth-combo-box.h"
21 #include "widgets/modest-validating-entry.h"
22 #include "widgets/modest-retrieve-combo-box.h"
23 #include "widgets/modest-limit-retrieve-combo-box.h"
24 #include "modest-text-utils.h"
25 #include "modest-account-mgr.h"
26 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
27 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
28 #include "maemo/modest-connection-specific-smtp-window.h"
29 #include <gconf/gconf-client.h>
30 #include <string.h> /* For strlen(). */
31
32 /* Include config.h so that _() works: */
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
38
39 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
40
41 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
42         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
43
44 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
45
46 struct _ModestAccountSettingsDialogPrivate
47 {
48 };
49
50 static void
51 enable_buttons (ModestAccountSettingsDialog *self);
52
53 static gboolean
54 save_configuration (ModestAccountSettingsDialog *dialog);
55
56 static void
57 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
58                                                                                                                         GValue *value, GParamSpec *pspec)
59 {
60         switch (property_id) {
61         default:
62                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63         }
64 }
65
66 static void
67 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
68                                                                                                                         const GValue *value, GParamSpec *pspec)
69 {
70         switch (property_id) {
71         default:
72                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
73         }
74 }
75
76 static void
77 modest_account_settings_dialog_dispose (GObject *object)
78 {
79         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
80                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
81 }
82
83 static void
84 modest_account_settings_dialog_finalize (GObject *object)
85 {
86         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
87         
88         if (self->account_name)
89                 g_free (self->account_name);
90                 
91         if (self->original_account_title)
92                 g_free (self->original_account_title);
93                 
94         if (self->account_manager)
95                 g_object_unref (G_OBJECT (self->account_manager));
96                 
97         if (self->specific_window)
98                 gtk_widget_destroy (self->specific_window);
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         modest_serversecurity_combo_box_fill (
413                 MODEST_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 (modest_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_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
505 {
506         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
507         
508         /* Create the window if necessary: */
509         if (!(self->specific_window)) {
510                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
511                 modest_connection_specific_smtp_window_fill_with_connections (
512                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
513                         self->account_name);
514         }
515
516         /* Show the window: */  
517         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
518         gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE);
519     gtk_widget_show (self->specific_window);
520 }
521
522 static void
523 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
524 {
525         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
526         
527         ModestProtocol protocol_security = 
528                 modest_secureauth_combo_box_get_active_secureauth (
529                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
530         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
531         
532         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
533         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
534 }
535
536 static void
537 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
538 {
539         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
540         
541         const gint port_number = 
542                 modest_serversecurity_combo_box_get_active_serversecurity_port (
543                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
544
545         if(port_number != 0) {
546                 gchar* str = g_strdup_printf ("%d", port_number);
547                 gtk_entry_set_text (GTK_ENTRY (self->entry_outgoing_port), str);
548                 g_free (str);   
549         }               
550 }
551
552 static void
553 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
554 {
555         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
556         
557         const gint port_number = 
558                 modest_serversecurity_combo_box_get_active_serversecurity_port (
559                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
560
561         if(port_number != 0) {
562                 gchar* str = g_strdup_printf ("%d", port_number);
563                 gtk_entry_set_text (GTK_ENTRY (self->entry_incoming_port), str);
564                 g_free (str);   
565         }               
566 }
567
568
569 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
570 {
571         GtkWidget *box = gtk_vbox_new (FALSE, 2);
572         
573         /* Create a size group to be used by all captions.
574          * Note that HildonCaption does not create a default size group if we do not specify one.
575          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
576         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
577          
578         /* The outgoing server widgets: */
579         if (!self->entry_outgoingserver)
580                 self->entry_outgoingserver = gtk_entry_new ();
581         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
582                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
583         gtk_widget_show (self->entry_outgoingserver);
584         connect_for_modified (self, self->entry_outgoingserver);
585         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
586         gtk_widget_show (caption);
587         
588         /* The secure authentication widgets: */
589         if (!self->combo_outgoing_auth)
590                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
591         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
592                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
593         gtk_widget_show (self->combo_outgoing_auth);
594         connect_for_modified (self, self->combo_outgoing_auth);
595         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
596         gtk_widget_show (caption);
597         
598         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
599         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
600         
601         /* The username widgets: */     
602         self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
603         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
604                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
605         gtk_widget_show (self->entry_outgoing_username);
606         connect_for_modified (self, self->entry_outgoing_username);
607         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
608         gtk_widget_show (self->caption_outgoing_username);
609         
610         /* Prevent the use of some characters in the username, 
611          * as required by our UI specification: */
612         easysetup_validating_entry_set_unallowed_characters_whitespace (
613                 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
614         
615         /* Set max length as in the UI spec:
616          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
617         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
618         
619         /* The password widgets: */     
620         self->entry_outgoing_password = gtk_entry_new ();
621         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
622         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
623         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
624                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
625         gtk_widget_show (self->entry_outgoing_password);
626         connect_for_modified (self, self->entry_outgoing_password);
627         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
628         gtk_widget_show (self->caption_outgoing_password);
629         
630         /* The secure connection widgets: */
631         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
632          * and modest_serversecurity_combo_box_set_active_serversecurity().
633          */
634         if (!self->combo_outgoing_security)
635                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
636         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
637                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
638         gtk_widget_show (self->combo_outgoing_security);
639         connect_for_modified (self, self->combo_outgoing_security);
640         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
641         gtk_widget_show (caption);
642         
643         /* Show a default port number when the security method changes, as per the UI spec: */
644         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
645         
646         /* The port widgets: */
647         if (!self->entry_outgoing_port)
648                 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
649         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
650                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
651         gtk_widget_show (self->entry_outgoing_port);
652         connect_for_modified (self, self->entry_outgoing_port);
653         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
654         gtk_widget_show (caption);
655         
656         /* connection-specific checkbox: */
657         if (!self->checkbox_outgoing_smtp_specific) {
658                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
659                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
660                         FALSE);
661         }
662         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
663         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
664         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
665         
666         /* Connection-specific SMTP-Severs Edit button: */
667         if (!self->button_outgoing_smtp_servers)
668                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
669         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
670                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
671         gtk_widget_show (self->button_outgoing_smtp_servers);
672         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
673         gtk_widget_show (caption);
674         
675         /* Only enable the button when the checkbox is checked: */
676         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
677                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
678                 
679         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
680                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
681         
682         
683         gtk_widget_show (GTK_WIDGET (box));
684         
685         return GTK_WIDGET (box);
686 }
687
688 static gboolean
689 check_data (ModestAccountSettingsDialog *self)
690 {
691         /* Check that the title is not already in use: */
692         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
693         if ((!account_title) || (strlen(account_title) == 0))
694                 return FALSE; /* Should be prevented already anyway. */
695                 
696         if (strcmp(account_title, self->original_account_title) != 0) {
697                 /* Check the changed title: */
698                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
699                         account_title);
700         
701                 if (name_in_use) {
702                         /* Warn the user via a dialog: */
703                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
704                 
705                         return FALSE;
706                 }
707         }
708
709         /* Check that the email address is valud: */
710         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
711         if ((!email_address) || (strlen(email_address) == 0))
712                 return FALSE;
713                         
714         if (!modest_text_utils_validate_email_address (email_address)) {
715                 /* Warn the user via a dialog: */
716                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
717                                          
718         /* Return focus to the email address entry: */
719         gtk_widget_grab_focus (self->entry_user_email);
720         
721                 return FALSE;
722         }
723         
724         /* TODO: The UI Spec wants us to check that the servernames are valid, 
725          * but does not specify how.
726          */
727          
728         return TRUE;
729 }
730 /*
731  */
732 static void 
733 on_response (GtkDialog *wizard_dialog,
734         gint response_id,
735         gpointer user_data)
736 {
737         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
738         enable_buttons (self);
739         
740         gboolean prevent_response = FALSE;
741         
742         /* Warn about unsaved changes: */
743         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
744                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
745                 (GtkDialogFlags)0,
746                  GTK_MESSAGE_INFO,
747                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
748                  _("imum_nc_wizard_confirm_lose_changes") ));
749                  
750                  const gint dialog_response = gtk_dialog_run (dialog);
751                  gtk_widget_destroy (GTK_WIDGET (dialog));
752                  
753                 if (dialog_response != GTK_RESPONSE_OK)
754                         prevent_response = TRUE;
755         }
756         /* Check for invalid input: */
757         else if (!check_data (self)) {
758                 prevent_response = TRUE;
759         }
760                 
761         if (prevent_response) {
762                 /* This is a nasty hack. murrayc. */
763                 /* Don't let the dialog close */
764         g_signal_stop_emission_by_name (wizard_dialog, "response");
765                 return; 
766         }
767                 
768                 
769         if (response_id == GTK_RESPONSE_OK) {
770                 /* Try to save the changes: */  
771                 const gboolean saved = save_configuration (self);
772                 if (saved)
773                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
774                 else
775                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
776         }
777 }
778
779 static void
780 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
781 {
782         /* Create the notebook to be used by the GtkDialog base class:
783          * Each page of the notebook will be a page of the wizard: */
784         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
785
786     
787     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
788         
789         /* Get the account manager object, 
790          * so we can check for existing accounts,
791          * and create new accounts: */
792         self->account_manager = modest_runtime_get_account_mgr ();
793         g_assert (self->account_manager);
794         g_object_ref (self->account_manager);
795         
796     /* Create the common pages, 
797      */
798         self->page_account_details = create_page_account_details (self);
799         self->page_user_details = create_page_user_details (self);
800         self->page_incoming = create_page_incoming (self);
801         self->page_outgoing = create_page_outgoing (self);
802         
803         /* Add the notebook pages: */
804         gtk_notebook_append_page (notebook, self->page_account_details, 
805                 gtk_label_new (_("mcen_ti_account_settings_account")));
806         gtk_notebook_append_page (notebook, self->page_user_details, 
807                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
808         gtk_notebook_append_page (notebook, self->page_incoming,
809                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
810         gtk_notebook_append_page (notebook, self->page_outgoing,
811                 gtk_label_new (_("mcen_ti_advsetup_sending")));
812                 
813         GtkDialog *dialog = GTK_DIALOG (self);
814         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
815         gtk_widget_show (GTK_WIDGET (notebook));
816         
817     /* Add the buttons: */
818     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
819     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
820     
821     /* Connect to the dialog's response signal: */
822     /* We use connect-before 
823      * so we can stop the signal emission, 
824      * to stop the default signal handler from closing the dialog.
825      */
826     g_signal_connect (G_OBJECT (self), "response",
827             G_CALLBACK (on_response), self); 
828             
829     self->modified = FALSE;      
830 }
831
832 ModestAccountSettingsDialog*
833 modest_account_settings_dialog_new (void)
834 {
835         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
836 }
837
838 /** Update the UI with the stored account details, so they can be edited.
839  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
840  */
841 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
842 {
843         if (!account_name)
844                 return;
845                 
846         /* Save the account name so we can refer to it later: */
847         if (dialog->account_name)
848                 g_free (dialog->account_name);
849         dialog->account_name = g_strdup (account_name);
850         
851                 
852         /* Get the account data for this account name: */
853         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
854                 account_name);
855         if (!account_data) {
856                 g_printerr ("modest: failed to get account data for %s\n", account_name);
857                 return;
858         }
859         
860         /* Save the account title so we can refer to it if the user changes it: */
861         if (dialog->original_account_title)
862                 g_free (dialog->original_account_title);
863         dialog->original_account_title = g_strdup (account_data->display_name);
864         
865
866         if (!(account_data->store_account)) {
867                 g_printerr ("modest: account has no stores: %s\n", account_name);
868                 return;
869         }
870                 
871         /* Show the account data in the widgets: */
872         
873         /* Note that we never show the non-display name in the UI.
874          * (Though the display name defaults to the non-display name at the start.) */
875         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
876                 account_data->display_name ? account_data->display_name : "");
877                 
878         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
879                 account_data->fullname ? account_data->fullname : "");
880         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
881                 account_data->email ? account_data->email : "");
882                 
883         ModestServerAccountData *incoming_account = account_data->store_account;
884                 
885         if (incoming_account)
886                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
887         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
888                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
889         if (!retrieve) {
890                 /* Default to something, though no default is specified in the UI spec: */
891                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
892         }
893         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
894         g_free (retrieve);
895         
896         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
897                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
898         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
899         
900         
901         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
902                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
903         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
904         
905         /* Only show the leave-on-server checkbox for POP, 
906          * as per the UI spec: */
907         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
908                 gtk_widget_hide (dialog->checkbox_leave_messages);
909         } else {
910                 gtk_widget_show (dialog->checkbox_leave_messages);
911         }
912                 
913         if (incoming_account) {
914                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
915                         incoming_account->username ? incoming_account->username : "");
916                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
917                         incoming_account->password ? incoming_account->password : "");
918                         
919                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
920                         incoming_account->hostname ? incoming_account->hostname : "");
921                         
922                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
923                         dialog->account_manager, incoming_account->account_name);
924                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
925                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
926                         
927                 update_incoming_server_title (dialog, incoming_account->proto);
928                 update_incoming_server_security_choices (dialog, incoming_account->proto);
929                 
930                 const ModestProtocol security = modest_server_account_get_security (
931                         dialog->account_manager, incoming_account->account_name);
932                 modest_serversecurity_combo_box_set_active_serversecurity (
933                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
934                 
935                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
936                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
937                 gchar *port_str = g_strdup_printf ("%d", port_num);
938                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_incoming_port), port_str);
939                 g_free (port_str);
940         
941                 /* TODO:
942         gchar            *uri;
943         ModestProtocol    proto;
944         gchar            *password;
945         time_t            last_updated;
946         GSList           *options;
947         */
948         
949         }
950         
951         ModestServerAccountData *outgoing_account = account_data->transport_account;
952         if (outgoing_account) {
953                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
954                         outgoing_account->hostname ? outgoing_account->hostname : "");
955                 
956                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
957                         outgoing_account->username ? outgoing_account->username : "");
958                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
959                         outgoing_account->password ? outgoing_account->password : "");
960                 
961                 /* Get the secure-auth setting: */
962                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
963                         dialog->account_manager, outgoing_account->account_name);
964                 modest_secureauth_combo_box_set_active_secureauth (
965                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
966                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
967                 
968                 modest_serversecurity_combo_box_fill (
969                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
970                 
971                 /* Get the security setting: */
972                 const ModestProtocol security = modest_server_account_get_security (
973                         dialog->account_manager, outgoing_account->account_name);
974                 modest_serversecurity_combo_box_set_active_serversecurity (
975                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
976                 
977                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
978                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
979                 gchar *port_str = g_strdup_printf ("%d", port_num);
980                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_outgoing_port), port_str);
981                 g_free (port_str);
982         }
983         
984         /* account_data->is_enabled,  */
985         /*account_data->is_default,  */
986
987         /* account_data->store_account->proto */
988
989         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
990         
991         /* Unset the modified flag so we can detect changes later: */
992         dialog->modified = FALSE;
993 }
994
995 static gboolean
996 save_configuration (ModestAccountSettingsDialog *dialog)
997 {
998         g_assert (dialog->account_name);
999         
1000         const gchar* account_name = dialog->account_name;
1001                 
1002         /* Set the account data from the widgets: */
1003         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1004         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1005                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1006         if (!test)
1007                 return FALSE;
1008                 
1009         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1010         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1011                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1012         if (!test)
1013                 return FALSE;
1014         
1015         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1016                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1017         modest_account_mgr_set_string (dialog->account_manager, account_name,
1018                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1019         g_free (retrieve);
1020         
1021         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1022                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1023         modest_account_mgr_set_int (dialog->account_manager, account_name,
1024                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1025         
1026         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1027         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1028                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1029         if (!test)
1030                 return FALSE;
1031                         
1032         /* Incoming: */
1033         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1034                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1035         g_assert (incoming_account_name);
1036         
1037         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1038         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1039                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1040         if (!test)
1041                 return FALSE;
1042                                 
1043         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1044         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1045                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1046         if (!test)
1047                 return FALSE;
1048                                 
1049         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1050         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1051                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1052         if (!test)
1053                 return FALSE;
1054                         
1055         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
1056                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
1057                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1058                         : MODEST_PROTOCOL_AUTH_NONE;
1059         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1060                         
1061         const ModestProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1062                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1063         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1064         
1065         /* port: */
1066         const gchar* port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incoming_port));
1067         gint port_num = 0;
1068         if (port_str)
1069                 port_num = atoi (port_str);
1070         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1071                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1072                 
1073         g_free (incoming_account_name);
1074         
1075         /* Outgoing: */
1076         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1077                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1078         g_assert (outgoing_account_name);
1079         
1080         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1081         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1082                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1083         if (!test)
1084                 return FALSE;
1085                 
1086         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1087         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1088                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1089         if (!test)
1090                 return FALSE;
1091                 
1092         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1093         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1094                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1095         if (!test)
1096                 return FALSE;
1097         
1098         const ModestProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1099                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1100         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1101         
1102         const ModestProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1103                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1104         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1105                 
1106         /* port: */
1107         port_str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_port));
1108         port_num = 0;
1109         if (port_str)
1110                 port_num = atoi (port_str);
1111         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1112                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1113                         
1114         g_free (outgoing_account_name);
1115         
1116         
1117         /* Set the changed account title last, to simplify the previous code: */
1118         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1119         if ((!account_title) || (strlen(account_title) == 0))
1120                 return FALSE; /* Should be prevented already anyway. */
1121                 
1122         if (strcmp(account_title, account_name) != 0) {
1123                 /* Change the title: */
1124                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1125                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1126                 if (!test)
1127                         return FALSE;
1128         }
1129         
1130         /* Save connection-specific SMTP server accounts: */
1131         if (dialog->specific_window)
1132                 return modest_connection_specific_smtp_window_save_server_accounts (
1133                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1134         else
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