1 /*
2  * %kadu copyright begin%
3  * Copyright 2011, 2012, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
4  * Copyright 2011, 2012, 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 "gadu-add-account-widget.h"
22 
23 #include "gadu-account-details.h"
24 #include "gadu-id-validator.h"
25 #include "gadu-protocol-factory.h"
26 
27 #include "accounts/account-manager.h"
28 #include "accounts/account-storage.h"
29 #include "gui/widgets/identities-combo-box.h"
30 #include "gui/widgets/simple-configuration-value-state-notifier.h"
31 #include "gui/windows/message-dialog.h"
32 #include "icons/icons-manager.h"
33 #include "identities/identity-manager.h"
34 #include "os/generic/url-opener.h"
35 #include "plugin/plugin-injected-factory.h"
36 #include "protocols/protocols-manager.h"
37 
38 #include <QtWidgets/QApplication>
39 #include <QtWidgets/QCheckBox>
40 #include <QtWidgets/QComboBox>
41 #include <QtWidgets/QDialogButtonBox>
42 #include <QtWidgets/QFormLayout>
43 #include <QtWidgets/QLabel>
44 #include <QtWidgets/QLineEdit>
45 #include <QtWidgets/QPushButton>
46 #include <QtWidgets/QRadioButton>
47 #include <QtWidgets/QVBoxLayout>
48 
GaduAddAccountWidget(bool showButtons,QWidget * parent)49 GaduAddAccountWidget::GaduAddAccountWidget(bool showButtons, QWidget *parent) :
50 		AccountAddWidget{parent},
51 		m_showButtons{showButtons}
52 {
53 }
54 
~GaduAddAccountWidget()55 GaduAddAccountWidget::~GaduAddAccountWidget()
56 {
57 }
58 
setAccountManager(AccountManager * accountManager)59 void GaduAddAccountWidget::setAccountManager(AccountManager *accountManager)
60 {
61 	m_accountManager = accountManager;
62 }
63 
setAccountStorage(AccountStorage * accountStorage)64 void GaduAddAccountWidget::setAccountStorage(AccountStorage *accountStorage)
65 {
66 	m_accountStorage = accountStorage;
67 }
68 
setIdentityManager(IdentityManager * identityManager)69 void GaduAddAccountWidget::setIdentityManager(IdentityManager *identityManager)
70 {
71 	m_identityManager = identityManager;
72 }
73 
setPluginInjectedFactory(PluginInjectedFactory * pluginInjectedFactory)74 void GaduAddAccountWidget::setPluginInjectedFactory(PluginInjectedFactory *pluginInjectedFactory)
75 {
76 	m_pluginInjectedFactory = pluginInjectedFactory;
77 }
78 
setUrlOpener(UrlOpener * urlOpener)79 void GaduAddAccountWidget::setUrlOpener(UrlOpener *urlOpener)
80 {
81 	m_urlOpener = urlOpener;
82 }
83 
init()84 void GaduAddAccountWidget::init()
85 {
86 	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
87 
88 	connect(m_accountManager, SIGNAL(accountRegistered(Account)), this, SLOT(dataChanged()));
89 
90 	createGui(m_showButtons);
91 	resetGui();
92 }
93 
createGui(bool showButtons)94 void GaduAddAccountWidget::createGui(bool showButtons)
95 {
96 	QVBoxLayout *mainLayout = new QVBoxLayout(this);
97 
98 	QWidget *formWidget = new QWidget(this);
99 	mainLayout->addWidget(formWidget);
100 
101 	QFormLayout *layout = new QFormLayout(formWidget);
102 
103 	AccountId = new QLineEdit(this);
104 	AccountId->setValidator(createOwnedGaduIdValidator(AccountId).get());
105 	connect(AccountId, SIGNAL(textEdited(QString)), this, SLOT(dataChanged()));
106 	layout->addRow(tr("Gadu-Gadu number") + ':', AccountId);
107 
108 	AccountPassword = new QLineEdit(this);
109 	connect(AccountPassword, SIGNAL(textEdited(QString)), this, SLOT(dataChanged()));
110 	AccountPassword->setEchoMode(QLineEdit::Password);
111 	layout->addRow(tr("Password") + ':', AccountPassword);
112 
113 	RememberPassword = new QCheckBox(tr("Remember Password"), this);
114 	layout->addRow(0, RememberPassword);
115 
116 	auto registerAccountLabel = new QLabel(QString("<a href='register'>%1</a>").arg(tr("Register Account")));
117 	registerAccountLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
118 	layout->addRow(0, registerAccountLabel);
119 	connect(registerAccountLabel, SIGNAL(linkActivated(QString)), this, SLOT(registerAccount()));
120 
121 	auto remindUinLabel = new QLabel(QString("<a href='change'>%1</a>").arg(tr("Remind GG number")));
122 	remindUinLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
123 	layout->addRow(0, remindUinLabel);
124 	connect(remindUinLabel, SIGNAL(linkActivated(QString)), this, SLOT(remindUin()));
125 
126 	auto remindPasswordLabel = new QLabel(QString("<a href='change'>%1</a>").arg(tr("Remind Password")));
127 	remindPasswordLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
128 	layout->addRow(0, remindPasswordLabel);
129 	connect(remindPasswordLabel, SIGNAL(linkActivated(QString)), this, SLOT(remindPassword()));
130 
131 	Identity = m_pluginInjectedFactory->makeInjected<IdentitiesComboBox>(this);
132 	connect(Identity, SIGNAL(currentIndexChanged(int)), this, SLOT(dataChanged()));
133 	layout->addRow(tr("Account Identity") + ':', Identity);
134 
135 	QLabel *infoLabel = new QLabel(tr("<font size='-1'><i>Select or enter the identity that will be associated with this account.</i></font>"), this);
136 	infoLabel->setWordWrap(true);
137 	infoLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
138 	infoLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
139 	layout->addRow(0, infoLabel);
140 
141 	mainLayout->addStretch(100);
142 
143 	QDialogButtonBox *buttons = new QDialogButtonBox(Qt::Horizontal, this);
144 	mainLayout->addWidget(buttons);
145 
146 	AddAccountButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogApplyButton), tr("Add Account"), this);
147 	QPushButton *cancelButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogCancelButton), tr("Cancel"), this);
148 
149 	buttons->addButton(AddAccountButton, QDialogButtonBox::AcceptRole);
150 	buttons->addButton(cancelButton, QDialogButtonBox::DestructiveRole);
151 
152 	connect(AddAccountButton, SIGNAL(clicked(bool)), this, SLOT(apply()));
153 	connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(cancel()));
154 
155 	if (!showButtons)
156 		buttons->hide();
157 }
158 
resetGui()159 void GaduAddAccountWidget::resetGui()
160 {
161 	AccountId->clear();
162 	AccountPassword->clear();
163 	RememberPassword->setChecked(true);
164 	m_identityManager->removeUnused();
165 	Identity->setCurrentIndex(0);
166 
167 	dataChanged();
168 }
169 
apply()170 void GaduAddAccountWidget::apply()
171 {
172 	auto gaduAccount = m_accountStorage->create("gadu");
173 
174 	gaduAccount.setId(AccountId->text());
175 	gaduAccount.setPassword(AccountPassword->text());
176 	gaduAccount.setHasPassword(!AccountPassword->text().isEmpty());
177 	gaduAccount.setRememberPassword(RememberPassword->isChecked());
178 	// bad code: order of calls is important here
179 	// we have to set identity after password
180 	// so in cache of identity status container it already knows password and can do status change without asking user for it
181 	gaduAccount.setAccountIdentity(Identity->currentIdentity());
182 
183 	GaduAccountDetails *details = dynamic_cast<GaduAccountDetails *>(gaduAccount.details());
184 	if (details)
185 		details->setState(StorableObject::StateNew);
186 
187 	resetGui();
188 
189 	emit accountCreated(gaduAccount);
190 }
191 
cancel()192 void GaduAddAccountWidget::cancel()
193 {
194 	resetGui();
195 }
196 
dataChanged()197 void GaduAddAccountWidget::dataChanged()
198 {
199 	bool valid = !AccountId->text().isEmpty()
200 			&& !AccountPassword->text().isEmpty()
201 			&& !m_accountManager->byId("gadu", AccountId->text())
202 			&& Identity->currentIdentity();
203 
204 	AddAccountButton->setEnabled(valid);
205 
206 	if (AccountId->text().isEmpty()
207 			&& AccountPassword->text().isEmpty()
208 			&& RememberPassword->isChecked()
209 			&& 0 == Identity->currentIndex())
210 		simpleStateNotifier()->setState(StateNotChanged);
211 	else
212 		simpleStateNotifier()->setState(valid ? StateChangedDataValid : StateChangedDataInvalid);
213 }
214 
registerAccount()215 void GaduAddAccountWidget::registerAccount()
216 {
217 	m_urlOpener->openUrl("https://login.gg.pl/rejestracja-gg/");
218 }
219 
remindUin()220 void GaduAddAccountWidget::remindUin()
221 {
222 	m_urlOpener->openUrl("https://login.gg.pl/account/remindGG_email/");
223 }
224 
remindPassword()225 void GaduAddAccountWidget::remindPassword()
226 {
227 	m_urlOpener->openUrl("https://login.gg.pl/account/remindPassword/");
228 }
229 
230 #include "moc_gadu-add-account-widget.cpp"
231