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-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"
31 #include "gui/widgets/label.h"
32 #include "gui/widgets/textfield.h"
33 
35 
36 #include "net/logindata.h"
37 #include "net/loginhandler.h"
38 
39 #include "utils/delete2.h"
40 #include "utils/gettext.h"
41 
42 #include <sstream>
43 
44 #include "debug.h"
45 
47  // TRANSLATORS: change email dialog header
48  Window(_("Change Email Address"), Modal_true, nullptr, "changeemail.xml"),
50  mFirstEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
51  nullptr, std::string(), false)),
52  mSecondEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
53  nullptr, std::string(), false)),
54  // TRANSLATORS: button in change email dialog
55  mChangeEmailButton(new Button(this, _("Change Email Address"),
56  "change_email", BUTTON_SKIN, this)),
57  // TRANSLATORS: button in change email dialog
58  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
59  mWrongDataNoticeListener(new WrongDataNoticeListener),
60  mLoginData(&data)
61 {
62  // TRANSLATORS: label in change email dialog
63  Label *const accountLabel = new Label(this, strprintf(_("Account: %s"),
64  mLoginData->username.c_str()));
65  Label *const newEmailLabel = new Label(this,
66  // TRANSLATORS: label in change email dialog
67  _("Type new email address twice:"));
68 
69  const int width = 200;
70  const int height = 130;
71  setContentSize(width, height);
72 
73  accountLabel->setPosition(5, 5);
74  accountLabel->setWidth(130);
75 
76  newEmailLabel->setPosition(
77  5, accountLabel->getY() + accountLabel->getHeight() + 7);
78  newEmailLabel->setWidth(width - 5);
79 
81  5, newEmailLabel->getY() + newEmailLabel->getHeight() + 7);
83 
87 
89  width - 5 - mCancelButton->getWidth(),
90  height - 5 - mCancelButton->getHeight());
93  mCancelButton->getY());
94 
95  add(accountLabel);
96  add(newEmailLabel);
101 
102  center();
104 
105  mFirstEmailField->setActionEventId("change_email");
106  mSecondEmailField->setActionEventId("change_email");
107 }
108 
110 {
112 }
113 
115 {
116  const std::string &eventId = event.getId();
117  if (eventId == "cancel")
118  {
120  }
121  else if (eventId == "change_email")
122  {
123  const std::string username = mLoginData->username;
124  const std::string &newFirstEmail = mFirstEmailField->getText();
125  const std::string &newSecondEmail = mSecondEmailField->getText();
126  logger->log("ChangeEmailDialog::Email change, Username is %s",
127  username.c_str());
128 
129  std::stringstream errorMsg;
130  int error = 0;
131 
132  const unsigned int min = loginHandler->getMinPasswordLength();
133  const unsigned int max = loginHandler->getMaxPasswordLength();
134 
135  if (newFirstEmail.length() < min)
136  {
137  // First email address too short
138  // TRANSLATORS: change email error
139  errorMsg << strprintf(_("The new email address needs to be at "
140  "least %u characters long."), min);
141  error = 1;
142  }
143  else if (newFirstEmail.length() > max)
144  {
145  // First email address too long
146  // TRANSLATORS: change email error
147  errorMsg << strprintf(_("The new email address needs to be "
148  "less than %u characters long."), max);
149  error = 1;
150  }
151  else if (newFirstEmail != newSecondEmail)
152  {
153  // Second Pass mismatch
154  // TRANSLATORS: change email error
155  errorMsg << _("The email address entries mismatch.");
156  error = 2;
157  }
158 
159  if (error > 0)
160  {
161  if (error == 1)
163  else // if (error == 2)
165 
166  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
167  // TRANSLATORS: change email error header
168  _("Error"),
169  errorMsg.str(),
170  // TRANSLATORS: ok dialog button
171  _("OK"),
173  Modal_true,
175  nullptr,
176  260);
178  }
179  else
180  {
181  // No errors detected, change account password.
183  // Set the new email address
184  mLoginData->email = newFirstEmail;
186  }
187  }
188 }
TextField * mFirstEmailField
#define _(s)
Definition: gettext.h:34
int getWidth() const
Definition: widget.h:220
const bool LoseFocusOnTab_true
TextField * mSecondEmailField
void setWidth(const int width)
Definition: widget.cpp:132
void setState(const StateT state)
Definition: client.h:65
int getY() const
Definition: widget.h:287
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
std::string email
Definition: logindata.h:66
#define new
Definition: debug_new.h:147
void setContentSize(int width, int height)
Definition: window.cpp:491
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
void action(const ActionEvent &event)
uint32_t data
virtual unsigned int getMinPasswordLength() const
Definition: loginhandler.h:61
virtual void add(Widget *const widget)
std::string username
Definition: logindata.h:58
#define nullptr
Definition: localconsts.h:44
WrongDataNoticeListener * mWrongDataNoticeListener
const bool ShowCenter_true
Definition: showcenter.h:29
void setPosition(const int x, const int y)
Definition: widget.cpp:160
virtual unsigned int getMaxPasswordLength() const
Definition: loginhandler.h:64
void setEnabled(const bool enabled)
Definition: widget.h:351
ChangeEmailDialog(LoginData &data)
LoginData * mLoginData
int getX() const
Definition: widget.h:268
void center()
Definition: window.cpp:1416
int getHeight() const
Definition: widget.h:239
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