-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountdialog.cpp
196 lines (178 loc) · 6.9 KB
/
accountdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
*
* RamseyX Client: client program of distributed computing project RamseyX
*
* Copyright (C) 2013-2014 Zizheng Tai <zizheng.tai@gmail.com>, et al.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
#include "accountdialog.h"
#include "ui_accountdialog.h"
#include "signupdialog.h"
#include <QMessageBox>
#include <QKeyEvent>
#include <QRegExp>
#include <QToolTip>
AccountDialog::AccountDialog(
QWidget *parent,
bool isAccountLocked,
const QString &username,
const QString &password) :
QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
ui(new Ui::AccountDialog),
isLocked(isAccountLocked),
validateAccountThread(new QThread(this)),
validateAccountWorker(new ValidateAccountWorker)
{
ui->setupUi(this);
setFixedSize(size());
connect(ui->logInButton, SIGNAL(clicked()), this, SLOT(onLogInButtonClicked()));
connect(ui->signUpButton, SIGNAL(clicked()), this, SLOT(onSignUpButtonClicked()));
validateAccountWorker->moveToThread(validateAccountThread);
connect(this, SIGNAL(validateAccount(QString, QString)), validateAccountWorker, SLOT(doWork(QString, QString)));
connect(validateAccountWorker, SIGNAL(send(int)), this, SLOT(onValidateAccountThreadFinished(int)));
connect(validateAccountThread, SIGNAL(finished()), validateAccountWorker, SLOT(deleteLater()));
validateAccountThread->start();
if (isLocked)
{
ui->usernameEdit->setText(username);
ui->passwordEdit->setText(password);
ui->usernameEdit->setDisabled(true);
ui->passwordEdit->setDisabled(true);
ui->logInButton->setText(tr("&Log Out"));
}
}
AccountDialog::~AccountDialog()
{
validateAccountThread->quit();
validateAccountThread->wait();
delete ui;
}
void AccountDialog::keyPressEvent(QKeyEvent *event)
{
if (event->key() != Qt::Key_Escape)
QDialog::keyPressEvent(event);
}
void AccountDialog::onLogInButtonClicked()
{
if (isLocked)
{
// Warn user
if (!theController().allListsEmpty() && QMessageBox::Cancel ==
QMessageBox::warning(
this,
tr("RamseyX Client"),
tr("WARNING: You have task(s) to be completed or uploaded.") + "\t\t\n" +
tr("Switching to another account will invalidate current task(s).") + "\t\t\n",
QMessageBox::Ok, QMessageBox::Cancel))
return;
// Change UI status
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
ui->logInButton->setText(tr("&Log In"));
// Notify MainWindow
emit lock(isLocked = false, QString(), QString());
}
else
{
QString username(ui->usernameEdit->text());
QString password(ui->passwordEdit->text());
// Validate account format
if (!QRegExp("[a-zA-Z0-9_]{1,16}").exactMatch(username))
{
QToolTip::showText(
ui->usernameEdit->mapToGlobal(QPoint()),
tr("Username should only contain letters, numbers and underscores,") +
"\n" + tr("and should have a length of 1-16 characters."),
this);
return;
}
if (!QRegExp("[a-zA-Z0-9_]{4,16}").exactMatch(password))
{
QToolTip::showText(
ui->passwordEdit->mapToGlobal(QPoint()),
tr("Password should only contain letters, numbers and underscores,") +
"\n" + tr("and should have a length of 4-16 characters."),
this);
return;
}
// Change UI status
ui->usernameEdit->setDisabled(true);
ui->passwordEdit->setDisabled(true);
ui->logInButton->setDisabled(true);
ui->logInButton->setText(tr("Logging In..."));
// Create thread
emit validateAccount(username, password);
}
}
void AccountDialog::onSignUpButtonClicked()
{
SignUpDialog dlg(this);
dlg.exec();
}
void AccountDialog::onValidateAccountThreadFinished(int errorCode)
{
switch (errorCode)
{
case RX_ERR_SUCCESS:
QMessageBox::information(
this,
tr("RamseyX Client"),
tr("Login succeeded!") + "\t\t\n",
QMessageBox::Ok);
break;
case RX_ERR_CONNECTION_FAILED:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("Failed to connect to server.") + "\t\t\n" +
tr("Please try again later.") + "\t\t\n",
QMessageBox::Ok);
ui->logInButton->setText(tr("&Log In"));
ui->logInButton->setEnabled(true);
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
return;
case RX_ERR_WRONG_USR_PWD:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("Username or password incorrect.") + "\t\t\n",
QMessageBox::Ok);
ui->logInButton->setText(tr("&Log In"));
ui->logInButton->setEnabled(true);
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
ui->usernameEdit->setFocus();
return;
default:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("Validation failed.") + "\t\t\n" +
tr("Please try again later.") + "\t\t\n",
QMessageBox::Ok);
ui->logInButton->setText(tr("&Log In"));
ui->logInButton->setEnabled(true);
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
return;
}
// Notify MainWindow
emit lock(isLocked = true, ui->usernameEdit->text(), ui->passwordEdit->text());
// Change UI status
ui->logInButton->setText(tr("&Log Out"));
ui->logInButton->setEnabled(true);
}