Merged the new plugin system from branch plugin-system.
[modest] / src / maemo / easysetup / modest-easysetup-wizard-dialog.c
1
2 /* Copyright (c) 2006, Nokia Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * * Neither the name of the Nokia Corporation nor the names of its
15  *   contributors may be used to endorse or promote products derived from
16  *   this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31
32 #include "modest-easysetup-wizard-dialog.h"
33 #include <glib/gi18n.h>
34 #include <gtk/gtknotebook.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkcombobox.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkcheckbutton.h>
41 #include <gtk/gtkmessagedialog.h>
42 #include <gtk/gtkseparator.h>
43 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
44 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
45 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
46 #include "widgets/modest-validating-entry.h"
47 #include "modest-text-utils.h"
48 #include "modest-conf.h"
49 #include "modest-defs.h"
50 #include "modest-account-mgr.h"
51 #include "modest-signal-mgr.h"
52 #include "modest-account-mgr-helpers.h"
53 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
54 #include "maemo/modest-connection-specific-smtp-window.h"
55 #include "widgets/modest-ui-constants.h"
56 #include "widgets/modest-default-account-settings-dialog.h"
57 #include "widgets/modest-easysetup-wizard-page.h"
58 #include "maemo/modest-maemo-utils.h"
59 #include "modest-utils.h"
60 #include "maemo/modest-hildon-includes.h"
61 #include "maemo/modest-maemo-security-options-view.h"
62 #include <modest-account-protocol.h>
63
64 /* Include config.h so that _() works: */
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
70
71 #define MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
72                                                     MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, \
73                                                     ModestEasysetupWizardDialogPrivate))
74
75 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
76
77
78 typedef enum {
79         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
80         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
81 } ModestEasysetupWizardDialogServerChanges;
82
83 struct _ModestEasysetupWizardDialogPrivate
84 {
85         ModestPresets *presets;
86
87         /* Remember what fields the user edited manually to not prefill them
88          * again. */
89         ModestEasysetupWizardDialogServerChanges server_changes;
90         
91         /* Check if the user changes a field to show a confirmation dialog */
92         gboolean dirty;
93
94         /* If we have a pending load of settings or not. */
95         gboolean pending_load_settings;
96         
97         /* Used by derived widgets to query existing accounts,
98          * and to create new accounts: */
99         ModestAccountMgr *account_manager;
100         ModestAccountSettings *settings;
101         
102         /* notebook pages: */
103         GtkWidget *page_welcome;
104         
105         GtkWidget *page_account_details;
106         GtkWidget *combo_account_country;
107         GtkWidget *combo_account_serviceprovider;
108         GtkWidget *entry_account_title;
109         
110         GtkWidget *page_user_details;
111         GtkWidget *entry_user_name;
112         GtkWidget *entry_user_username;
113         GtkWidget *entry_user_password;
114         GtkWidget *entry_user_email;
115         
116         GtkWidget *page_complete_easysetup;
117         
118         GtkWidget *page_custom_incoming;
119         GtkWidget *combo_incoming_servertype;
120         GtkWidget *caption_incoming;
121         GtkWidget *entry_incomingserver;
122
123         /* Security widgets */
124         GtkWidget *incoming_security;
125         GtkWidget *outgoing_security;
126
127         GtkWidget *page_custom_outgoing;
128         GtkWidget *entry_outgoingserver;
129         GtkWidget *combo_outgoing_security;
130         GtkWidget *combo_outgoing_auth;
131         GtkWidget *checkbox_outgoing_smtp_specific;
132         GtkWidget *button_outgoing_smtp_servers;
133         
134         GtkWidget *page_complete_customsetup;
135
136         ModestProtocolType last_plugin_protocol_selected;
137         GSList *missing_data_signals;
138 };
139
140 static void save_to_settings (ModestEasysetupWizardDialog *self);
141 static void real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next);
142
143 static gboolean
144 on_delete_event (GtkWidget *widget,
145                  GdkEvent *event,
146                  ModestEasysetupWizardDialog *wizard)
147 {
148         gtk_dialog_response (GTK_DIALOG (wizard), GTK_RESPONSE_CANCEL);
149         return TRUE;
150 }
151
152 static void
153 on_easysetup_changed(GtkWidget* widget, ModestEasysetupWizardDialog* wizard)
154 {
155         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard);
156         g_return_if_fail (priv != NULL);
157         priv->dirty = TRUE;
158 }
159
160 static void
161 modest_easysetup_wizard_dialog_dispose (GObject *object)
162 {
163         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
164                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
165 }
166
167 static void
168 modest_easysetup_wizard_dialog_finalize (GObject *object)
169 {
170         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
171         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
172         
173         if (priv->account_manager)
174                 g_object_unref (G_OBJECT (priv->account_manager));
175                 
176         if (priv->presets)
177                 modest_presets_destroy (priv->presets);
178
179         if (priv->settings)
180                 g_object_unref (priv->settings);
181
182         if (priv->missing_data_signals) {
183                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
184                 priv->missing_data_signals = NULL;
185         }
186                 
187         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
188 }
189
190 static void
191 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
192
193 static void
194 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
195
196 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
197
198 static gint 
199 get_port_from_protocol (ModestProtocolType server_type,
200                         gboolean alternate_port)
201 {
202         int server_port = 0;
203         ModestProtocol *protocol;
204         ModestProtocolRegistry *protocol_registry;
205
206         protocol_registry = modest_runtime_get_protocol_registry ();
207         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, server_type);
208
209         if (alternate_port) {
210                 server_port = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
211         } else {
212                 server_port = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
213         }
214         return server_port;
215 }
216
217 /* static GList*  */
218 /* check_for_supported_auth_methods (ModestEasysetupWizardDialog* self) */
219 /* { */
220 /*      GError *error = NULL; */
221 /*      ModestProtocolType protocol_type; */
222 /*      const gchar* hostname; */
223 /*      const gchar* username; */
224 /*      ModestProtocolType security_protocol_incoming_type; */
225 /*      ModestProtocolRegistry *registry; */
226 /*      int port_num;  */
227 /*      GList *list_auth_methods; */
228 /*      ModestEasysetupWizardDialogPrivate *priv; */
229         
230 /*      priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self); */
231 /*      registry = modest_runtime_get_protocol_registry (); */
232 /*      protocol_type = easysetup_servertype_combo_box_get_active_servertype ( */
233 /*              EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype)); */
234 /*      hostname = gtk_entry_get_text(GTK_ENTRY(priv->entry_incomingserver)); */
235 /*      username = gtk_entry_get_text(GTK_ENTRY(priv->entry_user_username)); */
236 /*      security_protocol_incoming_type = modest_serversecurity_combo_box_get_active_serversecurity */
237 /*              (MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security)); */
238 /*      port_num = get_port_from_protocol(protocol_type, FALSE);  */
239 /*      list_auth_methods = modest_utils_get_supported_secure_authentication_methods (protocol_type, hostname, port_num,  */
240 /*                                                                                    username, GTK_WINDOW (self), &error); */
241
242 /*      if (list_auth_methods) { */
243 /*              /\* TODO: Select the correct method *\/ */
244 /*              GList* list = NULL; */
245 /*              GList* method; */
246 /*              for (method = list_auth_methods; method != NULL; method = g_list_next(method)) { */
247 /*                      ModestProtocolType auth_protocol_type = (ModestProtocolType) (GPOINTER_TO_INT(method->data)); */
248 /*                      if (modest_protocol_registry_protocol_type_has_tag (registry, auth_protocol_type, */
249 /*                                                                          MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS)) { */
250 /*                              list = g_list_append(list, GINT_TO_POINTER(auth_protocol_type)); */
251 /*                      } */
252 /*              } */
253
254 /*              g_list_free(list_auth_methods); */
255
256 /*              if (list) */
257 /*                      return list; */
258 /*      } */
259
260 /*      if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() || */
261 /*                      error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED) */
262 /*      { */
263 /*              modest_platform_information_banner (GTK_WIDGET(self), NULL, */
264 /*                                                  _("mcen_ib_unableto_discover_auth_methods")); */
265 /*      } */
266
267 /*      if(error != NULL) */
268 /*              g_error_free(error); */
269
270 /*      return NULL; */
271 /* } */
272
273 /* static gboolean check_has_supported_auth_methods(ModestEasysetupWizardDialog* self) */
274 /* { */
275 /*      GList* methods = check_for_supported_auth_methods(self); */
276 /*      if (!methods) */
277 /*      { */
278 /*              return FALSE; */
279 /*      } */
280
281 /*      g_list_free(methods); */
282 /*      return TRUE; */
283 /* } */
284
285 /* static ModestProtocolType check_first_supported_auth_method(ModestEasysetupWizardDialog* self) */
286 /* { */
287 /*      ModestProtocolType result; */
288
289 /*      result = MODEST_PROTOCOLS_AUTH_PASSWORD; */
290
291 /*      GList* methods = check_for_supported_auth_methods(self); */
292 /*      if (methods) */
293 /*      { */
294 /*              /\* Use the first one: *\/ */
295 /*              result = (ModestProtocolType) (GPOINTER_TO_INT(methods->data)); */
296 /*              g_list_free(methods); */
297 /*      } */
298
299 /*      return result; */
300 /* } */
301
302 static void
303 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
304 {
305         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
306         
307         /* Call the vfunc, which may be overridden by derived classes: */
308         if (klass->enable_buttons) {
309                 GtkNotebook *notebook = NULL;
310                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
311                 
312                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
313                 if (current_page_num == -1)
314                         return;
315                         
316                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
317                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
318         }
319 }
320
321 static void
322 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
323 {
324         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
325         g_assert(self);
326         invoke_enable_buttons_vfunc(self);
327 }
328
329 static void
330 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
331 {
332         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
333         g_assert(self);
334         invoke_enable_buttons_vfunc(self);
335 }
336
337 /** This is a convenience function to create a caption containing a mandatory widget.
338  * When the widget is edited, the enable_buttons() vfunc will be called.
339  */
340 static GtkWidget* 
341 create_caption_new_with_asterisk (ModestEasysetupWizardDialog *self,
342                                   GtkSizeGroup *group,
343                                   const gchar *value,
344                                   GtkWidget *control,
345                                   GtkWidget *icon,
346                                   HildonCaptionStatus flag)
347 {
348         GtkWidget *caption = NULL;
349   
350         /* Note: Previously, the translated strings already contained the "*",
351          * Comment out this code if they do again.
352          */
353         /* Add a * character to indicate mandatory fields,
354          * as specified in our "Email UI Specification": */
355         if (flag == HILDON_CAPTION_MANDATORY) {
356                 gchar* title = g_strdup_printf("%s*", value);
357                 caption = hildon_caption_new (group, title, control, icon, flag);       
358                 g_free(title);
359         }       
360         else
361                 caption = hildon_caption_new (group, value, control, icon, flag);
362
363         /* Connect to the appropriate changed signal for the widget, 
364          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
365          */
366         if (GTK_IS_ENTRY (control)) {
367                 g_signal_connect (G_OBJECT (control), "changed",
368                                   G_CALLBACK (on_caption_entry_changed), self);
369                 
370         }
371         else if (GTK_IS_COMBO_BOX (control)) {
372                 g_signal_connect (G_OBJECT (control), "changed",
373                                   G_CALLBACK (on_caption_combobox_changed), self);
374         }
375          
376         return caption;
377 }
378            
379 static GtkWidget*
380 create_page_welcome (ModestEasysetupWizardDialog *self)
381 {
382         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
383         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
384         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
385         /* So that it is not truncated: */
386         gtk_widget_set_size_request (label, 600, -1);
387         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
388         gtk_widget_show (label);
389         gtk_widget_show (GTK_WIDGET (box));
390         return GTK_WIDGET (box);
391 }
392
393 static void
394 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
395 {
396         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
397         g_assert(self);
398         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
399         
400         priv->dirty = TRUE;
401         
402         /* Fill the providers combo, based on the selected country: */
403         if (priv->presets != NULL) {
404                 gint mcc = easysetup_country_combo_box_get_active_country_mcc (
405                         MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
406                 easysetup_provider_combo_box_fill (
407                         EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider), priv->presets, mcc);
408         }
409 }
410
411 static void
412 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
413 {
414         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
415         g_assert(self);
416         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
417         
418         priv->dirty = TRUE;
419         
420         /* Fill the providers combo, based on the selected country: */
421         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
422                 EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider));
423         
424         gchar* domain_name = NULL;
425         if(provider_id)
426                 domain_name = modest_presets_get_domain (priv->presets, provider_id);
427         
428         if(!domain_name)
429                 domain_name = g_strdup (MODEST_EXAMPLE_EMAIL_ADDRESS);
430                 
431         if (priv->entry_user_email)
432                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), domain_name);
433                 
434         g_free (domain_name);
435         
436         g_free (provider_id);
437 }
438
439 static void
440 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
441 {
442         modest_platform_information_banner (GTK_WIDGET (self), NULL,
443                                             _CS("ckdg_ib_maximum_characters_reached"));
444 }
445
446 static GtkWidget*
447 create_page_account_details (ModestEasysetupWizardDialog *self)
448 {
449         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
450         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
451         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
452
453         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
454         gtk_widget_set_size_request (label, 600, -1);
455         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
456         gtk_widget_show (label);
457
458         /* Create a size group to be used by all captions.
459          * Note that HildonCaption does not create a default size group if we do not specify one.
460          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
461         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
462
463         /* The country widgets: */
464         priv->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
465         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_country"), 
466                                                               priv->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
467         gtk_widget_show (priv->combo_account_country);
468         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
469         gtk_widget_show (caption);
470         
471         /* connect to country combo's changed signal, so we can fill the provider combo: */
472         g_signal_connect (G_OBJECT (priv->combo_account_country), "changed",
473                           G_CALLBACK (on_combo_account_country), self);
474             
475         GtkWidget *separator = gtk_hseparator_new ();
476         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
477         gtk_widget_show (separator);
478             
479         /* The service provider widgets: */     
480         priv->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
481         gtk_widget_set_size_request (priv->combo_account_serviceprovider, 320, -1);
482         
483         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_serviceprovider"), 
484                                                    priv->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
485         gtk_widget_show (priv->combo_account_serviceprovider);
486         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
487         gtk_widget_show (caption);
488         
489         /* connect to providers combo's changed signal, so we can fill the email address: */
490         g_signal_connect (G_OBJECT (priv->combo_account_serviceprovider), "changed",
491                           G_CALLBACK (on_combo_account_serviceprovider), self);
492         
493         /* The description widgets: */  
494         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
495         g_signal_connect(G_OBJECT(priv->entry_account_title), "changed",
496                          G_CALLBACK(on_easysetup_changed), self);
497         /* Do use auto-capitalization: */
498         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_account_title), 
499                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
500         
501         /* Set a default account title, choosing one that does not already exist: */
502         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
503         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
504         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
505                 priv->account_manager, default_account_name_start);
506         g_free (default_account_name_start);
507         default_account_name_start = NULL;
508         
509         gtk_entry_set_text( GTK_ENTRY (priv->entry_account_title), default_account_name);
510         g_free (default_account_name);
511         default_account_name = NULL;
512
513         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
514                                                    priv->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
515         gtk_widget_show (priv->entry_account_title);
516         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
517         gtk_widget_show (caption);
518         
519         /* Prevent the use of some characters in the account title, 
520          * as required by our UI specification: */
521         GList *list_prevent = NULL;
522         list_prevent = g_list_append (list_prevent, "\\");
523         list_prevent = g_list_append (list_prevent, "/");
524         list_prevent = g_list_append (list_prevent, ":");
525         list_prevent = g_list_append (list_prevent, "*");
526         list_prevent = g_list_append (list_prevent, "?");
527         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
528         list_prevent = g_list_append (list_prevent, "“");
529         list_prevent = g_list_append (list_prevent, "<"); 
530         list_prevent = g_list_append (list_prevent, ">"); 
531         list_prevent = g_list_append (list_prevent, "|");
532         list_prevent = g_list_append (list_prevent, "^");       
533         modest_validating_entry_set_unallowed_characters (
534                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
535         g_list_free (list_prevent);
536         list_prevent = NULL;
537         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
538                                                                                                                                          modest_utils_on_entry_invalid_character, self);
539         
540         /* Set max length as in the UI spec:
541          * The UI spec seems to want us to show a dialog if we hit the maximum. */
542         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
543         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title), 
544                                               on_entry_max, self);
545         
546         gtk_widget_show (GTK_WIDGET (box));
547         
548         return GTK_WIDGET (box);
549 }
550
551 static GtkWidget*
552 create_page_user_details (ModestEasysetupWizardDialog *self)
553 {
554         GtkSizeGroup* sizegroup;
555         GtkWidget *box;
556         ModestEasysetupWizardDialogPrivate *priv;
557
558         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
559         
560         /* Create a size group to be used by all captions.
561          * Note that HildonCaption does not create a default size group if we do not specify one.
562          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
563         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
564         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
565          
566         /* The name widgets: (use auto cap) */
567         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
568         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name), 
569                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
570         
571         /* Set max length as in the UI spec:
572          * The UI spec seems to want us to show a dialog if we hit the maximum. */
573         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
574         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
575                                               on_entry_max, self);
576         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
577                                                               _("mcen_li_emailsetup_name"), priv->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
578         g_signal_connect(G_OBJECT(priv->entry_user_name), "changed", 
579                          G_CALLBACK(on_easysetup_changed), self);
580         gtk_widget_show (priv->entry_user_name);
581         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
582         gtk_widget_show (caption);
583         
584         /* Prevent the use of some characters in the name, 
585          * as required by our UI specification: */
586         GList *list_prevent = NULL;
587         list_prevent = g_list_append (list_prevent, "<");
588         list_prevent = g_list_append (list_prevent, ">");
589         modest_validating_entry_set_unallowed_characters (
590                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
591         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
592                 modest_utils_on_entry_invalid_character, self);
593         g_list_free (list_prevent);
594         
595         /* The username widgets: */     
596         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
597         /* Auto-capitalization is the default, so let's turn it off: */
598         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), 
599                                          HILDON_GTK_INPUT_MODE_FULL);
600         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
601                                                    priv->entry_user_username, 
602                                                     NULL, HILDON_CAPTION_MANDATORY);
603         gtk_widget_show (priv->entry_user_username);
604         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
605         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", 
606                          G_CALLBACK(on_easysetup_changed), self);
607         gtk_widget_show (caption);
608         
609         /* Prevent the use of some characters in the username, 
610          * as required by our UI specification: */
611         modest_validating_entry_set_unallowed_characters_whitespace (
612                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
613         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_username),
614                 modest_utils_on_entry_invalid_character, self);
615         
616         /* Set max length as in the UI spec:
617          * The UI spec seems to want us to show a dialog if we hit the maximum. */
618         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
619         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
620                                               on_entry_max, self);
621         
622         /* The password widgets: */     
623         priv->entry_user_password = gtk_entry_new ();
624         /* Auto-capitalization is the default, so let's turn it off: */
625         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
626                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
627         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
628         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
629         caption = create_caption_new_with_asterisk (self, sizegroup, 
630                                                    _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
631         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", 
632                          G_CALLBACK(on_easysetup_changed), self);
633         gtk_widget_show (priv->entry_user_password);
634         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
635         gtk_widget_show (caption);
636         
637         /* The email address widgets: */        
638         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
639         /* Auto-capitalization is the default, so let's turn it off: */
640         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
641         caption = create_caption_new_with_asterisk (self, sizegroup, 
642                                                    _("mcen_li_emailsetup_email_address"), priv->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
643         gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
644         gtk_widget_show (priv->entry_user_email);
645         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
646         g_signal_connect(G_OBJECT(priv->entry_user_email), "changed", 
647                          G_CALLBACK(on_easysetup_changed), self);
648         gtk_widget_show (caption);
649         
650         /* Set max length as in the UI spec:
651          * The UI spec seems to want us to show a dialog if we hit the maximum. */
652         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
653         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
654                                               on_entry_max, self);
655         
656         
657         gtk_widget_show (GTK_WIDGET (box));
658         
659         return GTK_WIDGET (box);
660 }
661
662 static GtkWidget* 
663 create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
664 {
665         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
666         
667         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
668         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
669         gtk_widget_set_size_request (label, 600, -1);
670         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
671         /* The documentation for gtk_label_set_line_wrap() says that we must 
672          * call gtk_widget_set_size_request() with a hard-coded width, 
673          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
674         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
675         gtk_widget_show (label);
676         
677         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
678         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
679         gtk_widget_set_size_request (label, 600, -1);
680         
681         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
682         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
683         gtk_widget_show (label);
684         
685         gtk_widget_show (GTK_WIDGET (box));
686         return GTK_WIDGET (box);
687 }
688
689 /** Change the caption title for the incoming server, 
690  * as specified in the UI spec:
691  */
692 static void 
693 update_incoming_server_title (ModestEasysetupWizardDialog *self)
694 {
695         ModestEasysetupWizardDialogPrivate* priv; 
696         ModestProtocolType protocol_type; 
697         ModestProtocolRegistry *protocol_registry;
698
699         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
700         protocol_registry = modest_runtime_get_protocol_registry ();
701         
702         protocol_type = easysetup_servertype_combo_box_get_active_servertype (
703                 EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype));
704
705         /* This could happen when the combo box has still no active iter */
706         if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
707                 gchar* incomingserver_title;
708                 const gchar *protocol_display_name; 
709
710                 protocol_display_name = modest_protocol_get_display_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type));
711                 
712                 incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), protocol_display_name);
713                 g_object_set (G_OBJECT (priv->caption_incoming), "label", incomingserver_title, NULL);
714                 g_free(incomingserver_title);
715         }
716 }
717
718 /** Change the caption title for the incoming server, 
719  * as specified in the UI spec:
720  */
721 static void 
722 update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
723 {
724         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
725         EasysetupServertypeComboBox *server_type_combo;
726         ModestProtocolType protocol_type;
727         ModestSecurityOptionsView *view;
728
729         server_type_combo = 
730                 EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype);
731         protocol_type = 
732                 easysetup_servertype_combo_box_get_active_servertype (server_type_combo);
733         
734         /* Fill the combo with appropriately titled choices for all
735            those protocols */
736         view = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
737         modest_security_options_view_set_server_type (view, protocol_type);
738 }
739
740 static void 
741 on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
742 {
743         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
744         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
745         EasysetupServertypeComboBox *combo;
746         ModestProtocolType protocol_type;
747
748         priv->dirty = TRUE;
749         
750         /* Update title */
751         update_incoming_server_title (self);
752
753         /* Update security options if needed */
754         combo = EASYSETUP_SERVERTYPE_COMBO_BOX (combobox);
755         protocol_type = easysetup_servertype_combo_box_get_active_servertype (combo);
756         update_incoming_server_security_choices (self);
757         gtk_widget_show (priv->incoming_security);
758
759         set_default_custom_servernames (self);
760 }
761
762 static void 
763 on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
764 {
765         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
766         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
767         priv->dirty = TRUE;
768         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
769 }
770
771 static GtkWidget* 
772 create_page_custom_incoming (ModestEasysetupWizardDialog *self)
773 {
774         ModestProtocolRegistry *protocol_registry;
775         ModestEasysetupWizardDialogPrivate* priv; 
776         GtkWidget *box; 
777         GtkWidget *scrolled_window;
778         GtkWidget *label;
779         GtkSizeGroup *sizegroup;
780         GtkWidget *caption;
781
782         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
783         protocol_registry = modest_runtime_get_protocol_registry ();
784
785         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
786         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
787
788         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
789                                         GTK_POLICY_NEVER,
790                                         GTK_POLICY_AUTOMATIC);
791
792         /* Show note that account type cannot be changed in future: */
793         label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
794         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
795         gtk_widget_set_size_request (label, 600, -1);
796         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
797         gtk_widget_show (label);
798         
799         /* Create a size group to be used by all captions.
800          * Note that HildonCaption does not create a default size group if we do not specify one.
801          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
802         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
803          
804         /* The incoming server widgets: */
805         priv->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new (TRUE));
806         caption = create_caption_new_with_asterisk (self, sizegroup, 
807                                                     _("mcen_li_emailsetup_type"), priv->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
808         gtk_widget_show (priv->combo_incoming_servertype);
809         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
810         gtk_widget_show (caption);
811         
812         priv->entry_incomingserver = gtk_entry_new ();
813         g_signal_connect(G_OBJECT(priv->entry_incomingserver), "changed", G_CALLBACK(on_easysetup_changed), self);
814         /* Auto-capitalization is the default, so let's turn it off: */
815         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
816         set_default_custom_servernames (self);
817
818         /* The caption title will be updated in update_incoming_server_title().
819          * so this default text will never be seen: */
820         /* (Note: Changing the title seems pointless. murrayc) */
821         priv->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
822                                                                   "Incoming Server", 
823                                                                    priv->entry_incomingserver, 
824                                                                    NULL, HILDON_CAPTION_MANDATORY);
825         update_incoming_server_title (self);
826         gtk_widget_show (priv->entry_incomingserver);
827         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
828         gtk_widget_show (priv->caption_incoming);
829         
830         /* Change the caption title when the servertype changes, 
831          * as in the UI spec: */
832         g_signal_connect (G_OBJECT (priv->combo_incoming_servertype), "changed",
833                           G_CALLBACK (on_combo_servertype_changed), self);
834
835         /* Remember when the servername was changed manually: */
836         g_signal_connect (G_OBJECT (priv->entry_incomingserver), "changed",
837                           G_CALLBACK (on_entry_incoming_servername_changed), self);
838
839         /* The secure connection widgets. These are only valid for
840            protocols with security */   
841         priv->incoming_security = 
842                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
843                                                         FALSE, sizegroup);
844         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security, 
845                             FALSE, FALSE, MODEST_MARGIN_HALF);
846
847         /* Set default selection */
848         easysetup_servertype_combo_box_set_active_servertype (
849                 EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype), 
850                 MODEST_PROTOCOLS_STORE_POP);
851
852         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), box);
853         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box),
854                                              gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrolled_window)));
855         gtk_widget_show (GTK_WIDGET (box));
856         gtk_widget_show (scrolled_window);
857
858         return GTK_WIDGET (scrolled_window);
859 }
860
861 static void
862 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
863 {
864         GtkWidget *widget = GTK_WIDGET (user_data);
865         
866         /* Enable the widget only if the toggle button is active: */
867         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
868         gtk_widget_set_sensitive (widget, enable);
869 }
870
871 /* Make the sensitivity of a widget depend on a toggle button.
872  */
873 static void
874 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
875 {
876         g_signal_connect (G_OBJECT (button), "toggled",
877                           G_CALLBACK (on_toggle_button_changed), widget);
878         
879         /* Set the starting sensitivity: */
880         on_toggle_button_changed (button, widget);
881 }
882
883 static void
884 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
885 {
886         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
887         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
888         GtkWidget *specific_window;
889         
890         /* We set dirty here because setting it depending on the connection specific dialog
891         seems overkill */
892         priv->dirty = TRUE;
893         
894         /* Create the window, if necessary: */
895         specific_window = (GtkWidget *) modest_connection_specific_smtp_window_new ();
896         modest_connection_specific_smtp_window_fill_with_connections (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), priv->account_manager);
897
898         /* Show the window */
899         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (specific_window));
900         gtk_widget_show (specific_window);
901 }
902
903 static void 
904 on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
905 {
906         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
907         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
908         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
909 }
910
911 static GtkWidget* 
912 create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
913 {
914         ModestEasysetupWizardDialogPrivate *priv;
915         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
916         
917         /* Create a size group to be used by all captions.
918          * Note that HildonCaption does not create a default size group if we do not specify one.
919          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
920         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
921          
922         /* The outgoing server widgets: */
923         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
924         priv->entry_outgoingserver = gtk_entry_new ();
925         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
926                   G_CALLBACK (on_easysetup_changed), self);
927         /* Auto-capitalization is the default, so let's turn it off: */
928         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
929         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
930                                                               _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
931         gtk_widget_show (priv->entry_outgoingserver);
932         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
933         gtk_widget_show (caption);
934         set_default_custom_servernames (self);
935
936         /* The secure connection widgets. These are only valid for
937            protocols with security */   
938         priv->outgoing_security = 
939                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
940                                                         FALSE, sizegroup);
941         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security, 
942                             FALSE, FALSE, MODEST_MARGIN_HALF);
943         gtk_widget_show (priv->outgoing_security);
944         
945         GtkWidget *separator = gtk_hseparator_new ();
946         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
947         gtk_widget_show (separator);
948         
949         /* connection-specific checkbox: */
950         priv->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
951         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
952                                       FALSE);
953         g_signal_connect (G_OBJECT (priv->checkbox_outgoing_smtp_specific), "toggled",
954                   G_CALLBACK (on_easysetup_changed), self);
955
956         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
957                                       priv->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
958         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
959         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
960         gtk_widget_show (caption);
961         
962         /* Connection-specific SMTP-Severs Edit button: */
963         priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
964         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
965                                       priv->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
966         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
967         gtk_widget_show (priv->button_outgoing_smtp_servers);
968         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
969         gtk_widget_show (caption);
970         
971         /* Only enable the button when the checkbox is checked: */
972         enable_widget_for_togglebutton (priv->button_outgoing_smtp_servers, 
973                                         GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific));
974                 
975         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
976                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
977
978         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
979                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
980         
981         
982         gtk_widget_show (GTK_WIDGET (box));
983         
984         return GTK_WIDGET (box);
985 }
986
987 static gboolean
988 show_advanced_edit(gpointer user_data)
989 {
990         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
991         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
992         gint response;
993         
994         /* Show the Account Settings window: */
995         ModestAccountSettingsDialog *dialog = modest_default_account_settings_dialog_new ();
996         if (priv->pending_load_settings) {
997                 save_to_settings (self);
998                 priv->pending_load_settings = FALSE;
999         }
1000         modest_account_settings_dialog_load_settings (dialog, priv->settings);
1001         
1002         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog));
1003         
1004         response = gtk_dialog_run (GTK_DIALOG (dialog));
1005
1006         gtk_widget_destroy (GTK_WIDGET (dialog));
1007         
1008         return FALSE; /* Do not call this timeout callback again. */
1009 }
1010
1011 static void
1012 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
1013 {
1014         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1015         
1016         /* Show the Account Settings window: */
1017         show_advanced_edit(self);
1018 }
1019 static GtkWidget* 
1020 create_page_complete_custom (ModestEasysetupWizardDialog *self)
1021 {
1022         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
1023         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
1024         GtkWidget *button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
1025         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1026         gtk_widget_set_size_request (label, 600, -1);
1027         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1028         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1029         gtk_widget_show (label);
1030         
1031         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
1032         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1033         gtk_widget_set_size_request (label, 600, -1);
1034         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1035         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1036         gtk_widget_show (label);
1037         
1038         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
1039                                                  button_edit, NULL, HILDON_CAPTION_OPTIONAL);
1040         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
1041         gtk_widget_show (button_edit);
1042         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
1043         gtk_widget_show (caption);
1044         
1045         g_signal_connect (G_OBJECT (button_edit), "clicked", 
1046                           G_CALLBACK (on_button_edit_advanced_settings), self);
1047         
1048         gtk_widget_show (GTK_WIDGET (box));
1049         return GTK_WIDGET (box);
1050 }
1051
1052
1053 /*
1054  */
1055 static void 
1056 on_response (ModestWizardDialog *wizard_dialog,
1057              gint response_id,
1058              gpointer user_data)
1059 {
1060         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1061
1062         invoke_enable_buttons_vfunc (self);
1063 }
1064
1065 static void 
1066 on_response_before (ModestWizardDialog *wizard_dialog,
1067                     gint response_id,
1068                     gpointer user_data)
1069 {
1070         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1071         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
1072         if (response_id == GTK_RESPONSE_CANCEL) {
1073                 /* This is mostly copied from
1074                  * src/maemo/modest-account-settings-dialog.c */
1075                 if (priv->dirty) {
1076                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
1077                                 _("imum_nc_wizard_confirm_lose_changes")));
1078                         /* TODO: These button names will be ambiguous, and not
1079                          * specified in the UI specification. */
1080
1081                         const gint dialog_response = gtk_dialog_run (dialog);
1082                         gtk_widget_destroy (GTK_WIDGET (dialog));
1083
1084                         if (dialog_response != GTK_RESPONSE_OK) {
1085                                 /* Don't let the dialog close */
1086                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1087                         }
1088                 }
1089         }
1090 }
1091
1092 typedef struct IdleData {
1093         ModestEasysetupWizardDialog *dialog;
1094         ModestPresets *presets;
1095 } IdleData;
1096
1097 static gboolean
1098 presets_idle (gpointer userdata)
1099 {
1100         IdleData *idle_data = (IdleData *) userdata;
1101         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (idle_data->dialog);
1102         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1103
1104         g_assert (idle_data->presets);
1105
1106         gdk_threads_enter ();
1107
1108         priv->presets = idle_data->presets;
1109
1110         if (MODEST_EASYSETUP_IS_COUNTRY_COMBO_BOX (priv->combo_account_country)) {
1111 /*              gint mcc = get_default_country_code(); */
1112                 gint mcc;
1113                 /* Fill the combo in an idle call, as it takes a lot of time */
1114                 easysetup_country_combo_box_load_data(
1115                         MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
1116 /*              easysetup_country_combo_box_set_active_country_mcc ( */
1117 /*                      MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country), mcc); */
1118                 easysetup_country_combo_box_set_active_country_locale (
1119                         MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
1120                 mcc = easysetup_country_combo_box_get_active_country_mcc (
1121                         MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
1122                 easysetup_provider_combo_box_fill (
1123                         EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider),
1124                         priv->presets, mcc);
1125         }
1126
1127         priv->dirty = FALSE;
1128
1129         g_object_unref (idle_data->dialog);
1130         g_free (idle_data);
1131
1132         gdk_threads_leave ();
1133
1134         return FALSE;
1135 }
1136
1137 static gpointer
1138 presets_loader (gpointer userdata)
1139 {
1140         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (userdata);
1141         ModestPresets *presets = NULL;
1142         IdleData *idle_data;
1143
1144         const gchar* path  = NULL;
1145         const gchar* path1 = MODEST_PROVIDER_DATA_FILE;
1146         const gchar* path2 = MODEST_MAEMO_PROVIDER_DATA_FILE;
1147         
1148         if (access(path1, R_OK) == 0) 
1149                 path = path1;
1150         else if (access(path2, R_OK) == 0)
1151                 path = path2;
1152         else {
1153                 g_warning ("%s: neither '%s' nor '%s' is a readable provider data file",
1154                            __FUNCTION__, path1, path2);
1155                 return NULL;
1156         }
1157
1158         presets = modest_presets_new (path);
1159         if (!presets) {
1160                 g_warning ("%s: failed to parse '%s'", __FUNCTION__, path);
1161                 return NULL;
1162         }
1163         
1164         idle_data = g_new0 (IdleData, 1);
1165         idle_data->dialog = self;
1166         idle_data->presets = presets;
1167         
1168         g_idle_add (presets_idle, idle_data);   
1169
1170         return NULL;
1171 }
1172
1173 static void
1174 modest_easysetup_wizard_dialog_append_page (GtkNotebook *notebook,
1175                                             GtkWidget *page,
1176                                             const gchar *label)
1177 {
1178         gint index;
1179         /* Append page and set attributes */
1180         index = gtk_notebook_append_page (notebook, page, gtk_label_new (label));
1181         gtk_container_child_set (GTK_CONTAINER (notebook), page,
1182                                  "tab-expand", TRUE, "tab-fill", TRUE,
1183                                  NULL);
1184         gtk_widget_show (page);
1185 }
1186
1187 static void
1188 init_user_page (ModestEasysetupWizardDialogPrivate *priv)
1189 {
1190         priv->page_user_details = NULL;
1191         priv->entry_user_name = NULL;
1192         priv->entry_user_username = NULL;
1193         priv->entry_user_password = NULL;
1194         priv->entry_user_email = NULL;
1195 }
1196
1197 static void
1198 init_incoming_page (ModestEasysetupWizardDialogPrivate *priv)
1199 {
1200         priv->page_custom_incoming = NULL;
1201         priv->combo_incoming_servertype = NULL;
1202         priv->caption_incoming = NULL;
1203         priv->entry_incomingserver = NULL;
1204         priv->entry_user_email = NULL;
1205         priv->incoming_security = NULL;
1206 }
1207
1208 static void
1209 init_outgoing_page (ModestEasysetupWizardDialogPrivate *priv)
1210 {
1211         priv->page_custom_outgoing = NULL;
1212         priv->entry_outgoingserver = NULL;
1213         priv->combo_outgoing_security = NULL;
1214         priv->combo_outgoing_auth = NULL;
1215         priv->checkbox_outgoing_smtp_specific = NULL;
1216         priv->button_outgoing_smtp_servers = NULL;
1217         priv->outgoing_security = NULL;
1218 }
1219
1220 static void
1221 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1222 {
1223         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1224         
1225         /* Create the notebook to be used by the ModestWizardDialog base class:
1226          * Each page of the notebook will be a page of the wizard: */
1227         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1228         
1229         /* Set the notebook used by the ModestWizardDialog base class: */
1230         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1231     
1232         /* Set the wizard title:
1233          * The actual window title will be a combination of this and the page's tab label title. */
1234         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1235
1236         /* Read in the information about known service providers: */
1237         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1238         
1239         /* The server fields did not have been manually changed yet */
1240         priv->server_changes = 0;
1241         priv->pending_load_settings = TRUE;
1242
1243         /* Get the account manager object, 
1244          * so we can check for existing accounts,
1245          * and create new accounts: */
1246         priv->account_manager = modest_runtime_get_account_mgr ();
1247         g_object_ref (priv->account_manager);
1248         
1249         /* Initialize fields */
1250         priv->page_welcome = create_page_welcome (self);
1251         priv->page_account_details = create_page_account_details (self);
1252
1253         init_user_page (priv);
1254         init_incoming_page (priv);
1255         init_outgoing_page (priv);
1256
1257         priv->page_complete_easysetup = NULL;       
1258         priv->page_complete_customsetup = NULL;
1259         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1260         priv->missing_data_signals = NULL;
1261
1262         /* Add the common pages */
1263         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_welcome, 
1264                                                     _("mcen_ti_emailsetup_welcome"));
1265         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_account_details, 
1266                                                     _("mcen_ti_accountdetails"));
1267                 
1268         /* Connect to the dialog's response signal so we can enable/disable buttons 
1269          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1270          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1271          * so that we can be enable/disable after ModestWizardDialog has done its own 
1272          * enabling/disabling of buttons.
1273          * 
1274          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1275          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1276          * 
1277          * It's not enough to connect to the notebook's switch-page signal, because 
1278          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1279          * _after_ switching the page (understandably).
1280          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1281          * then gtk_notebook_get_current_page() would return an incorrect value.)
1282          */
1283         g_signal_connect_after (G_OBJECT (self), "response",
1284                                 G_CALLBACK (on_response), self);
1285
1286         /* This is to show a confirmation dialog when the user hits cancel */
1287         g_signal_connect (G_OBJECT (self), "response",
1288                           G_CALLBACK (on_response_before), self);
1289
1290         g_signal_connect (G_OBJECT (self), "delete-event",
1291                           G_CALLBACK (on_delete_event), self);
1292
1293         /* Reset dirty, because there was no user input until now */
1294         priv->dirty = FALSE;
1295         
1296         /* When this window is shown, hibernation should not be possible, 
1297          * because there is no sensible way to save the state: */
1298         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1299                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1300
1301         /* Load provider presets */
1302         g_object_ref (self);
1303         g_thread_create (presets_loader, self, FALSE, NULL);
1304
1305         hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_wizardwelcome",
1306                                         modest_maemo_utils_get_osso_context()); 
1307
1308         priv->settings = modest_account_settings_new ();
1309 }
1310
1311 ModestEasysetupWizardDialog*
1312 modest_easysetup_wizard_dialog_new (void)
1313 {       
1314         
1315         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1316 }
1317
1318 static void 
1319 create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1320 {
1321         ModestEasysetupWizardDialogPrivate *priv;
1322         GtkNotebook *notebook = NULL;
1323
1324         g_object_get (self, "wizard-notebook", &notebook, NULL);
1325         g_assert(notebook);
1326
1327         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1328
1329         if (!priv->page_user_details) {
1330                 priv->page_user_details = create_page_user_details (self);
1331         }                       
1332
1333         /* Create the custom pages: */
1334         if(!(priv->page_custom_incoming)) {
1335                 priv->page_custom_incoming = create_page_custom_incoming (self);
1336         }
1337                 
1338         /* TODO: only if needed */
1339         if(!(priv->page_custom_outgoing)) {
1340                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1341         }
1342         
1343         if(!(priv->page_complete_customsetup)) {
1344                 priv->page_complete_customsetup = create_page_complete_custom (self);
1345         }
1346
1347         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_user_details)))
1348                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_user_details,
1349                                                             _("mcen_ti_emailsetup_userdetails"));
1350
1351         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1352                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_incoming, 
1353                                                             _("mcen_ti_emailsetup_incomingdetails"));
1354         
1355         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))           
1356                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_outgoing, 
1357                                                             _("mcen_ti_emailsetup_outgoingdetails"));
1358                 
1359         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup)))
1360                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_customsetup, 
1361                                                             _("mcen_ti_emailsetup_complete"));
1362                         
1363         /* This is unnecessary with GTK+ 2.10: */
1364         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1365 }
1366         
1367 static void 
1368 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1369 {
1370         ModestEasysetupWizardDialogPrivate *priv;
1371         GtkNotebook *notebook = NULL;
1372
1373         g_object_get (self, "wizard-notebook", &notebook, NULL);
1374         g_assert(notebook);
1375         
1376         /* Create the easysetup-specific pages: */
1377         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1378         if(!priv->page_complete_easysetup)
1379                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1380
1381         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup)))
1382                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_easysetup, 
1383                                                             _("mcen_ti_emailsetup_complete"));
1384                         
1385         /* This is unnecessary with GTK+ 2.10: */
1386         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1387 }
1388
1389 /* */
1390 static void
1391 remove_non_common_tabs (GtkNotebook *notebook, 
1392                         gboolean remove_user_details) 
1393 {
1394         gint starting_tab;
1395         /* The first 2 tabs are the common ones (welcome tab and the
1396            providers tab), so we always remove starting from the
1397            end */
1398
1399         starting_tab = (remove_user_details) ? 2 : 3;
1400         while (gtk_notebook_get_n_pages (notebook) > starting_tab) 
1401                 gtk_notebook_remove_page (notebook, -1); 
1402 }
1403
1404 static void
1405 on_missing_mandatory_data (ModestAccountProtocol *protocol,
1406                            gboolean missing,
1407                            gpointer user_data)
1408 {
1409         real_enable_buttons (MODEST_WIZARD_DIALOG (user_data), !missing);
1410 }
1411
1412 /* After the user details page,
1413  * the following pages depend on whether "Other" was chosen 
1414  * in the provider combobox on the account page
1415  */
1416 static void 
1417 create_subsequent_pages (ModestEasysetupWizardDialog *self)
1418 {
1419         ModestEasysetupWizardDialogPrivate *priv;
1420         EasysetupProviderComboBox *combo;
1421         EasysetupProviderComboBoxIdType id_type;
1422         GtkNotebook *notebook;
1423
1424         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1425         combo = EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider);
1426         id_type = easysetup_provider_combo_box_get_active_id_type (combo);
1427         g_object_get (self, "wizard-notebook", &notebook, NULL);
1428
1429         if (id_type == EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER) {
1430                 /* "Other..." was selected: */
1431
1432                 /* If we come from a rollbacked easysetup */
1433                 if (priv->page_complete_easysetup) {
1434                         remove_non_common_tabs (notebook, FALSE);
1435                         priv->page_complete_easysetup = NULL;
1436                 } 
1437                 
1438                 /* If we come from a rollbacked plugin protocol setup */
1439                 if (priv->last_plugin_protocol_selected != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1440                         remove_non_common_tabs (notebook, TRUE);
1441                         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1442                         modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1443                         priv->missing_data_signals = NULL;
1444                 }
1445
1446                 create_subsequent_customsetup_pages (self);
1447         } else {
1448                 /* If we come from a rollbacked custom setup */
1449                 if (priv->page_custom_incoming) {
1450                         remove_non_common_tabs (notebook, TRUE);
1451                         init_user_page (priv);
1452                         init_incoming_page (priv);
1453                         init_outgoing_page (priv);
1454                         init_user_page (priv);
1455                         priv->page_complete_customsetup = NULL;
1456                 }
1457
1458                 /* It's a pluggable protocol and not a provider with presets */
1459                 if (id_type == EASYSETUP_PROVIDER_COMBO_BOX_ID_PLUGIN_PROTOCOL) {
1460                         ModestProtocol *protocol;
1461                         gchar *proto_name;
1462                         ModestProtocolType proto_type;
1463
1464                         
1465                         /* If we come from a rollbacked easy setup */
1466                         if (priv->last_plugin_protocol_selected == 
1467                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID &&
1468                             priv->page_complete_easysetup) {
1469                                 remove_non_common_tabs (notebook, TRUE);
1470                                 init_user_page (priv);
1471                                 priv->page_complete_easysetup = NULL;
1472                         }
1473                         
1474                         proto_name = easysetup_provider_combo_box_get_active_provider_id (combo);
1475                         protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
1476                                                                                   MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1477                                                                                   proto_name);
1478                         proto_type = modest_protocol_get_type_id (protocol);
1479
1480                         if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol) &&
1481                             proto_type != priv->last_plugin_protocol_selected) {
1482                                 ModestPairList *tabs;
1483                                 GSList *tmp;
1484
1485                                 /* Remember the last selected plugin protocol */
1486                                 priv->last_plugin_protocol_selected = proto_type;
1487
1488                                 /* Get tabs */
1489                                 tabs = modest_account_protocol_get_easysetupwizard_tabs (MODEST_ACCOUNT_PROTOCOL (protocol));
1490                                 tmp = (GSList *) tabs;
1491                                 while (tmp) {
1492                                         ModestPair *pair = (ModestPair *) tmp->data;
1493                                         modest_easysetup_wizard_dialog_append_page (notebook,
1494                                                                                     GTK_WIDGET (pair->second),
1495                                                                                     (const gchar *) pair->first);
1496
1497                                         /* Connect signals */
1498                                         priv->missing_data_signals = 
1499                                                 modest_signal_mgr_connect (priv->missing_data_signals, 
1500                                                                            G_OBJECT (pair->second), 
1501                                                                            "missing-mandatory-data",
1502                                                                            G_CALLBACK (on_missing_mandatory_data), 
1503                                                                            self);
1504
1505                                         g_free (pair->first);
1506                                         tmp = g_slist_next (tmp);
1507                                         /* Critical: if you don't show the page then the dialog will ignore it */
1508 /*                                      gtk_widget_show (GTK_WIDGET (pair->second)); */
1509                                 }
1510                                 modest_pair_list_free (tabs);
1511                         }
1512                         g_free (proto_name);
1513                 } else {
1514                         if (priv->last_plugin_protocol_selected != 
1515                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1516                                 remove_non_common_tabs (notebook, TRUE);
1517                                 init_user_page (priv);
1518                                 priv->page_complete_easysetup = NULL;
1519                                 priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1520                                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1521                                 priv->missing_data_signals = NULL;
1522                         }
1523                         if (!priv->page_user_details) {
1524                                 priv->page_user_details = create_page_user_details (self);
1525                                 modest_easysetup_wizard_dialog_append_page (notebook, 
1526                                                                             priv->page_user_details,
1527                                                                             _("mcen_ti_emailsetup_userdetails"));
1528                         }
1529                 }
1530                 
1531                 /* Create the easysetup pages: */
1532                 create_subsequent_easysetup_pages (self);
1533         }
1534 }
1535
1536
1537 static gchar*
1538 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocolType protocol_type)
1539 {
1540         const gchar* hostname = NULL;
1541         gchar* at;
1542         gchar* domain;
1543         ModestProtocolRegistry *protocol_registry;
1544         ModestProtocol *protocol;
1545
1546         if (!email_address)
1547                 return NULL;
1548         
1549         at = g_utf8_strchr (email_address, -1, '@');
1550         if (!at || (g_utf8_strlen (at, -1) < 2))
1551                 return NULL;
1552                 
1553         domain = g_utf8_next_char (at);
1554         if(!domain)
1555                 return NULL;
1556
1557         protocol_registry = modest_runtime_get_protocol_registry ();
1558         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
1559                 
1560         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS) ||
1561             modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
1562                 hostname = modest_protocol_get_name (protocol);
1563         }
1564         
1565         if (!hostname)
1566                 return NULL;
1567                 
1568         return g_strdup_printf ("%s.%s", hostname, domain);
1569 }
1570
1571 static void 
1572 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1573 {
1574         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1575
1576         if (!priv->entry_incomingserver)
1577                 return;
1578                 
1579         /* Set a default domain for the server, based on the email address,
1580          * if no server name was already specified.
1581          */
1582         if (priv->entry_user_email
1583             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1584                 const ModestProtocolType protocol_type = easysetup_servertype_combo_box_get_active_servertype (
1585                         EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype));
1586
1587                 /* This could happen when the combo box has still no active iter */
1588                 if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1589                         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));    
1590                         gchar* servername = util_get_default_servername_from_email_address (email_address, 
1591                                                                                             protocol_type);
1592
1593                         /* Do not set the INCOMING_CHANGED flag because of this edit */
1594                         g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1595                         gtk_entry_set_text (GTK_ENTRY (priv->entry_incomingserver), servername);
1596                         g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1597                         
1598                         g_free (servername);
1599                 }
1600         }
1601         
1602         /* Set a default domain for the server, based on the email address,
1603          * if no server name was already specified.
1604          */
1605         if (!priv->entry_outgoingserver)
1606                 return;
1607                 
1608         if (priv->entry_user_email
1609             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1610                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));
1611                 
1612                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOLS_TRANSPORT_SMTP);
1613
1614                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1615                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1616                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), servername);
1617                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1618
1619                 g_free (servername);
1620         }
1621 }
1622
1623 static gchar*
1624 get_entered_account_title (ModestEasysetupWizardDialog *self)
1625 {
1626         ModestEasysetupWizardDialogPrivate *priv;
1627         const gchar* account_title;
1628
1629         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1630         account_title = gtk_entry_get_text (GTK_ENTRY (priv->entry_account_title));
1631
1632         if (!account_title || (g_utf8_strlen (account_title, -1) == 0)) {
1633                 return NULL;
1634         } else {
1635                 /* Strip it of whitespace at the start and end: */
1636                 gchar *result = g_strdup (account_title);
1637                 result = g_strstrip (result);
1638                 
1639                 if (!result)
1640                         return NULL;
1641                         
1642                 if (g_utf8_strlen (result, -1) == 0) {
1643                         g_free (result);
1644                         return NULL;    
1645                 }
1646                 
1647                 return result;
1648         }
1649 }
1650
1651 static gboolean
1652 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1653 {
1654         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1655         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1656         ModestProtocolRegistry *protocol_registry;
1657
1658         protocol_registry = modest_runtime_get_protocol_registry ();
1659
1660         /* if are browsing pages previous to the last one, then we have pending settings in
1661          * this wizard */
1662         if (next_page != NULL)
1663                 priv->pending_load_settings = TRUE;
1664         
1665         /* Do extra validation that couldn't be done for every key press,
1666          * either because it was too slow,
1667          * or because it requires interaction:
1668          */
1669         if (current_page == priv->page_account_details) {       
1670                 /* Check that the title is not already in use: */
1671                 gchar* account_title = get_entered_account_title (self);
1672                 if (!account_title)
1673                         return FALSE;
1674                         
1675                 /* Aavoid a clash with an existing display name: */
1676                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1677                         priv->account_manager, account_title);
1678
1679                 if (name_in_use) {
1680                         /* Warn the user via a dialog: */
1681                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1682             
1683                         return FALSE;
1684                 }
1685
1686                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1687                 create_subsequent_pages (self);
1688
1689         } else if (current_page == priv->page_user_details) {
1690                 /* Check that the email address is valud: */
1691                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1692                 if ((!email_address) || (g_utf8_strlen (email_address, -1) == 0))
1693                         return FALSE;
1694                         
1695                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1696                         /* Warn the user via a dialog: */
1697                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1698                                              
1699                         /* Return focus to the email address entry: */
1700                         gtk_widget_grab_focus (priv->entry_user_email);
1701                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1702
1703                         return FALSE;
1704                 }
1705         }
1706           
1707         if (next_page == priv->page_custom_incoming) {
1708                 set_default_custom_servernames (self);
1709         } else if (next_page == priv->page_custom_outgoing) {
1710                 set_default_custom_servernames (self);
1711
1712                 /* Check if the server supports secure authentication */
1713 /*              if (modest_security_options_view_auth_check (security_options)) */
1714 /*                      if (!check_has_supported_auth_methods (self)) */
1715 /*                              return FALSE; */
1716                 gtk_widget_show (priv->outgoing_security);
1717         }
1718         
1719         /* If this is the last page, and this is a click on Finish, 
1720          * then attempt to create the dialog.
1721          */
1722         if(!next_page && 
1723            current_page != priv->page_account_details) /* This is NULL when this is a click on Finish. */
1724         {
1725                 if (priv->pending_load_settings) {
1726                         save_to_settings (self);
1727                 }
1728
1729                 /* We check if there's already another account with the same configuration */
1730                 if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
1731                         modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1732                         return FALSE;
1733                 }
1734
1735                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1736         }
1737         
1738         
1739         return TRUE;
1740 }
1741
1742 static gboolean entry_is_empty (GtkWidget *entry)
1743 {
1744         if (!entry)
1745                 return FALSE;
1746                 
1747         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1748         if ((!text) || (g_utf8_strlen (text, -1) == 0))
1749                 return TRUE;
1750         else {
1751                 /* Strip it of whitespace at the start and end: */
1752                 gchar *stripped = g_strdup (text);
1753                 stripped = g_strstrip (stripped);
1754                 
1755                 if (!stripped)
1756                         return TRUE;
1757                         
1758                 const gboolean result = (g_utf8_strlen (stripped, -1) == 0);
1759                 
1760                 g_free (stripped);
1761                 return result;
1762         }
1763 }
1764
1765 static void
1766 real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next)
1767 {               
1768         GtkNotebook *notebook = NULL;
1769         gboolean is_finish_tab;
1770         GtkWidget *current;
1771         ModestEasysetupWizardDialogPrivate *priv;
1772
1773         /* Get data */
1774         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (dialog);
1775         g_object_get (dialog, "wizard-notebook", &notebook, NULL);
1776         
1777         /* Disable the Finish button until we are on the last page,
1778          * because HildonWizardDialog enables this for all but the
1779          * first page */
1780         current = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
1781         is_finish_tab = ((current == priv->page_complete_easysetup) ||
1782                          (current == priv->page_complete_customsetup));
1783
1784         if (is_finish_tab) {
1785                 /* Disable Next on the last page no matter what the
1786                    argument say */
1787                 enable_next = FALSE;
1788         } else {
1789                 /* Not the last one */
1790                 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1791                                                    MODEST_WIZARD_DIALOG_FINISH,
1792                                                    FALSE);
1793         }
1794         
1795         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1796                                            MODEST_WIZARD_DIALOG_NEXT,
1797                                            enable_next);
1798 }
1799
1800 static void
1801 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1802 {
1803         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1804         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1805         
1806         gboolean enable_next = TRUE;
1807         if (current_page == priv->page_welcome) {
1808                 enable_next = TRUE;
1809         } else if (current_page == priv->page_account_details) {
1810                 /* The account details title is mandatory: */
1811                 if (entry_is_empty(priv->entry_account_title))
1812                         enable_next = FALSE;
1813         } else if (current_page == priv->page_user_details) {   
1814                 /* The user details username is mandatory: */
1815                 if (entry_is_empty(priv->entry_user_username))
1816                         enable_next = FALSE;
1817                         
1818                 /* The user details email address is mandatory: */
1819                 if (enable_next && entry_is_empty (priv->entry_user_email))
1820                         enable_next = FALSE;
1821         } else if (current_page == priv->page_custom_incoming) {
1822                 /* The custom incoming server is mandatory: */
1823                 if (entry_is_empty(priv->entry_incomingserver))
1824                         enable_next = FALSE;
1825         } else if (MODEST_IS_EASYSETUP_WIZARD_PAGE (current_page)) {
1826                 enable_next = !modest_easysetup_wizard_page_validate (
1827                        MODEST_EASYSETUP_WIZARD_PAGE (current_page));
1828         }
1829                         
1830         /* Enable/disable buttons */ 
1831         real_enable_buttons (dialog, enable_next);
1832 }
1833
1834 static void
1835 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1836 {
1837         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1838         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1839
1840
1841         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1842         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1843         
1844         /* Provide a vfunc implementation so we can decide 
1845          * when to enable/disable the prev/next buttons.
1846          */
1847         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1848         base_klass->before_next = on_before_next;
1849         base_klass->enable_buttons = on_enable_buttons;
1850 }
1851
1852 /**
1853  * save_to_settings:
1854  * @self: a #ModestEasysetupWizardDialog
1855  *
1856  * takes information from all the wizard and stores it in settings
1857  */
1858 static void
1859 save_to_settings (ModestEasysetupWizardDialog *self)
1860 {
1861         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1862         guint special_port;
1863         gchar *provider_id;
1864         gchar* display_name;
1865         const gchar *username, *password;
1866         gchar *store_hostname, *transport_hostname;
1867         guint store_port, transport_port;
1868         ModestProtocolRegistry *protocol_registry;
1869         ModestProtocolType store_protocol, transport_protocol;
1870         ModestProtocolType store_security, transport_security;
1871         ModestProtocolType store_auth_protocol, transport_auth_protocol;
1872         ModestServerAccountSettings *store_settings, *transport_settings;
1873         const gchar *fullname, *email_address;
1874         EasysetupProviderComboBox *combo;
1875         EasysetupProviderComboBoxIdType id_type;
1876
1877         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1878         combo = EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider);
1879         protocol_registry = modest_runtime_get_protocol_registry ();
1880
1881         /* Get details from the specified presets: */
1882         id_type = easysetup_provider_combo_box_get_active_id_type (combo);
1883         provider_id = easysetup_provider_combo_box_get_active_provider_id (combo);
1884                 
1885         /* Let the plugin save the settings. We do a return in order
1886            to save an indentation level */
1887         if (id_type == EASYSETUP_PROVIDER_COMBO_BOX_ID_PLUGIN_PROTOCOL) {
1888                 ModestProtocol *protocol;
1889
1890                 protocol = modest_protocol_registry_get_protocol_by_name (
1891                        modest_runtime_get_protocol_registry (),
1892                        MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1893                        provider_id);
1894
1895                 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1896                         gint n_pages, i = 0;
1897                         GtkNotebook *notebook;
1898                         GList *wizard_pages = NULL;
1899
1900                         g_object_get (self, "wizard-notebook", &notebook, NULL);
1901                         n_pages = gtk_notebook_get_n_pages (notebook);
1902                         for (i = 0; i < n_pages; i++) {
1903                                 GtkWidget *page = gtk_notebook_get_nth_page (notebook, i);
1904                                 if (MODEST_IS_EASYSETUP_WIZARD_PAGE (page))
1905                                         wizard_pages  = g_list_append (wizard_pages, page);
1906                         }
1907                         modest_account_protocol_save_wizard_settings (MODEST_ACCOUNT_PROTOCOL (protocol),
1908                                                                       wizard_pages,
1909                                                                       priv->settings);
1910                         g_list_free (wizard_pages);
1911                 } else {
1912                         g_warning ("The selected protocol is a plugin protocol "//
1913                                    "but it's not a ModestAccountProtocol");
1914                 }
1915
1916                 return;
1917         }
1918
1919         /* username and password (for both incoming and outgoing): */
1920         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1921         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1922
1923         store_settings = modest_account_settings_get_store_settings (priv->settings);
1924         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1925
1926         /* Incoming server: */
1927         /* Note: We need something as default for the transport store protocol values, 
1928          * or modest_account_mgr_add_server_account will fail. */
1929         store_port = 0;
1930         store_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1931         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
1932         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1933
1934         if (provider_id) {
1935                 ModestProtocolType store_provider_server_type;
1936                 gboolean store_provider_use_alternate_port;
1937                 /* Use presets: */
1938                 store_hostname = modest_presets_get_server (priv->presets, provider_id, 
1939                                                             TRUE /* store */);
1940                 
1941                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
1942                                                                          provider_id, 
1943                                                                          TRUE /* store */);
1944                 store_security  = modest_presets_get_info_server_security (priv->presets,
1945                                                                                     provider_id, 
1946                                                                                     TRUE /* store */);
1947                 store_auth_protocol  = modest_presets_get_info_server_auth (priv->presets,
1948                                                                                      provider_id, 
1949                                                                                      TRUE /* store */);
1950                 store_provider_use_alternate_port  = modest_presets_get_info_server_use_alternate_port (priv->presets,
1951                                                                                                         provider_id, 
1952                                                                                                         TRUE /* store */);
1953
1954                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1955                 if (store_provider_server_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID)
1956                         store_protocol = MODEST_PROTOCOLS_STORE_POP;
1957                 else
1958                         store_protocol = store_provider_server_type;
1959
1960                 /* we check if there is a *special* port */
1961                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
1962                 if (special_port != 0)
1963                         store_port = special_port;
1964                 else 
1965                         store_port = get_port_from_protocol(store_provider_server_type, store_security);
1966
1967                 modest_server_account_settings_set_security_protocol (store_settings, 
1968                                                                       store_security);
1969                 modest_server_account_settings_set_auth_protocol (store_settings, 
1970                                                                   store_auth_protocol);
1971                 if (store_port != 0)
1972                         modest_server_account_settings_set_port (store_settings, store_port);
1973         } else {
1974                 /* Use custom pages because no preset was specified: */
1975                 store_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver) ));               
1976                 store_protocol = easysetup_servertype_combo_box_get_active_servertype (
1977                         EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype));              
1978
1979                 modest_security_options_view_save_settings (
1980                                     MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
1981                                     priv->settings);
1982         }
1983
1984         /* now we store the common store account settings */
1985         modest_server_account_settings_set_hostname (store_settings, store_hostname);
1986         modest_server_account_settings_set_username (store_settings, username);
1987         modest_server_account_settings_set_password (store_settings, password);
1988         modest_server_account_settings_set_protocol (store_settings, store_protocol);
1989
1990         g_object_unref (store_settings);
1991         g_free (store_hostname);
1992         
1993         /* Outgoing server: */
1994         transport_hostname = NULL;
1995         transport_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1996         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
1997         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1998         transport_port = 0;
1999         
2000         if (provider_id) {
2001                 ModestProtocolType transport_provider_server_type;
2002                 ModestProtocolType transport_provider_security;
2003
2004                 /* Use presets */
2005                 transport_hostname = modest_presets_get_server (priv->presets, provider_id, 
2006                                                                 FALSE /* transport */);
2007                         
2008                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2009                                                                                       provider_id, 
2010                                                                                       FALSE /* transport */);           
2011                 transport_provider_security = modest_presets_get_info_server_security (priv->presets, 
2012                                                                                        provider_id, 
2013                                                                                        FALSE /* transport */);
2014
2015                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
2016                 transport_protocol = transport_provider_server_type;
2017                 transport_security = transport_provider_security;
2018                 if (transport_security == MODEST_PROTOCOLS_CONNECTION_SSL) {
2019                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
2020                         /* we check if there is a *special* port */
2021                         special_port = modest_presets_get_port (priv->presets, provider_id,
2022                                                                 FALSE /* transport */);
2023                         if (special_port != 0)
2024                                 transport_port = special_port;
2025                         else 
2026                                 transport_port = 465;
2027                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
2028                 } else {
2029                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
2030                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2031                 }
2032
2033                 modest_server_account_settings_set_security_protocol (transport_settings, 
2034                                                                       transport_security);
2035                 modest_server_account_settings_set_auth_protocol (transport_settings, 
2036                                                                   transport_auth_protocol);
2037                 if (transport_port != 0)
2038                         modest_server_account_settings_set_port (transport_settings, 
2039                                                                  transport_port);
2040         } else {
2041                 ModestProtocolRegistry *registry;
2042                 ModestProtocol *store_proto;
2043
2044                 registry = modest_runtime_get_protocol_registry ();
2045                 /* Use custom pages because no preset was specified: */
2046                 transport_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver) ));
2047
2048                 store_proto = modest_protocol_registry_get_protocol_by_type (registry, 
2049                                                                              store_protocol);
2050
2051                 if (transport_protocol == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
2052                         /* fallback to SMTP if none was specified */
2053                         g_warning ("No transport protocol was specified for store %d (%s)",
2054                                    modest_protocol_get_type_id (store_proto),
2055                                    modest_protocol_get_display_name (store_proto));
2056                         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP;
2057                 }
2058
2059                 modest_security_options_view_save_settings (
2060                                     MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security),
2061                                     priv->settings);
2062         }
2063
2064         /* now we store the common transport account settings */
2065         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
2066         modest_server_account_settings_set_username (transport_settings, username);
2067         modest_server_account_settings_set_password (transport_settings, password);
2068         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
2069
2070         g_object_unref (transport_settings);
2071         g_free (transport_hostname);
2072         
2073         fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
2074         email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
2075         modest_account_settings_set_fullname (priv->settings, fullname);
2076         modest_account_settings_set_email_address (priv->settings, email_address);
2077         /* we don't set retrieve type to preserve advanced settings if
2078            any. By default account settings are set to headers only */
2079         
2080         /* Save the connection-specific SMTP server accounts. */
2081         modest_account_settings_set_use_connection_specific_smtp 
2082                 (priv->settings, 
2083                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
2084
2085         display_name = get_entered_account_title (self);
2086         modest_account_settings_set_display_name (priv->settings, display_name);
2087         g_free (display_name);
2088 }
2089