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