* fixed some more typos
[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, ModestUI *modest_ui)
116 {
117         ModestAccountMgr *acc_mgr;
118         ModestIdentityMgr *id_mgr;
119         const gchar *account_name="default";
120         ModestUIPrivate *priv;
121         ModestConf *conf;
122         gchar *tmptext;
123
124         g_return_if_fail (MODEST_IS_UI(modest_ui));
125         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
126         conf = priv->modest_conf;
127
128
129         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
130         if (!acc_mgr) {
131                 g_warning ("failed to instantiate account mgr");
132                 return;
133         }
134
135         if (modest_account_mgr_account_exists (acc_mgr, account_name, NULL)) {
136                 if (!modest_account_mgr_remove_account(acc_mgr, account_name, NULL)) {
137                         g_warning ("could not delete existing account");
138                 }
139         }
140
141         if (!modest_account_mgr_add_account (acc_mgr, account_name, "defaultstore", "defaulttransport", NULL))
142                 g_warning ("failed to add default account");
143         else
144         {
145                 tmptext=get_text_from_combobox(glade_xml_get_widget(glade_xml, "AWMailboxtypeComboBox"));
146                 modest_account_mgr_add_server_account (acc_mgr, "defaultstore",
147                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWInServerComboEntry"))),
148                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWUserNameEntry"))),
149                                                        NULL,
150                                                        tmptext);
151                 free(tmptext);
152                 modest_account_mgr_add_server_account (acc_mgr, "defaulttransport",
153                                                        gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWOutServerComboEntry"))),
154                                                        NULL,
155                                                        NULL,
156                                                        "smtp");
157
158         }
159         id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (conf));
160         if (modest_identity_mgr_identity_exists(id_mgr, "defaultidentity", NULL)) {
161                 if (!modest_identity_mgr_remove_identity(id_mgr, "defaultidentity", NULL)) {
162                         g_warning ("could not delete existing default identity");
163                 }
164         }
165         if (!modest_identity_mgr_add_identity (id_mgr,
166                                                MODEST_IDENTITY_DEFAULT_IDENTITY,
167                                                gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "AWEMailAddressEntry"))),
168                                                "", "", FALSE, NULL, FALSE ))
169                 g_warning ("failed to add default identity");
170
171         g_object_unref (G_OBJECT(acc_mgr));
172         g_object_unref (G_OBJECT(id_mgr));
173
174 }
175
176
177 void wizard_account_dialog(ModestUI *modest_ui)
178 {
179         GladeXML *glade_xml;
180         GtkWidget *dialog;
181         ModestUIPrivate *priv;
182         GtkWidget *FinishButton;
183         GtkWidget *BackButton;
184         GtkWidget *NextButton;
185         GtkWidget *CancelButton;
186         GtkWidget *Notebook;
187         GtkWidget *awidget;
188         gint cp;
189         gint result;
190         gint finishallowed=0;
191         gboolean account_added_successfully;
192
193         g_return_if_fail(MODEST_IS_UI(modest_ui));
194         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
195
196         glade_xml = glade_xml_new(MODEST_GLADE, "account_wizard", NULL);
197
198         dialog = glade_xml_get_widget(glade_xml, "account_wizard");
199
200         gtk_widget_show_all(dialog);
201
202         FinishButton=glade_xml_get_widget(glade_xml, "AWFinishButton");
203         BackButton=glade_xml_get_widget(glade_xml, "AWBackButton");
204         NextButton=glade_xml_get_widget(glade_xml, "AWNextButton");
205         CancelButton=glade_xml_get_widget(glade_xml, "AWCancelButton");
206         Notebook=glade_xml_get_widget(glade_xml, "AWNotebook");
207
208         gtk_widget_set_sensitive(FinishButton, FALSE);
209
210         do
211         {
212                 cp=gtk_notebook_get_current_page(GTK_NOTEBOOK(Notebook));
213                 switch (cp)
214                 {
215                 case 0:
216                         gtk_widget_set_sensitive(BackButton, FALSE);
217                         break;
218                 case 1:
219                         gtk_widget_set_sensitive(BackButton, TRUE);
220                         break;
221                 case 2:
222                         gtk_widget_set_sensitive(FinishButton, FALSE);
223                         gtk_widget_set_sensitive(NextButton, TRUE);
224                         break;
225                 case 3:
226                         gtk_widget_set_sensitive(FinishButton, TRUE);
227                         gtk_widget_set_sensitive(NextButton, FALSE);
228                         break;
229                 default:
230                         g_error("I'm on page %d of notebook AWNotebook, which shouldn't have happened. Pulling emergency breaks.", cp);
231                         break;
232                 }
233
234                 result=gtk_dialog_run(GTK_DIALOG(dialog));
235
236                 switch (result)
237                 {
238                 case 1:
239                         if (advance_sanity_check(NULL, glade_xml, cp)==TRUE)
240                                 gtk_notebook_next_page(GTK_NOTEBOOK(Notebook));
241                         break;
242                 case 2:
243                         gtk_notebook_prev_page(GTK_NOTEBOOK(Notebook));
244                         break;
245                 case GTK_RESPONSE_ACCEPT:
246                         account_added_successfully=wizard_account_add(glade_xml, modest_ui);
247                         break;
248                 default:
249                         account_added_successfully=FALSE;
250                 }
251         }
252         while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
253
254         gtk_widget_destroy(dialog);
255         g_object_unref(glade_xml);
256 }
257
258 void new_wizard_account (GtkWidget *widget,
259                          gpointer user_data)
260 {
261         /* for now: */
262         wizard_account_dialog(MODEST_UI(user_data));
263 }
264
265 void setup_account_wizardry_menu (GtkWidget *menu,
266                                   ModestUI *modest_ui)
267 {
268         ModestUIPrivate *priv;
269         gint retval;
270         GSList *account_name_list;
271         GSList *account_name_list_iter;
272         GtkWidget *awidget;
273         gint menucounter;
274
275         g_return_if_fail(MODEST_IS_UI(modest_ui));
276         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
277
278         account_name_list=modest_account_mgr_account_names(priv->modest_acc_mgr, NULL);
279
280         menucounter=1; /* The first item is supposed to be the "New Account..." item. */
281         for (account_name_list_iter=account_name_list;
282              account_name_list_iter!=NULL;
283              account_name_list_iter=g_slist_next(account_name_list_iter))
284         {
285                 awidget=gtk_menu_item_new_with_label(account_name_list_iter->data);
286                 gtk_widget_show(awidget);
287                 gtk_menu_attach(GTK_MENU(menu),
288                                 awidget,
289                                 0, 1, menucounter, menucounter+1);
290         }
291
292         g_slist_free(account_name_list);
293 }
294
295