* added licensing boilerplate to source files
[modest] / src / hildon / modest-ui-wizard.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 /* modest-ui-wizard.c */
32
33 #include <gtk/gtk.h>
34 #include <glade/glade.h>
35 #include <glib/gi18n.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /*HAVE_CONFIG_H*/
41
42 #include "../modest-account-mgr.h"
43 #include "../modest-identity-mgr.h"
44
45 #include "modest-ui-glade.h"
46 #include "modest-ui-wizard.h"
47
48
49 static void wizard_incoming_button_toggled(GtkWidget *button,
50                                            gpointer userdata) {
51         GtkWidget *awidget;
52         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button))==TRUE) {
53                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutUserNameEntry");
54                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), FALSE);
55                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutPasswordEntry");
56                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), FALSE);
57         }
58         else {
59                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutUserNameEntry");
60                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), TRUE);
61                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutPasswordEntry");
62                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), TRUE);
63         }
64 }
65
66 void wizard_missing_notification(GtkWindow *parent, gchar *info_message) {
67         GtkWidget *DenyDialog;
68
69         DenyDialog=gtk_message_dialog_new(parent,
70                                           GTK_DIALOG_MODAL,
71                                           GTK_MESSAGE_INFO,
72                                           GTK_BUTTONS_OK,
73                                           "%s",
74                                           info_message);
75
76         gtk_dialog_run(GTK_DIALOG(DenyDialog));
77
78         gtk_widget_destroy(DenyDialog);
79 }
80
81 gchar *get_text_from_combobox (GtkWidget *combobox){
82         /* Remember to free the returned variable after usage! */
83
84         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(combobox));
85         GtkTreeIter iter;
86
87         gchar *value;
88
89         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combobox), &iter)) {
90                 gtk_tree_model_get(GTK_TREE_MODEL(model),
91                                    &iter,
92                                    0, &value,
93                                    -1);
94         }
95
96         return value;
97 }
98
99
100 gboolean advance_sanity_check(GladeXML *glade_xml, gint cp) {
101         gchar *tmptext;
102
103         /* FIXME:
104          * all calls to wizard_missing_notification lack the parent window.
105          */
106
107         switch (cp) {
108         case 1:
109                 /* Only needed if the "mailbox name" field is used in the first page of the wizard.
110                  * if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWMailboxnameEntry"))))==0)
111                  * {
112                  *         wizard_missing_notification(NULL, "Please enter mailbox name");
113                  *         return FALSE;
114                  * }
115                  */
116                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))))==0) {
117                         wizard_missing_notification(NULL, "Please enter the E-Mail address.");
118                         return FALSE;
119                 }
120                 return TRUE;
121                 break;
122         case 2:
123                 tmptext=gtk_combo_box_get_active_text(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox")));
124                 if (tmptext==NULL) {
125                         wizard_missing_notification(NULL, "Please select mailbox type.");
126                         return FALSE;
127                 }
128                 g_free(tmptext);
129                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))))==0) {
130                         wizard_missing_notification(NULL, "Please specify incoming server adress.");
131                         return FALSE;
132                 }
133                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))))==0) {
134                         wizard_missing_notification(NULL, "Please enter user name.");
135                         return FALSE;
136                 }
137                 return TRUE;
138                 break;
139         case 3:
140                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))))==0) {
141                         wizard_missing_notification(NULL, "Please specify outgoing server address.");
142                         return FALSE;
143                 }
144                 /* smtp servers may work without a username
145                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton")))==FALSE
146                     && strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutUserNameEntry"))))==0) {
147                         wizard_missing_notification(NULL, "Please enter user name.");
148                         return FALSE;
149                 } */
150
151                 return TRUE;
152                 break;
153         }
154
155         return FALSE;
156 }
157
158 gchar *search_unused_account_or_identity_name(gpointer mgr, gchar *draft) {
159         GString *tmpaccount_name;
160         gint counter;
161
162         tmpaccount_name=g_string_new("");
163         g_string_printf(tmpaccount_name, "%s", draft);
164         if(MODEST_IS_ACCOUNT_MGR(mgr)) {
165                 for(counter=0; modest_account_mgr_server_account_exists(mgr, tmpaccount_name->str, NULL); counter++)
166                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
167         }
168         else
169                 for(counter=0;      modest_identity_mgr_identity_exists(mgr, tmpaccount_name->str, NULL); counter++)
170                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
171
172         return g_string_free(tmpaccount_name, FALSE);
173 }
174
175 gboolean wizard_account_add(GladeXML *glade_xml, ModestUI *modest_ui)
176 {
177         ModestAccountMgr *acc_mgr;
178         ModestIdentityMgr *id_mgr;
179         gchar *store;
180         gchar *transport;
181         gchar *identity;
182         ModestUIPrivate *priv;
183         ModestConf *conf;
184         gchar *tmptext;
185         gchar *tmptext2;
186
187         g_return_val_if_fail (MODEST_IS_UI(modest_ui), FALSE);
188         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
189         conf = priv->modest_conf;
190
191         acc_mgr = priv->modest_acc_mgr;
192         id_mgr = priv->modest_id_mgr;
193
194         tmptext2=get_text_from_combobox(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox"));
195         tmptext=g_utf8_strdown(tmptext2, -1);
196         g_free(tmptext2);
197
198         store=search_unused_account_or_identity_name(acc_mgr, "incoming");
199         modest_account_mgr_add_server_account (acc_mgr,
200                                                store,
201                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))),
202                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
203                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWPasswordEntry"))),
204                                                tmptext);
205         g_free(store);
206         g_free(tmptext);
207
208         transport=search_unused_account_or_identity_name(acc_mgr, "outgoing");
209         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton")))==TRUE)
210                 modest_account_mgr_add_server_account (acc_mgr,
211                                                        transport,
212                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
213                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
214                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWPasswordEntry"))),
215                                                        "smtp");
216         else
217                 modest_account_mgr_add_server_account (acc_mgr,
218                                                        transport,
219                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
220                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutUserNameEntry"))),
221                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutPasswordEntry"))),
222                                                        "smtp");
223
224         identity=search_unused_account_or_identity_name(id_mgr, MODEST_IDENTITY_DEFAULT_IDENTITY);
225         if (!modest_identity_mgr_add_identity (id_mgr,
226                                                identity,
227                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWRealNameEntry"))),
228                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))),
229                                                "", "", FALSE, transport, FALSE ))
230                 g_warning ("failed to add default identity");
231
232         g_free(transport);
233         g_free(identity);
234         return TRUE;
235 }
236
237
238 void wizard_account_dialog(ModestUI *modest_ui)
239 {
240         GladeXML *glade_xml;
241         GtkWidget *dialog;
242         ModestUIPrivate *priv;
243         GtkWidget *finish_button;
244         GtkWidget *back_button;
245         GtkWidget *next_button;
246         GtkWidget *cancel_button;
247         GtkWidget *notebook;
248         GtkWidget *use_incoming_button;
249         gint cp;
250         gint result;
251         gboolean account_added_successfully=FALSE;
252
253         g_return_if_fail(MODEST_IS_UI(modest_ui));
254         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
255
256         glade_xml = glade_xml_new(MODEST_GLADE, "account_wizard", NULL);
257
258         dialog = glade_xml_get_widget(glade_xml, "account_wizard");
259
260         gtk_widget_show_all(dialog);
261
262         finish_button=glade_xml_get_widget(glade_xml, "AWFinishButton");
263         back_button=glade_xml_get_widget(glade_xml, "AWBackButton");
264         next_button=glade_xml_get_widget(glade_xml, "AWNextButton");
265         cancel_button=glade_xml_get_widget(glade_xml, "AWCancelButton");
266         notebook=glade_xml_get_widget(glade_xml, "AWNotebook");
267
268         gtk_widget_set_sensitive(finish_button, FALSE);
269
270         use_incoming_button=glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton");
271         g_signal_connect(use_incoming_button,
272                          "toggled",
273                          G_CALLBACK(wizard_incoming_button_toggled),
274                          glade_xml);
275
276         /* First page not used currently. It's reserved for the account preset. */
277         gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 1);
278
279         do {
280                 cp=gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
281                 switch (cp) {
282                 case 1:
283                         gtk_widget_set_sensitive(back_button, FALSE);
284                         break;
285                 case 2:
286                         gtk_widget_set_sensitive(back_button, TRUE);
287                         break;
288                 case 3:
289                         gtk_widget_set_sensitive(finish_button, FALSE);
290                         gtk_widget_set_sensitive(next_button, TRUE);
291                         break;
292                 case 4:
293                         gtk_widget_set_sensitive(finish_button, TRUE);
294                         gtk_widget_set_sensitive(next_button, FALSE);
295                         break;
296                 default:
297                         g_error("I'm on page %d of notebook AWNotebook, which shouldn't have happened. Pulling emergency breaks.", cp);
298                         break;
299                 }
300
301                 result=gtk_dialog_run(GTK_DIALOG(dialog));
302
303                 switch (result) {
304                 case 1:
305                         if (advance_sanity_check(glade_xml, cp)==TRUE)
306                                 gtk_notebook_next_page(GTK_NOTEBOOK(notebook));
307                         break;
308                 case 2:
309                         gtk_notebook_prev_page(GTK_NOTEBOOK(notebook));
310                         break;
311                 case GTK_RESPONSE_ACCEPT:
312                         account_added_successfully=wizard_account_add(glade_xml, modest_ui);
313                         break;
314                 default:
315                         account_added_successfully=FALSE;
316                 }
317         }
318         while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
319
320         gtk_widget_destroy(dialog);
321         g_object_unref(glade_xml);
322 }
323
324 void new_wizard_account (GtkWidget *widget,
325                          gpointer user_data)
326 {
327         /* This will probably never be used to modify any existing account. */
328         wizard_account_dialog(MODEST_UI(user_data));
329 }
330
331