-
Notifications
You must be signed in to change notification settings - Fork 0
/
signupdialog.cpp
208 lines (195 loc) · 7.45 KB
/
signupdialog.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
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
*
* 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 "signupdialog.h"
#include "ui_signupdialog.h"
#include <QMessageBox>
#include <QKeyEvent>
#include <QRegExp>
#include <QToolTip>
SignUpDialog::SignUpDialog(QWidget *parent) :
QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
ui(new Ui::SignUpDialog),
signUpThread(new QThread(this)),
signUpWorker(new SignUpWorker)
{
ui->setupUi(this);
setFixedSize(size());
connect(ui->submitButton, SIGNAL(clicked()), this, SLOT(onSubmitButtonClicked()));
signUpWorker->moveToThread(signUpThread);
connect(this, SIGNAL(signUp(QString, QString, QString, QString)),
signUpWorker, SLOT(doWork(QString, QString, QString, QString)));
connect(signUpWorker, SIGNAL(send(int)), this, SLOT(onSignUpThreadFinished(int)));
connect(signUpThread, SIGNAL(finished()), signUpWorker, SLOT(deleteLater()));
signUpThread->start();
}
SignUpDialog::~SignUpDialog()
{
signUpThread->quit();
signUpThread->wait();
delete ui;
}
void SignUpDialog::keyPressEvent(QKeyEvent *event)
{
if (event->key() != Qt::Key_Escape)
QDialog::keyPressEvent(event);
}
void SignUpDialog::onSubmitButtonClicked()
{
QString username(ui->usernameEdit->text());
QString password(ui->passwordEdit->text());
QString cpassword(ui->confirmPasswordEdit->text());
QString email(ui->emailEdit->text().toLower());
QString recommender(ui->recommenderEdit->text());
// Validate fields 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 (password != cpassword)
{
QToolTip::showText(
ui->confirmPasswordEdit->mapToGlobal(QPoint()),
tr("Passwords don\'t match."),
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;
}
if (!QRegExp("[0-9a-z_\\-]+(\\.[0-9a-z_\\-]+)*@([0-9a-z_\\-]+\\.)+[a-z]+").exactMatch(email))
{
QToolTip::showText(
ui->emailEdit->mapToGlobal(QPoint()),
tr("Invalid email address."),
this);
return;
}
if (!recommender.isEmpty() && !QRegExp("[a-zA-Z0-9_]{1,16}").exactMatch(recommender))
{
QToolTip::showText(
ui->recommenderEdit->mapToGlobal(QPoint()),
tr("Recommender name should only contain letters, numbers and underscores,") +
"\n" + tr("and should have a length of 1-16 characters."),
this);
return;
}
// Change UI status
ui->usernameEdit->setDisabled(true);
ui->passwordEdit->setDisabled(true);
ui->confirmPasswordEdit->setDisabled(true);
ui->emailEdit->setDisabled(true);
ui->recommenderEdit->setDisabled(true);
ui->submitButton->setDisabled(true);
ui->submitButton->setText(tr("Submitting..."));
// Create thread
emit signUp(username, password, email, recommender);
}
void SignUpDialog::onSignUpThreadFinished(int errorCode)
{
switch (errorCode)
{
case RX_ERR_SUCCESS:
QMessageBox::information(
this,
tr("RamseyX Client"),
tr("Signup succeeded.") + "\t\t\n\n" +
ui->usernameEdit->text() +
tr(", you are now a member of RamseyX! Log in to join all other volunteers "
"and contribute your computing power to mathematics!") + "\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);
break;
case RX_ERR_USER_ALREADY_EXIST:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("This username is already in use.") + "\t\t\n" +
tr("Please try another.") + "\t\t\n",
QMessageBox::Ok);
break;
case RX_ERR_EMAIL_ALREADY_EXIST:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("This email address is already in use.") + "\t\t\n" +
tr("Please try another.") + "\t\t\n",
QMessageBox::Ok);
break;
case RX_ERR_INVALID_RECOMMENDER:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("The specified recommender doesn't exist.") + "\t\t\n",
QMessageBox::Ok);
break;
default:
QMessageBox::critical(
this,
tr("RamseyX Client"),
tr("Submission failed.") + "\t\t\n" +
tr("Please try again later.") + "\t\t\n",
QMessageBox::Ok);
break;
}
// Change UI status
ui->submitButton->setText(tr("&Submit"));
ui->submitButton->setEnabled(true);
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
ui->confirmPasswordEdit->setEnabled(true);
ui->emailEdit->setEnabled(true);
ui->recommenderEdit->setEnabled(true);
switch (errorCode)
{
case RX_ERR_SUCCESS:
close();
break;
case RX_ERR_USER_ALREADY_EXIST:
ui->usernameEdit->setFocus();
break;
case RX_ERR_EMAIL_ALREADY_EXIST:
ui->emailEdit->setFocus();
break;
case RX_ERR_INVALID_RECOMMENDER:
ui->recommenderEdit->setFocus();
break;
default:
break;
}
}