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