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