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