Mana
Loading...
Searching...
No Matches
changeemaildialog.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "client.h"
25#include "log.h"
26
27#include "gui/register.h"
28#include "gui/okdialog.h"
29
30#include "gui/widgets/button.h"
31#include "gui/widgets/label.h"
33
34#include "net/logindata.h"
35#include "net/loginhandler.h"
36#include "net/net.h"
37
38#include "utils/gettext.h"
39#include "utils/stringutils.h"
40
41#include <string>
42#include <sstream>
43
45 Window(_("Change Email Address"), true),
46 mWrongDataNoticeListener(new WrongDataNoticeListener),
47 mLoginData(loginData)
48{
49 gcn::Label *accountLabel = new Label(strprintf(_("Account: %s"),
50 mLoginData->username.c_str()));
51 gcn::Label *newEmailLabel = new Label(_("Type new email address twice:"));
54 mChangeEmailButton = new Button(_("Change Email Address"), "change_email", this);
55 mCancelButton = new Button(_("Cancel"), "cancel", this);
56
57 const int width = 200;
58 const int height = 130;
59 setContentSize(width, height);
60
61 accountLabel->setPosition(5, 5);
62 accountLabel->setWidth(130);
63
64 newEmailLabel->setPosition(
65 5, accountLabel->getY() + accountLabel->getHeight() + 7);
66 newEmailLabel->setWidth(width - 5);
67
68 mFirstEmailField->setPosition(
69 5, newEmailLabel->getY() + newEmailLabel->getHeight() + 7);
70 mFirstEmailField->setWidth(130);
71
72 mSecondEmailField->setPosition(
73 5, mFirstEmailField->getY() + mFirstEmailField->getHeight() + 7);
74 mSecondEmailField->setWidth(130);
75
76 mCancelButton->setPosition(
77 width - 5 - mCancelButton->getWidth(),
78 height - 5 - mCancelButton->getHeight());
79 mChangeEmailButton->setPosition(
80 mCancelButton->getX() - 5 - mChangeEmailButton->getWidth(),
81 mCancelButton->getY());
82
83 add(accountLabel);
84 add(newEmailLabel);
88 add(mCancelButton);
89
90 center();
91 setVisible(true);
92 mFirstEmailField->requestFocus();
93
94 mFirstEmailField->setActionEventId("change_email");
95 mSecondEmailField->setActionEventId("change_email");
96}
97
102
103void ChangeEmailDialog::action(const gcn::ActionEvent &event)
104{
105 if (event.getId() == "cancel")
106 {
108 }
109 else if (event.getId() == "change_email")
110 {
111
112 const std::string username = mLoginData->username.c_str();
113 const std::string newFirstEmail = mFirstEmailField->getText();
114 const std::string newSecondEmail = mSecondEmailField->getText();
115 Log::info("ChangeEmailDialog::Email change, Username is %s",
116 username.c_str());
117
118 std::stringstream errorMessage;
119 int error = 0;
120
121 unsigned int min = Net::getLoginHandler()->getMinPasswordLength();
122 unsigned int max = Net::getLoginHandler()->getMaxPasswordLength();
123
124 if (newFirstEmail.length() < min)
125 {
126 // First email address too short
127 errorMessage << strprintf(_("The new email address needs to be at "
128 "least %d characters long."), min);
129 error = 1;
130 }
131 else if (newFirstEmail.length() > max - 1 )
132 {
133 // First email address too long
134 errorMessage << strprintf(_("The new email address needs to be "
135 "less than %d characters long."), max);
136 error = 1;
137 }
138 else if (newFirstEmail != newSecondEmail)
139 {
140 // Second Pass mismatch
141 errorMessage << _("The email address entries mismatch.");
142 error = 2;
143 }
144
145 if (error > 0)
146 {
147 if (error == 1)
148 {
150 }
151 else if (error == 2)
152 {
154 }
155
156 auto *dlg = new OkDialog(_("Error"), errorMessage.str());
157 dlg->addActionListener(mWrongDataNoticeListener);
158 }
159 else
160 {
161 // No errors detected, change account password.
162 mChangeEmailButton->setEnabled(false);
163 // Set the new email address
164 mLoginData->email = newFirstEmail;
166 }
167 }
168}
Button widget.
Definition button.h:38
gcn::TextField * mFirstEmailField
gcn::Button * mChangeEmailButton
gcn::Button * mCancelButton
WrongDataNoticeListener * mWrongDataNoticeListener
gcn::TextField * mSecondEmailField
ChangeEmailDialog(LoginData *loginData)
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
static void setState(State state)
Definition client.h:169
Label widget.
Definition label.h:34
std::string email
Definition logindata.h:39
std::string username
Definition logindata.h:33
virtual unsigned int getMaxPasswordLength() const
virtual unsigned int getMinPasswordLength() const
An 'Ok' button dialog.
Definition okdialog.h:34
A text field.
Definition textfield.h:72
A window.
Definition window.h:59
void center()
Positions the window in the center of it's parent.
Definition window.cpp:768
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
void setContentSize(int width, int height)
Sets the size of this window.
Definition window.cpp:151
Listener used while dealing with wrong data.
Definition register.h:38
void setTarget(gcn::TextField *textField)
Definition register.cpp:44
std::string errorMessage
Definition client.cpp:94
LoginData loginData
Definition client.cpp:95
@ STATE_CHAR_SELECT
Definition client.h:71
@ STATE_CHANGEEMAIL_ATTEMPT
Definition client.h:84
#define _(s)
Definition gettext.h:38
void info(const char *log_text,...) LOG_PRINTF_ATTR
LoginHandler * getLoginHandler()
Definition net.cpp:95
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.