Success page added
[accounts-sso:libaccounts-ui.git] / lib / AccountsUI / add-account-page.cpp
1 /*
2  * This file is part of accounts-ui
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  *
6  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 //project
24 #include "add-account-page.h"
25 #include "service-selection-page.h"
26 #include "service-model.h"
27 #include "service-helper.h"
28 #include "sort-service-model.h"
29 #include "provider-plugin-process.h"
30 #include "account-sync-handler.h"
31 #include "genericaccountsetupform.h"
32
33 //Accounts
34 #include <Accounts/Account>
35 #include <Accounts/Manager>
36
37 //Meegotouch
38 #include <MLocale>
39 #include <MLabel>
40 #include <MLayout>
41 #include <MLinearLayoutPolicy>
42 #include <MImageWidget>
43 #include <MButton>
44 #include <MWidgetCreator>
45
46 //Qt
47 #include <QString>
48 #include <QSortFilterProxyModel>
49 #include <QRegExp>
50 #include <QDebug>
51
52 using namespace Accounts;
53
54 M_REGISTER_WIDGET_NO_CREATE(AccountsUI::AddAccountPage)
55
56 namespace AccountsUI {
57
58 class AddAccountPagePrivate
59 {
60 public:
61     AddAccountPagePrivate()
62             : context(0),
63             syncHandler(0)
64     {}
65     ~AddAccountPagePrivate()
66     {
67         qDeleteAll(serviceContextList);
68     }
69     AbstractAccountSetupContext *context;
70     QList<AbstractServiceSetupContext*> serviceContextList;
71     AccountSyncHandler *syncHandler;
72     QList<AbstractSetupContext*> abstractContexts;
73     QString serviceType;
74 };
75
76 AddAccountPage::AddAccountPage(AbstractAccountSetupContext *context,
77                                const QString &serviceType,
78                                QGraphicsItem *parent)
79         : MApplicationPage(parent)
80         , d_ptr(new AddAccountPagePrivate())
81 {
82     Q_D(AddAccountPage);
83     Q_ASSERT(context);
84     setStyleName("AddAccountPage");
85     setEscapeMode(MApplicationPageModel::EscapeAuto);
86     d->context = context;
87     d->serviceType = context->serviceType();
88 }
89
90 AddAccountPage::~AddAccountPage()
91 {
92     Q_D(AddAccountPage);
93     delete d;
94 }
95
96 void AddAccountPage::createContent()
97 {
98     Q_D(AddAccountPage);
99     Q_ASSERT(centralWidget());
100
101     //% "Add new account"
102     setTitle(qtTrId("qtn_acc_add_new_account_title"));
103
104     // layout
105     MLayout *layout = new MLayout(centralWidget());
106     MLinearLayoutPolicy *layoutPolicy =
107             new MLinearLayoutPolicy( layout, Qt::Vertical );
108
109     // plugin widget has the provider info and credentials widget
110     QGraphicsLayoutItem *pluginWidget = d->context->widget();
111     layoutPolicy->addItem(pluginWidget);
112
113     // TODO : this part is just for testing purposes, to jump to service selection page, without going through authentication
114     if (!qgetenv("ACCOUNTSUI_SKIP_VALIDATION").isEmpty()) {
115         MButton *nextButton = new MButton("Skip Validation");
116         connect(nextButton, SIGNAL(clicked()), this, SLOT(navigateToServiceSelectionPage()));
117         layoutPolicy->addItem(nextButton);
118     }
119
120     // login Ok, go to next page
121     connect(d->context, SIGNAL(validated()), SLOT(navigateToServiceSelectionPage()));
122     connect(d->context, SIGNAL(validated()), SLOT(showMenuBar()));
123
124     //process indicator
125     connect(d->context, SIGNAL(validating()), SLOT(hideMenuBar()));
126     connect(d->context, SIGNAL(error(AccountsUI::ErrorCode, const QString &)),
127             this, SLOT(onError(AccountsUI::ErrorCode, const QString &)));
128
129     //cancelling
130     connect((GenericAccountSetupForm*)pluginWidget, SIGNAL(stopButtonPressed()),
131             d->context, SLOT(stopAuthSession()));
132     connect((GenericAccountSetupForm*)pluginWidget, SIGNAL(stopButtonPressed()),
133             d->context, SLOT(showMenuBar()));
134 }
135
136
137 void AddAccountPage::navigateToServiceSelectionPage()
138 {
139     Q_D(AddAccountPage);
140     disconnect(d->context, SIGNAL(validated()), this, SLOT(navigateToServiceSelectionPage()));
141
142     ServiceModel *serviceModel = new ServiceModel(d->context->account(), this);
143
144     SortServiceModel *sortModel = new SortServiceModel(this);
145     sortModel->setSourceModel(serviceModel);
146     sortModel->sort(ServiceModel::ServiceNameColumn);
147
148     QAbstractProxyModel *proxy = sortModel;
149
150     for (int i = 0; i < proxy->rowCount(); i++) {
151         QModelIndex index = proxy->index(i, 0);
152         const QVariant vServiceHelper = index.data(ServiceModel::ServiceHelperColumn);
153         ServiceHelper *serviceHelper = vServiceHelper.value<ServiceHelper *>();
154         AbstractServiceSetupContext *context =
155             serviceHelper->serviceSetupContext(d->context, this);
156         d->serviceContextList.append(context);
157     }
158
159     if (d->serviceContextList.count() == 0 ||
160         (d->serviceContextList.count() == 1 &&
161         !d->serviceContextList.at(0)->hasMandatorySettings())) {
162
163         d->syncHandler = new AccountSyncHandler(this);
164         connect(d->syncHandler, SIGNAL(syncStateChanged(const SyncState&)),
165                 this, SLOT(onSyncStateChanged(const SyncState&)));
166
167         d->context->account()->selectService(NULL);
168         d->context->account()->setEnabled(true);
169         d->abstractContexts.append(d->context);
170         if (d->serviceContextList.count() == 1) {
171             d->context->account()->selectService(d->serviceContextList.at(0)->service());
172             d->context->account()->setEnabled(true);
173             d->abstractContexts.append(d->serviceContextList.at(0));
174         }
175
176         setProgressIndicatorVisible(true);
177
178         qDebug() << Q_FUNC_INFO;
179         d->syncHandler->validate(d->abstractContexts);
180         return;
181     }
182
183     openServiceSelectionPage(d->context, d->serviceContextList);
184 }
185
186 void AddAccountPage::openServiceSelectionPage(AccountsUI::AbstractAccountSetupContext *context,
187                                               QList<AccountsUI::AbstractServiceSetupContext *> &serviceContextList)
188 {
189     ServiceSelectionPage *serviceSelectionPage =
190         new ServiceSelectionPage(d->context, d->serviceContextList, d->serviceType);
191     connect(serviceSelectionPage,SIGNAL(backButtonClicked()),
192             this,SLOT(appear()));
193     connect(serviceSelectionPage,SIGNAL(backButtonClicked()),
194             this, SLOT(clearServiceContextList()));
195     connect(serviceSelectionPage,SIGNAL(backButtonClicked()),
196             serviceSelectionPage,SLOT(deleteLater()));
197
198     serviceSelectionPage->appear();
199 }
200
201 void AddAccountPage::onSyncStateChanged(const SyncState &state)
202 {
203
204     qDebug() << Q_FUNC_INFO;
205     Q_D(AddAccountPage);
206
207     switch (state) {
208         case NotValidated:
209             qDebug() << Q_FUNC_INFO << __LINE__;
210             showMenuBar();
211             break;
212         case NotStored:
213             qDebug() << Q_FUNC_INFO << __LINE__;
214             showMenuBar();
215             break;
216         case Validated:
217             d->syncHandler->store(d->abstractContexts);
218             break;
219         case Stored:
220             connect(d->context->account(), SIGNAL(synced()),
221                     ProviderPluginProcess::instance(), SLOT(quit()));
222             d->context->account()->sync();
223             showMenuBar();
224             break;
225         default:
226             return;
227     }
228 }
229
230 void AddAccountPage::clearServiceContextList()
231 {
232     Q_D(AddAccountPage);
233     d->serviceContextList.clear();
234 }
235
236 void AddAccountPage::hideMenuBar()
237 {
238     setComponentsDisplayMode(NavigationBar, MApplicationPageModel::Hide);
239 }
240
241 void AddAccountPage::showMenuBar()
242 {
243     setComponentsDisplayMode(NavigationBar, MApplicationPageModel::Show);
244 }
245
246 void AddAccountPage::onError(AccountsUI::ErrorCode, const QString &)
247 {
248     showMenuBar();
249 }
250
251 } //namespace