2007-05-17 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-account-settings-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 #include "modest-account-settings-dialog.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkstock.h>
42 #include "modest-hildon-includes.h"
43
44 #include "widgets/modest-serversecurity-combo-box.h"
45 #include "widgets/modest-secureauth-combo-box.h"
46 #include "widgets/modest-validating-entry.h"
47 #include "widgets/modest-retrieve-combo-box.h"
48 #include "widgets/modest-limit-retrieve-combo-box.h"
49 #include "modest-text-utils.h"
50 #include "modest-account-mgr.h"
51 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
52 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
53 #include "maemo/modest-connection-specific-smtp-window.h"
54 #include "maemo/modest-signature-editor-dialog.h"
55 #include "widgets/modest-ui-constants.h"
56
57 #include <tny-camel-transport-account.h>
58 #include <tny-camel-imap-store-account.h>
59 #include <tny-camel-pop-store-account.h>
60
61 #include <gconf/gconf-client.h>
62 #include <string.h> /* For strlen(). */
63
64 /* Include config.h so that _() works: */
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
70
71 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
72
73 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
74         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
75
76 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
77
78 struct _ModestAccountSettingsDialogPrivate
79 {
80 };
81
82 static void
83 enable_buttons (ModestAccountSettingsDialog *self);
84
85 static gboolean
86 save_configuration (ModestAccountSettingsDialog *dialog);
87
88 static void
89 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
90                                                                                                                         GValue *value, GParamSpec *pspec)
91 {
92         switch (property_id) {
93         default:
94                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95         }
96 }
97
98 static void
99 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
100                                                                                                                         const GValue *value, GParamSpec *pspec)
101 {
102         switch (property_id) {
103         default:
104                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105         }
106 }
107
108 static void
109 modest_account_settings_dialog_dispose (GObject *object)
110 {
111         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
112                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
113 }
114
115 static void
116 modest_account_settings_dialog_finalize (GObject *object)
117 {
118         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
119         
120         if (self->account_name)
121                 g_free (self->account_name);
122                 
123         if (self->original_account_title)
124                 g_free (self->original_account_title);
125                 
126         if (self->account_manager)
127                 g_object_unref (G_OBJECT (self->account_manager));
128                 
129         if (self->specific_window)
130                 gtk_widget_destroy (self->specific_window);
131                 
132         if (self->signature_dialog)
133                 gtk_widget_destroy (self->signature_dialog);
134         
135         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
136 }
137
138 static void
139 show_error (GtkWindow *parent_window, const gchar* text);
140
141 static void
142 show_ok (GtkWindow *parent_window, const gchar* text);
143
144 static void
145 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
146
147 static void
148 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
149
150 static void
151 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
152 {
153         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
154         self->modified = TRUE;
155 }
156
157 static void
158 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
159 {
160         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
161         self->modified = TRUE;
162 }
163
164 static void
165 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
166 {
167         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
168         self->modified = TRUE;
169 }
170
171 /* Set a modified boolean whenever the widget is changed, 
172  * so we can check for it later.
173  */
174 static void
175 connect_for_modified (ModestAccountSettingsDialog *self, GtkWidget *widget)
176 {
177         if (GTK_IS_ENTRY (widget)) {
178           g_signal_connect (G_OBJECT (widget), "changed",
179                 G_CALLBACK (on_modified_entry_changed), self);  
180         } else if (GTK_IS_COMBO_BOX (widget)) {
181                 g_signal_connect (G_OBJECT (widget), "changed",
182                 G_CALLBACK (on_modified_combobox_changed), self);       
183         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
184                 g_signal_connect (G_OBJECT (widget), "toggled",
185                 G_CALLBACK (on_modified_checkbox_toggled), self);
186         }
187 }
188
189 static void
190 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
191 {
192         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
193         g_assert(self);
194         enable_buttons(self);
195 }
196
197 static void
198 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
199 {
200         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
201         g_assert(self);
202         enable_buttons(self);
203 }
204
205 /** This is a convenience function to create a caption containing a mandatory widget.
206  * When the widget is edited, the enable_buttons() vfunc will be called.
207  */
208 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
209         GtkSizeGroup *group,
210         const gchar *value,
211         GtkWidget *control,
212         GtkWidget *icon,
213         HildonCaptionStatus flag)
214 {
215   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
216   
217 /* The translated strings seem to already contain the *,
218  * but this code can be used if that is not true in future.
219  */
220 #if 0
221         /* Add a * character to indicate mandatory fields,
222          * as specified in our "Email UI Specification": */
223         if (flag == HILDON_CAPTION_MANDATORY) {
224                 gchar* title = g_strdup_printf("%s*", value);
225                 caption = hildon_caption_new (group, title, control, icon, flag);       
226                 g_free(title);
227         }       
228         else
229                 caption = hildon_caption_new (group, value, control, icon, flag);
230 #endif
231
232         /* Connect to the appropriate changed signal for the widget, 
233          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
234          */
235         if (GTK_IS_ENTRY (control)) {
236                 g_signal_connect (G_OBJECT (control), "changed",
237                 G_CALLBACK (on_caption_entry_changed), self);
238                 
239         }
240         else if (GTK_IS_COMBO_BOX (control)) {
241                 g_signal_connect (G_OBJECT (control), "changed",
242                 G_CALLBACK (on_caption_combobox_changed), self);
243         }
244          
245         return caption;
246 }
247
248
249 static void
250 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
251 {
252         ModestAccountSettingsDialog *dialog = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
253         show_error (GTK_WINDOW (dialog), _("ckdg_ib_maximum_characters_reached"));
254 }
255
256 static GtkWidget*
257 create_page_account_details (ModestAccountSettingsDialog *self)
258 {
259         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
260         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
261         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
262         gtk_widget_show (label);
263         
264         /* Create a size group to be used by all captions.
265          * Note that HildonCaption does not create a default size group if we do not specify one.
266          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
267         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
268            
269         /* The description widgets: */  
270         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
271         /* Do use auto-capitalization: */
272         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
273                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
274         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
275                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
276         gtk_widget_show (self->entry_account_title);
277         connect_for_modified (self, self->entry_account_title);
278         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
279         gtk_widget_show (caption);
280         
281         /* Prevent the use of some characters in the account title, 
282          * as required by our UI specification: */
283         GList *list_prevent = NULL;
284         list_prevent = g_list_append (list_prevent, "\\");
285         list_prevent = g_list_append (list_prevent, "/");
286         list_prevent = g_list_append (list_prevent, ":");
287         list_prevent = g_list_append (list_prevent, "*");
288         list_prevent = g_list_append (list_prevent, "?");
289         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
290         list_prevent = g_list_append (list_prevent, "“");
291         list_prevent = g_list_append (list_prevent, "<"); 
292         list_prevent = g_list_append (list_prevent, ">"); 
293         list_prevent = g_list_append (list_prevent, "|");
294         list_prevent = g_list_append (list_prevent, "^");       
295         modest_validating_entry_set_unallowed_characters (
296                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
297         g_list_free (list_prevent);
298         
299         /* Set max length as in the UI spec:
300          * The UI spec seems to want us to show a dialog if we hit the maximum. */
301         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
302         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
303                 on_entry_max, self);
304         
305         /* The retrieve combobox: */
306         self->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
307         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
308                 self->combo_retrieve, NULL, HILDON_CAPTION_MANDATORY);
309         gtk_widget_show (self->combo_retrieve);
310         connect_for_modified (self, self->combo_retrieve);
311         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
312         gtk_widget_show (caption);
313         
314         /* The limit-retrieve combobox: */
315         self->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
316         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
317                 self->combo_limit_retrieve, NULL, HILDON_CAPTION_MANDATORY);
318         gtk_widget_show (self->combo_limit_retrieve);
319         connect_for_modified (self, self->combo_limit_retrieve);
320         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
321         gtk_widget_show (caption);
322
323         /* The leave-messages widgets: */
324         if(!self->checkbox_leave_messages)
325                 self->checkbox_leave_messages = gtk_check_button_new ();
326         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
327                 self->checkbox_leave_messages, NULL, HILDON_CAPTION_MANDATORY);
328         gtk_widget_show (self->checkbox_leave_messages);
329         connect_for_modified (self, self->checkbox_leave_messages);
330         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
331         gtk_widget_show (caption);
332         
333         gtk_widget_show (GTK_WIDGET (box));
334         
335         return GTK_WIDGET (box);
336 }
337
338 static void
339 on_button_signature (GtkButton *button, gpointer user_data)
340 {
341         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
342         
343         /* Create the window, if necessary: */
344         if (!(self->signature_dialog)) {
345                 self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ());
346         
347                 gboolean use_signature = FALSE;
348                 gchar *signature = modest_account_mgr_get_signature(self->account_manager, self->account_name, 
349                         &use_signature);
350                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
351                 modest_signature_editor_dialog_set_settings (
352                         MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog), 
353                         use_signature, signature, account_title);
354                 g_free (signature);
355                 signature = NULL;
356         }
357
358         /* Show the window: */  
359         gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self));
360         gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE);
361     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog));
362     gtk_widget_hide (self->signature_dialog);
363     if (response != GTK_RESPONSE_OK) {
364         /* Destroy the widget now, and its data: */
365         gtk_widget_destroy (self->signature_dialog);
366         self->signature_dialog = NULL;
367     }
368     else {
369         /* Mark modified, so we use the dialog's data later: */
370         self->modified = TRUE;  
371     }
372 }
373
374 static GtkWidget*
375 create_page_user_details (ModestAccountSettingsDialog *self)
376 {
377         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
378         
379         /* Create a size group to be used by all captions.
380          * Note that HildonCaption does not create a default size group if we do not specify one.
381          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
382         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
383          
384         /* The name widgets: */
385         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
386         /* Auto-capitalization is the default, so let's turn it off: */
387         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
388         /* Set max length as in the UI spec:
389          * The UI spec seems to want us to show a dialog if we hit the maximum. */
390         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
391         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
392                 on_entry_max, self);
393         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
394                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
395         gtk_widget_show (self->entry_user_name);
396         connect_for_modified (self, self->entry_user_name);
397         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
398         gtk_widget_show (caption);
399         
400         /* Prevent the use of some characters in the name, 
401          * as required by our UI specification: */
402         GList *list_prevent = NULL;
403         list_prevent = g_list_append (list_prevent, "<");
404         list_prevent = g_list_append (list_prevent, ">");
405         modest_validating_entry_set_unallowed_characters (
406                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
407         g_list_free (list_prevent);
408         
409         /* The username widgets: */     
410         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
411         /* Auto-capitalization is the default, so let's turn it off: */
412         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
413         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
414                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
415         gtk_widget_show (self->entry_user_username);
416         connect_for_modified (self, self->entry_user_username);
417         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
418         gtk_widget_show (caption);
419         
420         /* Prevent the use of some characters in the username, 
421          * as required by our UI specification: */
422         modest_validating_entry_set_unallowed_characters_whitespace (
423                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
424         
425         /* Set max length as in the UI spec:
426          * The UI spec seems to want us to show a dialog if we hit the maximum. */
427         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
428         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
429                 on_entry_max, self);
430         
431         /* The password widgets: */     
432         self->entry_user_password = gtk_entry_new ();
433         /* Auto-capitalization is the default, so let's turn it off: */
434         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
435                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
436         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
437         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
438         caption = create_caption_new_with_asterix (self, sizegroup, 
439                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
440         gtk_widget_show (self->entry_user_password);
441         connect_for_modified (self, self->entry_user_password);
442         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
443         gtk_widget_show (caption);
444         
445         /* The email address widgets: */        
446         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
447         /* Auto-capitalization is the default, so let's turn it off: */
448         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
449         caption = create_caption_new_with_asterix (self, sizegroup, 
450                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
451         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
452         gtk_widget_show (self->entry_user_email);
453         connect_for_modified (self, self->entry_user_email);
454         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
455         gtk_widget_show (caption);
456         
457         /* Set max length as in the UI spec:
458          * The UI spec seems to want us to show a dialog if we hit the maximum. */
459         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
460         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
461                 on_entry_max, self);
462         
463         /* Signature button: */
464         if (!self->button_signature)
465                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
466         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
467                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
468         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
469         gtk_widget_show (self->button_signature);
470         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
471         gtk_widget_show (caption);
472                 
473         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
474                 G_CALLBACK (on_button_signature), self);
475                 
476         gtk_widget_show (GTK_WIDGET (box));
477         
478         return GTK_WIDGET (box);
479 }
480
481 /** Change the caption title for the incoming server, 
482  * as specified in the UI spec:
483  */
484 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
485 {
486         const gchar* type = 
487                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
488                         _("mail_fi_emailtype_pop3") : 
489                         _("mail_fi_emailtype_imap") );
490                         
491                 
492         /* Note that this produces a compiler warning, 
493          * because the compiler does not know that the translated string will have a %s in it.
494          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
495         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
496         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
497         g_free(incomingserver_title);
498 }
499
500 /** Change the caption title for the incoming server, 
501  * as specified in the UI spec:
502  */
503 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
504 {
505         /* Fill the combo with appropriately titled choices for POP or IMAP. */
506         /* The choices are the same, but the titles are different, as in the UI spec. */
507         modest_serversecurity_combo_box_fill (
508                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
509 }
510            
511 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
512 {
513         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
514         
515         /* Create a size group to be used by all captions.
516          * Note that HildonCaption does not create a default size group if we do not specify one.
517          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
518         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
519          
520         /* The incoming server widgets: */
521         if(!self->entry_incomingserver)
522                 self->entry_incomingserver = gtk_entry_new ();
523         /* Auto-capitalization is the default, so let's turn it off: */
524         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
525
526         if (self->caption_incoming)
527           gtk_widget_destroy (self->caption_incoming);
528            
529         /* The caption title will be updated in update_incoming_server_title().
530          * so this default text will never be seen: */
531         /* (Note: Changing the title seems pointless. murrayc) */
532         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
533                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
534         gtk_widget_show (self->entry_incomingserver);
535         connect_for_modified (self, self->entry_incomingserver);
536         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
537         gtk_widget_show (self->caption_incoming);
538         
539         /* The secure connection widgets: */
540         /* This will be filled by update_incoming_server_security_choices(). */
541         if (!self->combo_incoming_security)
542                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
543         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
544                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
545         gtk_widget_show (self->combo_incoming_security);
546         connect_for_modified (self, self->combo_incoming_security);
547         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
548         gtk_widget_show (caption);
549         
550         /* Show a default port number when the security method changes, as per the UI spec: */
551         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
552         
553         
554         /* The port widgets: */
555         /* TODO: There are various rules about this in the UI spec. */
556         if (!self->entry_incoming_port)
557                 self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
558         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
559                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
560         gtk_widget_show (self->entry_incoming_port);
561         connect_for_modified (self, self->entry_incoming_port);
562         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
563         gtk_widget_show (caption);
564         
565         /* The secure authentication widgets: */
566         if(!self->checkbox_incoming_auth)
567                 self->checkbox_incoming_auth = gtk_check_button_new ();
568         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
569                 self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
570         gtk_widget_show (self->checkbox_incoming_auth);
571         connect_for_modified (self, self->checkbox_incoming_auth);
572         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
573         gtk_widget_show (caption);
574         
575         gtk_widget_show (GTK_WIDGET (box));
576         
577         return GTK_WIDGET (box);
578 }
579
580 static void
581 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
582 {
583         GtkWidget *widget = GTK_WIDGET (user_data);
584         
585         /* Enable the widget only if the toggle button is active: */
586         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
587         gtk_widget_set_sensitive (widget, enable);
588 }
589
590 /* Make the sensitivity of a widget depend on a toggle button.
591  */
592 static void
593 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
594 {
595         g_signal_connect (G_OBJECT (button), "toggled",
596                 G_CALLBACK (on_toggle_button_changed), widget);
597         
598         /* Set the starting sensitivity: */
599         on_toggle_button_changed (button, widget);
600 }
601
602 static void
603 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
604 {
605         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
606         
607         /* Create the window if necessary: */
608         if (!(self->specific_window)) {
609                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
610                 modest_connection_specific_smtp_window_fill_with_connections (
611                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
612                         self->account_name);
613         }
614
615         /* Show the window: */  
616         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
617         gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE);
618     gtk_widget_show (self->specific_window);
619 }
620
621 static void
622 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
623 {
624         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
625         
626         ModestAuthProtocol protocol_security = 
627                 modest_secureauth_combo_box_get_active_secureauth (
628                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
629         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
630         
631         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
632         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
633 }
634
635 static void
636 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
637 {
638         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
639         
640         const gint port_number = 
641                 modest_serversecurity_combo_box_get_active_serversecurity_port (
642                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
643
644         if(port_number != 0) {
645                 hildon_number_editor_set_value (
646                         HILDON_NUMBER_EDITOR (self->entry_outgoing_port), port_number);
647         }               
648 }
649
650 static void
651 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
652 {
653         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
654         
655         const gint port_number = 
656                 modest_serversecurity_combo_box_get_active_serversecurity_port (
657                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
658
659         if(port_number != 0) {
660                 hildon_number_editor_set_value (
661                         HILDON_NUMBER_EDITOR (self->entry_incoming_port), port_number);
662         }               
663 }
664
665
666 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
667 {
668         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
669         
670         /* Create a size group to be used by all captions.
671          * Note that HildonCaption does not create a default size group if we do not specify one.
672          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
673         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
674          
675         /* The outgoing server widgets: */
676         if (!self->entry_outgoingserver)
677                 self->entry_outgoingserver = gtk_entry_new ();
678         /* Auto-capitalization is the default, so let's turn it off: */
679         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
680         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
681                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
682         gtk_widget_show (self->entry_outgoingserver);
683         connect_for_modified (self, self->entry_outgoingserver);
684         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
685         gtk_widget_show (caption);
686         
687         /* The secure authentication widgets: */
688         if (!self->combo_outgoing_auth)
689                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
690         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
691                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
692         gtk_widget_show (self->combo_outgoing_auth);
693         connect_for_modified (self, self->combo_outgoing_auth);
694         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
695         gtk_widget_show (caption);
696         
697         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
698         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
699         
700         /* The username widgets: */     
701         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
702         /* Auto-capitalization is the default, so let's turn it off: */
703         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_username), HILDON_GTK_INPUT_MODE_FULL);
704         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
705                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
706         gtk_widget_show (self->entry_outgoing_username);
707         connect_for_modified (self, self->entry_outgoing_username);
708         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
709         gtk_widget_show (self->caption_outgoing_username);
710         
711         /* Prevent the use of some characters in the username, 
712          * as required by our UI specification: */
713         modest_validating_entry_set_unallowed_characters_whitespace (
714                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
715         
716         /* Set max length as in the UI spec:
717          * The UI spec seems to want us to show a dialog if we hit the maximum. */
718         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
719         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
720                 on_entry_max, self);
721                 
722         /* The password widgets: */     
723         self->entry_outgoing_password = gtk_entry_new ();
724         /* Auto-capitalization is the default, so let's turn it off: */
725         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_password), 
726                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
727         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
728         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
729         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
730                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
731         gtk_widget_show (self->entry_outgoing_password);
732         connect_for_modified (self, self->entry_outgoing_password);
733         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
734         gtk_widget_show (self->caption_outgoing_password);
735         
736         /* The secure connection widgets: */
737         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
738          * and modest_serversecurity_combo_box_set_active_serversecurity().
739          */
740         if (!self->combo_outgoing_security)
741                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
742         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
743                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
744         gtk_widget_show (self->combo_outgoing_security);
745         connect_for_modified (self, self->combo_outgoing_security);
746         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
747         gtk_widget_show (caption);
748         
749         /* Show a default port number when the security method changes, as per the UI spec: */
750         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
751         
752         /* The port widgets: */
753         if (!self->entry_outgoing_port)
754                 self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
755         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
756                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
757         gtk_widget_show (self->entry_outgoing_port);
758         connect_for_modified (self, self->entry_outgoing_port);
759         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
760         gtk_widget_show (caption);
761         
762         GtkWidget *separator = gtk_hseparator_new ();
763         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
764         gtk_widget_show (separator);
765         
766         /* connection-specific checkbox: */
767         if (!self->checkbox_outgoing_smtp_specific) {
768                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
769                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
770                         FALSE);
771         }
772         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
773                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
774         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
775         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
776         gtk_widget_show (caption);
777         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
778         
779         /* Connection-specific SMTP-Severs Edit button: */
780         if (!self->button_outgoing_smtp_servers)
781                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
782         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
783                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
784         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
785         gtk_widget_show (self->button_outgoing_smtp_servers);
786         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
787         gtk_widget_show (caption);
788         
789         /* Only enable the button when the checkbox is checked: */
790         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
791                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
792                 
793         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
794                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
795                 
796         gtk_widget_show (GTK_WIDGET (box));
797         
798         return GTK_WIDGET (box);
799 }
800
801 static gboolean
802 check_data (ModestAccountSettingsDialog *self)
803 {
804         /* Check that the title is not already in use: */
805         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
806         if ((!account_title) || (strlen(account_title) == 0))
807                 return FALSE; /* Should be prevented already anyway. */
808                 
809         if (strcmp(account_title, self->original_account_title) != 0) {
810                 /* Check the changed title: */
811                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
812                         account_title);
813         
814                 if (name_in_use) {
815                         /* Warn the user via a dialog: */
816                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
817                 
818                         return FALSE;
819                 }
820         }
821
822         /* Check that the email address is valud: */
823         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
824         if ((!email_address) || (strlen(email_address) == 0))
825                 return FALSE;
826                         
827         if (!modest_text_utils_validate_email_address (email_address)) {
828                 /* Warn the user via a dialog: */
829                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
830                                          
831         /* Return focus to the email address entry: */
832         gtk_widget_grab_focus (self->entry_user_email);
833         
834                 return FALSE;
835         }
836         
837         /* TODO: The UI Spec wants us to check that the servernames are valid, 
838          * but does not specify how.
839          */
840          
841         return TRUE;
842 }
843 /*
844  */
845 static void 
846 on_response (GtkDialog *wizard_dialog,
847         gint response_id,
848         gpointer user_data)
849 {
850         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
851         enable_buttons (self);
852         
853         gboolean prevent_response = FALSE;
854         
855         /* Warn about unsaved changes: */
856         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
857                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
858                         _("imum_nc_wizard_confirm_lose_changes")));
859                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
860                  
861                  const gint dialog_response = gtk_dialog_run (dialog);
862                  gtk_widget_destroy (GTK_WIDGET (dialog));
863                  
864                 if (dialog_response != GTK_RESPONSE_OK)
865                         prevent_response = TRUE;
866         }
867         /* Check for invalid input: */
868         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
869                 prevent_response = TRUE;
870         }
871                 
872         if (prevent_response) {
873                 /* This is a nasty hack. murrayc. */
874                 /* Don't let the dialog close */
875         g_signal_stop_emission_by_name (wizard_dialog, "response");
876                 return; 
877         }
878                 
879         if (response_id == GTK_RESPONSE_OK) {
880                 /* Try to save the changes: */  
881                 const gboolean saved = save_configuration (self);
882                 if (saved) {
883                         /* Do not show the account-saved dialog if we are just saving this 
884                          * temporarily, because from the user's point of view it will not 
885                          * really be saved (saved + enabled) until later.
886                          */
887                         const gboolean enabled = 
888                                 modest_account_mgr_get_enabled (self->account_manager, self->account_name);
889                         if (enabled)
890                                 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
891                 }
892                 else
893                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
894         }
895 }
896
897 static void
898 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
899 {
900         /* Create the notebook to be used by the GtkDialog base class:
901          * Each page of the notebook will be a page of the wizard: */
902         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
903
904     
905     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
906         
907         /* Get the account manager object, 
908          * so we can check for existing accounts,
909          * and create new accounts: */
910         self->account_manager = modest_runtime_get_account_mgr ();
911         g_assert (self->account_manager);
912         g_object_ref (self->account_manager);
913         
914     /* Create the common pages, 
915      */
916         self->page_account_details = create_page_account_details (self);
917         self->page_user_details = create_page_user_details (self);
918         self->page_incoming = create_page_incoming (self);
919         self->page_outgoing = create_page_outgoing (self);
920         
921         /* Add the notebook pages: */
922         gtk_notebook_append_page (notebook, self->page_account_details, 
923                 gtk_label_new (_("mcen_ti_account_settings_account")));
924         gtk_notebook_append_page (notebook, self->page_user_details, 
925                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
926         gtk_notebook_append_page (notebook, self->page_incoming,
927                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
928         gtk_notebook_append_page (notebook, self->page_outgoing,
929                 gtk_label_new (_("mcen_ti_advsetup_sending")));
930                 
931         GtkDialog *dialog = GTK_DIALOG (self);
932         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
933         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
934         gtk_widget_show (GTK_WIDGET (notebook));
935         
936     /* Add the buttons: */
937     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
938     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
939     
940     /* Connect to the dialog's response signal: */
941     /* We use connect-before 
942      * so we can stop the signal emission, 
943      * to stop the default signal handler from closing the dialog.
944      */
945     g_signal_connect (G_OBJECT (self), "response",
946             G_CALLBACK (on_response), self); 
947             
948     self->modified = FALSE;
949     
950     /* When this window is shown, hibernation should not be possible, 
951          * because there is no sensible way to save the state: */
952     modest_window_mgr_prevent_hibernation_while_window_is_shown (
953         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
954 }
955
956 ModestAccountSettingsDialog*
957 modest_account_settings_dialog_new (void)
958 {
959         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
960 }
961
962 /** Update the UI with the stored account details, so they can be edited.
963  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
964  */
965 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
966 {
967         if (!account_name)
968                 return;
969                 
970         /* Save the account name so we can refer to it later: */
971         if (dialog->account_name)
972                 g_free (dialog->account_name);
973         dialog->account_name = g_strdup (account_name);
974         
975                 
976         /* Get the account data for this account name: */
977         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
978                 account_name);
979         if (!account_data) {
980                 g_printerr ("modest: failed to get account data for %s\n", account_name);
981                 return;
982         }
983         
984         /* Save the account title so we can refer to it if the user changes it: */
985         if (dialog->original_account_title)
986                 g_free (dialog->original_account_title);
987         dialog->original_account_title = g_strdup (account_data->display_name);
988         
989
990         if (!(account_data->store_account)) {
991                 g_printerr ("modest: account has no stores: %s\n", account_name);
992                 return;
993         }
994                 
995         /* Show the account data in the widgets: */
996         
997         /* Note that we never show the non-display name in the UI.
998          * (Though the display name defaults to the non-display name at the start.) */
999         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1000                 account_data->display_name ? account_data->display_name : "");
1001                 
1002         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1003                 account_data->fullname ? account_data->fullname : "");
1004         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1005                 account_data->email ? account_data->email : "");
1006                 
1007         ModestServerAccountData *incoming_account = account_data->store_account;
1008                 
1009         if (incoming_account)
1010                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
1011         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
1012                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
1013         if (!retrieve) {
1014                 /* Default to something, though no default is specified in the UI spec: */
1015                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
1016         }
1017         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
1018         g_free (retrieve);
1019         
1020         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
1021                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
1022         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
1023         
1024         
1025         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
1026                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
1027         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
1028         
1029         /* Only show the leave-on-server checkbox for POP, 
1030          * as per the UI spec: */
1031         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
1032                 gtk_widget_hide (dialog->checkbox_leave_messages);
1033         } else {
1034                 gtk_widget_show (dialog->checkbox_leave_messages);
1035         }
1036                 
1037         if (incoming_account) {
1038                 /* Remember this for later: */
1039                 dialog->incoming_protocol = incoming_account->proto;
1040                 
1041                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1042                         incoming_account->username ? incoming_account->username : "");
1043                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1044                         incoming_account->password ? incoming_account->password : "");
1045                         
1046                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1047                         incoming_account->hostname ? incoming_account->hostname : "");
1048                         
1049                 /* The UI spec says:
1050                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1051          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1052                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1053          */
1054                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1055                         dialog->account_manager, incoming_account->account_name);
1056                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1057                         secure_auth != MODEST_PROTOCOL_AUTH_PASSWORD);
1058                 /* Note that MODEST_PROTOCOL_AUTH_PLAIN should probably never be used. */
1059                         
1060                 update_incoming_server_title (dialog, incoming_account->proto);
1061                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1062                 
1063                 const ModestConnectionProtocol security = modest_server_account_get_security (
1064                         dialog->account_manager, incoming_account->account_name);
1065                 modest_serversecurity_combo_box_set_active_serversecurity (
1066                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1067                 
1068                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1069                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1070                         
1071                 if (port_num == 0) {
1072                         /* Show the appropriate port number: */
1073                         on_combo_incoming_security_changed (
1074                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1075                 } else {
1076                         /* Keep the user-entered port-number,
1077                          * or the already-appropriate automatic port number: */
1078                         hildon_number_editor_set_value (
1079                                 HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1080                 }
1081         }
1082         
1083         ModestServerAccountData *outgoing_account = account_data->transport_account;
1084         if (outgoing_account) {
1085                 /* Remember this for later: */
1086                 dialog->outgoing_protocol = outgoing_account->proto;
1087                 
1088                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1089                         outgoing_account->hostname ? outgoing_account->hostname : "");
1090                 
1091                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1092                         outgoing_account->username ? outgoing_account->username : "");
1093                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1094                         outgoing_account->password ? outgoing_account->password : "");
1095                 
1096                 /* Get the secure-auth setting: */
1097                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1098                         dialog->account_manager, outgoing_account->account_name);
1099                 modest_secureauth_combo_box_set_active_secureauth (
1100                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1101                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1102                 
1103                 modest_serversecurity_combo_box_fill (
1104                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1105                 
1106                 /* Get the security setting: */
1107                 const ModestConnectionProtocol security = modest_server_account_get_security (
1108                         dialog->account_manager, outgoing_account->account_name);
1109                 modest_serversecurity_combo_box_set_active_serversecurity (
1110                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1111                 
1112                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1113                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1114                 if (port_num == 0) {
1115                         /* Show the appropriate port number: */
1116                         on_combo_outgoing_security_changed (
1117                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1118                 }
1119                 else {
1120                         /* Keep the user-entered port-number,
1121                          * or the already-appropriate automatic port number: */
1122                         hildon_number_editor_set_value (
1123                                 HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1124                 }
1125         }
1126         
1127         /* account_data->is_enabled,  */
1128         /*account_data->is_default,  */
1129
1130         /* account_data->store_account->proto */
1131
1132         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1133         
1134         /* Unset the modified flag so we can detect changes later: */
1135         dialog->modified = FALSE;
1136 }
1137
1138 static GList* get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
1139         const gchar* hostname, gint port, GtkWindow *parent_window);
1140
1141 static gboolean
1142 save_configuration (ModestAccountSettingsDialog *dialog)
1143 {
1144         g_assert (dialog->account_name);
1145         
1146         const gchar* account_name = dialog->account_name;
1147                 
1148         /* Set the account data from the widgets: */
1149         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1150         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1151                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1152         if (!test)
1153                 return FALSE;
1154                 
1155         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1156         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1157                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1158         if (!test)
1159                 return FALSE;
1160                 
1161         /* Signature: */
1162         if (dialog->signature_dialog) {
1163                 gboolean use_signature = FALSE;
1164         gchar *signature = modest_signature_editor_dialog_get_settings (
1165                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1166                 &use_signature);
1167         
1168         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1169                 signature, use_signature);
1170         g_free (signature);
1171     }
1172         
1173         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1174                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1175         modest_account_mgr_set_string (dialog->account_manager, account_name,
1176                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1177         g_free (retrieve);
1178         
1179         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1180                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1181         modest_account_mgr_set_int (dialog->account_manager, account_name,
1182                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1183         
1184         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1185         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1186                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1187         if (!test)
1188                 return FALSE;
1189                         
1190         /* Incoming: */
1191         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1192                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1193         g_assert (incoming_account_name);
1194         
1195         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1196         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1197                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1198         if (!test)
1199                 return FALSE;
1200                                 
1201         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1202         modest_server_account_set_username (dialog->account_manager, incoming_account_name, username);
1203         
1204         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1205         modest_server_account_set_password (dialog->account_manager, incoming_account_name, password);
1206                         
1207         /* port: */
1208         gint port_num = hildon_number_editor_get_value (
1209                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1210         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1211                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1212                         
1213         /* The UI spec says:
1214          * If secure authentication is unchecked, allow sending username and password also as plain text.
1215          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1216          */
1217         ModestAuthProtocol protocol_authentication_incoming = 
1218                 MODEST_PROTOCOL_AUTH_PASSWORD;
1219         if (gtk_toggle_button_get_active (
1220                         GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))) {
1221                 GList *list_auth_methods = 
1222                         get_supported_secure_authentication_methods (dialog->incoming_protocol, 
1223                                 hostname, port_num, GTK_WINDOW (dialog));       
1224                 if (list_auth_methods) {
1225                         /* Use the first supported method.
1226                          * TODO: Should we prioritize them, to prefer a particular one? */
1227                         protocol_authentication_incoming = 
1228                                 (ModestAuthProtocol)(GPOINTER_TO_INT(list_auth_methods->data));
1229                         g_list_free (list_auth_methods);
1230                 }
1231                 else
1232                         g_warning ("%s: get_supported_secure_authentication_methods() returned NULL.\n", __FUNCTION__);
1233                 
1234         }
1235         
1236         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1237         
1238         
1239         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1240                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1241         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1242         
1243         
1244                 
1245         g_free (incoming_account_name);
1246         
1247         /* Outgoing: */
1248         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1249                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1250         g_assert (outgoing_account_name);
1251         
1252         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1253         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1254                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1255         if (!test)
1256                 return FALSE;
1257                 
1258         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1259         modest_server_account_set_username (dialog->account_manager, outgoing_account_name,
1260                 username);
1261                 
1262         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1263         modest_server_account_set_password (dialog->account_manager, outgoing_account_name,
1264                 password);
1265         
1266         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1267                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1268         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1269         
1270         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1271                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1272         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1273         
1274         /* port: */
1275         port_num = hildon_number_editor_get_value (
1276                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1277         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1278                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1279                         
1280         g_free (outgoing_account_name);
1281         
1282         
1283         /* Set the changed account title last, to simplify the previous code: */
1284         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1285         if ((!account_title) || (strlen(account_title) == 0))
1286                 return FALSE; /* Should be prevented already anyway. */
1287                 
1288         if (strcmp(account_title, account_name) != 0) {
1289                 /* Change the title: */
1290                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1291                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1292                 if (!test)
1293                         return FALSE;
1294         }
1295         
1296         /* Save connection-specific SMTP server accounts: */
1297         if (dialog->specific_window)
1298                 return modest_connection_specific_smtp_window_save_server_accounts (
1299                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1300         else
1301                 return TRUE;
1302 }
1303
1304 static gboolean entry_is_empty (GtkWidget *entry)
1305 {
1306         if (!entry)
1307                 return FALSE;
1308                 
1309         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1310         if ((!text) || (strlen(text) == 0))
1311                 return TRUE;
1312         else
1313                 return FALSE;
1314 }
1315
1316 static void
1317 enable_buttons (ModestAccountSettingsDialog *self)
1318 {
1319         gboolean enable_ok = TRUE;
1320         
1321         /* The account details title is mandatory: */
1322         if (entry_is_empty(self->entry_account_title))
1323                         enable_ok = FALSE;
1324
1325         /* The user details username is mandatory: */
1326         if (entry_is_empty(self->entry_user_username))
1327                 enable_ok = FALSE;
1328                 
1329         /* The user details email address is mandatory: */
1330         if (enable_ok && entry_is_empty (self->entry_user_email))
1331                 enable_ok = FALSE;
1332
1333         /* The custom incoming server is mandatory: */
1334         if (entry_is_empty(self->entry_incomingserver))
1335                 enable_ok = FALSE;
1336                         
1337         /* Enable the buttons, 
1338          * identifying them via their associated response codes:
1339          */
1340         GtkDialog *dialog_base = GTK_DIALOG (self);
1341     gtk_dialog_set_response_sensitive (dialog_base,
1342                                        GTK_RESPONSE_OK,
1343                                        enable_ok);
1344 }
1345
1346 static void
1347 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1348 {
1349         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1350         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1351
1352
1353         object_class->get_property = modest_account_settings_dialog_get_property;
1354         object_class->set_property = modest_account_settings_dialog_set_property;
1355         object_class->dispose = modest_account_settings_dialog_dispose;
1356         object_class->finalize = modest_account_settings_dialog_finalize;
1357 }
1358  
1359 static void
1360 show_error (GtkWindow *parent_window, const gchar* text)
1361 {
1362         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1363         /*
1364         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1365                 (GtkDialogFlags)0,
1366                  GTK_MESSAGE_ERROR,
1367                  GTK_BUTTONS_OK,
1368                  text ));
1369         */       
1370         
1371         gtk_dialog_run (dialog);
1372         gtk_widget_destroy (GTK_WIDGET (dialog));
1373 }
1374
1375 static void
1376 show_ok (GtkWindow *parent_window, const gchar* text)
1377 {
1378         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1379         /*
1380         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1381                 (GtkDialogFlags)0,
1382                  GTK_MESSAGE_INFO,
1383                  GTK_BUTTONS_OK,
1384                  text ));
1385         */
1386                  
1387         gtk_dialog_run (dialog);
1388         gtk_widget_destroy (GTK_WIDGET (dialog));
1389 }
1390
1391 /* TODO: Enable this when tinymail has the API: */
1392 #if 0
1393 typedef struct 
1394 {
1395         gboolean finished;
1396         GList *result;
1397 } ModestGetSupportedAuthInfo;
1398
1399 static void on_camel_account_get_supported_secure_authentication_status (
1400         GObject *self, TnyStatus *status, gpointer user_data)
1401 {
1402         printf ("DEBUG: %s.\n", __FUNCTION__);
1403 }
1404
1405 static void
1406 on_camel_account_get_supported_secure_authentication (
1407   TnyCamelAccount *self, gboolean cancelled,
1408   GList *auth_types, GError **err, 
1409   gpointer user_data)
1410 {
1411         printf ("DEBUG: %s.\n", __FUNCTION__);
1412                 
1413         ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
1414         g_return_if_fail (info);
1415         
1416         if (!auth_types) {
1417                 printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
1418                 info->finished = TRUE; /* We are blocking, waiting for this. */
1419                 return;
1420         }
1421                 
1422         ModestPairList* pairs = modest_protocol_info_get_protocol_auth_pair_list ();
1423
1424         /* Get the enum value for the strings: */
1425         GList *result = NULL;
1426         GList* iter = auth_types;
1427         while (iter) {
1428                 const gchar *auth_name = (const gchar*)iter->data;
1429                 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
1430                 ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs, 
1431                         auth_name);
1432                 if (matching)
1433                         g_list_append (result, GINT_TO_POINTER((ModestConnectionProtocol)matching->second));
1434                                 
1435                 iter = g_list_next (iter);      
1436         }
1437         
1438         g_list_free (auth_types);
1439         
1440         modest_pair_list_free (pairs);
1441         
1442         info->result = result;
1443         info->finished = TRUE; /* We are blocking, waiting for this. */
1444 }
1445 #endif
1446
1447
1448 static GList* get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
1449         const gchar* hostname, gint port, GtkWindow *parent_window)
1450 {
1451         return NULL;
1452         
1453 /* TODO: Enable this when tinymail has the API: */
1454 #if 0
1455         g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
1456         
1457         /*
1458         result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
1459         */
1460         
1461         /* Create a TnyCamelAccount so we can use 
1462          * tny_camel_account_get_supported_secure_authentication(): */
1463         TnyAccount * tny_account = NULL;
1464         switch (proto) {
1465         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
1466         case MODEST_PROTOCOL_TRANSPORT_SMTP:
1467                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
1468         case MODEST_PROTOCOL_STORE_POP:
1469                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
1470         case MODEST_PROTOCOL_STORE_IMAP:
1471                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
1472         case MODEST_PROTOCOL_STORE_MAILDIR:
1473         case MODEST_PROTOCOL_STORE_MBOX:
1474                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
1475         default:
1476                 tny_account = NULL;
1477         }
1478         
1479         if (!tny_account) {
1480                 g_printerr ("%s could not create tny account.", __FUNCTION__);
1481                 return NULL;
1482         }
1483         
1484         /* Set proto, so that the prepare_func() vfunc will work when we call 
1485          * set_session(): */
1486          /* TODO: Why isn't this done in account_new()? */
1487         tny_account_set_proto (tny_account,
1488                                modest_protocol_info_get_transport_store_protocol_name(proto));
1489                                
1490         /* Set the session for the account, so we can use it: */
1491         ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
1492         TnySessionCamel *session = 
1493                 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
1494         g_return_val_if_fail (session, NULL);
1495         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
1496         
1497         tny_account_set_hostname (tny_account, hostname);
1498         
1499         if(port > 0)
1500                 tny_account_set_port (tny_account, port);
1501                 
1502
1503         /* Ask camel to ask the server, asynchronously: */
1504         ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
1505         info->finished = FALSE;
1506         info->result = NULL;
1507         
1508         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, 
1509                 _("Asking the server for supported secure authentication mechanisms.")));
1510         gtk_dialog_run (dialog);
1511         
1512         printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
1513         tny_camel_account_get_supported_secure_authentication (
1514                 TNY_CAMEL_ACCOUNT (tny_account),
1515                 on_camel_account_get_supported_secure_authentication,
1516                 on_camel_account_get_supported_secure_authentication_status,
1517                 info);
1518                 
1519         printf ("DEBUG: %s: AFTER STARTING.\n", __FUNCTION__);
1520                 
1521         /* Block until the callback has been called,
1522          * driving the main context, so that the (idle handler) callback can be 
1523          * called, and so that our dialog is clickable: */
1524         while (!(info->finished)) {
1525                 printf ("DEBUG: %s: finished is FALSE.\n", __FUNCTION__);
1526                 if (g_main_context_pending (NULL)) {
1527                         printf ("DEBUG: iterating\n");
1528                         g_main_context_iteration (NULL, FALSE);
1529                         printf ("DEBUG: after iterating\n");
1530                 }
1531         }
1532         
1533         printf ("DEBUG: %s: FINISHED.\n", __FUNCTION__);
1534         
1535         gtk_widget_destroy (GTK_WIDGET (dialog));
1536                 
1537         GList *result = info->result;
1538         g_slice_free (ModestGetSupportedAuthInfo, info);
1539         info = NULL;
1540         
1541         return result;
1542 #endif
1543 }
1544
1545
1546
1547