ManaPlus
changepassworddialog.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-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "client.h"
27 
28 #include "gui/windows/okdialog.h"
29 
30 #include "gui/widgets/button.h"
33 #include "gui/widgets/label.h"
34 #include "gui/widgets/layoutcell.h"
35 
37 
38 #include "net/logindata.h"
39 #include "net/loginhandler.h"
40 
41 #include "utils/delete2.h"
42 #include "utils/gettext.h"
43 
44 #include <sstream>
45 
46 #include "debug.h"
47 
49  // TRANSLATORS: change password window name
50  Window(_("Change Password"), Modal_true, nullptr, "changepassword.xml"),
52  mOldPassField(new PasswordField(this, std::string())),
53  mFirstPassField(new PasswordField(this, std::string())),
54  mSecondPassField(new PasswordField(this, std::string())),
55  // TRANSLATORS: change password dialog button
56  mChangePassButton(new Button(this, _("Change Password"),
57  "change_password", BUTTON_SKIN, this)),
58  // TRANSLATORS: change password dialog button
59  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
60  mWrongDataNoticeListener(new WrongDataNoticeListener),
61  mLoginData(&data)
62 {
63  Label *const accountLabel = new Label(this,
64  // TRANSLATORS: change password dialog label
65  strprintf(_("Account: %s"), mLoginData->username.c_str()));
66 
67  place(0, 0, accountLabel, 3, 1);
68  // TRANSLATORS: change password dialog label
69  place(0, 1, new Label(this, _("Password:")), 3, 1);
70  place(0, 2, mOldPassField, 3, 1).setPadding(1);
71  // TRANSLATORS: change password dialog label
72  place(0, 3, new Label(this, _("Type new password twice:")), 3, 1);
73  place(0, 4, mFirstPassField, 3, 1).setPadding(1);
74  place(0, 5, mSecondPassField, 3, 1).setPadding(1);
75  place(1, 6, mCancelButton, 1, 1);
76  place(2, 6, mChangePassButton, 1, 1);
77  reflowLayout(200, 0);
78 
79  center();
81 
82  mOldPassField->setActionEventId("change_password");
83  mFirstPassField->setActionEventId("change_password");
84  mSecondPassField->setActionEventId("change_password");
85 }
86 
88 {
90 }
91 
93 {
94  const std::string &eventId = event.getId();
95  if (eventId == "cancel")
96  {
98  }
99  else if (eventId == "change_password")
100  {
101  const std::string username = mLoginData->username;
102  const std::string &oldPassword = mOldPassField->getText();
103  const std::string &newFirstPass = mFirstPassField->getText();
104  const std::string &newSecondPass = mSecondPassField->getText();
105  logger->log("ChangePasswordDialog::Password change, Username is %s",
106  username.c_str());
107 
108  std::stringstream errorMsg;
109  int error = 0;
110 
111  const unsigned int min = loginHandler->getMinPasswordLength();
112  const unsigned int max = loginHandler->getMaxPasswordLength();
113 
114  // Check old Password
115  if (oldPassword.empty())
116  {
117  // No old password
118  // TRANSLATORS: change password error
119  errorMsg << _("Enter the old password first.");
120  error = 1;
121  }
122  else if (newFirstPass.length() < min)
123  {
124  // First password too short
125  // TRANSLATORS: change password error
126  errorMsg << strprintf(_("The new password needs to be at least"
127  " %u characters long."), min);
128  error = 2;
129  }
130  else if (newFirstPass.length() > max)
131  {
132  // First password too long
133  // TRANSLATORS: change password error
134  errorMsg << strprintf(_("The new password needs to be less "
135  "than %u characters long."), max);
136  error = 2;
137  }
138  else if (newFirstPass != newSecondPass)
139  {
140  // Second Pass mismatch
141  // TRANSLATORS: change password error
142  errorMsg << _("The new password entries mismatch.");
143  error = 3;
144  }
145 
146  if (error > 0)
147  {
148  if (error == 1)
150  else if (error == 2)
152  else // if (error == 3)
154 
155  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
156  // TRANSLATORS: change password error header
157  _("Error"),
158  errorMsg.str(),
159  // TRANSLATORS: ok dialog button
160  _("OK"),
162  Modal_true,
164  nullptr,
165  260);
167  }
168  else
169  {
170  // No errors detected, change account password.
172  // Set the new password
173  mLoginData->password = oldPassword;
174  mLoginData->newPassword = newFirstPass;
176  }
177  }
178 }
const std::string BUTTON_SKIN
Definition: button.h:89
Definition: button.h:102
WrongDataNoticeListener * mWrongDataNoticeListener
void action(const ActionEvent &event)
ChangePasswordDialog(LoginData &data)
void setState(const StateT state)
Definition: client.h:66
Definition: label.h:91
LayoutCell & setPadding(int p)
Definition: layoutcell.h:60
void log(const char *const log_text,...)
Definition: logger.cpp:269
std::string username
Definition: logindata.h:59
std::string password
Definition: logindata.h:60
std::string newPassword
Definition: logindata.h:61
virtual unsigned int getMaxPasswordLength() const
Definition: loginhandler.h:68
virtual unsigned int getMinPasswordLength() const
Definition: loginhandler.h:65
const std::string & getText() const
Definition: textfield.h:224
void setEnabled(const bool enabled)
Definition: widget.h:352
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
virtual void requestFocus()
Definition: widget.cpp:204
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
Definition: window.h:102
void center()
Definition: window.cpp:1417
void reflowLayout(int w, int h)
Definition: window.cpp:1396
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setTarget(TextField *const textField)
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
Client * client
Definition: client.cpp:118
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
Logger * logger
Definition: logger.cpp:89
Net::LoginHandler * loginHandler
Definition: net.cpp:90
uint32_t data
const bool Modal_true
Definition: modal.h:30
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:82
@ CHAR_SELECT
Definition: state.h:47
@ CHANGEPASSWORD_ATTEMPT
Definition: state.h:57
const bool ShowCenter_true
Definition: showcenter.h:30
std::string strprintf(const char *const format,...)