* remove, does not compile anymore after tinymail changes
[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 #include <gtk/gtk.h>
30 #include <glade/glade.h>
31 #include <glib/gi18n.h>
32 #include <string.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif /*HAVE_CONFIG_H*/
37
38 #include "../modest-account-mgr.h"
39 #include "../modest-identity-mgr.h"
40
41 #include "modest-ui-glade.h"
42 #include "modest-ui-wizard.h"
43
44
45 static void wizard_incoming_button_toggled(GtkWidget *button,
46                                            gpointer userdata) {
47         GtkWidget *awidget;
48         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button))==TRUE) {
49                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutUserNameEntry");
50                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), FALSE);
51                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutPasswordEntry");
52                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), FALSE);
53         }
54         else {
55                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutUserNameEntry");
56                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), TRUE);
57                 awidget=glade_xml_get_widget(GLADE_XML(userdata), "AWOutPasswordEntry");
58                 gtk_widget_set_sensitive(GTK_WIDGET(awidget), TRUE);
59         }
60 }
61
62 void wizard_missing_notification(GtkWindow *parent, gchar *info_message) {
63         GtkWidget *DenyDialog;
64
65         DenyDialog=gtk_message_dialog_new(parent,
66                                           GTK_DIALOG_MODAL,
67                                           GTK_MESSAGE_INFO,
68                                           GTK_BUTTONS_OK,
69                                           "%s",
70                                           info_message);
71
72         gtk_dialog_run(GTK_DIALOG(DenyDialog));
73
74         gtk_widget_destroy(DenyDialog);
75 }
76
77 gchar *get_text_from_combobox (GtkWidget *combobox){
78         /* Remember to free the returned variable after usage! */
79
80         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(combobox));
81         GtkTreeIter iter;
82
83         gchar *value;
84
85         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combobox), &iter)) {
86                 gtk_tree_model_get(GTK_TREE_MODEL(model),
87                                    &iter,
88                                    0, &value,
89                                    -1);
90         }
91
92         return value;
93 }
94
95
96 gboolean advance_sanity_check(GladeXML *glade_xml, gint cp) {
97         gchar *tmptext;
98
99         /* FIXME:
100          * all calls to wizard_missing_notification lack the parent window.
101          */
102
103         switch (cp) {
104         case 1:
105                 /* Only needed if the "mailbox name" field is used in the first page of the wizard.
106                  * if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWMailboxnameEntry"))))==0)
107                  * {
108                  *         wizard_missing_notification(NULL, "Please enter mailbox name");
109                  *         return FALSE;
110                  * }
111                  */
112                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))))==0) {
113                         wizard_missing_notification(NULL, "Please enter the E-Mail address.");
114                         return FALSE;
115                 }
116                 return TRUE;
117                 break;
118         case 2:
119                 tmptext=gtk_combo_box_get_active_text(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox")));
120                 if (tmptext==NULL) {
121                         wizard_missing_notification(NULL, "Please select mailbox type.");
122                         return FALSE;
123                 }
124                 g_free(tmptext);
125                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))))==0) {
126                         wizard_missing_notification(NULL, "Please specify incoming server adress.");
127                         return FALSE;
128                 }
129                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))))==0) {
130                         wizard_missing_notification(NULL, "Please enter user name.");
131                         return FALSE;
132                 }
133                 return TRUE;
134                 break;
135         case 3:
136                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))))==0) {
137                         wizard_missing_notification(NULL, "Please specify outgoing server address.");
138                         return FALSE;
139                 }
140                 /* smtp servers may work without a username
141                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton")))==FALSE
142                     && strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutUserNameEntry"))))==0) {
143                         wizard_missing_notification(NULL, "Please enter user name.");
144                         return FALSE;
145                 } */
146
147                 return TRUE;
148                 break;
149         }
150
151         return FALSE;
152 }
153
154 gchar *search_unused_account_or_identity_name(gpointer mgr, gchar *draft) {
155         GString *tmpaccount_name;
156         gint counter;
157
158         tmpaccount_name=g_string_new("");
159         g_string_printf(tmpaccount_name, "%s", draft);
160         if(MODEST_IS_ACCOUNT_MGR(mgr)) {
161                 for(counter=0; modest_account_mgr_account_exists(mgr, tmpaccount_name->str, TRUE, NULL); counter++)
162                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
163         }
164         else
165                 for(counter=0;      modest_identity_mgr_identity_exists(mgr, tmpaccount_name->str, NULL); counter++)
166                         g_string_printf(tmpaccount_name, "%s%d", draft, counter);
167
168         return g_string_free(tmpaccount_name, FALSE);
169 }
170
171 gboolean wizard_account_add(GladeXML *glade_xml, ModestUI *modest_ui)
172 {
173         ModestAccountMgr *acc_mgr;
174         ModestIdentityMgr *id_mgr;
175         gchar *store;
176         gchar *transport;
177         gchar *identity;
178         ModestUIPrivate *priv;
179         ModestConf *conf;
180         gchar *tmptext;
181         gchar *tmptext2;
182
183         g_return_val_if_fail (MODEST_IS_UI(modest_ui), FALSE);
184         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
185         conf = priv->modest_conf;
186
187         acc_mgr = priv->modest_acc_mgr;
188         id_mgr = priv->modest_id_mgr;
189
190         tmptext2=get_text_from_combobox(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox"));
191         tmptext=g_utf8_strdown(tmptext2, -1);
192         g_free(tmptext2);
193
194         store=search_unused_account_or_identity_name(acc_mgr, "incoming");
195         modest_account_mgr_add_server_account (acc_mgr,
196                                                store,
197                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))),
198                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
199                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWPasswordEntry"))),
200                                                tmptext);
201         g_free(store);
202         g_free(tmptext);
203
204         transport=search_unused_account_or_identity_name(acc_mgr, "outgoing");
205         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton")))==TRUE)
206                 modest_account_mgr_add_server_account (acc_mgr,
207                                                        transport,
208                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
209                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
210                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWPasswordEntry"))),
211                                                        "smtp");
212         else
213                 modest_account_mgr_add_server_account (acc_mgr,
214                                                        transport,
215                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
216                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutUserNameEntry"))),
217                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutPasswordEntry"))),
218                                                        "smtp");
219
220         identity=search_unused_account_or_identity_name(id_mgr, MODEST_IDENTITY_DEFAULT_IDENTITY);
221         if (!modest_identity_mgr_add_identity (id_mgr,
222                                                identity,
223                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWRealNameEntry"))),
224                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))),
225                                                "", "", FALSE, transport, FALSE ))
226                 g_warning ("failed to add default identity");
227
228         g_free(transport);
229         g_free(identity);
230         return TRUE;
231 }
232
233
234 void wizard_account_dialog(ModestUI *modest_ui)
235 {
236         GladeXML *glade_xml;
237         GtkWidget *dialog;
238         ModestUIPrivate *priv;
239         GtkWidget *finish_button;
240         GtkWidget *back_button;
241         GtkWidget *next_button;
242         GtkWidget *cancel_button;
243         GtkWidget *notebook;
244         GtkWidget *use_incoming_button;
245         gint cp;
246         gint result;
247         gboolean account_added_successfully=FALSE;
248
249         g_return_if_fail(MODEST_IS_UI(modest_ui));
250         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
251
252         glade_xml = glade_xml_new(MODEST_GLADE, "account_wizard", NULL);
253
254         dialog = glade_xml_get_widget(glade_xml, "account_wizard");
255
256         gtk_widget_show_all(dialog);
257
258         finish_button=glade_xml_get_widget(glade_xml, "AWFinishButton");
259         back_button=glade_xml_get_widget(glade_xml, "AWBackButton");
260         next_button=glade_xml_get_widget(glade_xml, "AWNextButton");
261         cancel_button=glade_xml_get_widget(glade_xml, "AWCancelButton");
262         notebook=glade_xml_get_widget(glade_xml, "AWNotebook");
263
264         gtk_widget_set_sensitive(finish_button, FALSE);
265
266         use_incoming_button=glade_xml_get_widget(glade_xml, "AWUseIncomingCheckButton");
267         g_signal_connect(use_incoming_button,
268                          "toggled",
269                          G_CALLBACK(wizard_incoming_button_toggled),
270                          glade_xml);
271
272         /* First page not used currently. It's reserved for the account preset. */
273         gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 1);
274
275         do {
276                 cp=gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
277                 switch (cp) {
278                 case 1:
279                         gtk_widget_set_sensitive(back_button, FALSE);
280                         break;
281                 case 2:
282                         gtk_widget_set_sensitive(back_button, TRUE);
283                         break;
284                 case 3:
285                         gtk_widget_set_sensitive(finish_button, FALSE);
286                         gtk_widget_set_sensitive(next_button, TRUE);
287                         break;
288                 case 4:
289                         gtk_widget_set_sensitive(finish_button, TRUE);
290                         gtk_widget_set_sensitive(next_button, FALSE);
291                         break;
292                 default:
293                         g_error("I'm on page %d of notebook AWNotebook, which shouldn't have happened. Pulling emergency breaks.", cp);
294                         break;
295                 }
296
297                 result=gtk_dialog_run(GTK_DIALOG(dialog));
298
299                 switch (result) {
300                 case 1:
301                         if (advance_sanity_check(glade_xml, cp)==TRUE)
302                                 gtk_notebook_next_page(GTK_NOTEBOOK(notebook));
303                         break;
304                 case 2:
305                         gtk_notebook_prev_page(GTK_NOTEBOOK(notebook));
306                         break;
307                 case GTK_RESPONSE_ACCEPT:
308                         account_added_successfully=wizard_account_add(glade_xml, modest_ui);
309                         break;
310                 default:
311                         account_added_successfully=FALSE;
312                 }
313         }
314         while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
315
316         gtk_widget_destroy(dialog);
317         g_object_unref(glade_xml);
318 }
319
320 void new_wizard_account (GtkWidget *widget,
321                          gpointer user_data)
322 {
323         /* This will probably never be used to modify any existing account. */
324         wizard_account_dialog(MODEST_UI(user_data));
325 }
326
327