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",
64  BUTTON_SKIN, this)),
65  // TRANSLATORS: register dialog. button.
66  mCancelButton(new Button(this, _("Cancel"), "cancel",
67  BUTTON_SKIN, this)),
68  mMaleButton(nullptr),
69  mFemaleButton(nullptr),
70  mWrongDataNoticeListener(new WrongDataNoticeListener)
71 {
72  setCloseButton(true);
73 
74  // TRANSLATORS: register dialog. label.
75  Label *const userLabel = new Label(this, _("Name:"));
76  // TRANSLATORS: register dialog. label.
77  Label *const passwordLabel = new Label(this, _("Password:"));
78  // TRANSLATORS: register dialog. label.
79  Label *const confirmLabel = new Label(this, _("Confirm:"));
80 
81  ContainerPlacer placer(nullptr, nullptr);
82  placer = getPlacer(0, 0);
83  placer(0, 0, userLabel, 1, 1);
84  placer(0, 1, passwordLabel, 1, 1);
85  placer(0, 2, confirmLabel, 1, 1);
86 
87  placer(1, 0, mUserField, 3, 1).setPadding(2);
88  placer(1, 1, mPasswordField, 3, 1).setPadding(2);
89  placer(1, 2, mConfirmField, 3, 1).setPadding(2);
90 
91  int row = 3;
92 
93  if (features.getIntValue("forceAccountGender") == -1)
94  {
95  // TRANSLATORS: register dialog. button.
96  mMaleButton = new RadioButton(this, _("Male"), "sex", true);
97  // TRANSLATORS: register dialog. button.
98  mFemaleButton = new RadioButton(this, _("Female"), "sex", false);
99  placer(1, row, mMaleButton, 1, 1);
100  placer(2, row, mFemaleButton, 1, 1);
101 
102  row++;
103  }
104 
106  {
107  // TRANSLATORS: register dialog. label.
108  Label *const emailLabel = new Label(this, _("Email:"));
109  mEmailField = new TextField(this,
110  std::string(),
112  nullptr,
113  std::string(),
114  false);
115  placer(0, row, emailLabel, 1, 1);
116  placer(1, row, mEmailField, 3, 1).setPadding(2);
118  mEmailField->setActionEventId("register");
120 // row++;
121  }
122 
123  placer = getPlacer(0, 2);
124  placer(1, 0, mRegisterButton, 1, 1);
125  placer(2, 0, mCancelButton, 1, 1);
126  reflowLayout(250, 0);
127 
128  mUserField->addKeyListener(this);
131 
132  mUserField->setActionEventId("register");
133  mPasswordField->setActionEventId("register");
134  mConfirmField->setActionEventId("register");
135 
139 
140  center();
141 }
142 
144 {
149  mUserField->getText().length()));
150 
152 }
153 
155 {
157 }
158 
160 {
161  const std::string &eventId = event.getId();
162  if (eventId == "cancel")
163  {
164  close();
165  }
166  else if (eventId == "register" && canSubmit())
167  {
168  const std::string &user = mUserField->getText();
169  logger->log("RegisterDialog::register Username is %s", user.c_str());
170 
171  std::string errorMsg;
172  int error = 0;
173 
174  const unsigned int minUser = loginHandler->getMinUserNameLength();
175  const unsigned int maxUser = loginHandler->getMaxUserNameLength();
176  const unsigned int minPass = loginHandler->getMinPasswordLength();
177  const unsigned int maxPass = loginHandler->getMaxPasswordLength();
178 
179  if (user.length() < minUser)
180  {
181  // Name too short
182  errorMsg = strprintf
183  // TRANSLATORS: error message
184  (_("The username needs to be at least %u characters long."),
185  minUser);
186  error = 1;
187  }
188  else if (user.length() > maxUser - 1)
189  {
190  // Name too long
191  errorMsg = strprintf
192  // TRANSLATORS: error message
193  (_("The username needs to be less than %u characters long."),
194  maxUser);
195  error = 1;
196  }
197  else if (mPasswordField->getText().length() < minPass)
198  {
199  // Pass too short
200  errorMsg = strprintf
201  // TRANSLATORS: error message
202  (_("The password needs to be at least %u characters long."),
203  minPass);
204  error = 2;
205  }
206  else if (mPasswordField->getText().length() > maxPass)
207  {
208  // Pass too long
209  errorMsg = strprintf
210  // TRANSLATORS: error message
211  (_("The password needs to be less than %u characters long."),
212  maxPass);
213  error = 2;
214  }
215  else if (mPasswordField->getText() != mConfirmField->getText())
216  {
217  // Password does not match with the confirmation one
218  // TRANSLATORS: error message
219  errorMsg = _("Passwords do not match.");
220  error = 2;
221  }
222  else if ((mEmailField != nullptr) &&
223  mEmailField->getText().find('@') == std::string::npos)
224  {
225  // TRANSLATORS: error message
226  errorMsg = _("Incorrect email.");
227  error = 1;
228  }
229  else if (mEmailField != nullptr &&
230  mEmailField->getText().size() > 40)
231  {
232  // TRANSLATORS: error message
233  errorMsg = _("Email too long.");
234  error = 1;
235  }
236 
237  if (error > 0)
238  {
239  if (error == 1)
240  {
242  }
243  else if (error == 2)
244  {
245  // Reset password confirmation
246  mPasswordField->setText("");
247  mConfirmField->setText("");
249  }
250 
251  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
252  // TRANSLATORS: error message
253  _("Error"), errorMsg, _("OK"),
255  Modal_true,
257  nullptr,
258  260);
260  }
261  else
262  {
263  // No errors detected, register the new user.
264  mRegisterButton->setEnabled(false);
267  if (features.getIntValue("forceAccountGender") == -1)
268  {
269  if ((mFemaleButton != nullptr) && mFemaleButton->isSelected())
271  else
273  }
274  else
275  {
277  CAST_U8(features.getIntValue("forceAccountGender")));
278  }
279 
280  if (mEmailField != nullptr)
282  mLoginData->registerLogin = true;
283 
285  }
286  }
287 }
288 
290 {
291  if (event.isConsumed())
292  {
294  return;
295  }
296  const InputActionT actionId = event.getActionId();
297  if (actionId == InputAction::GUI_CANCEL)
298  {
300  }
301  else if (actionId == InputAction::GUI_SELECT ||
302  actionId == InputAction::GUI_SELECT2)
303  {
305  }
306  else
307  {
309  }
310 }
311 
313 {
314  return !mUserField->getText().empty() &&
315  !mPasswordField->getText().empty() &&
316  !mConfirmField->getText().empty() &&
318  ((mEmailField == nullptr) || !mEmailField->getText().empty());
319 }
320 
322 {
324  Window::close();
325 }
#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:96
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:117
#define delete2(var)
Definition: delete2.h:24
TextField * mUserField
static GenderT intToGender(const uint8_t sex) A_CONST
Definition: being.h:942
virtual unsigned int getMinUserNameLength() const
Definition: loginhandler.h:55
void setText(const std::string &text)
Definition: textfield.cpp:802
Logger * logger
Definition: logger.cpp:88
std::string email
Definition: logindata.h:66
virtual void close()
Definition: window.cpp:901
#define new
Definition: debug_new.h:147
Net::ServerFeatures * serverFeatures
Definition: net.cpp:98
virtual void requestFocus()
Definition: widget.cpp:203
const std::string BUTTON_SKIN
Definition: button.h:88
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
Definition: label.h:87
Net::LoginHandler * loginHandler
Definition: net.cpp:87
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:715
#define nullptr
Definition: localconsts.h:44
const bool ShowCenter_true
Definition: showcenter.h:29
void setCloseButton(const bool flag)
Definition: window.cpp:748
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:1390
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:777
void center()
Definition: window.cpp:1416
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:264
virtual bool haveEmailOnRegister() const =0
const std::string & getText() const
Definition: textfield.h:223
void reflowLayout(int w, int h)
Definition: window.cpp:1395
void postInit()
Definition: window.cpp:248
virtual unsigned int getMaxUserNameLength() const
Definition: loginhandler.h:58
TextField * mConfirmField