ManaPlus
registerdialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "client.h"
26 #include "configuration.h"
27 
28 #include "being/being.h"
29 
30 #include "gui/windows/okdialog.h"
31 
32 #include "gui/widgets/button.h"
35 #include "gui/widgets/label.h"
36 #include "gui/widgets/layoutcell.h"
39 
41 
42 #include "net/logindata.h"
43 #include "net/loginhandler.h"
44 #include "net/serverfeatures.h"
45 
46 #include "utils/delete2.h"
47 #include "utils/gettext.h"
48 
49 #include "debug.h"
50 
52  // TRANSLATORS: register dialog name
53  Window(_("Register"), Modal_false, nullptr, "register.xml"),
55  KeyListener(),
56  mLoginData(&data),
57  mUserField(new TextField(this, mLoginData->username, LoseFocusOnTab_true,
58  nullptr, std::string(), false)),
59  mPasswordField(new PasswordField(this, mLoginData->password)),
60  mConfirmField(new PasswordField(this, std::string())),
61  mEmailField(nullptr),
62  // TRANSLATORS: register dialog. button.
63  mRegisterButton(new Button(this, _("Register"), "register", this)),
64  // TRANSLATORS: register dialog. button.
65  mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
66  mMaleButton(nullptr),
67  mFemaleButton(nullptr),
68  mWrongDataNoticeListener(new WrongDataNoticeListener)
69 {
70  setCloseButton(true);
71 
72  // TRANSLATORS: register dialog. label.
73  Label *const userLabel = new Label(this, _("Name:"));
74  // TRANSLATORS: register dialog. label.
75  Label *const passwordLabel = new Label(this, _("Password:"));
76  // TRANSLATORS: register dialog. label.
77  Label *const confirmLabel = new Label(this, _("Confirm:"));
78 
79  ContainerPlacer placer(nullptr, nullptr);
80  placer = getPlacer(0, 0);
81  placer(0, 0, userLabel, 1, 1);
82  placer(0, 1, passwordLabel, 1, 1);
83  placer(0, 2, confirmLabel, 1, 1);
84 
85  placer(1, 0, mUserField, 3, 1).setPadding(2);
86  placer(1, 1, mPasswordField, 3, 1).setPadding(2);
87  placer(1, 2, mConfirmField, 3, 1).setPadding(2);
88 
89  int row = 3;
90 
91  if (features.getIntValue("forceAccountGender") == -1)
92  {
93  // TRANSLATORS: register dialog. button.
94  mMaleButton = new RadioButton(this, _("Male"), "sex", true);
95  // TRANSLATORS: register dialog. button.
96  mFemaleButton = new RadioButton(this, _("Female"), "sex", false);
97  placer(1, row, mMaleButton, 1, 1);
98  placer(2, row, mFemaleButton, 1, 1);
99 
100  row++;
101  }
102 
104  {
105  // TRANSLATORS: register dialog. label.
106  Label *const emailLabel = new Label(this, _("Email:"));
107  mEmailField = new TextField(this,
108  std::string(),
110  nullptr,
111  std::string(),
112  false);
113  placer(0, row, emailLabel, 1, 1);
114  placer(1, row, mEmailField, 3, 1).setPadding(2);
116  mEmailField->setActionEventId("register");
118 // row++;
119  }
120 
121  placer = getPlacer(0, 2);
122  placer(1, 0, mRegisterButton, 1, 1);
123  placer(2, 0, mCancelButton, 1, 1);
124  reflowLayout(250, 0);
125 
126  mUserField->addKeyListener(this);
129 
130  mUserField->setActionEventId("register");
131  mPasswordField->setActionEventId("register");
132  mConfirmField->setActionEventId("register");
133 
137 
138  center();
139 }
140 
142 {
147  mUserField->getText().length()));
148 
150 }
151 
153 {
155 }
156 
158 {
159  const std::string &eventId = event.getId();
160  if (eventId == "cancel")
161  {
162  close();
163  }
164  else if (eventId == "register" && canSubmit())
165  {
166  const std::string &user = mUserField->getText();
167  logger->log("RegisterDialog::register Username is %s", user.c_str());
168 
169  std::string errorMsg;
170  int error = 0;
171 
172  const unsigned int minUser = loginHandler->getMinUserNameLength();
173  const unsigned int maxUser = loginHandler->getMaxUserNameLength();
174  const unsigned int minPass = loginHandler->getMinPasswordLength();
175  const unsigned int maxPass = loginHandler->getMaxPasswordLength();
176 
177  if (user.length() < minUser)
178  {
179  // Name too short
180  errorMsg = strprintf
181  // TRANSLATORS: error message
182  (_("The username needs to be at least %u characters long."),
183  minUser);
184  error = 1;
185  }
186  else if (user.length() > maxUser - 1)
187  {
188  // Name too long
189  errorMsg = strprintf
190  // TRANSLATORS: error message
191  (_("The username needs to be less than %u characters long."),
192  maxUser);
193  error = 1;
194  }
195  else if (mPasswordField->getText().length() < minPass)
196  {
197  // Pass too short
198  errorMsg = strprintf
199  // TRANSLATORS: error message
200  (_("The password needs to be at least %u characters long."),
201  minPass);
202  error = 2;
203  }
204  else if (mPasswordField->getText().length() > maxPass)
205  {
206  // Pass too long
207  errorMsg = strprintf
208  // TRANSLATORS: error message
209  (_("The password needs to be less than %u characters long."),
210  maxPass);
211  error = 2;
212  }
213  else if (mPasswordField->getText() != mConfirmField->getText())
214  {
215  // Password does not match with the confirmation one
216  // TRANSLATORS: error message
217  errorMsg = _("Passwords do not match.");
218  error = 2;
219  }
220  else if ((mEmailField != nullptr) &&
221  mEmailField->getText().find('@') == std::string::npos)
222  {
223  // TRANSLATORS: error message
224  errorMsg = _("Incorrect email.");
225  error = 1;
226  }
227  else if (mEmailField != nullptr &&
228  mEmailField->getText().size() > 40)
229  {
230  // TRANSLATORS: error message
231  errorMsg = _("Email too long.");
232  error = 1;
233  }
234 
235  if (error > 0)
236  {
237  if (error == 1)
238  {
240  }
241  else if (error == 2)
242  {
243  // Reset password confirmation
244  mPasswordField->setText("");
245  mConfirmField->setText("");
247  }
248 
249  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
250  // TRANSLATORS: error message
251  _("Error"), errorMsg, _("OK"),
253  Modal_true,
255  nullptr,
256  260);
258  }
259  else
260  {
261  // No errors detected, register the new user.
262  mRegisterButton->setEnabled(false);
265  if (features.getIntValue("forceAccountGender") == -1)
266  {
267  if ((mFemaleButton != nullptr) && mFemaleButton->isSelected())
269  else
271  }
272  else
273  {
275  CAST_U8(features.getIntValue("forceAccountGender")));
276  }
277 
278  if (mEmailField != nullptr)
280  mLoginData->registerLogin = true;
281 
283  }
284  }
285 }
286 
288 {
289  if (event.isConsumed())
290  {
292  return;
293  }
294  const InputActionT actionId = event.getActionId();
295  if (actionId == InputAction::GUI_CANCEL)
296  {
298  }
299  else if (actionId == InputAction::GUI_SELECT ||
300  actionId == InputAction::GUI_SELECT2)
301  {
303  }
304  else
305  {
307  }
308 }
309 
311 {
312  return !mUserField->getText().empty() &&
313  !mPasswordField->getText().empty() &&
314  !mConfirmField->getText().empty() &&
316  ((mEmailField == nullptr) || !mEmailField->getText().empty());
317 }
318 
320 {
322  Window::close();
323 }
#define CAST_U32
Definition: cast.h:30
#define _(s)
Definition: gettext.h:34
WrongDataNoticeListener * mWrongDataNoticeListener
const bool LoseFocusOnTab_true
#define CAST_U8
Definition: cast.h:26
Button * mCancelButton
void setState(const StateT state)
Definition: client.h:65
const bool Visible_true
Definition: visible.h:29
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
RegisterDialog(LoginData &loginData)
bool registerLogin
Definition: logindata.h:74
Definition: window.h:98
Definition: button.h:94
void setTarget(TextField *const textField)
RadioButton * mFemaleButton
void setCaretPosition(unsigned int position)
Definition: textfield.cpp:751
int getIntValue(const std::string &key) const
const std::string & getActionEventId() const
Definition: widget.h:604
void action(const ActionEvent &event)
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Client * client
Definition: client.cpp:113
#define delete2(var)
Definition: delete2.h:24
TextField * mUserField
static GenderT intToGender(const uint8_t sex) A_CONST
Definition: being.h:936
virtual unsigned int getMinUserNameLength() const
Definition: loginhandler.h:55
void setText(const std::string &text)
Definition: textfield.cpp:802
Logger * logger
Definition: logger.cpp:95
std::string email
Definition: logindata.h:66
virtual void close()
Definition: window.cpp:897
#define new
Definition: debug_new.h:147
Net::ServerFeatures * serverFeatures
Definition: net.cpp:94
virtual void requestFocus()
Definition: widget.cpp:203
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
Definition: label.h:87
Net::LoginHandler * loginHandler
Definition: net.cpp:83
uint32_t data
virtual unsigned int getMinPasswordLength() const
Definition: loginhandler.h:61
LoginData * mLoginData
Button * mRegisterButton
std::string username
Definition: logindata.h:58
InputAction ::T InputActionT
Definition: inputaction.h:714
#define nullptr
Definition: localconsts.h:44
const bool ShowCenter_true
Definition: showcenter.h:29
void setCloseButton(const bool flag)
Definition: window.cpp:744
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
TextField * mEmailField
bool isSelected() const
Definition: radiobutton.h:141
virtual unsigned int getMaxPasswordLength() const
Definition: loginhandler.h:64
bool isConsumed() const
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1386
void setEnabled(const bool enabled)
Definition: widget.h:351
TextField * mPasswordField
RadioButton * mMaleButton
void keyPressed(KeyEvent &event)
virtual void setVisible(Visible visible)
Definition: window.cpp:773
void center()
Definition: window.cpp:1412
GenderT gender
Definition: logindata.h:70
StateT getState() const
Definition: client.h:68
std::string password
Definition: logindata.h:59
Configuration features
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:81
bool canSubmit() const
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
void log(const char *const log_text,...)
Definition: logger.cpp:243
virtual bool haveEmailOnRegister() const =0
const std::string & getText() const
Definition: textfield.h:223
void reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248
virtual unsigned int getMaxUserNameLength() const
Definition: loginhandler.h:58
TextField * mConfirmField