Mana
Loading...
Searching...
No Matches
register.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/register.h"
23
24#include "client.h"
25#include "log.h"
26
27#include "gui/logindialog.h"
28#include "gui/okdialog.h"
29
30#include "gui/widgets/button.h"
31#include "gui/widgets/label.h"
32#include "gui/widgets/layout.h"
36
37#include "net/logindata.h"
38#include "net/loginhandler.h"
39#include "net/net.h"
40
41#include "utils/gettext.h"
42#include "utils/stringutils.h"
43
44void WrongDataNoticeListener::setTarget(gcn::TextField *textField)
45{
46 mTarget = textField;
47}
48
49void WrongDataNoticeListener::action(const gcn::ActionEvent &event)
50{
51 if (event.getId() == "ok")
52 mTarget->requestFocus();
53}
54
56 Window(_("Register")),
57 mEmailField(nullptr),
58 mMaleButton(nullptr),
59 mFemaleButton(nullptr),
60 mWrongDataNoticeListener(new WrongDataNoticeListener),
61 mLoginData(loginData)
62{
63 int optionalActions = Net::getLoginHandler()->supportedOptionalActions();
64
65 gcn::Label *userLabel = new Label(_("Name:"));
66 gcn::Label *passwordLabel = new Label(_("Password:"));
67 gcn::Label *confirmLabel = new Label(_("Confirm:"));
71 mRegisterButton = new Button(_("Register"), "register", this);
72 mCancelButton = new Button(_("Cancel"), "cancel", this);
73
75 place = getPlacer(0, 0);
76 place(0, 0, userLabel);
77 place(0, 1, passwordLabel);
78 place(0, 2, confirmLabel);
79
80 place(1, 0, mUserField, 3).setPadding(2);
81 place(1, 1, mPasswordField, 3).setPadding(2);
82 place(1, 2, mConfirmField, 3).setPadding(2);
83
84 int row = 3;
85
86 if (optionalActions & Net::LoginHandler::SetGenderOnRegister)
87 {
88 mMaleButton = new RadioButton(_("Male"), "sex", true);
89 mFemaleButton = new RadioButton(_("Female"), "sex", false);
90 place(1, row, mMaleButton);
91 place(2, row, mFemaleButton);
92
93 row++;
94 }
95
96 if (optionalActions & Net::LoginHandler::SetEmailOnRegister)
97 {
98 gcn::Label *emailLabel = new Label(_("Email:"));
100 place(0, row, emailLabel);
101 place(1, row, mEmailField, 3).setPadding(2);
102
103 row++;
104 }
105
106 place = getPlacer(0, 2);
107 place(1, 0, mRegisterButton);
108 place(2, 0, mCancelButton);
109 reflowLayout(250, 0);
110
111 mUserField->addKeyListener(this);
112 mPasswordField->addKeyListener(this);
113 mConfirmField->addKeyListener(this);
114
115 /* TODO:
116 * This is a quick and dirty way to respond to the ENTER key, regardless of
117 * which text field is selected. There may be a better way now with the new
118 * input system of Guichan 0.6.0. See also the login dialog.
119 */
120 mUserField->setActionEventId("register");
121 mPasswordField->setActionEventId("register");
122 mConfirmField->setActionEventId("register");
123
124 mUserField->addActionListener(this);
125 mPasswordField->addActionListener(this);
126 mConfirmField->addActionListener(this);
127
128 center();
129 setVisible(true);
130 mUserField->requestFocus();
131 mUserField->setCaretPosition(mUserField->getText().length());
132
133 mRegisterButton->setEnabled(canSubmit());
134}
135
140
141void RegisterDialog::action(const gcn::ActionEvent &event)
142{
143 if (event.getId() == "cancel")
144 {
146 }
147 else if (event.getId() == "register" && canSubmit())
148 {
149 const std::string user = mUserField->getText();
150 Log::info("RegisterDialog::register Username is %s", user.c_str());
151
152 std::string errorMessage;
153 int error = 0;
154
155 unsigned int minUser = Net::getLoginHandler()->getMinUserNameLength();
156 unsigned int maxUser = Net::getLoginHandler()->getMaxUserNameLength();
157 unsigned int minPass = Net::getLoginHandler()->getMinPasswordLength();
158 unsigned int maxPass = Net::getLoginHandler()->getMaxPasswordLength();
159
160 if (user.length() < minUser)
161 {
162 // Name too short
164 (_("The username needs to be at least %d characters long."),
165 minUser);
166 error = 1;
167 }
168 else if (user.length() > maxUser - 1)
169 {
170 // Name too long
172 (_("The username needs to be less than %d characters long."),
173 maxUser);
174 error = 1;
175 }
176 else if (mPasswordField->getText().length() < minPass)
177 {
178 // Pass too short
180 (_("The password needs to be at least %d characters long."),
181 minPass);
182 error = 2;
183 }
184 else if (mPasswordField->getText().length() > maxPass - 1)
185 {
186 // Pass too long
188 (_("The password needs to be less than %d characters long."),
189 maxPass);
190 error = 2;
191 }
192 else if (mPasswordField->getText() != mConfirmField->getText())
193 {
194 // Password does not match with the confirmation one
195 errorMessage = _("Passwords do not match.");
196 error = 2;
197 }
198
199 // TODO: Check if a valid email address was given
200
201 if (error > 0)
202 {
203 if (error == 1)
204 {
206 }
207 else if (error == 2)
208 {
209 // Reset password confirmation
210 mPasswordField->setText(std::string());
211 mConfirmField->setText(std::string());
212
214 }
215
216 auto *dlg = new OkDialog(_("Error"), errorMessage);
217 dlg->addActionListener(mWrongDataNoticeListener);
218 }
219 else
220 {
221 // No errors detected, register the new user.
222 mRegisterButton->setEnabled(false);
223
224 mLoginData->username = mUserField->getText();
225 mLoginData->password = mPasswordField->getText();
226 if (mFemaleButton)
227 mLoginData->gender = mFemaleButton->isSelected() ?
229 if (mEmailField)
230 mLoginData->email = mEmailField->getText();
232
234 }
235 }
236}
237
238void RegisterDialog::keyPressed(gcn::KeyEvent &keyEvent)
239{
240 mRegisterButton->setEnabled(canSubmit());
241}
242
244{
245 return !mUserField->getText().empty() &&
246 !mPasswordField->getText().empty() &&
247 !mConfirmField->getText().empty() &&
249}
Button widget.
Definition button.h:38
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
This class is a helper for adding widgets to nested tables in a window.
Definition layout.h:34
Label widget.
Definition label.h:34
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
Gender gender
Definition logindata.h:42
std::string email
Definition logindata.h:39
std::string username
Definition logindata.h:33
std::string password
Definition logindata.h:34
bool registerLogin
Whether an account is being registered.
Definition logindata.h:45
virtual unsigned int getMinUserNameLength() const
virtual unsigned int getMaxPasswordLength() const
virtual int supportedOptionalActions() const =0
virtual unsigned int getMaxUserNameLength() const
virtual unsigned int getMinPasswordLength() const
An 'Ok' button dialog.
Definition okdialog.h:34
A password field.
Guichan based RadioButton with custom look.
Definition radiobutton.h:30
gcn::TextField * mEmailField
Definition register.h:85
gcn::TextField * mConfirmField
Definition register.h:84
bool canSubmit() const
Returns whether submit can be enabled.
Definition register.cpp:243
WrongDataNoticeListener * mWrongDataNoticeListener
Definition register.h:92
~RegisterDialog() override
Definition register.cpp:136
gcn::Button * mCancelButton
Definition register.h:88
gcn::RadioButton * mMaleButton
Definition register.h:89
LoginData * mLoginData
Definition register.h:94
gcn::TextField * mUserField
Definition register.h:82
void keyPressed(gcn::KeyEvent &keyEvent) override
Called when a key is pressed in one of the text fields.
Definition register.cpp:238
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
Definition register.cpp:141
gcn::Button * mRegisterButton
Definition register.h:87
gcn::RadioButton * mFemaleButton
Definition register.h:90
gcn::TextField * mPasswordField
Definition register.h:83
RegisterDialog(LoginData *loginData)
Constructor.
Definition register.cpp:55
A text field.
Definition textfield.h:72
A window.
Definition window.h:59
void center()
Positions the window in the center of it's parent.
Definition window.cpp:768
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
ContainerPlacer getPlacer(int x, int y)
Returns a proxy for adding widgets in an inner table of the layout.
Definition window.cpp:743
void reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
Definition window.cpp:748
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
Listener used while dealing with wrong data.
Definition register.h:38
void setTarget(gcn::TextField *textField)
Definition register.cpp:44
void action(const gcn::ActionEvent &event) override
Definition register.cpp:49
gcn::TextField * mTarget
Definition register.h:43
std::string errorMessage
Definition client.cpp:94
LoginData loginData
Definition client.cpp:95
@ STATE_REGISTER
Definition client.h:78
@ STATE_REGISTER_ATTEMPT
Definition client.h:79
@ STATE_LOGIN
Definition client.h:64
#define _(s)
Definition gettext.h:38
void info(const char *log_text,...) LOG_PRINTF_ATTR
LoginHandler * getLoginHandler()
Definition net.cpp:95
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.