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         if (!self->caption_leave_messages) {
327                 self->caption_leave_messages = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
328                         self->checkbox_leave_messages, NULL, HILDON_CAPTION_MANDATORY);
329         }
330                         
331         gtk_widget_show (self->checkbox_leave_messages);
332         connect_for_modified (self, self->checkbox_leave_messages);
333         gtk_box_pack_start (GTK_BOX (box), self->caption_leave_messages, FALSE, FALSE, MODEST_MARGIN_HALF);
334         gtk_widget_show (self->caption_leave_messages);
335         
336         gtk_widget_show (GTK_WIDGET (box));
337         
338         return GTK_WIDGET (box);
339 }
340
341 static void
342 on_button_signature (GtkButton *button, gpointer user_data)
343 {
344         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
345         
346         /* Create the window, if necessary: */
347         if (!(self->signature_dialog)) {
348                 self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ());
349         
350                 gboolean use_signature = FALSE;
351                 gchar *signature = modest_account_mgr_get_signature(self->account_manager, self->account_name, 
352                         &use_signature);
353                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
354                 modest_signature_editor_dialog_set_settings (
355                         MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog), 
356                         use_signature, signature, account_title);
357                 g_free (signature);
358                 signature = NULL;
359         }
360
361         /* Show the window: */  
362         gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self));
363         gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE);
364     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog));
365     gtk_widget_hide (self->signature_dialog);
366     if (response != GTK_RESPONSE_OK) {
367         /* Destroy the widget now, and its data: */
368         gtk_widget_destroy (self->signature_dialog);
369         self->signature_dialog = NULL;
370     }
371     else {
372         /* Mark modified, so we use the dialog's data later: */
373         self->modified = TRUE;  
374     }
375 }
376
377 static GtkWidget*
378 create_page_user_details (ModestAccountSettingsDialog *self)
379 {
380         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
381         
382         /* Create a size group to be used by all captions.
383          * Note that HildonCaption does not create a default size group if we do not specify one.
384          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
385         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
386          
387         /* The name widgets: */
388         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
389         /* Auto-capitalization is the default, so let's turn it off: */
390         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
391         /* Set max length as in the UI spec:
392          * The UI spec seems to want us to show a dialog if we hit the maximum. */
393         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
394         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
395                 on_entry_max, self);
396         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
397                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
398         gtk_widget_show (self->entry_user_name);
399         connect_for_modified (self, self->entry_user_name);
400         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
401         gtk_widget_show (caption);
402         
403         /* Prevent the use of some characters in the name, 
404          * as required by our UI specification: */
405         GList *list_prevent = NULL;
406         list_prevent = g_list_append (list_prevent, "<");
407         list_prevent = g_list_append (list_prevent, ">");
408         modest_validating_entry_set_unallowed_characters (
409                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
410         g_list_free (list_prevent);
411         
412         /* The username widgets: */     
413         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
414         /* Auto-capitalization is the default, so let's turn it off: */
415         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
416         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
417                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
418         gtk_widget_show (self->entry_user_username);
419         connect_for_modified (self, self->entry_user_username);
420         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
421         gtk_widget_show (caption);
422         
423         /* Prevent the use of some characters in the username, 
424          * as required by our UI specification: */
425         modest_validating_entry_set_unallowed_characters_whitespace (
426                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
427         
428         /* Set max length as in the UI spec:
429          * The UI spec seems to want us to show a dialog if we hit the maximum. */
430         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
431         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
432                 on_entry_max, self);
433         
434         /* The password widgets: */     
435         self->entry_user_password = gtk_entry_new ();
436         /* Auto-capitalization is the default, so let's turn it off: */
437         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
438                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
439         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
440         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
441         caption = create_caption_new_with_asterix (self, sizegroup, 
442                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
443         gtk_widget_show (self->entry_user_password);
444         connect_for_modified (self, self->entry_user_password);
445         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
446         gtk_widget_show (caption);
447         
448         /* The email address widgets: */        
449         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
450         /* Auto-capitalization is the default, so let's turn it off: */
451         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
452         caption = create_caption_new_with_asterix (self, sizegroup, 
453                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
454         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
455         gtk_widget_show (self->entry_user_email);
456         connect_for_modified (self, self->entry_user_email);
457         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
458         gtk_widget_show (caption);
459         
460         /* Set max length as in the UI spec:
461          * The UI spec seems to want us to show a dialog if we hit the maximum. */
462         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
463         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
464                 on_entry_max, self);
465         
466         /* Signature button: */
467         if (!self->button_signature)
468                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
469         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
470                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
471         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
472         gtk_widget_show (self->button_signature);
473         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
474         gtk_widget_show (caption);
475                 
476         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
477                 G_CALLBACK (on_button_signature), self);
478                 
479         gtk_widget_show (GTK_WIDGET (box));
480         
481         return GTK_WIDGET (box);
482 }
483
484 /** Change the caption title for the incoming server, 
485  * as specified in the UI spec:
486  */
487 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
488 {
489         const gchar* type = 
490                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
491                         _("mail_fi_emailtype_pop3") : 
492                         _("mail_fi_emailtype_imap") );
493                         
494                 
495         /* Note that this produces a compiler warning, 
496          * because the compiler does not know that the translated string will have a %s in it.
497          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
498         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
499         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
500         g_free(incomingserver_title);
501 }
502
503 /** Change the caption title for the incoming server, 
504  * as specified in the UI spec:
505  */
506 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
507 {
508         /* Fill the combo with appropriately titled choices for POP or IMAP. */
509         /* The choices are the same, but the titles are different, as in the UI spec. */
510         modest_serversecurity_combo_box_fill (
511                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
512 }
513            
514 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
515 {
516         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
517         
518         /* Create a size group to be used by all captions.
519          * Note that HildonCaption does not create a default size group if we do not specify one.
520          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
521         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
522          
523         /* The incoming server widgets: */
524         if(!self->entry_incomingserver)
525                 self->entry_incomingserver = gtk_entry_new ();
526         /* Auto-capitalization is the default, so let's turn it off: */
527         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
528
529         if (self->caption_incoming)
530           gtk_widget_destroy (self->caption_incoming);
531            
532         /* The caption title will be updated in update_incoming_server_title().
533          * so this default text will never be seen: */
534         /* (Note: Changing the title seems pointless. murrayc) */
535         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
536                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
537         gtk_widget_show (self->entry_incomingserver);
538         connect_for_modified (self, self->entry_incomingserver);
539         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
540         gtk_widget_show (self->caption_incoming);
541         
542         /* The secure connection widgets: */
543         /* This will be filled by update_incoming_server_security_choices(). */
544         if (!self->combo_incoming_security)
545                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
546         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
547                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
548         gtk_widget_show (self->combo_incoming_security);
549         connect_for_modified (self, self->combo_incoming_security);
550         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
551         gtk_widget_show (caption);
552         
553         /* Show a default port number when the security method changes, as per the UI spec: */
554         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
555         
556         
557         /* The port widgets: */
558         /* TODO: There are various rules about this in the UI spec. */
559         if (!self->entry_incoming_port)
560                 self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
561         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
562                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
563         gtk_widget_show (self->entry_incoming_port);
564         connect_for_modified (self, self->entry_incoming_port);
565         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
566         gtk_widget_show (caption);
567         
568         /* The secure authentication widgets: */
569         if(!self->checkbox_incoming_auth)
570                 self->checkbox_incoming_auth = gtk_check_button_new ();
571         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
572                 self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
573         gtk_widget_show (self->checkbox_incoming_auth);
574         connect_for_modified (self, self->checkbox_incoming_auth);
575         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
576         gtk_widget_show (caption);
577         
578         gtk_widget_show (GTK_WIDGET (box));
579         
580         return GTK_WIDGET (box);
581 }
582
583 static void
584 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
585 {
586         GtkWidget *widget = GTK_WIDGET (user_data);
587         
588         /* Enable the widget only if the toggle button is active: */
589         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
590         gtk_widget_set_sensitive (widget, enable);
591 }
592
593 /* Make the sensitivity of a widget depend on a toggle button.
594  */
595 static void
596 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
597 {
598         g_signal_connect (G_OBJECT (button), "toggled",
599                 G_CALLBACK (on_toggle_button_changed), widget);
600         
601         /* Set the starting sensitivity: */
602         on_toggle_button_changed (button, widget);
603 }
604
605 static void
606 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
607 {
608         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
609         
610         /* Create the window if necessary: */
611         if (!(self->specific_window)) {
612                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
613                 modest_connection_specific_smtp_window_fill_with_connections (
614                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
615                         self->account_name);
616         }
617
618         /* Show the window: */  
619         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
620         gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE);
621     gtk_widget_show (self->specific_window);
622 }
623
624 static void
625 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
626 {
627         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
628         
629         ModestAuthProtocol protocol_security = 
630                 modest_secureauth_combo_box_get_active_secureauth (
631                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
632         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
633         
634         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
635         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
636 }
637
638 static void
639 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
640 {
641         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
642         
643         const gint port_number = 
644                 modest_serversecurity_combo_box_get_active_serversecurity_port (
645                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
646
647         if(port_number != 0) {
648                 hildon_number_editor_set_value (
649                         HILDON_NUMBER_EDITOR (self->entry_outgoing_port), port_number);
650         }               
651 }
652
653 static void
654 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
655 {
656         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
657         
658         const gint port_number = 
659                 modest_serversecurity_combo_box_get_active_serversecurity_port (
660                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
661
662         if(port_number != 0) {
663                 hildon_number_editor_set_value (
664                         HILDON_NUMBER_EDITOR (self->entry_incoming_port), port_number);
665         }               
666 }
667
668
669 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
670 {
671         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
672         
673         /* Create a size group to be used by all captions.
674          * Note that HildonCaption does not create a default size group if we do not specify one.
675          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
676         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
677          
678         /* The outgoing server widgets: */
679         if (!self->entry_outgoingserver)
680                 self->entry_outgoingserver = gtk_entry_new ();
681         /* Auto-capitalization is the default, so let's turn it off: */
682         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
683         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
684                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
685         gtk_widget_show (self->entry_outgoingserver);
686         connect_for_modified (self, self->entry_outgoingserver);
687         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
688         gtk_widget_show (caption);
689         
690         /* The secure authentication widgets: */
691         if (!self->combo_outgoing_auth)
692                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
693         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
694                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
695         gtk_widget_show (self->combo_outgoing_auth);
696         connect_for_modified (self, self->combo_outgoing_auth);
697         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
698         gtk_widget_show (caption);
699         
700         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
701         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
702         
703         /* The username widgets: */     
704         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
705         /* Auto-capitalization is the default, so let's turn it off: */
706         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_username), HILDON_GTK_INPUT_MODE_FULL);
707         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
708                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
709         gtk_widget_show (self->entry_outgoing_username);
710         connect_for_modified (self, self->entry_outgoing_username);
711         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
712         gtk_widget_show (self->caption_outgoing_username);
713         
714         /* Prevent the use of some characters in the username, 
715          * as required by our UI specification: */
716         modest_validating_entry_set_unallowed_characters_whitespace (
717                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
718         
719         /* Set max length as in the UI spec:
720          * The UI spec seems to want us to show a dialog if we hit the maximum. */
721         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
722         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
723                 on_entry_max, self);
724                 
725         /* The password widgets: */     
726         self->entry_outgoing_password = gtk_entry_new ();
727         /* Auto-capitalization is the default, so let's turn it off: */
728         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_password), 
729                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
730         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
731         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
732         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
733                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
734         gtk_widget_show (self->entry_outgoing_password);
735         connect_for_modified (self, self->entry_outgoing_password);
736         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
737         gtk_widget_show (self->caption_outgoing_password);
738         
739         /* The secure connection widgets: */
740         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
741          * and modest_serversecurity_combo_box_set_active_serversecurity().
742          */
743         if (!self->combo_outgoing_security)
744                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
745         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
746                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
747         gtk_widget_show (self->combo_outgoing_security);
748         connect_for_modified (self, self->combo_outgoing_security);
749         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
750         gtk_widget_show (caption);
751         
752         /* Show a default port number when the security method changes, as per the UI spec: */
753         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
754         
755         /* The port widgets: */
756         if (!self->entry_outgoing_port)
757                 self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
758         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
759                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
760         gtk_widget_show (self->entry_outgoing_port);
761         connect_for_modified (self, self->entry_outgoing_port);
762         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
763         gtk_widget_show (caption);
764         
765         GtkWidget *separator = gtk_hseparator_new ();
766         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
767         gtk_widget_show (separator);
768         
769         /* connection-specific checkbox: */
770         if (!self->checkbox_outgoing_smtp_specific) {
771                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
772                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
773                         FALSE);
774         }
775         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
776                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
777         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
778         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
779         gtk_widget_show (caption);
780         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
781         
782         /* Connection-specific SMTP-Severs Edit button: */
783         if (!self->button_outgoing_smtp_servers)
784                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
785         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
786                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
787         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
788         gtk_widget_show (self->button_outgoing_smtp_servers);
789         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
790         gtk_widget_show (caption);
791         
792         /* Only enable the button when the checkbox is checked: */
793         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
794                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
795                 
796         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
797                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
798                 
799         gtk_widget_show (GTK_WIDGET (box));
800         
801         return GTK_WIDGET (box);
802 }
803
804 static gboolean
805 check_data (ModestAccountSettingsDialog *self)
806 {
807         /* Check that the title is not already in use: */
808         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
809         if ((!account_title) || (strlen(account_title) == 0))
810                 return FALSE; /* Should be prevented already anyway. */
811                 
812         if (strcmp(account_title, self->original_account_title) != 0) {
813                 /* Check the changed title: */
814                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
815                         account_title);
816         
817                 if (name_in_use) {
818                         /* Warn the user via a dialog: */
819                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
820                 
821                         return FALSE;
822                 }
823         }
824
825         /* Check that the email address is valud: */
826         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
827         if ((!email_address) || (strlen(email_address) == 0))
828                 return FALSE;
829                         
830         if (!modest_text_utils_validate_email_address (email_address)) {
831                 /* Warn the user via a dialog: */
832                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
833                                          
834         /* Return focus to the email address entry: */
835         gtk_widget_grab_focus (self->entry_user_email);
836         
837                 return FALSE;
838         }
839         
840         /* TODO: The UI Spec wants us to check that the servernames are valid, 
841          * but does not specify how.
842          */
843          
844         return TRUE;
845 }
846 /*
847  */
848 static void 
849 on_response (GtkDialog *wizard_dialog,
850         gint response_id,
851         gpointer user_data)
852 {
853         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
854         enable_buttons (self);
855         
856         gboolean prevent_response = FALSE;
857         
858         /* Warn about unsaved changes: */
859         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
860                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
861                         _("imum_nc_wizard_confirm_lose_changes")));
862                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
863                  
864                  const gint dialog_response = gtk_dialog_run (dialog);
865                  gtk_widget_destroy (GTK_WIDGET (dialog));
866                  
867                 if (dialog_response != GTK_RESPONSE_OK)
868                         prevent_response = TRUE;
869         }
870         /* Check for invalid input: */
871         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
872                 prevent_response = TRUE;
873         }
874                 
875         if (prevent_response) {
876                 /* This is a nasty hack. murrayc. */
877                 /* Don't let the dialog close */
878         g_signal_stop_emission_by_name (wizard_dialog, "response");
879                 return; 
880         }
881                 
882         if (response_id == GTK_RESPONSE_OK) {
883                 /* Try to save the changes: */  
884                 const gboolean saved = save_configuration (self);
885                 if (saved) {
886                         /* Do not show the account-saved dialog if we are just saving this 
887                          * temporarily, because from the user's point of view it will not 
888                          * really be saved (saved + enabled) until later.
889                          */
890                         const gboolean enabled = 
891                                 modest_account_mgr_get_enabled (self->account_manager, self->account_name);
892                         if (enabled)
893                                 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
894                 }
895                 else
896                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
897         }
898 }
899
900 static void
901 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
902 {
903         /* Create the notebook to be used by the GtkDialog base class:
904          * Each page of the notebook will be a page of the wizard: */
905         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
906
907     
908     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
909         
910         /* Get the account manager object, 
911          * so we can check for existing accounts,
912          * and create new accounts: */
913         self->account_manager = modest_runtime_get_account_mgr ();
914         g_assert (self->account_manager);
915         g_object_ref (self->account_manager);
916         
917     /* Create the common pages, 
918      */
919         self->page_account_details = create_page_account_details (self);
920         self->page_user_details = create_page_user_details (self);
921         self->page_incoming = create_page_incoming (self);
922         self->page_outgoing = create_page_outgoing (self);
923         
924         /* Add the notebook pages: */
925         gtk_notebook_append_page (notebook, self->page_account_details, 
926                 gtk_label_new (_("mcen_ti_account_settings_account")));
927         gtk_notebook_append_page (notebook, self->page_user_details, 
928                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
929         gtk_notebook_append_page (notebook, self->page_incoming,
930                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
931         gtk_notebook_append_page (notebook, self->page_outgoing,
932                 gtk_label_new (_("mcen_ti_advsetup_sending")));
933                 
934         GtkDialog *dialog = GTK_DIALOG (self);
935         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
936         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
937         gtk_widget_show (GTK_WIDGET (notebook));
938         
939     /* Add the buttons: */
940     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
941     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
942     
943     /* Connect to the dialog's response signal: */
944     /* We use connect-before 
945      * so we can stop the signal emission, 
946      * to stop the default signal handler from closing the dialog.
947      */
948     g_signal_connect (G_OBJECT (self), "response",
949             G_CALLBACK (on_response), self); 
950             
951     self->modified = FALSE;
952     
953     /* When this window is shown, hibernation should not be possible, 
954          * because there is no sensible way to save the state: */
955     modest_window_mgr_prevent_hibernation_while_window_is_shown (
956         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
957 }
958
959 ModestAccountSettingsDialog*
960 modest_account_settings_dialog_new (void)
961 {
962         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
963 }
964
965 /** Update the UI with the stored account details, so they can be edited.
966  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
967  */
968 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
969 {
970         if (!account_name)
971                 return;
972                 
973         /* Save the account name so we can refer to it later: */
974         if (dialog->account_name)
975                 g_free (dialog->account_name);
976         dialog->account_name = g_strdup (account_name);
977         
978                 
979         /* Get the account data for this account name: */
980         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
981                 account_name);
982         if (!account_data) {
983                 g_printerr ("modest: failed to get account data for %s\n", account_name);
984                 return;
985         }
986         
987         /* Save the account title so we can refer to it if the user changes it: */
988         if (dialog->original_account_title)
989                 g_free (dialog->original_account_title);
990         dialog->original_account_title = g_strdup (account_data->display_name);
991         
992
993         if (!(account_data->store_account)) {
994                 g_printerr ("modest: account has no stores: %s\n", account_name);
995                 return;
996         }
997                 
998         /* Show the account data in the widgets: */
999         
1000         /* Note that we never show the non-display name in the UI.
1001          * (Though the display name defaults to the non-display name at the start.) */
1002         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1003                 account_data->display_name ? account_data->display_name : "");
1004                 
1005         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1006                 account_data->fullname ? account_data->fullname : "");
1007         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1008                 account_data->email ? account_data->email : "");
1009                 
1010         ModestServerAccountData *incoming_account = account_data->store_account;
1011                 
1012         if (incoming_account)
1013                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
1014         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
1015                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
1016         if (!retrieve) {
1017                 /* Default to something, though no default is specified in the UI spec: */
1018                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
1019         }
1020         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
1021         g_free (retrieve);
1022         
1023         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
1024                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
1025         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
1026         
1027         
1028         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
1029                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
1030         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
1031         
1032         /* Only show the leave-on-server checkbox for POP, 
1033          * as per the UI spec: */
1034         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
1035                 gtk_widget_hide (dialog->caption_leave_messages);
1036         } else {
1037                 gtk_widget_show (dialog->caption_leave_messages);
1038         }
1039                 
1040         if (incoming_account) {
1041                 /* Remember this for later: */
1042                 dialog->incoming_protocol = incoming_account->proto;
1043                 
1044                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1045                         incoming_account->username ? incoming_account->username : "");
1046                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1047                         incoming_account->password ? incoming_account->password : "");
1048                         
1049                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1050                         incoming_account->hostname ? incoming_account->hostname : "");
1051                         
1052                 /* The UI spec says:
1053                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1054          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1055                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1056          */
1057                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1058                         dialog->account_manager, incoming_account->account_name);
1059                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1060                         secure_auth != MODEST_PROTOCOL_AUTH_PASSWORD);
1061                 /* Note that MODEST_PROTOCOL_AUTH_PLAIN should probably never be used. */
1062                         
1063                 update_incoming_server_title (dialog, incoming_account->proto);
1064                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1065                 
1066                 const ModestConnectionProtocol security = modest_server_account_get_security (
1067                         dialog->account_manager, incoming_account->account_name);
1068                 modest_serversecurity_combo_box_set_active_serversecurity (
1069                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1070                 
1071                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1072                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1073                         
1074                 if (port_num == 0) {
1075                         /* Show the appropriate port number: */
1076                         on_combo_incoming_security_changed (
1077                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1078                 } else {
1079                         /* Keep the user-entered port-number,
1080                          * or the already-appropriate automatic port number: */
1081                         hildon_number_editor_set_value (
1082                                 HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1083                 }
1084         }
1085         
1086         ModestServerAccountData *outgoing_account = account_data->transport_account;
1087         if (outgoing_account) {
1088                 /* Remember this for later: */
1089                 dialog->outgoing_protocol = outgoing_account->proto;
1090                 
1091                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1092                         outgoing_account->hostname ? outgoing_account->hostname : "");
1093                 
1094                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1095                         outgoing_account->username ? outgoing_account->username : "");
1096                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1097                         outgoing_account->password ? outgoing_account->password : "");
1098                 
1099                 /* Get the secure-auth setting: */
1100                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1101                         dialog->account_manager, outgoing_account->account_name);
1102                 modest_secureauth_combo_box_set_active_secureauth (
1103                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1104                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1105                 
1106                 modest_serversecurity_combo_box_fill (
1107                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1108                 
1109                 /* Get the security setting: */
1110                 const ModestConnectionProtocol security = modest_server_account_get_security (
1111                         dialog->account_manager, outgoing_account->account_name);
1112                 modest_serversecurity_combo_box_set_active_serversecurity (
1113                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1114                 
1115                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1116                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1117                 if (port_num == 0) {
1118                         /* Show the appropriate port number: */
1119                         on_combo_outgoing_security_changed (
1120                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1121                 }
1122                 else {
1123                         /* Keep the user-entered port-number,
1124                          * or the already-appropriate automatic port number: */
1125                         hildon_number_editor_set_value (
1126                                 HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1127                 }
1128         }
1129         
1130         /* account_data->is_enabled,  */
1131         /*account_data->is_default,  */
1132
1133         /* account_data->store_account->proto */
1134
1135         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1136         
1137         /* Unset the modified flag so we can detect changes later: */
1138         dialog->modified = FALSE;
1139 }
1140
1141 static GList* get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
1142         const gchar* hostname, gint port, GtkWindow *parent_window);
1143
1144 static gboolean
1145 save_configuration (ModestAccountSettingsDialog *dialog)
1146 {
1147         g_assert (dialog->account_name);
1148         
1149         const gchar* account_name = dialog->account_name;
1150                 
1151         /* Set the account data from the widgets: */
1152         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1153         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1154                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1155         if (!test)
1156                 return FALSE;
1157                 
1158         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1159         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1160                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1161         if (!test)
1162                 return FALSE;
1163                 
1164         /* Signature: */
1165         if (dialog->signature_dialog) {
1166                 gboolean use_signature = FALSE;
1167         gchar *signature = modest_signature_editor_dialog_get_settings (
1168                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1169                 &use_signature);
1170         
1171         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1172                 signature, use_signature);
1173         g_free (signature);
1174     }
1175         
1176         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1177                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1178         modest_account_mgr_set_string (dialog->account_manager, account_name,
1179                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1180         g_free (retrieve);
1181         
1182         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1183                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1184         modest_account_mgr_set_int (dialog->account_manager, account_name,
1185                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1186         
1187         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1188         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1189                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1190         if (!test)
1191                 return FALSE;
1192                         
1193         /* Incoming: */
1194         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1195                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1196         g_assert (incoming_account_name);
1197         
1198         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1199         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1200                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1201         if (!test)
1202                 return FALSE;
1203                                 
1204         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1205         modest_server_account_set_username (dialog->account_manager, incoming_account_name, username);
1206         
1207         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1208         modest_server_account_set_password (dialog->account_manager, incoming_account_name, password);
1209                         
1210         /* port: */
1211         gint port_num = hildon_number_editor_get_value (
1212                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1213         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1214                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1215                         
1216         /* The UI spec says:
1217          * If secure authentication is unchecked, allow sending username and password also as plain text.
1218          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1219          */
1220         ModestAuthProtocol protocol_authentication_incoming = 
1221                 MODEST_PROTOCOL_AUTH_PASSWORD;
1222         if (gtk_toggle_button_get_active (
1223                         GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))) {
1224                 GList *list_auth_methods = 
1225                         get_supported_secure_authentication_methods (dialog->incoming_protocol, 
1226                                 hostname, port_num, GTK_WINDOW (dialog));       
1227                 if (list_auth_methods) {
1228                         /* Use the first supported method.
1229                          * TODO: Should we prioritize them, to prefer a particular one? */
1230                         protocol_authentication_incoming = 
1231                                 (ModestAuthProtocol)(GPOINTER_TO_INT(list_auth_methods->data));
1232                         g_list_free (list_auth_methods);
1233                 }
1234                 else
1235                         g_warning ("%s: get_supported_secure_authentication_methods() returned NULL.\n", __FUNCTION__);
1236                 
1237         }
1238         
1239         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1240         
1241         
1242         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1243                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1244         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1245         
1246         
1247                 
1248         g_free (incoming_account_name);
1249         
1250         /* Outgoing: */
1251         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1252                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1253         g_assert (outgoing_account_name);
1254         
1255         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1256         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1257                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1258         if (!test)
1259                 return FALSE;
1260                 
1261         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1262         modest_server_account_set_username (dialog->account_manager, outgoing_account_name,
1263                 username);
1264                 
1265         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1266         modest_server_account_set_password (dialog->account_manager, outgoing_account_name,
1267                 password);
1268         
1269         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1270                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1271         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1272         
1273         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1274                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1275         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1276         
1277         /* port: */
1278         port_num = hildon_number_editor_get_value (
1279                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1280         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1281                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1282                         
1283         g_free (outgoing_account_name);
1284         
1285         
1286         /* Set the changed account title last, to simplify the previous code: */
1287         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1288         if ((!account_title) || (strlen(account_title) == 0))
1289                 return FALSE; /* Should be prevented already anyway. */
1290                 
1291         if (strcmp(account_title, account_name) != 0) {
1292                 /* Change the title: */
1293                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1294                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1295                 if (!test)
1296                         return FALSE;
1297         }
1298         
1299         /* Save connection-specific SMTP server accounts: */
1300         if (dialog->specific_window)
1301                 return modest_connection_specific_smtp_window_save_server_accounts (
1302                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1303         else
1304                 return TRUE;
1305 }
1306
1307 static gboolean entry_is_empty (GtkWidget *entry)
1308 {
1309         if (!entry)
1310                 return FALSE;
1311                 
1312         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1313         if ((!text) || (strlen(text) == 0))
1314                 return TRUE;
1315         else
1316                 return FALSE;
1317 }
1318
1319 static void
1320 enable_buttons (ModestAccountSettingsDialog *self)
1321 {
1322         gboolean enable_ok = TRUE;
1323         
1324         /* The account details title is mandatory: */
1325         if (entry_is_empty(self->entry_account_title))
1326                         enable_ok = FALSE;
1327
1328         /* The user details username is mandatory: */
1329         if (entry_is_empty(self->entry_user_username))
1330                 enable_ok = FALSE;
1331                 
1332         /* The user details email address is mandatory: */
1333         if (enable_ok && entry_is_empty (self->entry_user_email))
1334                 enable_ok = FALSE;
1335
1336         /* The custom incoming server is mandatory: */
1337         if (entry_is_empty(self->entry_incomingserver))
1338                 enable_ok = FALSE;
1339                         
1340         /* Enable the buttons, 
1341          * identifying them via their associated response codes:
1342          */
1343         GtkDialog *dialog_base = GTK_DIALOG (self);
1344     gtk_dialog_set_response_sensitive (dialog_base,
1345                                        GTK_RESPONSE_OK,
1346                                        enable_ok);
1347 }
1348
1349 static void
1350 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1351 {
1352         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1353         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1354
1355
1356         object_class->get_property = modest_account_settings_dialog_get_property;
1357         object_class->set_property = modest_account_settings_dialog_set_property;
1358         object_class->dispose = modest_account_settings_dialog_dispose;
1359         object_class->finalize = modest_account_settings_dialog_finalize;
1360 }
1361  
1362 static void
1363 show_error (GtkWindow *parent_window, const gchar* text)
1364 {
1365         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1366         /*
1367         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1368                 (GtkDialogFlags)0,
1369                  GTK_MESSAGE_ERROR,
1370                  GTK_BUTTONS_OK,
1371                  text ));
1372         */       
1373         
1374         gtk_dialog_run (dialog);
1375         gtk_widget_destroy (GTK_WIDGET (dialog));
1376 }
1377
1378 static void
1379 show_ok (GtkWindow *parent_window, const gchar* text)
1380 {
1381         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1382         /*
1383         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1384                 (GtkDialogFlags)0,
1385                  GTK_MESSAGE_INFO,
1386                  GTK_BUTTONS_OK,
1387                  text ));
1388         */
1389                  
1390         gtk_dialog_run (dialog);
1391         gtk_widget_destroy (GTK_WIDGET (dialog));
1392 }
1393
1394 /* TODO: Enable this when tinymail has the API: */
1395 #if 0
1396 typedef struct 
1397 {
1398         gboolean finished;
1399         GList *result;
1400 } ModestGetSupportedAuthInfo;
1401
1402 static void on_camel_account_get_supported_secure_authentication_status (
1403         GObject *self, TnyStatus *status, gpointer user_data)
1404 {
1405         printf ("DEBUG: %s.\n", __FUNCTION__);
1406 }
1407
1408 static void
1409 on_camel_account_get_supported_secure_authentication (
1410   TnyCamelAccount *self, gboolean cancelled,
1411   GList *auth_types, GError **err, 
1412   gpointer user_data)
1413 {
1414         printf ("DEBUG: %s.\n", __FUNCTION__);
1415                 
1416         ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
1417         g_return_if_fail (info);
1418         
1419         if (!auth_types) {
1420                 printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
1421                 info->finished = TRUE; /* We are blocking, waiting for this. */
1422                 return;
1423         }
1424                 
1425         ModestPairList* pairs = modest_protocol_info_get_protocol_auth_pair_list ();
1426
1427         /* Get the enum value for the strings: */
1428         GList *result = NULL;
1429         GList* iter = auth_types;
1430         while (iter) {
1431                 const gchar *auth_name = (const gchar*)iter->data;
1432                 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
1433                 ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs, 
1434                         auth_name);
1435                 if (matching)
1436                         g_list_append (result, GINT_TO_POINTER((ModestConnectionProtocol)matching->second));
1437                                 
1438                 iter = g_list_next (iter);      
1439         }
1440         
1441         g_list_free (auth_types);
1442         
1443         modest_pair_list_free (pairs);
1444         
1445         info->result = result;
1446         info->finished = TRUE; /* We are blocking, waiting for this. */
1447 }
1448 #endif
1449
1450
1451 static GList* get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
1452         const gchar* hostname, gint port, GtkWindow *parent_window)
1453 {
1454         return NULL;
1455         
1456 /* TODO: Enable this when tinymail has the API: */
1457 #if 0
1458         g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
1459         
1460         /*
1461         result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
1462         */
1463         
1464         /* Create a TnyCamelAccount so we can use 
1465          * tny_camel_account_get_supported_secure_authentication(): */
1466         TnyAccount * tny_account = NULL;
1467         switch (proto) {
1468         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
1469         case MODEST_PROTOCOL_TRANSPORT_SMTP:
1470                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
1471         case MODEST_PROTOCOL_STORE_POP:
1472                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
1473         case MODEST_PROTOCOL_STORE_IMAP:
1474                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
1475         case MODEST_PROTOCOL_STORE_MAILDIR:
1476         case MODEST_PROTOCOL_STORE_MBOX:
1477                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
1478         default:
1479                 tny_account = NULL;
1480         }
1481         
1482         if (!tny_account) {
1483                 g_printerr ("%s could not create tny account.", __FUNCTION__);
1484                 return NULL;
1485         }
1486         
1487         /* Set proto, so that the prepare_func() vfunc will work when we call 
1488          * set_session(): */
1489          /* TODO: Why isn't this done in account_new()? */
1490         tny_account_set_proto (tny_account,
1491                                modest_protocol_info_get_transport_store_protocol_name(proto));
1492                                
1493         /* Set the session for the account, so we can use it: */
1494         ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
1495         TnySessionCamel *session = 
1496                 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
1497         g_return_val_if_fail (session, NULL);
1498         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
1499         
1500         tny_account_set_hostname (tny_account, hostname);
1501         
1502         if(port > 0)
1503                 tny_account_set_port (tny_account, port);
1504                 
1505
1506         /* Ask camel to ask the server, asynchronously: */
1507         ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
1508         info->finished = FALSE;
1509         info->result = NULL;
1510         
1511         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, 
1512                 _("Asking the server for supported secure authentication mechanisms.")));
1513         gtk_dialog_run (dialog);
1514         
1515         printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
1516         tny_camel_account_get_supported_secure_authentication (
1517                 TNY_CAMEL_ACCOUNT (tny_account),
1518                 on_camel_account_get_supported_secure_authentication,
1519                 on_camel_account_get_supported_secure_authentication_status,
1520                 info);
1521                 
1522         printf ("DEBUG: %s: AFTER STARTING.\n", __FUNCTION__);
1523                 
1524         /* Block until the callback has been called,
1525          * driving the main context, so that the (idle handler) callback can be 
1526          * called, and so that our dialog is clickable: */
1527         while (!(info->finished)) {
1528                 printf ("DEBUG: %s: finished is FALSE.\n", __FUNCTION__);
1529                 if (g_main_context_pending (NULL)) {
1530                         printf ("DEBUG: iterating\n");
1531                         g_main_context_iteration (NULL, FALSE);
1532                         printf ("DEBUG: after iterating\n");
1533                 }
1534         }
1535         
1536         printf ("DEBUG: %s: FINISHED.\n", __FUNCTION__);
1537         
1538         gtk_widget_destroy (GTK_WIDGET (dialog));
1539                 
1540         GList *result = info->result;
1541         g_slice_free (ModestGetSupportedAuthInfo, info);
1542         info = NULL;
1543         
1544         return result;
1545 #endif
1546 }
1547
1548
1549
1550