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