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