1 /*
2  * %kadu copyright begin%
3  * Copyright 2011, 2012, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
4  * Copyright 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
5  * %kadu copyright end%
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config-wizard-set-up-account-page.h"
22 
23 #include "accounts/account-manager.h"
24 #include "configuration/configuration-manager.h"
25 #include "configuration/configuration.h"
26 #include "core/myself.h"
27 #include "gui/widgets/account-add-widget.h"
28 #include "gui/widgets/account-create-widget.h"
29 #include "gui/windows/your-accounts.h"
30 #include "protocols/protocol-factory.h"
31 
32 #include <QtWidgets/QCheckBox>
33 #include <QtWidgets/QFormLayout>
34 #include <QtWidgets/QLabel>
35 
ConfigWizardSetUpAccountPage(QWidget * parent)36 ConfigWizardSetUpAccountPage::ConfigWizardSetUpAccountPage(QWidget *parent) :
37 		ConfigWizardPage(parent), AccountSuccessfullyCreated(false)
38 {
39 	setDescription(tr("<p>Please enter your account data.</p><p>Go back if you want to select a different Account Setup option.</p>"));
40 
41 	createGui();
42 }
43 
~ConfigWizardSetUpAccountPage()44 ConfigWizardSetUpAccountPage::~ConfigWizardSetUpAccountPage()
45 {
46 }
47 
setAccountManager(AccountManager * accountManager)48 void ConfigWizardSetUpAccountPage::setAccountManager(AccountManager *accountManager)
49 {
50 	m_accountManager = accountManager;
51 }
52 
setConfigurationManager(ConfigurationManager * configurationManager)53 void ConfigWizardSetUpAccountPage::setConfigurationManager(ConfigurationManager *configurationManager)
54 {
55 	m_configurationManager = configurationManager;
56 }
57 
setMyself(Myself * myself)58 void ConfigWizardSetUpAccountPage::setMyself(Myself *myself)
59 {
60 	m_myself = myself;
61 }
62 
createGui()63 void ConfigWizardSetUpAccountPage::createGui()
64 {
65 	formLayout()->addRow(new QLabel(tr("<h3>Account Setup</h3>"), this));
66 }
67 
isComplete() const68 bool ConfigWizardSetUpAccountPage::isComplete() const
69 {
70 	if (AccountWidget && AccountWidget.data()->stateNotifier())
71 		return StateChangedDataValid == AccountWidget.data()->stateNotifier()->state();
72 
73 	return true;
74 }
75 
initializePage()76 void ConfigWizardSetUpAccountPage::initializePage()
77 {
78 	ProtocolFactory *pf = field("choose-network.protocol-factory").value<ProtocolFactory *>();
79 	if (!pf)
80 		return;
81 
82 	if (field("choose-network.new").toBool())
83 		AccountWidget = pf->newCreateAccountWidget(false, this);
84 	else if (field("choose-network.existing").toBool())
85 		AccountWidget = pf->newAddAccountWidget(false, this);
86 
87 	if (AccountWidget)
88 	{
89 		formLayout()->addRow(QString(), AccountWidget.data());
90 
91 		if (AccountWidget.data()->stateNotifier())
92 			connect(AccountWidget.data()->stateNotifier(), SIGNAL(stateChanged(ConfigurationValueState)), this, SIGNAL(completeChanged()));
93 		// NOTE: This signal is declared by AccountCreateWidget and AccountCreateWidget
94 		// but not by ModalConfigurationWidget. It will work correctly with Qt meta-object system, though.
95 		connect(AccountWidget.data(), SIGNAL(accountCreated(Account)), this, SLOT(accountCreated(Account)));
96 		// Same as above, window() is QWizard.
97 		connect(AccountWidget.data(), SIGNAL(destroyed()), window(), SLOT(back()));
98 	}
99 }
100 
cleanupPage()101 void ConfigWizardSetUpAccountPage::cleanupPage()
102 {
103 	if (AccountWidget)
104 	{
105 		disconnect(AccountWidget.data(), SIGNAL(destroyed()), window(), SLOT(back()));
106 		disconnect(AccountWidget.data(), 0, this, 0);
107 		delete AccountWidget.data();
108 	}
109 
110 	QWizardPage::cleanupPage();
111 }
112 
validatePage()113 bool ConfigWizardSetUpAccountPage::validatePage()
114 {
115 	if (!AccountWidget)
116 		return true;
117 
118 	AccountWidget.data()->apply();
119 
120 	// apply() call should have blocked until accountCreated() was emitted,
121 	// so AccountSuccessfullyCreated should now be filled.
122 	return AccountSuccessfullyCreated;
123 }
124 
accountCreated(Account account)125 void ConfigWizardSetUpAccountPage::accountCreated(Account account)
126 {
127 	if (!account)
128 	{
129 		AccountSuccessfullyCreated = false;
130 		return;
131 	}
132 
133 	m_accountManager->addItem(account);
134 	account.accountContact().setOwnerBuddy(m_myself->buddy());
135 
136 	AccountSuccessfullyCreated = true;
137 
138 	m_configurationManager->flush();
139 }
140 
141 #include "moc_config-wizard-set-up-account-page.cpp"
142