7cf1314b51a55114896d94feeaf2b47861d45872
[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         GtkDialog *DenyDialog;
59         gchar *tmptext;
60
61         /* FIXME:
62          * all calls to wizard_missing_notification lack the parent window.
63          */
64
65         switch (cp)
66         {
67         case 0:
68                 /* Only needed if the "mailbox name" field is used in the first page of the wizard.
69                  * if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWMailboxnameEntry"))))==0)
70                  * {
71                  *         wizard_missing_notification(NULL, "Please enter mailbox name");
72                  *         return FALSE;
73                  * }
74                  */
75                 tmptext=gtk_combo_box_get_active_text(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox")));
76                 if (tmptext==NULL)
77                 {
78                         wizard_missing_notification(NULL, "Please select mailbox type.");
79                         return FALSE;
80                 }
81                 free(tmptext);
82                 return TRUE;
83                 break;
84         case 1:
85                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))))==0)
86                 {
87                         wizard_missing_notification(NULL, "Please enter user name.");
88                         return FALSE;
89                 }
90                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))))==0)
91                 {
92                         wizard_missing_notification(NULL, "Please enter the E-Mail address.");
93                         return FALSE;
94                 }
95                 return TRUE;
96                 break;
97         case 2:
98                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))))==0)
99                 {
100                         wizard_missing_notification(NULL, "Please specify incoming server adress.");
101                         return FALSE;
102                 }
103                 if (strlen(gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))))==0)
104                 {
105                         wizard_missing_notification(NULL, "Please specify outgoing server address.");
106                         return FALSE;
107                 }
108                 return TRUE;
109                 break;
110         }
111
112         return FALSE;
113 }
114
115 gboolean wizard_account_add(GladeXML *glade_xml, ModestUIPrivate *priv)
116 {
117         ModestAccountMgr *acc_mgr;
118         ModestIdentityMgr *id_mgr;
119         const gchar *account_name="default";
120         ModestConf *conf=priv->modest_conf;
121         gchar *tmptext;
122
123         g_return_if_fail (conf);
124
125         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
126         if (!acc_mgr) {
127                 g_warning ("failed to instantiate account mgr");
128                 return;
129         }
130
131         if (modest_account_mgr_account_exists (acc_mgr, account_name, NULL)) {
132                 if (!modest_account_mgr_remove_account(acc_mgr, account_name, NULL)) {
133                         g_warning ("could not delete existing account");
134                 }
135         }
136
137         if (!modest_account_mgr_add_account (acc_mgr, account_name, "defaultstore", "defaulttransport", NULL))
138                 g_warning ("failed to add default account");
139         else
140         {
141                 tmptext=get_text_from_combobox(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox"));
142                 modest_account_mgr_add_server_account (acc_mgr, "defaultstore",
143                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))),
144                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
145                                                        NULL,
146                                                        tmptext);
147                 free(tmptext);
148                 modest_account_mgr_add_server_account (acc_mgr, "defaulttransport",
149                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
150                                                        NULL,
151                                                        NULL,
152                                                        "smtp");
153
154         }
155         id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (conf));
156         if (modest_identity_mgr_identity_exists(id_mgr, "defaultidentity", NULL)) {
157                 if (!modest_identity_mgr_remove_identity(id_mgr, "defaultidentity", NULL)) {
158                         g_warning ("could not delete existing default identity");
159                 }
160         }
161         if (!modest_identity_mgr_add_identity (id_mgr,
162                                                MODEST_IDENTITY_DEFAULT_IDENTITY,
163                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))),
164                                                "", "", FALSE, NULL, FALSE ))
165                 g_warning ("failed to add default identity");
166
167         g_object_unref (G_OBJECT(acc_mgr));
168         g_object_unref (G_OBJECT(id_mgr));
169
170 }
171
172
173 void wizard_account_dialog(ModestUI *modest_ui)
174 {
175         GladeXML *glade_xml;
176         GtkWidget *dialog;
177         ModestUIPrivate *priv;
178         GtkWidget *FinishButton;
179         GtkWidget *BackButton;
180         GtkWidget *NextButton;
181         GtkWidget *CancelButton;
182         GtkWidget *Notebook;
183         GtkWidget *awidget;
184         gint cp;
185         gint result;
186         gint finishallowed=0;
187         gboolean account_added_successfully;
188
189         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
190
191         glade_xml = glade_xml_new(MODEST_GLADE, "account_wizard", NULL);
192
193         dialog = glade_xml_get_widget(glade_xml, "account_wizard");
194
195         gtk_widget_show_all(dialog);
196
197         FinishButton=glade_xml_get_widget(glade_xml, "AWFinishButton");
198         BackButton=glade_xml_get_widget(glade_xml, "AWBackButton");
199         NextButton=glade_xml_get_widget(glade_xml, "AWNextButton");
200         CancelButton=glade_xml_get_widget(glade_xml, "AWCancelButton");
201         Notebook=glade_xml_get_widget(glade_xml, "AWNotebook");
202
203         gtk_widget_set_sensitive(FinishButton, FALSE);
204
205         do
206         {
207                 cp=gtk_notebook_get_current_page(GTK_NOTEBOOK(Notebook));
208                 switch (cp)
209                 {
210                 case 0:
211                         gtk_widget_set_sensitive(BackButton, FALSE);
212                         break;
213                 case 1:
214                         gtk_widget_set_sensitive(BackButton, TRUE);
215                         break;
216                 case 2:
217                         gtk_widget_set_sensitive(FinishButton, FALSE);
218                         gtk_widget_set_sensitive(NextButton, TRUE);
219                         break;
220                 case 3:
221                         gtk_widget_set_sensitive(FinishButton, TRUE);
222                         gtk_widget_set_sensitive(NextButton, FALSE);
223                         break;
224                 default:
225                         g_error("I'm on page %d of notebook AWNotebook, which shouldn't have happened. Pulling Emeregency breaks.", cp);
226                         break;
227                 }
228
229                 result=gtk_dialog_run(GTK_DIALOG(dialog));
230
231                 switch (result)
232                 {
233                 case 1:
234                         if (advance_sanity_check(NULL, glade_xml, cp)==TRUE)
235                                 gtk_notebook_next_page(GTK_NOTEBOOK(Notebook));
236                         break;
237                 case 2:
238                         gtk_notebook_prev_page(GTK_NOTEBOOK(Notebook));
239                         break;
240                 case GTK_RESPONSE_ACCEPT:
241                         account_added_successfully=wizard_account_add(glade_xml, priv);
242                         break;
243                 default:
244                         account_added_successfully=FALSE;
245                 }
246         }
247         while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
248
249         gtk_widget_destroy(dialog);
250         g_object_unref(glade_xml);
251 }
252
253 void new_wizard_account (GtkWidget *widget,
254                          gpointer user_data)
255 {
256         /* for now: */
257         wizard_account_dialog(MODEST_UI(user_data));
258 }
259