1f17530ad819eb747f86d92bd251af8d8c07830a
[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, ModestProtocol 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, ModestProtocol 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         
797         gtk_widget_show (GTK_WIDGET (box));
798         
799         return GTK_WIDGET (box);
800 }
801
802 static gboolean
803 check_data (ModestAccountSettingsDialog *self)
804 {
805         /* Check that the title is not already in use: */
806         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
807         if ((!account_title) || (strlen(account_title) == 0))
808                 return FALSE; /* Should be prevented already anyway. */
809                 
810         if (strcmp(account_title, self->original_account_title) != 0) {
811                 /* Check the changed title: */
812                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
813                         account_title);
814         
815                 if (name_in_use) {
816                         /* Warn the user via a dialog: */
817                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
818                 
819                         return FALSE;
820                 }
821         }
822
823         /* Check that the email address is valud: */
824         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
825         if ((!email_address) || (strlen(email_address) == 0))
826                 return FALSE;
827                         
828         if (!modest_text_utils_validate_email_address (email_address)) {
829                 /* Warn the user via a dialog: */
830                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
831                                          
832         /* Return focus to the email address entry: */
833         gtk_widget_grab_focus (self->entry_user_email);
834         
835                 return FALSE;
836         }
837         
838         /* TODO: The UI Spec wants us to check that the servernames are valid, 
839          * but does not specify how.
840          */
841          
842         return TRUE;
843 }
844 /*
845  */
846 static void 
847 on_response (GtkDialog *wizard_dialog,
848         gint response_id,
849         gpointer user_data)
850 {
851         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
852         enable_buttons (self);
853         
854         gboolean prevent_response = FALSE;
855         
856         /* Warn about unsaved changes: */
857         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
858                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
859                         _("imum_nc_wizard_confirm_lose_changes")));
860                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
861                  
862                  const gint dialog_response = gtk_dialog_run (dialog);
863                  gtk_widget_destroy (GTK_WIDGET (dialog));
864                  
865                 if (dialog_response != GTK_RESPONSE_OK)
866                         prevent_response = TRUE;
867         }
868         /* Check for invalid input: */
869         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
870                 prevent_response = TRUE;
871         }
872                 
873         if (prevent_response) {
874                 /* This is a nasty hack. murrayc. */
875                 /* Don't let the dialog close */
876         g_signal_stop_emission_by_name (wizard_dialog, "response");
877                 return; 
878         }
879                 
880         if (response_id == GTK_RESPONSE_OK) {
881                 /* Try to save the changes: */  
882                 const gboolean saved = save_configuration (self);
883                 if (saved) {
884                         /* Do not show the account-saved dialog if we are just saving this 
885                          * temporarily, because from the user's point of view it will not 
886                          * really be saved (saved + enabled) until later.
887                          */
888                         const gboolean enabled = 
889                                 modest_account_mgr_get_enabled (self->account_manager, self->account_name);
890                         if (enabled)
891                                 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
892                 }
893                 else
894                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
895         }
896 }
897
898 static void
899 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
900 {
901         /* Create the notebook to be used by the GtkDialog base class:
902          * Each page of the notebook will be a page of the wizard: */
903         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
904
905     
906     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
907         
908         /* Get the account manager object, 
909          * so we can check for existing accounts,
910          * and create new accounts: */
911         self->account_manager = modest_runtime_get_account_mgr ();
912         g_assert (self->account_manager);
913         g_object_ref (self->account_manager);
914         
915     /* Create the common pages, 
916      */
917         self->page_account_details = create_page_account_details (self);
918         self->page_user_details = create_page_user_details (self);
919         self->page_incoming = create_page_incoming (self);
920         self->page_outgoing = create_page_outgoing (self);
921         
922         /* Add the notebook pages: */
923         gtk_notebook_append_page (notebook, self->page_account_details, 
924                 gtk_label_new (_("mcen_ti_account_settings_account")));
925         gtk_notebook_append_page (notebook, self->page_user_details, 
926                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
927         gtk_notebook_append_page (notebook, self->page_incoming,
928                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
929         gtk_notebook_append_page (notebook, self->page_outgoing,
930                 gtk_label_new (_("mcen_ti_advsetup_sending")));
931                 
932         GtkDialog *dialog = GTK_DIALOG (self);
933         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
934         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
935         gtk_widget_show (GTK_WIDGET (notebook));
936         
937     /* Add the buttons: */
938     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
939     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
940     
941     /* Connect to the dialog's response signal: */
942     /* We use connect-before 
943      * so we can stop the signal emission, 
944      * to stop the default signal handler from closing the dialog.
945      */
946     g_signal_connect (G_OBJECT (self), "response",
947             G_CALLBACK (on_response), self); 
948             
949     self->modified = FALSE;
950     
951     /* When this window is shown, hibernation should not be possible, 
952          * because there is no sensible way to save the state: */
953     modest_window_mgr_prevent_hibernation_while_window_is_shown (
954         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
955 }
956
957 ModestAccountSettingsDialog*
958 modest_account_settings_dialog_new (void)
959 {
960         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
961 }
962
963 /** Update the UI with the stored account details, so they can be edited.
964  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
965  */
966 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
967 {
968         if (!account_name)
969                 return;
970                 
971         /* Save the account name so we can refer to it later: */
972         if (dialog->account_name)
973                 g_free (dialog->account_name);
974         dialog->account_name = g_strdup (account_name);
975         
976                 
977         /* Get the account data for this account name: */
978         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
979                 account_name);
980         if (!account_data) {
981                 g_printerr ("modest: failed to get account data for %s\n", account_name);
982                 return;
983         }
984         
985         /* Save the account title so we can refer to it if the user changes it: */
986         if (dialog->original_account_title)
987                 g_free (dialog->original_account_title);
988         dialog->original_account_title = g_strdup (account_data->display_name);
989         
990
991         if (!(account_data->store_account)) {
992                 g_printerr ("modest: account has no stores: %s\n", account_name);
993                 return;
994         }
995                 
996         /* Show the account data in the widgets: */
997         
998         /* Note that we never show the non-display name in the UI.
999          * (Though the display name defaults to the non-display name at the start.) */
1000         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1001                 account_data->display_name ? account_data->display_name : "");
1002                 
1003         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1004                 account_data->fullname ? account_data->fullname : "");
1005         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1006                 account_data->email ? account_data->email : "");
1007                 
1008         ModestServerAccountData *incoming_account = account_data->store_account;
1009                 
1010         if (incoming_account)
1011                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
1012         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
1013                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
1014         if (!retrieve) {
1015                 /* Default to something, though no default is specified in the UI spec: */
1016                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
1017         }
1018         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
1019         g_free (retrieve);
1020         
1021         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
1022                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
1023         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
1024         
1025         
1026         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
1027                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
1028         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
1029         
1030         /* Only show the leave-on-server checkbox for POP, 
1031          * as per the UI spec: */
1032         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
1033                 gtk_widget_hide (dialog->checkbox_leave_messages);
1034         } else {
1035                 gtk_widget_show (dialog->checkbox_leave_messages);
1036         }
1037                 
1038         if (incoming_account) {
1039                 /* Remember this for later: */
1040                 dialog->incoming_protocol = incoming_account->proto;
1041                 
1042                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1043                         incoming_account->username ? incoming_account->username : "");
1044                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1045                         incoming_account->password ? incoming_account->password : "");
1046                         
1047                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1048                         incoming_account->hostname ? incoming_account->hostname : "");
1049                         
1050                 /* The UI spec says:
1051                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1052          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1053                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1054          */
1055                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1056                         dialog->account_manager, incoming_account->account_name);
1057                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1058                         secure_auth != MODEST_PROTOCOL_AUTH_PASSWORD);
1059                 /* Note that MODEST_PROTOCOL_AUTH_PLAIN should probably never be used. */
1060                         
1061                 update_incoming_server_title (dialog, incoming_account->proto);
1062                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1063                 
1064                 const ModestConnectionProtocol security = modest_server_account_get_security (
1065                         dialog->account_manager, incoming_account->account_name);
1066                 modest_serversecurity_combo_box_set_active_serversecurity (
1067                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1068                 
1069                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1070                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1071                 hildon_number_editor_set_value (
1072                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1073         
1074         }
1075         
1076         ModestServerAccountData *outgoing_account = account_data->transport_account;
1077         if (outgoing_account) {
1078                 /* Remember this for later: */
1079                 dialog->outgoing_protocol = outgoing_account->proto;
1080                 
1081                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1082                         outgoing_account->hostname ? outgoing_account->hostname : "");
1083                 
1084                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1085                         outgoing_account->username ? outgoing_account->username : "");
1086                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1087                         outgoing_account->password ? outgoing_account->password : "");
1088                 
1089                 /* Get the secure-auth setting: */
1090                 const ModestAuthProtocol secure_auth = modest_server_account_get_secure_auth(
1091                         dialog->account_manager, outgoing_account->account_name);
1092                 modest_secureauth_combo_box_set_active_secureauth (
1093                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1094                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1095                 
1096                 modest_serversecurity_combo_box_fill (
1097                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1098                 
1099                 /* Get the security setting: */
1100                 const ModestConnectionProtocol security = modest_server_account_get_security (
1101                         dialog->account_manager, outgoing_account->account_name);
1102                 modest_serversecurity_combo_box_set_active_serversecurity (
1103                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1104                 
1105                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1106                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1107                 hildon_number_editor_set_value (
1108                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1109         }
1110         
1111         /* account_data->is_enabled,  */
1112         /*account_data->is_default,  */
1113
1114         /* account_data->store_account->proto */
1115
1116         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1117         
1118         /* Unset the modified flag so we can detect changes later: */
1119         dialog->modified = FALSE;
1120 }
1121
1122 static GList* get_supported_secure_authentication_methods (ModestProtocol proto, 
1123         const gchar* hostname, gint port, GtkWindow *parent_window);
1124
1125 static gboolean
1126 save_configuration (ModestAccountSettingsDialog *dialog)
1127 {
1128         g_assert (dialog->account_name);
1129         
1130         const gchar* account_name = dialog->account_name;
1131                 
1132         /* Set the account data from the widgets: */
1133         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1134         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1135                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1136         if (!test)
1137                 return FALSE;
1138                 
1139         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1140         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1141                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1142         if (!test)
1143                 return FALSE;
1144                 
1145         /* Signature: */
1146         if (dialog->signature_dialog) {
1147                 gboolean use_signature = FALSE;
1148         gchar *signature = modest_signature_editor_dialog_get_settings (
1149                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1150                 &use_signature);
1151         
1152         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1153                 signature, use_signature);
1154         g_free (signature);
1155     }
1156         
1157         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1158                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1159         modest_account_mgr_set_string (dialog->account_manager, account_name,
1160                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1161         g_free (retrieve);
1162         
1163         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1164                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1165         modest_account_mgr_set_int (dialog->account_manager, account_name,
1166                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1167         
1168         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1169         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1170                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1171         if (!test)
1172                 return FALSE;
1173                         
1174         /* Incoming: */
1175         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1176                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1177         g_assert (incoming_account_name);
1178         
1179         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1180         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1181                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1182         if (!test)
1183                 return FALSE;
1184                                 
1185         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1186         modest_server_account_set_username (dialog->account_manager, incoming_account_name, username);
1187         
1188         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1189         modest_server_account_set_password (dialog->account_manager, incoming_account_name, password);
1190                         
1191         /* port: */
1192         gint port_num = hildon_number_editor_get_value (
1193                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1194         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1195                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1196                         
1197         /* The UI spec says:
1198          * If secure authentication is unchecked, allow sending username and password also as plain text.
1199          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1200          */
1201         ModestAuthProtocol protocol_authentication_incoming = 
1202                 MODEST_PROTOCOL_AUTH_PASSWORD;
1203         if (gtk_toggle_button_get_active (
1204                         GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth))) {
1205                 GList *list_auth_methods = 
1206                         get_supported_secure_authentication_methods (dialog->incoming_protocol, 
1207                                 hostname, port_num, GTK_WINDOW (dialog));       
1208                 if (list_auth_methods) {
1209                         /* Use the first supported method.
1210                          * TODO: Should we prioritize them, to prefer a particular one? */
1211                         protocol_authentication_incoming = 
1212                                 (ModestAuthProtocol)(GPOINTER_TO_INT(list_auth_methods->data));
1213                         g_list_free (list_auth_methods);
1214                 }
1215                 else
1216                         g_warning ("%s: get_supported_secure_authentication_methods() returned NULL.\n", __FUNCTION__);
1217                 
1218         }
1219         
1220         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1221         
1222         
1223         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1224                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1225         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1226         
1227         
1228                 
1229         g_free (incoming_account_name);
1230         
1231         /* Outgoing: */
1232         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1233                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1234         g_assert (outgoing_account_name);
1235         
1236         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1237         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1238                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1239         if (!test)
1240                 return FALSE;
1241                 
1242         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1243         modest_server_account_set_username (dialog->account_manager, outgoing_account_name,
1244                 username);
1245                 
1246         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1247         modest_server_account_set_password (dialog->account_manager, outgoing_account_name,
1248                 password);
1249         
1250         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1251                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1252         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1253         
1254         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1255                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1256         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1257         
1258         /* port: */
1259         port_num = hildon_number_editor_get_value (
1260                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1261         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1262                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1263                         
1264         g_free (outgoing_account_name);
1265         
1266         
1267         /* Set the changed account title last, to simplify the previous code: */
1268         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1269         if ((!account_title) || (strlen(account_title) == 0))
1270                 return FALSE; /* Should be prevented already anyway. */
1271                 
1272         if (strcmp(account_title, account_name) != 0) {
1273                 /* Change the title: */
1274                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1275                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1276                 if (!test)
1277                         return FALSE;
1278         }
1279         
1280         /* Save connection-specific SMTP server accounts: */
1281         if (dialog->specific_window)
1282                 return modest_connection_specific_smtp_window_save_server_accounts (
1283                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1284         else
1285                 return TRUE;
1286 }
1287
1288 static gboolean entry_is_empty (GtkWidget *entry)
1289 {
1290         if (!entry)
1291                 return FALSE;
1292                 
1293         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1294         if ((!text) || (strlen(text) == 0))
1295                 return TRUE;
1296         else
1297                 return FALSE;
1298 }
1299
1300 static void
1301 enable_buttons (ModestAccountSettingsDialog *self)
1302 {
1303         gboolean enable_ok = TRUE;
1304         
1305         /* The account details title is mandatory: */
1306         if (entry_is_empty(self->entry_account_title))
1307                         enable_ok = FALSE;
1308
1309         /* The user details username is mandatory: */
1310         if (entry_is_empty(self->entry_user_username))
1311                 enable_ok = FALSE;
1312                 
1313         /* The user details email address is mandatory: */
1314         if (enable_ok && entry_is_empty (self->entry_user_email))
1315                 enable_ok = FALSE;
1316
1317         /* The custom incoming server is mandatory: */
1318         if (entry_is_empty(self->entry_incomingserver))
1319                 enable_ok = FALSE;
1320                         
1321         /* Enable the buttons, 
1322          * identifying them via their associated response codes:
1323          */
1324         GtkDialog *dialog_base = GTK_DIALOG (self);
1325     gtk_dialog_set_response_sensitive (dialog_base,
1326                                        GTK_RESPONSE_OK,
1327                                        enable_ok);
1328 }
1329
1330 static void
1331 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1332 {
1333         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1334         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1335
1336
1337         object_class->get_property = modest_account_settings_dialog_get_property;
1338         object_class->set_property = modest_account_settings_dialog_set_property;
1339         object_class->dispose = modest_account_settings_dialog_dispose;
1340         object_class->finalize = modest_account_settings_dialog_finalize;
1341 }
1342  
1343 static void
1344 show_error (GtkWindow *parent_window, const gchar* text)
1345 {
1346         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1347         /*
1348         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1349                 (GtkDialogFlags)0,
1350                  GTK_MESSAGE_ERROR,
1351                  GTK_BUTTONS_OK,
1352                  text ));
1353         */       
1354         
1355         gtk_dialog_run (dialog);
1356         gtk_widget_destroy (GTK_WIDGET (dialog));
1357 }
1358
1359 static void
1360 show_ok (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_INFO,
1367                  GTK_BUTTONS_OK,
1368                  text ));
1369         */
1370                  
1371         gtk_dialog_run (dialog);
1372         gtk_widget_destroy (GTK_WIDGET (dialog));
1373 }
1374
1375 /* TODO: Enable this when tinymail has the API: */
1376 #if 0
1377 typedef struct 
1378 {
1379         gboolean finished;
1380         GList *result;
1381 } ModestGetSupportedAuthInfo;
1382
1383 static void on_camel_account_get_supported_secure_authentication_status (
1384         GObject *self, TnyStatus *status, gpointer user_data)
1385 {
1386         printf ("DEBUG: %s.\n", __FUNCTION__);
1387 }
1388
1389 static void
1390 on_camel_account_get_supported_secure_authentication (
1391   TnyCamelAccount *self, gboolean cancelled,
1392   GList *auth_types, GError **err, 
1393   gpointer user_data)
1394 {
1395         printf ("DEBUG: %s.\n", __FUNCTION__);
1396                 
1397         ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
1398         g_return_if_fail (info);
1399         
1400         if (!auth_types) {
1401                 printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
1402                 info->finished = TRUE; /* We are blocking, waiting for this. */
1403                 return;
1404         }
1405                 
1406         ModestPairList* pairs = modest_protocol_info_get_protocol_auth_pair_list ();
1407
1408         /* Get the enum value for the strings: */
1409         GList *result = NULL;
1410         GList* iter = auth_types;
1411         while (iter) {
1412                 const gchar *auth_name = (const gchar*)iter->data;
1413                 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
1414                 ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs, 
1415                         auth_name);
1416                 if (matching)
1417                         g_list_append (result, GINT_TO_POINTER((ModestSecureAuthentication)matching->second));
1418                                 
1419                 iter = g_list_next (iter);      
1420         }
1421         
1422         g_list_free (auth_types);
1423         
1424         modest_pair_list_free (pairs);
1425         
1426         info->result = result;
1427         info->finished = TRUE; /* We are blocking, waiting for this. */
1428 }
1429 #endif
1430
1431
1432 static GList* get_supported_secure_authentication_methods (ModestProtocol proto, 
1433         const gchar* hostname, gint port, GtkWindow *parent_window)
1434 {
1435         return NULL;
1436         
1437 /* TODO: Enable this when tinymail has the API: */
1438 #if 0
1439         g_return_val_if_fail (proto != MODEST_PROTOCOL_UNKNOWN, NULL);
1440         
1441         /*
1442         result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
1443         */
1444         
1445         /* Create a TnyCamelAccount so we can use 
1446          * tny_camel_account_get_supported_secure_authentication(): */
1447         TnyAccount * tny_account = NULL;
1448         switch (proto) {
1449         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
1450         case MODEST_PROTOCOL_TRANSPORT_SMTP:
1451                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
1452         case MODEST_PROTOCOL_STORE_POP:
1453                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
1454         case MODEST_PROTOCOL_STORE_IMAP:
1455                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
1456         case MODEST_PROTOCOL_STORE_MAILDIR:
1457         case MODEST_PROTOCOL_STORE_MBOX:
1458                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
1459         default:
1460                 tny_account = NULL;
1461         }
1462         
1463         if (!tny_account) {
1464                 g_printerr ("%s could not create tny account.", __FUNCTION__);
1465                 return NULL;
1466         }
1467         
1468         /* Set proto, so that the prepare_func() vfunc will work when we call 
1469          * set_session(): */
1470          /* TODO: Why isn't this done in account_new()? */
1471         tny_account_set_proto (tny_account,
1472                                modest_protocol_info_get_protocol_name(proto));
1473                                
1474         /* Set the session for the account, so we can use it: */
1475         ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
1476         TnySessionCamel *session = 
1477                 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
1478         g_return_val_if_fail (session, NULL);
1479         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
1480         
1481         tny_account_set_hostname (tny_account, hostname);
1482         
1483         if(port > 0)
1484                 tny_account_set_port (tny_account, port);
1485                 
1486
1487         /* Ask camel to ask the server, asynchronously: */
1488         ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
1489         info->finished = FALSE;
1490         info->result = NULL;
1491         
1492         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, 
1493                 _("Asking the server for supported secure authentication mechanisms.")));
1494         gtk_dialog_run (dialog);
1495         
1496         printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
1497         tny_camel_account_get_supported_secure_authentication (
1498                 TNY_CAMEL_ACCOUNT (tny_account),
1499                 on_camel_account_get_supported_secure_authentication,
1500                 on_camel_account_get_supported_secure_authentication_status,
1501                 info);
1502                 
1503         printf ("DEBUG: %s: AFTER STARTING.\n", __FUNCTION__);
1504                 
1505         /* Block until the callback has been called,
1506          * driving the main context, so that the (idle handler) callback can be 
1507          * called, and so that our dialog is clickable: */
1508         while (!(info->finished)) {
1509                 printf ("DEBUG: %s: finished is FALSE.\n", __FUNCTION__);
1510                 if (g_main_context_pending (NULL)) {
1511                         printf ("DEBUG: iterating\n");
1512                         g_main_context_iteration (NULL, FALSE);
1513                         printf ("DEBUG: after iterating\n");
1514                 }
1515         }
1516         
1517         printf ("DEBUG: %s: FINISHED.\n", __FUNCTION__);
1518         
1519         gtk_widget_destroy (GTK_WIDGET (dialog));
1520                 
1521         GList *result = info->result;
1522         g_slice_free (ModestGetSupportedAuthInfo, info);
1523         info = NULL;
1524         
1525         return result;
1526 #endif
1527 }
1528
1529
1530
1531