2007-04-06 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-easysetup-wizard.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <hildon-widgets/hildon-caption.h>
18 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
19 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
20 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
21 #include "maemo/easysetup/modest-easysetup-serversecurity-combo-box.h"
22 #include "maemo/easysetup/modest-easysetup-secureauth-combo-box.h"
23 #include "maemo/easysetup/modest-validating-entry.h"
24 #include "modest-text-utils.h"
25 #include "modest-account-mgr.h"
26 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
27 #include "maemo/modest-connection-specific-smtp-window.h"
28 #include <gconf/gconf-client.h>
29 #include <string.h> /* For strlen(). */
30
31 /* Include config.h so that _() works: */
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
37
38 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
39
40 #define WIZARD_DIALOG_GET_PRIVATE(o) \
41         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
42
43 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
44
45 struct _ModestEasysetupWizardDialogPrivate
46 {
47         ModestPresets *presets;
48 };
49
50 static void
51 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
52                                                                                                                         GValue *value, GParamSpec *pspec)
53 {
54         switch (property_id) {
55         default:
56                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
57         }
58 }
59
60 static void
61 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
62                                                                                                                         const GValue *value, GParamSpec *pspec)
63 {
64         switch (property_id) {
65         default:
66                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
67         }
68 }
69
70 static void
71 modest_easysetup_wizard_dialog_dispose (GObject *object)
72 {
73         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
74                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
75 }
76
77 static void
78 modest_easysetup_wizard_dialog_finalize (GObject *object)
79 {
80         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
81         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
82         
83         if (self->account_manager)
84                 g_object_unref (G_OBJECT (self->account_manager));
85                 
86         if (priv->presets)
87                 modest_presets_destroy (priv->presets);
88         
89         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
90 }
91
92 static void
93 show_error (GtkWindow *parent_window, const gchar* text);
94
95 static gboolean
96 create_account (ModestEasysetupWizardDialog *self);
97
98 static void
99 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
100
101 static void
102 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
103
104 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
105
106 static gchar*
107 util_increment_name (const gchar* text);
108
109 static void
110 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
111 {
112         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
113         
114         /* Call the vfunc, which may be overridden by derived classes: */
115         if (klass->enable_buttons) {
116                 GtkNotebook *notebook = NULL;
117                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
118                 
119                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
120                 if (current_page_num == -1)
121                         return;
122                         
123                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
124                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
125         }
126 }
127
128 static void
129 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
130 {
131         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
132         g_assert(self);
133         invoke_enable_buttons_vfunc(self);
134 }
135
136 static void
137 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
138 {
139         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
140         g_assert(self);
141         invoke_enable_buttons_vfunc(self);
142 }
143
144 /** This is a convenience function to create a caption containing a mandatory widget.
145  * When the widget is edited, the enable_buttons() vfunc will be called.
146  */
147 static GtkWidget* create_caption_new_with_asterix(ModestEasysetupWizardDialog *self,
148         GtkSizeGroup *group,
149         const gchar *value,
150         GtkWidget *control,
151         GtkWidget *icon,
152         HildonCaptionStatus flag)
153 {
154   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
155   
156 /* The translated strings seem to already contain the *,
157  * but this code can be used if that is not true in future.
158  */
159 #if 0
160         /* Add a * character to indicate mandatory fields,
161          * as specified in our "Email UI Specification": */
162         if (flag == HILDON_CAPTION_MANDATORY) {
163                 gchar* title = g_strdup_printf("%s*", value);
164                 caption = hildon_caption_new (group, title, control, icon, flag);       
165                 g_free(title);
166         }       
167         else
168                 caption = hildon_caption_new (group, value, control, icon, flag);
169 #endif
170
171         /* Connect to the appropriate changed signal for the widget, 
172          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
173          */
174         if (GTK_IS_ENTRY (control)) {
175                 g_signal_connect (G_OBJECT (control), "changed",
176                 G_CALLBACK (on_caption_entry_changed), self);
177                 
178         }
179         else if (GTK_IS_COMBO_BOX (control)) {
180                 g_signal_connect (G_OBJECT (control), "changed",
181                 G_CALLBACK (on_caption_combobox_changed), self);
182         }
183          
184         return caption;
185 }
186            
187 static GtkWidget*
188 create_page_welcome (ModestEasysetupWizardDialog *self)
189 {
190         GtkWidget *box = gtk_vbox_new (FALSE, 2);
191         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
192         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
193         gtk_widget_show (label);
194         gtk_widget_show (GTK_WIDGET (box));
195         return GTK_WIDGET (box);
196 }
197
198 static void
199 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
200 {
201         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
202         g_assert(self);
203         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
204         
205         /* Fill the providers combo, based on the selected country: */
206         gint mcc_id = easysetup_country_combo_box_get_active_country_id (
207                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
208         easysetup_provider_combo_box_fill (
209                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, mcc_id);
210 }
211
212 static void
213 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
214 {
215         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
216         g_assert(self);
217         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
218         
219         /* Fill the providers combo, based on the selected country: */
220         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
221                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
222         
223         gchar* domain_name = NULL;
224         if(provider_id)
225           domain_name = modest_presets_get_domain (priv->presets, provider_id);
226         
227         if(!domain_name)
228                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
229                 
230         if (self->entry_user_email)
231                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
232                 
233     g_free (domain_name);
234         
235         g_free (provider_id);
236 }
237
238
239 static GtkWidget*
240 create_page_account_details (ModestEasysetupWizardDialog *self)
241 {
242         GtkWidget *box = gtk_vbox_new (FALSE, 2);
243         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
244         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
245         gtk_widget_show (label);
246         
247         /* Create a size group to be used by all captions.
248          * Note that HildonCaption does not create a default size group if we do not specify one.
249          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
250         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
251
252         /* The country widgets: */
253         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
254         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_country"), 
255                 self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
256         gtk_widget_show (self->combo_account_country);
257         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
258         gtk_widget_show (caption);
259         
260         /* connect to country combo's changed signal, so we can fill the provider combo: */
261     g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
262             G_CALLBACK (on_combo_account_country), self);
263             
264         
265         /* The service provider widgets: */     
266         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
267         
268         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_serviceprovider"), 
269                 self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
270         gtk_widget_show (self->combo_account_serviceprovider);
271         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
272         gtk_widget_show (caption);
273         
274         /* connect to providers combo's changed signal, so we can fill the email address: */
275     g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
276             G_CALLBACK (on_combo_account_serviceprovider), self);
277         
278         /* TODO: Default to the current country somehow.
279          * But I don't know how to get the information that is specified in the 
280          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
281          *
282          * This is probably not the best choice of gconf key:
283          * This is the  "mcc used in the last pairing", ie. the last connection you made.
284      * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
285      *
286          */
287         GConfClient *client = gconf_client_get_default ();
288         GError *error = NULL;
289         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
290         gint mcc_id = gconf_client_get_int(client, key, &error);
291         
292         if(mcc_id < 0)
293         mcc_id = 0;
294      
295     if (error) {
296         g_warning ("Error getting gconf key %s:\n%s", key, error->message);
297         g_error_free (error);
298         error = NULL;
299         
300         mcc_id = 0;
301     }
302     
303     /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
304      * This might just be a Maemo bug.
305      */
306     if (mcc_id == 0) 
307     {
308         /* For now, we default to Finland when there is nothing better: */
309         mcc_id = 244;
310     }
311    
312         easysetup_country_combo_box_set_active_country_id (
313                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
314                 
315         
316         /* The description widgets: */  
317         self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
318         
319         /* Set a default account title, choosing one that does not already exist: */
320         gchar* default_acount_name = g_strdup (_("mcen_ia_emailsetup_defaultname"));
321         while (modest_account_mgr_account_exists (self->account_manager, 
322                 default_acount_name, FALSE /*  server_account */)) {
323                         
324                 gchar * default_account_name2 = util_increment_name (default_acount_name);
325                 g_free (default_acount_name);
326                 default_acount_name = default_account_name2;
327         }
328         
329         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_acount_name);
330         g_free (default_acount_name);
331         default_acount_name = NULL;
332
333         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
334                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
335         gtk_widget_show (self->entry_account_title);
336         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
337         gtk_widget_show (caption);
338         
339         /* Prevent the use of some characters in the account title, 
340          * as required by our UI specification: */
341         GList *list_prevent = NULL;
342         list_prevent = g_list_append (list_prevent, "\\");
343         list_prevent = g_list_append (list_prevent, "/");
344         list_prevent = g_list_append (list_prevent, ":");
345         list_prevent = g_list_append (list_prevent, "*");
346         list_prevent = g_list_append (list_prevent, "?");
347         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
348         list_prevent = g_list_append (list_prevent, "“");
349         list_prevent = g_list_append (list_prevent, "<"); 
350         list_prevent = g_list_append (list_prevent, ">"); 
351         list_prevent = g_list_append (list_prevent, "|");
352         list_prevent = g_list_append (list_prevent, "^");       
353         easysetup_validating_entry_set_unallowed_characters (
354                 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
355         g_list_free (list_prevent);
356         
357         /* Set max length as in the UI spec:
358          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
359         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
360         
361         gtk_widget_show (GTK_WIDGET (box));
362         
363         return GTK_WIDGET (box);
364 }
365
366 static GtkWidget*
367 create_page_user_details (ModestEasysetupWizardDialog *self)
368 {
369         GtkWidget *box = gtk_vbox_new (FALSE, 2);
370         
371         /* Create a size group to be used by all captions.
372          * Note that HildonCaption does not create a default size group if we do not specify one.
373          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
374         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
375          
376         /* The name widgets: */
377         self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
378         /* Set max length as in the UI spec:
379          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
380         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
381         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
382                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
383         gtk_widget_show (self->entry_user_name);
384         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
385         gtk_widget_show (caption);
386         
387         /* Prevent the use of some characters in the name, 
388          * as required by our UI specification: */
389         GList *list_prevent = NULL;
390         list_prevent = g_list_append (list_prevent, "<");
391         list_prevent = g_list_append (list_prevent, ">");
392         easysetup_validating_entry_set_unallowed_characters (
393                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
394         g_list_free (list_prevent);
395         
396         /* The username widgets: */     
397         self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
398         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
399                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
400         gtk_widget_show (self->entry_user_username);
401         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
402         gtk_widget_show (caption);
403         
404         /* Prevent the use of some characters in the username, 
405          * as required by our UI specification: */
406         easysetup_validating_entry_set_unallowed_characters_whitespace (
407                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
408         
409         /* Set max length as in the UI spec:
410          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
411         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
412         
413         /* The password widgets: */     
414         self->entry_user_password = gtk_entry_new ();
415         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
416         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
417         caption = create_caption_new_with_asterix (self, sizegroup, 
418                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
419         gtk_widget_show (self->entry_user_password);
420         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
421         gtk_widget_show (caption);
422         
423         /* The email address widgets: */        
424         self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
425         caption = create_caption_new_with_asterix (self, sizegroup, 
426                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
427         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
428         gtk_widget_show (self->entry_user_email);
429         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
430         gtk_widget_show (caption);
431         
432         /* Set max length as in the UI spec:
433          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
434         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
435         
436         
437         gtk_widget_show (GTK_WIDGET (box));
438         
439         return GTK_WIDGET (box);
440 }
441
442 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
443 {
444         GtkWidget *box = gtk_vbox_new (FALSE, 2);
445         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
446         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
447         gtk_widget_show (label);
448         gtk_widget_show (GTK_WIDGET (box));
449         return GTK_WIDGET (box);
450 }
451
452 /** Change the caption title for the incoming server, 
453  * as specified in the UI spec:
454  */
455 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
456 {
457         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
458                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
459         const gchar* type = 
460                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
461                         _("mail_fi_emailtype_pop3") : 
462                         _("mail_fi_emailtype_imap") );
463                         
464                 
465         /* Note that this produces a compiler warning, 
466          * because the compiler does not know that the translated string will have a %s in it.
467          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
468         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
469         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
470         g_free(incomingserver_title);
471 }
472
473 /** Change the caption title for the incoming server, 
474  * as specified in the UI spec:
475  */
476 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
477 {
478         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
479                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
480         
481         /* Fill the combo with appropriately titled choices for POP or IMAP. */
482         /* The choices are the same, but the titles are different, as in the UI spec. */
483         easysetup_serversecurity_combo_box_fill (
484                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
485 }
486
487 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
488 {
489         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
490         update_incoming_server_title (self);
491         update_incoming_server_security_choices (self);
492 }
493            
494 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
495 {
496         GtkWidget *box = gtk_vbox_new (FALSE, 2);
497         
498         /* Create a size group to be used by all captions.
499          * Note that HildonCaption does not create a default size group if we do not specify one.
500          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
501         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
502          
503         /* The incoming server widgets: */
504         if (!self->combo_incoming_servertype)
505                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
506         easysetup_servertype_combo_box_set_active_servertype (
507                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
508         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
509                 _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
510         gtk_widget_show (self->combo_incoming_servertype);
511         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
512         gtk_widget_show (caption);
513         
514         if(!self->entry_incomingserver)
515                 self->entry_incomingserver = gtk_entry_new ();
516         set_default_custom_servernames (self);
517
518         if (self->caption_incoming)
519           gtk_widget_destroy (self->caption_incoming);
520            
521         /* The caption title will be updated in update_incoming_server_title().
522          * so this default text will never be seen: */
523         /* (Note: Changing the title seems pointless. murrayc) */
524         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
525                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
526         update_incoming_server_title (self);
527         gtk_widget_show (self->entry_incomingserver);
528         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
529         gtk_widget_show (self->caption_incoming);
530         
531         /* Change the caption title when the servertype changes, 
532          * as in the UI spec: */
533          g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
534                 G_CALLBACK (on_combo_servertype_changed), self);
535         
536         /* The secure connection widgets: */    
537         if (!self->combo_incoming_security)
538                 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
539         update_incoming_server_security_choices (self);
540         easysetup_serversecurity_combo_box_set_active_serversecurity (
541                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_SECURITY_NONE);
542         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
543                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
544         gtk_widget_show (self->combo_incoming_security);
545         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
546         gtk_widget_show (caption);
547         
548         if(!self->checkbox_incoming_auth)
549                 self->checkbox_incoming_auth = 
550                         gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
551         gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
552         gtk_widget_show (self->checkbox_incoming_auth);
553         
554         gtk_widget_show (GTK_WIDGET (box));
555         
556         return GTK_WIDGET (box);
557 }
558
559 static void
560 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
561 {
562         GtkWidget *widget = GTK_WIDGET (user_data);
563         
564         /* Enable the widget only if the toggle button is active: */
565         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
566         gtk_widget_set_sensitive (widget, enable);
567 }
568
569 /* Make the sensitivity of a widget depend on a toggle button.
570  */
571 static void
572 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
573 {
574         g_signal_connect (G_OBJECT (button), "toggled",
575                 G_CALLBACK (on_toggle_button_changed), widget);
576         
577         /* Set the starting sensitivity: */
578         on_toggle_button_changed (button, widget);
579 }
580         
581 static void
582 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
583 {
584         /* Destroy the window when it is closed: */
585         gtk_widget_destroy (GTK_WIDGET (window));
586 }
587
588 static void
589 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
590 {
591
592         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
593         
594         /* Show the window: */
595         /* TODO: Retrieve the chosen settings,
596          * so we can supply them when creating the connection somehow.
597          */
598         GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
599         gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
600         g_signal_connect (G_OBJECT (window), "hide",
601                 G_CALLBACK (on_smtp_servers_window_hide), self);
602     gtk_widget_show (window);
603 }
604
605 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
606 {
607         GtkWidget *box = gtk_vbox_new (FALSE, 2);
608         
609         /* Create a size group to be used by all captions.
610          * Note that HildonCaption does not create a default size group if we do not specify one.
611          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
612         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
613          
614         /* The outgoing server widgets: */
615         if (!self->entry_outgoingserver)
616                 self->entry_outgoingserver = gtk_entry_new ();
617         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
618                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
619         gtk_widget_show (self->entry_outgoingserver);
620         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
621         gtk_widget_show (caption);
622         set_default_custom_servernames (self);
623         
624         /* The secure connection widgets: */    
625         if (!self->combo_outgoing_security)
626                 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
627         easysetup_serversecurity_combo_box_fill (
628                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
629         easysetup_serversecurity_combo_box_set_active_serversecurity (
630                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_SECURITY_NONE);
631         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
632                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
633         gtk_widget_show (self->combo_outgoing_security);
634         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
635         gtk_widget_show (caption);
636         
637         /* The secure authentication widgets: */
638         if (!self->combo_outgoing_auth)
639                 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
640         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
641                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
642         gtk_widget_show (self->combo_outgoing_auth);
643         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
644         gtk_widget_show (caption);
645         
646         /* connection-specific checkbox: */
647         if (!self->checkbox_outgoing_smtp_specific) {
648                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
649                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
650                         FALSE);
651         }
652         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
653         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
654         
655         /* Connection-specific SMTP-Severs Edit button: */
656         if (!self->button_outgoing_smtp_servers)
657                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
658         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
659                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
660         gtk_widget_show (self->button_outgoing_smtp_servers);
661         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
662         gtk_widget_show (caption);
663         
664         /* Only enable the button when the checkbox is checked: */
665         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
666                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
667                 
668         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
669                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
670         
671         
672         gtk_widget_show (GTK_WIDGET (box));
673         
674         return GTK_WIDGET (box);
675 }
676
677 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
678 {
679         GtkWidget *box = gtk_vbox_new (FALSE, 2);
680         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
681         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
682         gtk_widget_show (label);
683         
684         if (!self->button_edit)
685                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
686         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
687                 self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
688         gtk_widget_show (self->button_edit);
689         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
690         gtk_widget_show (caption);
691         
692         gtk_widget_show (GTK_WIDGET (box));
693         return GTK_WIDGET (box);
694 }
695
696
697 /*
698  */
699 static void 
700 on_response (ModestWizardDialog *wizard_dialog,
701         gint response_id,
702         gpointer user_data)
703 {
704         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
705         invoke_enable_buttons_vfunc (self);
706 }
707
708 static void
709 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
710 {
711         /* Create the notebook to be used by the ModestWizardDialog base class:
712          * Each page of the notebook will be a page of the wizard: */
713         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
714         
715     /* Set the notebook used by the ModestWizardDialog base class: */
716     g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
717     
718     /* Set the wizard title:
719      * The actual window title will be a combination of this and the page's tab label title. */
720     g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
721
722         /* Read in the information about known service providers: */
723         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
724         
725         const gchar* filepath = "/usr/share/operator-wizard/provider-data.keyfile";
726         priv->presets = modest_presets_new (filepath); /* TODO: the actual filepath. */
727         if (!(priv->presets))
728         {
729                 const gchar* filepath_hack = HACK_TOP_SRCDIR "src/maemo/easysetup/provider-data-test.keyfile";
730                 g_warning ("Could not locate the official provider data keyfile from %s, "
731                         "so attempting to load it instead from %s", filepath, filepath_hack);
732                 priv->presets = modest_presets_new (filepath_hack); /* TODO: the actual filepath. */
733         }
734         
735         g_assert(priv->presets);
736         
737         
738         /* Get the account manager object, 
739          * so we can check for existing accounts,
740          * and create new accounts: */
741         self->account_manager = modest_runtime_get_account_mgr ();
742         g_assert (self->account_manager);
743         g_object_ref (self->account_manager);
744         
745     /* Create the common pages, 
746      */
747     self->page_welcome = create_page_welcome (self);
748         self->page_account_details = create_page_account_details (self);
749         self->page_user_details = create_page_user_details (self);
750         
751         /* Add the common pages: */
752         gtk_notebook_append_page (notebook, self->page_welcome, 
753                 gtk_label_new (_("mcen_ti_emailsetup_welcome")));
754         gtk_notebook_append_page (notebook, self->page_account_details, 
755                 gtk_label_new (_("mcen_ti_accountdetails")));
756         gtk_notebook_append_page (notebook, self->page_user_details, 
757                 gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
758                 
759         /* Create and add the easysetup-specific pages,
760          * because we need _some_ final page to enable the Next and Finish buttons: */
761         create_subsequent_easysetup_pages (self);
762
763             
764     /* Connect to the dialog's response signal so we can enable/disable buttons 
765      * for the newly-selected page, because the prev/next buttons cause response to be emitted.
766      * Note that we use g_signal_connect_after() instead of g_signal_connect()
767      * so that we can be enable/disable after ModestWizardDialog has done its own 
768      * enabling/disabling of buttons.
769      * 
770      * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
771      * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
772      * 
773      * It's not enough to connect to the notebook's switch-page signal, because 
774      * ModestWizardDialog's "response" signal handler enables the buttons itself, 
775      * _after_ switching the page (understandably).
776      * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
777      * then gtk_notebook_get_current_page() would return an incorrect value.)
778      */
779     g_signal_connect_after (G_OBJECT (self), "response",
780             G_CALLBACK (on_response), self);       
781 }
782
783 ModestEasysetupWizardDialog*
784 modest_easysetup_wizard_dialog_new (void)
785 {
786         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
787 }
788
789 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
790 {
791         GtkNotebook *notebook = NULL;
792         g_object_get (self, "wizard-notebook", &notebook, NULL);
793         g_assert(notebook);
794         
795         /* Create the custom pages: */
796         if(!(self->page_custom_incoming)) {
797                 self->page_custom_incoming = create_page_custom_incoming (self);
798         }
799                 
800         if(!(self->page_custom_outgoing)) {
801                 self->page_custom_outgoing = create_page_custom_outgoing (self);
802         }
803         
804         if(!(self->page_complete_customsetup)) {
805                 self->page_complete_customsetup = create_page_complete_custom (self);
806         }
807         
808         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
809                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
810                         gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
811         
812         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
813                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
814                         gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
815                 
816         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
817                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
818                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
819 }
820         
821 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
822 {
823         GtkNotebook *notebook = NULL;
824         g_object_get (self, "wizard-notebook", &notebook, NULL);
825         g_assert(notebook);
826         
827         /* Create the easysetup-specific pages: */
828         if(!self->page_complete_easysetup)
829                 self->page_complete_easysetup = create_page_complete_easysetup (self);
830
831         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
832                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
833                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
834                         
835 }
836 /* After the user details page,
837  * the following pages depend on whether "Other" was chosen 
838  * in the provider combobox on the account page
839  */
840 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
841 {
842         if (easysetup_provider_combo_box_get_active_provider_id (
843                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
844                 /* "Other..." was selected: */
845                 
846                 /* Make sure that the easysetup pages do not exist: */
847                 if(self->page_complete_easysetup) {
848                         gtk_widget_destroy (self->page_complete_easysetup);
849                         self->page_complete_easysetup = NULL;
850                 }
851                 
852                 create_subsequent_customsetup_pages (self);
853         }       
854         else {
855                 /* A specific provider was selected: */
856                 {
857                         /* Make sure that the custom pages do not exist:
858                          * Because they will be used if they exist, when creating the account. */
859                         if(self->page_custom_incoming) {
860                                 gtk_widget_destroy (self->page_custom_incoming);
861                                 self->page_custom_incoming = NULL;
862                         }
863                         
864                         if(self->page_custom_outgoing) {
865                                 gtk_widget_destroy (self->page_custom_outgoing);
866                                 self->page_custom_outgoing = NULL;
867                         }
868                         
869                         if(self->page_complete_customsetup) {
870                                 gtk_widget_destroy (self->page_complete_customsetup);
871                                 self->page_complete_customsetup = NULL;
872                         }
873                 }
874                 
875                 /* Create the easysetup pages: */
876                 create_subsequent_easysetup_pages (self);
877         }
878 }
879
880 static gchar*
881 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
882 {
883         if (!email_address)
884                 return NULL;
885         
886         gchar* at = g_utf8_strchr (email_address, -1, '@');
887         if (!at || (g_utf8_strlen (at, -1) < 2))
888                 return NULL;
889                 
890         gchar* domain = g_utf8_next_char (at);
891         if(!domain)
892                 return NULL;
893                 
894         const gchar* hostname = NULL;
895         if (servertype == MODEST_PROTOCOL_STORE_POP)
896                 hostname = "pop";
897         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
898                 hostname = "imap";
899         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
900                 hostname = "smtp";
901         
902         if (!hostname)
903                 return NULL;
904                 
905         return g_strdup_printf ("%s.%s", hostname, domain);
906 }
907
908 /* Add a number to the end of the text, or increment a number that is already there.
909  */
910 static gchar*
911 util_increment_name (const gchar* text)
912 {
913         /* Get the end character,
914          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
915          */
916         const gchar* end = NULL;
917         if (!g_utf8_validate (text, -1, &end))
918                 return NULL;
919   
920         if (!end)
921                 return NULL;
922                 
923         --end; /* Go to before the null-termination. */
924                 
925         /* Look at each UTF-8 characer, starting at the end: */
926         const gchar* p = end;
927         const gchar* alpha_end = NULL;
928         while (p)
929         {       
930                 /* Stop when we reach the first character that is not a numeric digit: */
931                 const gunichar ch = g_utf8_get_char (p);
932                 if (!g_unichar_isdigit (ch)) {
933                         alpha_end = p;
934                         break;
935                 }
936                 
937                 p = g_utf8_prev_char (p);       
938         }
939         
940         if(!alpha_end) {
941                 /* The text must consist completely of numeric digits. */
942                 alpha_end = text;
943         }
944         else
945                 ++alpha_end;
946         
947         /* Intepret and increment the number, if any: */
948         gint num = atol (alpha_end);
949         ++num;
950         
951         /* Get the name part: */
952         gint name_len = alpha_end - text;
953         gchar *name_without_number = g_malloc(name_len + 1);
954         memcpy (name_without_number, text, name_len);
955         name_without_number[name_len] = 0;\
956         
957     /* Concatenate the text part and the new number: */ 
958         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
959         g_free (name_without_number);
960         
961         return result;  
962 }
963         
964 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
965 {
966         if (!account_wizard->entry_incomingserver)
967                 return;
968                 
969         /* Set a default domain for the server, based on the email address,
970          * if no server name was already specified.
971          */
972         const gchar* incoming_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_incomingserver));
973         if ((!incoming_existing || (strlen(incoming_existing) == 0)) 
974                 && account_wizard->entry_user_email) {
975                 const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
976                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
977                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
978                 
979                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
980                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
981                 g_free (servername);
982         }
983         
984         /* Set a default domain for the server, based on the email address,
985          * if no server name was already specified.
986          */
987         const gchar* outgoing_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_outgoingserver));
988         if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
989                 && account_wizard->entry_user_email) {
990                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
991                 
992                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
993                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
994                 g_free (servername);
995         }
996 }
997
998 static gboolean
999 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1000 {
1001         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1002         
1003         /* Do extra validation that couldn't be done for every key press,
1004          * either because it was too slow,
1005          * or because it requires interaction:
1006          */
1007         if (current_page == account_wizard->page_account_details) {     
1008                 /* Check that the title is not already in use: */
1009                 const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
1010                 if ((!account_name) || (strlen(account_name) == 0))
1011                         return FALSE;
1012                         
1013                 gboolean name_in_use = FALSE;
1014                 name_in_use = modest_account_mgr_account_exists (account_wizard->account_manager,
1015                         account_name, FALSE /*  server_account */);
1016                 
1017                 if (name_in_use) {
1018                         /* Warn the user via a dialog: */
1019                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing."));
1020             
1021                         return FALSE;
1022                 }
1023         }
1024         else if (current_page == account_wizard->page_user_details) {
1025                 /* Check that the email address is valud: */
1026                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1027                 if ((!email_address) || (strlen(email_address) == 0))
1028                         return FALSE;
1029                         
1030                 if (!modest_text_utils_validate_email_address (email_address)) {
1031                         /* Warn the user via a dialog: */
1032                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
1033                                              
1034             /* Return focus to the email address entry: */
1035             gtk_widget_grab_focus (account_wizard->entry_user_email);
1036             
1037                         return FALSE;
1038                 }
1039                 
1040                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1041                 create_subsequent_pages (account_wizard);
1042         }
1043         
1044         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1045          * but does not specify how.
1046          */
1047           
1048         if(next_page == account_wizard->page_custom_incoming) {
1049                 set_default_custom_servernames (account_wizard);
1050         }
1051         else if (next_page == account_wizard->page_custom_outgoing) {
1052                 set_default_custom_servernames (account_wizard);
1053         }
1054         
1055         /* If this is the last page, and this is a click on Finish, 
1056          * then attempt to create the dialog.
1057          */
1058         if(!next_page) /* This is NULL when this is a click on Finish. */
1059         {
1060                 create_account (account_wizard);
1061         }
1062         
1063         
1064         return TRUE;
1065 }
1066
1067 static gboolean entry_is_empty (GtkWidget *entry)
1068 {
1069         if (!entry)
1070                 return FALSE;
1071                 
1072         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1073         if ((!text) || (strlen(text) == 0))
1074                 return TRUE;
1075         else
1076                 return FALSE;
1077 }
1078
1079 static void
1080 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1081 {
1082         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1083         
1084         gboolean enable_next = TRUE;
1085         if (current_page == account_wizard->page_welcome) {
1086                 enable_next = TRUE;
1087         }
1088         else if (current_page == account_wizard->page_account_details) {
1089                 /* The account details title is mandatory: */
1090                 if (entry_is_empty(account_wizard->entry_account_title))
1091                         enable_next = FALSE;
1092         }
1093         else if (current_page == account_wizard->page_user_details) {   
1094                 /* The user details username is mandatory: */
1095                 if (entry_is_empty(account_wizard->entry_user_username))
1096                         enable_next = FALSE;
1097                         
1098                 /* The user details email address is mandatory: */
1099                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1100                         enable_next = FALSE;
1101         }
1102         else if (current_page == account_wizard->page_custom_incoming) {
1103                 /* The custom incoming server is mandatory: */
1104                 if (entry_is_empty(account_wizard->entry_incomingserver))
1105                         enable_next = FALSE;
1106         }
1107                         
1108         /* Enable the buttons, 
1109          * identifying them via their associated response codes:
1110          */
1111         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1112     gtk_dialog_set_response_sensitive (dialog_base,
1113                                        MODEST_WIZARD_DIALOG_NEXT,
1114                                        enable_next);
1115                                        
1116     /* Disable the Finish button until we are on the last page,
1117      * because HildonWizardDialog enables this for all but the first page: */
1118     GtkNotebook *notebook = NULL;
1119         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1120         
1121     gint current = gtk_notebook_get_current_page (notebook);
1122     gint last = gtk_notebook_get_n_pages (notebook) - 1;
1123     gboolean is_last = (current == last);
1124     
1125     if(!is_last) {
1126         gtk_dialog_set_response_sensitive (dialog_base,
1127                                        MODEST_WIZARD_DIALOG_FINISH,
1128                                        FALSE);
1129     }
1130 }
1131
1132 static void
1133 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1134 {
1135         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1136         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1137
1138
1139         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1140         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1141         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1142         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1143         
1144         /* Provide a vfunc implementation so we can decide 
1145          * when to enable/disable the prev/next buttons.
1146          */
1147         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1148         base_klass->before_next = on_before_next;
1149         base_klass->enable_buttons = on_enable_buttons;
1150 }
1151  
1152 static void
1153 show_error (GtkWindow *parent_window, const gchar* text)
1154 {
1155         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1156                 (GtkDialogFlags)0,
1157                  GTK_MESSAGE_ERROR,
1158                  GTK_BUTTONS_OK,
1159                  text ));
1160                  
1161                  gtk_dialog_run (dialog);
1162                  gtk_widget_destroy (GTK_WIDGET (dialog));
1163 }
1164
1165 /** Attempt to create the account from the information that the user has entered.
1166  * @result: TRUE if the account was successfully created.
1167  */
1168 gboolean
1169 create_account (ModestEasysetupWizardDialog *self)
1170 {
1171         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1172         
1173         const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1174
1175         /* Some checks: */
1176         if (!account_name)
1177                 return FALSE;
1178                 
1179         /* We should have checked for this already, 
1180          * and changed that name accordingly, 
1181          * but let's check again just in case:
1182          */
1183         if (modest_account_mgr_account_exists (self->account_manager, account_name, FALSE)) 
1184                 return FALSE;   
1185                 
1186         /* username and password (for both incoming and outgoing): */
1187         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1188         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1189         
1190         /* Incoming server: */
1191         /* Note: We need something as default for the ModestProtocol values, 
1192          * or modest_account_mgr_add_server_account will fail. */
1193         gchar* servername_incoming = NULL;
1194         ModestProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1195         ModestProtocol protocol_security_incoming = MODEST_PROTOCOL_SECURITY_NONE;
1196         ModestProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1197         
1198         /* Get details from the specified presets: */
1199         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1200                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1201         if(provider_id) {
1202                 /* Use presets: */
1203                 
1204                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1205                         TRUE /* incoming */);
1206                 
1207                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets, provider_id, 
1208                         TRUE /* incoming */);
1209                 
1210         
1211                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1212                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP)
1213                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1214                 else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP)
1215                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1216                                 
1217                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets, provider_id, 
1218                         TRUE /* incoming */);
1219                         
1220                 
1221                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1222                         protocol_security_incoming = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1223                 
1224                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1225                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1226         }
1227         else {
1228                 /* Use custom pages because no preset was specified: */
1229                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1230                 
1231                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1232                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1233                 
1234                 protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
1235                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1236                 
1237                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1238                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1239                         : MODEST_PROTOCOL_AUTH_NONE;
1240                 
1241         }
1242         
1243         /* First we add the 2 server accounts, and then we add the account that uses them.
1244          * If we don't do it in this order then we will experience a crash. */
1245          
1246         /* Add a (incoming) server account, to be used by the account: */
1247         gchar *store_name = g_strconcat (account_name, "_store", NULL);
1248         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1249                 store_name,
1250                 servername_incoming,
1251                 username, password,
1252                 protocol_incoming,
1253                 protocol_security_incoming,
1254                 protocol_authentication_incoming);              
1255                 
1256         g_free (servername_incoming);
1257         
1258         if (!created) {
1259                 /* TODO: Provide a Logical ID for the text: */
1260                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1261                 return FALSE;   
1262         }
1263         
1264         /* Sanity check: */
1265         /* There must be at least one account now: */
1266         GSList *account_names = modest_account_mgr_account_names (self->account_manager);
1267         if(account_names != NULL)
1268         {
1269                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1270         }
1271         g_slist_free (account_names);
1272         
1273         
1274         /* Outgoing server: */
1275         gchar* servername_outgoing = NULL;
1276         ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1277         ModestProtocol protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1278         ModestProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1279         
1280         if(provider_id) {
1281                 /* Use presets: */
1282                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1283                         FALSE /* incoming */);
1284                         
1285                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets, provider_id, 
1286                         FALSE /* incoming */);
1287                 
1288                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1289                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1290                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1291                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* TODO: Is this what we want? */
1292                 
1293                 ModestPresetsSecurity security_outgoing = 
1294                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1295                                 FALSE /* incoming */);
1296                         
1297                 protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1298                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP)
1299                         protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1300                 
1301                 protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1302                 /* TODO: There is no SMTP authentication enum for presets. */
1303         }
1304         else {
1305                 /* Use custom pages because no preset was specified: */
1306                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1307                 
1308                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1309
1310                 protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
1311                         EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1312                 
1313                 protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
1314                         EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1315                 
1316                 /* TODO: 
1317                 gboolean specific = gtk_toggle_button_get_active (
1318                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
1319                 */
1320                 
1321         }
1322             
1323         /* Add a (outgoing) server account to be used by the account: */
1324         gchar *transport_name = g_strconcat (account_name, "_transport", NULL); /* What is this good for? */
1325         created = modest_account_mgr_add_server_account (self->account_manager,
1326                 transport_name,
1327                 servername_outgoing,
1328                 username, password,
1329                 protocol_outgoing,
1330                 protocol_security_outgoing,
1331                 protocol_authentication_outgoing);
1332                 
1333         g_free (servername_outgoing);
1334                 
1335         if (!created) {
1336                 /* TODO: Provide a Logical ID for the text: */
1337                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1338                 return FALSE;   
1339         }
1340         
1341         
1342         /* Create the account, which will contain the two "server accounts": */
1343         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1344                 store_name, /* The name of our POP/IMAP server account. */
1345                 transport_name /* The name of our SMTP server account. */);
1346         g_free (store_name);
1347         g_free (transport_name);
1348         
1349         if (!created) {
1350                 /* TODO: Provide a Logical ID for the text: */
1351                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1352                 return FALSE;   
1353         }
1354         
1355         return FALSE;
1356 }
1357
1358