Mana
Loading...
Searching...
No Matches
unregisterdialog.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/okdialog.h"
28#include "gui/register.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(_("Unregister"), true),
46 mWrongDataNoticeListener(new WrongDataNoticeListener),
47 mLoginData(loginData)
48{
49 gcn::Label *userLabel = new Label(strprintf(_("Name: %s"), mLoginData->
50 username.c_str()));
51 gcn::Label *passwordLabel = new Label(_("Password:"));
53 mUnRegisterButton = new Button(_("Unregister"), "unregister", this);
54 mCancelButton = new Button(_("Cancel"), "cancel", this);
55
56 const int width = 210;
57 const int height = 80;
58 setContentSize(width, height);
59
60 userLabel->setPosition(5, 5);
61 userLabel->setWidth(width - 5);
62 mPasswordField->setPosition(
63 68, userLabel->getY() + userLabel->getHeight() + 7);
64 mPasswordField->setWidth(130);
65
66 passwordLabel->setPosition(5, mPasswordField->getY() + 1);
67
68 mCancelButton->setPosition(
69 width - 5 - mCancelButton->getWidth(),
70 height - 5 - mCancelButton->getHeight());
71 mUnRegisterButton->setPosition(
72 mCancelButton->getX() - 5 - mUnRegisterButton->getWidth(),
73 mCancelButton->getY());
74
75 add(userLabel);
76 add(passwordLabel);
77 add(mPasswordField);
79 add(mCancelButton);
80
81 center();
82 setVisible(true);
83 mPasswordField->requestFocus();
84 mPasswordField->setActionEventId("cancel");
85}
86
91
92void UnRegisterDialog::action(const gcn::ActionEvent &event)
93{
94 if (event.getId() == "cancel")
95 {
97 }
98 else if (event.getId() == "unregister")
99 {
100 const std::string &password = mPasswordField->getText();
101 Log::info("UnregisterDialog::unregistered, Username is %s",
102 mLoginData->username.c_str());
103
104 std::stringstream errorMessage;
105 bool error = false;
106
107 unsigned int min = Net::getLoginHandler()->getMinPasswordLength();
108 unsigned int max = Net::getLoginHandler()->getMaxPasswordLength();
109
110 // Check password
111 if (password.length() < min)
112 {
113 // Pass too short
114 errorMessage << strprintf(_("The password needs to be at least %d "
115 "characters long."), min);
116 error = true;
117 }
118 else if (password.length() > max - 1)
119 {
120 // Pass too long
121 errorMessage << strprintf(_("The password needs to be less than "
122 "%d characters long."), max);
123 error = true;
124 }
125
126 if (error)
127 {
129
130 auto *dlg = new OkDialog(_("Error"), errorMessage.str());
131 dlg->addActionListener(mWrongDataNoticeListener);
132 }
133 else
134 {
135 // No errors detected, unregister the new user.
136 mUnRegisterButton->setEnabled(false);
137 mLoginData->password = password;
139 }
140 }
141}
Button widget.
Definition button.h:38
static void setState(State state)
Definition client.h:169
Label widget.
Definition label.h:34
std::string username
Definition logindata.h:33
std::string password
Definition logindata.h:34
virtual unsigned int getMaxPasswordLength() const
virtual unsigned int getMinPasswordLength() const
An 'Ok' button dialog.
Definition okdialog.h:34
A password field.
LoginData * mLoginData
UnRegisterDialog(LoginData *loginData)
gcn::Button * mUnRegisterButton
WrongDataNoticeListener * mWrongDataNoticeListener
gcn::Button * mCancelButton
~UnRegisterDialog() override
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
gcn::TextField * mPasswordField
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_UNREGISTER_ATTEMPT
Definition client.h:87
@ STATE_CHAR_SELECT
Definition client.h:71
#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.