* now the password is entered into modest_conf as well
[modest] / src / gtk-glade / modest-ui-wizard.c
1 /* modest-ui-wizard.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <gtk/gtk.h>
6 #include <glade/glade.h>
7 #include <glib/gi18n.h>
8 #include <string.h>
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif /*HAVE_CONFIG_H*/
13
14 #include "../modest-account-mgr.h"
15
16 #include "modest-ui-glade.h"
17 #include "modest-ui-wizard.h"
18
19 void wizard_missing_notification(GtkWindow *parent, gchar *info_message)
20 {
21         GtkWidget *DenyDialog;
22
23         DenyDialog=gtk_message_dialog_new(parent,
24                                           GTK_DIALOG_MODAL,
25                                           GTK_MESSAGE_INFO,
26                                           GTK_BUTTONS_OK,
27                                           "%s",
28                                           info_message);
29
30         gtk_dialog_run(GTK_DIALOG(DenyDialog));
31
32         gtk_widget_destroy(DenyDialog);
33 }
34
35 gchar *get_text_from_combobox (GtkWidget *combobox)
36 {
37         /* Remember to free the returned variable after usage! */
38
39         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(combobox));
40         GtkTreeIter iter;
41
42         gchar *value;
43
44         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combobox), &iter))
45         {
46                 gtk_tree_model_get(GTK_TREE_MODEL(model),
47                                    &iter,
48                                    0, &value,
49                                    -1);
50         }
51
52         return value;
53 }
54
55
56 gboolean advance_sanity_check(GtkWindow *parent, GladeXML *glade_xml, gint cp)
57 {
58         gchar *tmptext;
59
60         /* FIXME:
61          * all calls to wizard_missing_notification lack the parent window.
62          */
63
64         switch (cp)
65         {
66         case 0:
67                 /* Only needed if the "mailbox name" field is used in the first page of the wizard.
68                  * if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWMailboxnameEntry"))))==0)
69                  * {
70                  *         wizard_missing_notification(NULL, "Please enter mailbox name");
71                  *         return FALSE;
72                  * }
73                  */
74                 tmptext=gtk_combo_box_get_active_text(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox")));
75                 if (tmptext==NULL)
76                 {
77                         wizard_missing_notification(NULL, "Please select mailbox type.");
78                         return FALSE;
79                 }
80                 free(tmptext);
81                 return TRUE;
82                 break;
83         case 1:
84                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))))==0)
85                 {
86                         wizard_missing_notification(NULL, "Please enter user name.");
87                         return FALSE;
88                 }
89                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))))==0)
90                 {
91                         wizard_missing_notification(NULL, "Please enter the E-Mail address.");
92                         return FALSE;
93                 }
94                 return TRUE;
95                 break;
96         case 2:
97                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))))==0)
98                 {
99                         wizard_missing_notification(NULL, "Please specify incoming server adress.");
100                         return FALSE;
101                 }
102                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))))==0)
103                 {
104                         wizard_missing_notification(NULL, "Please specify outgoing server address.");
105                         return FALSE;
106                 }
107                 return TRUE;
108                 break;
109         }
110
111         return FALSE;
112 }
113
114 gchar *search_unused_account_or_idenitity_name(gpointer mgr, gchar *draft)
115 {
116         GString *tmpaccount_name;
117         gint counter;
118
119         tmpaccount_name=g_string_new("");
120         g_string_printf(tmpaccount_name, "%s", draft);
121         if(MODEST_IS_ACCOUNT_MGR(mgr))
122         {
123                 for(counter=0; modest_account_mgr_server_account_exists(mgr, tmpaccount_name->str, NULL); counter++)
124                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
125         }
126         else
127                 for(counter=0;      modest_identity_mgr_identity_exists(mgr, tmpaccount_name->str, NULL); counter++)
128                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
129
130         return g_string_free(tmpaccount_name, FALSE);
131 }
132
133 gboolean wizard_account_add(GladeXML *glade_xml, ModestUI *modest_ui)
134 {
135         ModestAccountMgr *acc_mgr;
136         ModestIdentityMgr *id_mgr;
137         gchar *tmpaccount_name;
138         ModestUIPrivate *priv;
139         ModestConf *conf;
140         gchar *tmptext;
141         gchar *tmptext2;
142
143         g_return_val_if_fail (MODEST_IS_UI(modest_ui), FALSE);
144         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
145         conf = priv->modest_conf;
146
147         acc_mgr = priv->modest_acc_mgr;
148         id_mgr = priv->modest_id_mgr;
149
150         tmptext2=get_text_from_combobox(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox"));
151         tmptext=g_utf8_strdown(tmptext2, -1);
152         g_free(tmptext2);
153
154         tmpaccount_name=search_unused_account_or_idenitity_name(acc_mgr, "incoming");
155         modest_account_mgr_add_server_account (acc_mgr,
156                                                tmpaccount_name,
157                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))),
158                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
159                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWPasswordEntry"))),
160                                                tmptext);
161         g_free(tmpaccount_name);
162         g_free(tmptext);
163
164         tmpaccount_name=search_unused_account_or_idenitity_name(acc_mgr, "outgoing");
165         modest_account_mgr_add_server_account (acc_mgr,
166                                                tmpaccount_name,
167                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
168                                                NULL,
169                                                NULL,
170                                                "smtp");
171         g_free(tmpaccount_name);
172
173         tmpaccount_name=search_unused_account_or_idenitity_name(id_mgr, "default");
174         if (!modest_identity_mgr_add_identity (id_mgr,
175                                                tmpaccount_name,
176                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))),
177                                                "", "", FALSE, NULL, FALSE ))
178                 g_warning ("failed to add default identity");
179
180         g_free(tmpaccount_name);
181         return TRUE;
182 }
183
184
185 void wizard_account_dialog(ModestUI *modest_ui)
186 {
187         GladeXML *glade_xml;
188         GtkWidget *dialog;
189         ModestUIPrivate *priv;
190         GtkWidget *FinishButton;
191         GtkWidget *BackButton;
192         GtkWidget *NextButton;
193         GtkWidget *CancelButton;
194         GtkWidget *Notebook;
195         gint cp;
196         gint result;
197         gboolean account_added_successfully=FALSE;
198
199         g_return_if_fail(MODEST_IS_UI(modest_ui));
200         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
201
202         glade_xml = glade_xml_new(MODEST_GLADE, "account_wizard", NULL);
203
204         dialog = glade_xml_get_widget(glade_xml, "account_wizard");
205
206         gtk_widget_show_all(dialog);
207
208         FinishButton=glade_xml_get_widget(glade_xml, "AWFinishButton");
209         BackButton=glade_xml_get_widget(glade_xml, "AWBackButton");
210         NextButton=glade_xml_get_widget(glade_xml, "AWNextButton");
211         CancelButton=glade_xml_get_widget(glade_xml, "AWCancelButton");
212         Notebook=glade_xml_get_widget(glade_xml, "AWNotebook");
213
214         gtk_widget_set_sensitive(FinishButton, FALSE);
215
216         do
217         {
218                 cp=gtk_notebook_get_current_page(GTK_NOTEBOOK(Notebook));
219                 switch (cp)
220                 {
221                 case 0:
222                         gtk_widget_set_sensitive(BackButton, FALSE);
223                         break;
224                 case 1:
225                         gtk_widget_set_sensitive(BackButton, TRUE);
226                         break;
227                 case 2:
228                         gtk_widget_set_sensitive(FinishButton, FALSE);
229                         gtk_widget_set_sensitive(NextButton, TRUE);
230                         break;
231                 case 3:
232                         gtk_widget_set_sensitive(FinishButton, TRUE);
233                         gtk_widget_set_sensitive(NextButton, FALSE);
234                         break;
235                 default:
236                         g_error("I'm on page %d of notebook AWNotebook, which shouldn't have happened. Pulling emergency breaks.", cp);
237                         break;
238                 }
239
240                 result=gtk_dialog_run(GTK_DIALOG(dialog));
241
242                 switch (result)
243                 {
244                 case 1:
245                         if (advance_sanity_check(NULL, glade_xml, cp)==TRUE)
246                                 gtk_notebook_next_page(GTK_NOTEBOOK(Notebook));
247                         break;
248                 case 2:
249                         gtk_notebook_prev_page(GTK_NOTEBOOK(Notebook));
250                         break;
251                 case GTK_RESPONSE_ACCEPT:
252                         account_added_successfully=wizard_account_add(glade_xml, modest_ui);
253                         break;
254                 default:
255                         account_added_successfully=FALSE;
256                 }
257         }
258         while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
259
260         gtk_widget_destroy(dialog);
261         g_object_unref(glade_xml);
262 }
263
264 void new_wizard_account (GtkWidget *widget,
265                          gpointer user_data)
266 {
267         /* This will probably never be used to modify any existing account. */
268         wizard_account_dialog(MODEST_UI(user_data));
269 }
270
271