Mana
Loading...
Searching...
No Matches
logindialog.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
22#include "gui/logindialog.h"
23
24#include "client.h"
25
26#include "gui/okdialog.h"
27#include "gui/sdlinput.h"
28
29#include "gui/widgets/button.h"
31#include "gui/widgets/label.h"
32#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
43 Window(_("Login")),
44 mLoginData(loginData)
45{
46 gcn::Label *userLabel = new Label(_("Name:"));
47 gcn::Label *passLabel = new Label(_("Password:"));
48
51
52 mKeepCheck = new CheckBox(_("Remember username"), mLoginData->remember);
53 mRegisterButton = new Button(_("Register"), "register", this);
54 mServerButton = new Button(_("Change Server"), "server", this);
55 mLoginButton = new Button(_("Login"), "login", this);
56
57 mUserField->setActionEventId("login");
58 mPassField->setActionEventId("login");
59
60 mUserField->addKeyListener(this);
61 mPassField->addKeyListener(this);
62 mUserField->addActionListener(this);
63 mPassField->addActionListener(this);
64
65 place(0, 0, userLabel);
66 place(0, 1, passLabel);
67 place(1, 0, mUserField, 3).setPadding(2);
68 place(1, 1, mPassField, 3).setPadding(2);
69 place(0, 5, mKeepCheck, 4);
71 place(2, 6, mServerButton);
72 place(3, 6, mLoginButton);
74
75 addKeyListener(this);
76
77 center();
78 setVisible(true);
79
80 if (mUserField->getText().empty())
81 mUserField->requestFocus();
82 else
83 mPassField->requestFocus();
84
85 mLoginButton->setEnabled(canSubmit());
86}
87
89
90void LoginDialog::action(const gcn::ActionEvent &event)
91{
92 if (event.getId() == "login" && canSubmit())
93 {
94 mLoginData->username = mUserField->getText();
95 mLoginData->password = mPassField->getText();
96 mLoginData->remember = mKeepCheck->isSelected();
98
99 mRegisterButton->setEnabled(false);
100 mServerButton->setEnabled(false);
101 mLoginButton->setEnabled(false);
102
104 }
105 else if (event.getId() == "server")
106 {
108 }
109 else if (event.getId() == "register")
110 {
111 if (Net::getLoginHandler()->isRegistrationEnabled())
112 {
113 mLoginData->username = mUserField->getText();
114 mLoginData->password = mPassField->getText();
116 }
117 else
118 {
119 new OkDialog(_("Registration disabled"), _("You need to use the "
120 "website to register an account for this server."));
121 }
122 }
123}
124
125void LoginDialog::keyPressed(gcn::KeyEvent &keyEvent)
126{
127 gcn::Key key = keyEvent.getKey();
128
129 if (key.getValue() == Key::ESCAPE)
130 {
131 action(gcn::ActionEvent(nullptr, mServerButton->getActionEventId()));
132 }
133 else if (key.getValue() == Key::ENTER)
134 {
135 action(gcn::ActionEvent(nullptr, mLoginButton->getActionEventId()));
136 }
137 else
138 {
139 mLoginButton->setEnabled(canSubmit());
140 }
141}
142
144{
145 return !mUserField->getText().empty() &&
146 !mPassField->getText().empty() &&
148}
Button widget.
Definition button.h:38
Check box widget.
Definition checkbox.h:32
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
Label widget.
Definition label.h:34
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
LayoutCell & setHAlign(Alignment a)
Sets the horizontal alignment of the cell content.
Definition layout.h:185
std::string username
Definition logindata.h:33
std::string password
Definition logindata.h:34
bool remember
Whether to store the username.
Definition logindata.h:44
bool registerLogin
Whether an account is being registered.
Definition logindata.h:45
gcn::Button * mLoginButton
Definition logindialog.h:66
gcn::TextField * mPassField
Definition logindialog.h:63
LoginData * mLoginData
Definition logindialog.h:69
bool canSubmit() const
Returns whether submit can be enabled.
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
~LoginDialog() override
void keyPressed(gcn::KeyEvent &keyEvent) override
Called when a key is pressed in one of the text fields.
LoginDialog(LoginData *loginData)
gcn::Button * mRegisterButton
Definition logindialog.h:67
gcn::CheckBox * mKeepCheck
Definition logindialog.h:64
gcn::TextField * mUserField
Definition logindialog.h:62
gcn::Button * mServerButton
Definition logindialog.h:65
An 'Ok' button dialog.
Definition okdialog.h:34
A password field.
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 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
LoginData loginData
Definition client.cpp:95
@ STATE_REGISTER_PREP
Definition client.h:77
@ STATE_SWITCH_SERVER
Definition client.h:89
@ STATE_LOGIN_ATTEMPT
Definition client.h:65
@ STATE_LOGIN
Definition client.h:64
#define _(s)
Definition gettext.h:38
@ ENTER
Definition sdlinput.h:78
@ ESCAPE
Definition sdlinput.h:96
LoginHandler * getLoginHandler()
Definition net.cpp:95