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-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 
27 #include "gui/windows/okdialog.h"
28 
29 #include "gui/widgets/button.h"
32 #include "gui/widgets/label.h"
33 #include "gui/widgets/layoutcell.h"
34 
36 
37 #include "net/logindata.h"
38 #include "net/loginhandler.h"
39 
40 #include "utils/delete2.h"
41 #include "utils/gettext.h"
42 
43 #include <sstream>
44 
45 #include "debug.h"
46 
48  // TRANSLATORS: change password window name
49  Window(_("Change Password"), Modal_true, nullptr, "changepassword.xml"),
51  mOldPassField(new PasswordField(this, std::string())),
52  mFirstPassField(new PasswordField(this, std::string())),
53  mSecondPassField(new PasswordField(this, std::string())),
54  // TRANSLATORS: change password dialog button
55  mChangePassButton(new Button(this, _("Change Password"),
56  "change_password", BUTTON_SKIN, this)),
57  // TRANSLATORS: change password dialog button
58  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
59  mWrongDataNoticeListener(new WrongDataNoticeListener),
60  mLoginData(&data)
61 {
62  Label *const accountLabel = new Label(this,
63  // TRANSLATORS: change password dialog label
64  strprintf(_("Account: %s"), mLoginData->username.c_str()));
65 
66  place(0, 0, accountLabel, 3, 1);
67  // TRANSLATORS: change password dialog label
68  place(0, 1, new Label(this, _("Password:")), 3, 1);
69  place(0, 2, mOldPassField, 3, 1).setPadding(1);
70  // TRANSLATORS: change password dialog label
71  place(0, 3, new Label(this, _("Type new password twice:")), 3, 1);
72  place(0, 4, mFirstPassField, 3, 1).setPadding(1);
73  place(0, 5, mSecondPassField, 3, 1).setPadding(1);
74  place(1, 6, mCancelButton, 1, 1);
75  place(2, 6, mChangePassButton, 1, 1);
76  reflowLayout(200, 0);
77 
78  center();
80 
81  mOldPassField->setActionEventId("change_password");
82  mFirstPassField->setActionEventId("change_password");
83  mSecondPassField->setActionEventId("change_password");
84 }
85 
87 {
89 }
90 
92 {
93  const std::string &eventId = event.getId();
94  if (eventId == "cancel")
95  {
97  }
98  else if (eventId == "change_password")
99  {
100  const std::string username = mLoginData->username;
101  const std::string &oldPassword = mOldPassField->getText();
102  const std::string &newFirstPass = mFirstPassField->getText();
103  const std::string &newSecondPass = mSecondPassField->getText();
104  logger->log("ChangePasswordDialog::Password change, Username is %s",
105  username.c_str());
106 
107  std::stringstream errorMsg;
108  int error = 0;
109 
110  const unsigned int min = loginHandler->getMinPasswordLength();
111  const unsigned int max = loginHandler->getMaxPasswordLength();
112 
113  // Check old Password
114  if (oldPassword.empty())
115  {
116  // No old password
117  // TRANSLATORS: change password error
118  errorMsg << _("Enter the old password first.");
119  error = 1;
120  }
121  else if (newFirstPass.length() < min)
122  {
123  // First password too short
124  // TRANSLATORS: change password error
125  errorMsg << strprintf(_("The new password needs to be at least"
126  " %u characters long."), min);
127  error = 2;
128  }
129  else if (newFirstPass.length() > max)
130  {
131  // First password too long
132  // TRANSLATORS: change password error
133  errorMsg << strprintf(_("The new password needs to be less "
134  "than %u characters long."), max);
135  error = 2;
136  }
137  else if (newFirstPass != newSecondPass)
138  {
139  // Second Pass mismatch
140  // TRANSLATORS: change password error
141  errorMsg << _("The new password entries mismatch.");
142  error = 3;
143  }
144 
145  if (error > 0)
146  {
147  if (error == 1)
149  else if (error == 2)
151  else // if (error == 3)
153 
154  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
155  // TRANSLATORS: change password error header
156  _("Error"),
157  errorMsg.str(),
158  // TRANSLATORS: ok dialog button
159  _("OK"),
161  Modal_true,
163  nullptr,
164  260);
166  }
167  else
168  {
169  // No errors detected, change account password.
171  // Set the new password
172  mLoginData->password = oldPassword;
173  mLoginData->newPassword = newFirstPass;
175  }
176  }
177 }
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1383
#define _(s)
Definition: gettext.h:34
void action(const ActionEvent &event)
void setState(const StateT state)
Definition: client.h:65
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
Definition: window.h:98
Definition: button.h:96
void setTarget(TextField *const textField)
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Client * client
Definition: client.cpp:117
#define delete2(var)
Definition: delete2.h:24
Logger * logger
Definition: logger.cpp:88
#define new
Definition: debug_new.h:147
std::string newPassword
Definition: logindata.h:60
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
std::string username
Definition: logindata.h:58
#define nullptr
Definition: localconsts.h:44
const bool ShowCenter_true
Definition: showcenter.h:29
virtual unsigned int getMaxPasswordLength() const
Definition: loginhandler.h:64
void setEnabled(const bool enabled)
Definition: widget.h:351
LayoutCell & setPadding(int p)
Definition: layoutcell.h:59
WrongDataNoticeListener * mWrongDataNoticeListener
void center()
Definition: window.cpp:1416
std::string password
Definition: logindata.h:59
ChangePasswordDialog(LoginData &data)
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:81
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
void log(const char *const log_text,...)
Definition: logger.cpp:264
const std::string & getText() const
Definition: textfield.h:223
void reflowLayout(int w, int h)
Definition: window.cpp:1395