ManaPlus
changeemaildialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-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"
32 #include "gui/widgets/label.h"
33 #include "gui/widgets/textfield.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 email dialog header
49  Window(_("Change Email Address"), Modal_true, nullptr, "changeemail.xml"),
51  mFirstEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
52  nullptr, std::string(), false)),
53  mSecondEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
54  nullptr, std::string(), false)),
55  // TRANSLATORS: button in change email dialog
56  mChangeEmailButton(new Button(this, _("Change Email Address"),
57  "change_email", BUTTON_SKIN, this)),
58  // TRANSLATORS: button in change email dialog
59  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
60  mWrongDataNoticeListener(new WrongDataNoticeListener),
61  mLoginData(&data)
62 {
63  // TRANSLATORS: label in change email dialog
64  Label *const accountLabel = new Label(this, strprintf(_("Account: %s"),
65  mLoginData->username.c_str()));
66  Label *const newEmailLabel = new Label(this,
67  // TRANSLATORS: label in change email dialog
68  _("Type new email address twice:"));
69 
70  const int width = 200;
71  const int height = 130;
72  setContentSize(width, height);
73 
74  accountLabel->setPosition(5, 5);
75  accountLabel->setWidth(130);
76 
77  newEmailLabel->setPosition(
78  5, accountLabel->getY() + accountLabel->getHeight() + 7);
79  newEmailLabel->setWidth(width - 5);
80 
82  5, newEmailLabel->getY() + newEmailLabel->getHeight() + 7);
84 
88 
90  width - 5 - mCancelButton->getWidth(),
91  height - 5 - mCancelButton->getHeight());
94  mCancelButton->getY());
95 
96  add(accountLabel);
97  add(newEmailLabel);
102 
103  center();
105 
106  mFirstEmailField->setActionEventId("change_email");
107  mSecondEmailField->setActionEventId("change_email");
108 }
109 
111 {
113 }
114 
116 {
117  const std::string &eventId = event.getId();
118  if (eventId == "cancel")
119  {
121  }
122  else if (eventId == "change_email")
123  {
124  const std::string username = mLoginData->username;
125  const std::string &newFirstEmail = mFirstEmailField->getText();
126  const std::string &newSecondEmail = mSecondEmailField->getText();
127  logger->log("ChangeEmailDialog::Email change, Username is %s",
128  username.c_str());
129 
130  std::stringstream errorMsg;
131  int error = 0;
132 
133  const unsigned int min = loginHandler->getMinPasswordLength();
134  const unsigned int max = loginHandler->getMaxPasswordLength();
135 
136  if (newFirstEmail.length() < min)
137  {
138  // First email address too short
139  // TRANSLATORS: change email error
140  errorMsg << strprintf(_("The new email address needs to be at "
141  "least %u characters long."), min);
142  error = 1;
143  }
144  else if (newFirstEmail.length() > max)
145  {
146  // First email address too long
147  // TRANSLATORS: change email error
148  errorMsg << strprintf(_("The new email address needs to be "
149  "less than %u characters long."), max);
150  error = 1;
151  }
152  else if (newFirstEmail != newSecondEmail)
153  {
154  // Second Pass mismatch
155  // TRANSLATORS: change email error
156  errorMsg << _("The email address entries mismatch.");
157  error = 2;
158  }
159 
160  if (error > 0)
161  {
162  if (error == 1)
164  else // if (error == 2)
166 
167  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
168  // TRANSLATORS: change email error header
169  _("Error"),
170  errorMsg.str(),
171  // TRANSLATORS: ok dialog button
172  _("OK"),
174  Modal_true,
176  nullptr,
177  260);
179  }
180  else
181  {
182  // No errors detected, change account password.
184  // Set the new email address
185  mLoginData->email = newFirstEmail;
187  }
188  }
189 }
const std::string BUTTON_SKIN
Definition: button.h:89
virtual void add(Widget *const widget)
Definition: button.h:102
LoginData * mLoginData
void action(const ActionEvent &event)
TextField * mFirstEmailField
WrongDataNoticeListener * mWrongDataNoticeListener
ChangeEmailDialog(LoginData &data)
TextField * mSecondEmailField
void setState(const StateT state)
Definition: client.h:66
Definition: label.h:91
void log(const char *const log_text,...)
Definition: logger.cpp:269
std::string email
Definition: logindata.h:67
std::string username
Definition: logindata.h:59
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 setWidth(const int width)
Definition: widget.cpp:133
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
int getY() const
Definition: widget.h:288
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getX() const
Definition: widget.h:269
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
void center()
Definition: window.cpp:1417
void setContentSize(int width, int height)
Definition: window.cpp:492
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
const bool LoseFocusOnTab_true
uint32_t data
const bool Modal_true
Definition: modal.h:30
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:82
@ CHANGEEMAIL_ATTEMPT
Definition: state.h:60
@ CHAR_SELECT
Definition: state.h:47
const bool ShowCenter_true
Definition: showcenter.h:30
std::string strprintf(const char *const format,...)