Mana
Loading...
Searching...
No Matches
changepassworddialog.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-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"
33#include "gui/widgets/label.h"
34#include "gui/widgets/layout.h"
35
36#include "net/logindata.h"
37#include "net/loginhandler.h"
38#include "net/net.h"
39
40#include "utils/gettext.h"
41#include "utils/stringutils.h"
42
43#include <string>
44#include <sstream>
45
47 Window(_("Change Password"), true),
48 mWrongDataNoticeListener(new WrongDataNoticeListener),
49 mLoginData(loginData)
50{
51 gcn::Label *accountLabel = new Label(
52 strprintf(_("Account: %s"), mLoginData->username.c_str()));
56 mChangePassButton = new Button(_("Change Password"), "change_password",
57 this);
58 mCancelButton = new Button(_("Cancel"), "cancel", this);
59
60 place(0, 0, accountLabel, 3);
61 place(0, 1, new Label(_("Password:")), 3);
62 place(0, 2, mOldPassField, 3).setPadding(1);
63 place(0, 3, new Label(_("Type new password twice:")), 3);
64 place(0, 4, mFirstPassField, 3).setPadding(1);
66 place(1, 6, mCancelButton);
68 reflowLayout(200);
69
70 center();
71 setVisible(true);
72 mOldPassField->requestFocus();
73
74 mOldPassField->setActionEventId("change_password");
75 mFirstPassField->setActionEventId("change_password");
76 mSecondPassField->setActionEventId("change_password");
77}
78
83
84void ChangePasswordDialog::action(const gcn::ActionEvent &event)
85{
86 if (event.getId() == "cancel")
87 {
89 }
90 else if (event.getId() == "change_password")
91 {
92
93 const std::string username = mLoginData->username.c_str();
94 const std::string oldPassword = mOldPassField->getText();
95 const std::string newFirstPass = mFirstPassField->getText();
96 const std::string newSecondPass = mSecondPassField->getText();
97 Log::info("ChangePasswordDialog::Password change, Username is %s",
98 username.c_str());
99
100 std::stringstream errorMessage;
101 int error = 0;
102
103 unsigned int min = Net::getLoginHandler()->getMinPasswordLength();
104 unsigned int max = Net::getLoginHandler()->getMaxPasswordLength();
105
106 // Check old Password
107 if (oldPassword.empty())
108 {
109 // No old password
110 errorMessage << _("Enter the old password first.");
111 error = 1;
112 }
113 else if (newFirstPass.length() < min)
114 {
115 // First password too short
116 errorMessage << strprintf(_("The new password needs to be at least"
117 " %d characters long."), min);
118 error = 2;
119 }
120 else if (newFirstPass.length() > max - 1 )
121 {
122 // First password too long
123 errorMessage << strprintf(_("The new password needs to be less "
124 "than %d characters long."), max);
125 error = 2;
126 }
127 else if (newFirstPass != newSecondPass)
128 {
129 // Second Pass mismatch
130 errorMessage << _("The new password entries mismatch.");
131 error = 3;
132 }
133
134 if (error > 0)
135 {
136 if (error == 1)
137 {
139 }
140 else if (error == 2)
141 {
143 }
144 else if (error == 3)
145 {
147 }
148
149 auto *dlg = new OkDialog(_("Error"), errorMessage.str());
150 dlg->addActionListener(mWrongDataNoticeListener);
151 }
152 else
153 {
154 // No errors detected, change account password.
155 mChangePassButton->setEnabled(false);
156 // Set the new password
157 mLoginData->password = oldPassword;
158 mLoginData->newPassword = newFirstPass;
160 }
161 }
162}
Button widget.
Definition button.h:38
WrongDataNoticeListener * mWrongDataNoticeListener
gcn::TextField * mFirstPassField
ChangePasswordDialog(LoginData *loginData)
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
gcn::TextField * mOldPassField
gcn::TextField * mSecondPassField
static void setState(State state)
Definition client.h:169
Label widget.
Definition label.h:34
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
std::string username
Definition logindata.h:33
std::string password
Definition logindata.h:34
std::string newPassword
Definition logindata.h:36
virtual unsigned int getMaxPasswordLength() const
virtual unsigned int getMinPasswordLength() const
An 'Ok' button dialog.
Definition okdialog.h:34
A password field.
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 reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
Definition window.cpp:748
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
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_CHANGEPASSWORD_ATTEMPT
Definition client.h:81
#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.