GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/registerdialog.cpp Lines: 57 135 42.2 %
Date: 2017-11-29 Branches: 71 268 26.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  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
23
#include "gui/windows/registerdialog.h"
24
25
#include "client.h"
26
#include "configuration.h"
27
28
#include "being/being.h"
29
30
#include "gui/windows/okdialog.h"
31
32
#include "gui/widgets/button.h"
33
#include "gui/widgets/containerplacer.h"
34
#include "gui/widgets/createwidget.h"
35
#include "gui/widgets/label.h"
36
#include "gui/widgets/layoutcell.h"
37
#include "gui/widgets/passwordfield.h"
38
#include "gui/widgets/radiobutton.h"
39
40
#include "listeners/wrongdatanoticelistener.h"
41
42
#include "net/logindata.h"
43
#include "net/loginhandler.h"
44
#include "net/serverfeatures.h"
45
46
#include "utils/delete2.h"
47
#include "utils/gettext.h"
48
49
#include "debug.h"
50
51
2
RegisterDialog::RegisterDialog(LoginData &data) :
52
    // TRANSLATORS: register dialog name
53
2
    Window(_("Register"), Modal_false, nullptr, "register.xml"),
54
    ActionListener(),
55
    KeyListener(),
56
    mLoginData(&data),
57

2
    mUserField(new TextField(this, mLoginData->username)),
58

2
    mPasswordField(new PasswordField(this, mLoginData->password)),
59

2
    mConfirmField(new PasswordField(this)),
60
    mEmailField(nullptr),
61
    // TRANSLATORS: register dialog. button.
62

4
    mRegisterButton(new Button(this, _("Register"), "register", this)),
63
    // TRANSLATORS: register dialog. button.
64

4
    mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
65
    mMaleButton(nullptr),
66
    mFemaleButton(nullptr),
67





64
    mWrongDataNoticeListener(new WrongDataNoticeListener)
68
{
69
2
    setCloseButton(true);
70
71
    // TRANSLATORS: register dialog. label.
72

8
    Label *const userLabel = new Label(this, _("Name:"));
73
    // TRANSLATORS: register dialog. label.
74

8
    Label *const passwordLabel = new Label(this, _("Password:"));
75
    // TRANSLATORS: register dialog. label.
76

8
    Label *const confirmLabel = new Label(this, _("Confirm:"));
77
78
2
    ContainerPlacer placer;
79
2
    placer = getPlacer(0, 0);
80
2
    placer(0, 0, userLabel);
81
2
    placer(0, 1, passwordLabel);
82
2
    placer(0, 2, confirmLabel);
83
84
4
    placer(1, 0, mUserField, 3).setPadding(2);
85
4
    placer(1, 1, mPasswordField, 3).setPadding(2);
86
4
    placer(1, 2, mConfirmField, 3).setPadding(2);
87
88
2
    int row = 3;
89
90

8
    if (features.getIntValue("forceAccountGender") == -1)
91
    {
92
        // TRANSLATORS: register dialog. button.
93


14
        mMaleButton = new RadioButton(this, _("Male"), "sex", true);
94
        // TRANSLATORS: register dialog. button.
95


14
        mFemaleButton = new RadioButton(this, _("Female"), "sex", false);
96
2
        placer(1, row, mMaleButton);
97
2
        placer(2, row, mFemaleButton);
98
99
        row++;
100
    }
101
102

2
    if (serverFeatures->haveEmailOnRegister())
103
    {
104
        // TRANSLATORS: register dialog. label.
105
        Label *const emailLabel = new Label(this, _("Email:"));
106
        mEmailField = new TextField(this);
107
        placer(0, row, emailLabel);
108
        placer(1, row, mEmailField, 3).setPadding(2);
109
        mEmailField->addKeyListener(this);
110
        mEmailField->setActionEventId("register");
111
        mEmailField->addActionListener(this);
112
//        row++;
113
    }
114
115
2
    placer = getPlacer(0, 2);
116
2
    placer(1, 0, mRegisterButton);
117
2
    placer(2, 0, mCancelButton);
118
2
    reflowLayout(250, 0);
119
120
2
    mUserField->addKeyListener(this);
121
2
    mPasswordField->addKeyListener(this);
122
2
    mConfirmField->addKeyListener(this);
123
124
10
    mUserField->setActionEventId("register");
125
10
    mPasswordField->setActionEventId("register");
126
10
    mConfirmField->setActionEventId("register");
127
128
2
    mUserField->addActionListener(this);
129
2
    mPasswordField->addActionListener(this);
130
2
    mConfirmField->addActionListener(this);
131
132
2
    center();
133
2
}
134
135
2
void RegisterDialog::postInit()
136
{
137
2
    Window::postInit();
138
2
    setVisible(Visible_true);
139
2
    mUserField->requestFocus();
140
4
    mUserField->setCaretPosition(CAST_U32(
141
2
                                 mUserField->getText().length()));
142
143
4
    mRegisterButton->setEnabled(canSubmit());
144
2
}
145
146
10
RegisterDialog::~RegisterDialog()
147
{
148
4
    delete2(mWrongDataNoticeListener);
149
4
}
150
151
void RegisterDialog::action(const ActionEvent &event)
152
{
153
    const std::string &eventId = event.getId();
154
    if (eventId == "cancel")
155
    {
156
        close();
157
    }
158
    else if (eventId == "register" && canSubmit())
159
    {
160
        const std::string &user = mUserField->getText();
161
        logger->log("RegisterDialog::register Username is %s", user.c_str());
162
163
        std::string errorMsg;
164
        int error = 0;
165
166
        const unsigned int minUser = loginHandler->getMinUserNameLength();
167
        const unsigned int maxUser = loginHandler->getMaxUserNameLength();
168
        const unsigned int minPass = loginHandler->getMinPasswordLength();
169
        const unsigned int maxPass = loginHandler->getMaxPasswordLength();
170
171
        if (user.length() < minUser)
172
        {
173
            // Name too short
174
            errorMsg = strprintf
175
                // TRANSLATORS: error message
176
                (_("The username needs to be at least %u characters long."),
177
                 minUser);
178
            error = 1;
179
        }
180
        else if (user.length() > maxUser - 1)
181
        {
182
            // Name too long
183
            errorMsg = strprintf
184
                // TRANSLATORS: error message
185
                (_("The username needs to be less than %u characters long."),
186
                 maxUser);
187
            error = 1;
188
        }
189
        else if (mPasswordField->getText().length() < minPass)
190
        {
191
            // Pass too short
192
            errorMsg = strprintf
193
                // TRANSLATORS: error message
194
                (_("The password needs to be at least %u characters long."),
195
                 minPass);
196
            error = 2;
197
        }
198
        else if (mPasswordField->getText().length() > maxPass)
199
        {
200
            // Pass too long
201
            errorMsg = strprintf
202
                // TRANSLATORS: error message
203
                (_("The password needs to be less than %u characters long."),
204
                 maxPass);
205
            error = 2;
206
        }
207
        else if (mPasswordField->getText() != mConfirmField->getText())
208
        {
209
            // Password does not match with the confirmation one
210
            // TRANSLATORS: error message
211
            errorMsg = _("Passwords do not match.");
212
            error = 2;
213
        }
214
        else if ((mEmailField != nullptr) &&
215
                 mEmailField->getText().find('@') == std::string::npos)
216
        {
217
            // TRANSLATORS: error message
218
            errorMsg = _("Incorrect email.");
219
            error = 1;
220
        }
221
        else if (mEmailField != nullptr &&
222
                 mEmailField->getText().size() > 40)
223
        {
224
            // TRANSLATORS: error message
225
            errorMsg = _("Email too long.");
226
            error = 1;
227
        }
228
229
        if (error > 0)
230
        {
231
            if (error == 1)
232
            {
233
                mWrongDataNoticeListener->setTarget(this->mUserField);
234
            }
235
            else if (error == 2)
236
            {
237
                // Reset password confirmation
238
                mPasswordField->setText("");
239
                mConfirmField->setText("");
240
                mWrongDataNoticeListener->setTarget(this->mPasswordField);
241
            }
242
243
            OkDialog *const dlg = CREATEWIDGETR(OkDialog,
244
                // TRANSLATORS: error message
245
                _("Error"), errorMsg, _("OK"),
246
                DialogType::ERROR,
247
                Modal_true,
248
                ShowCenter_true,
249
                nullptr,
250
                260);
251
            dlg->addActionListener(mWrongDataNoticeListener);
252
        }
253
        else
254
        {
255
            // No errors detected, register the new user.
256
            mRegisterButton->setEnabled(false);
257
            mLoginData->username = mUserField->getText();
258
            mLoginData->password = mPasswordField->getText();
259
            if (features.getIntValue("forceAccountGender") == -1)
260
            {
261
                if ((mFemaleButton != nullptr) && mFemaleButton->isSelected())
262
                    mLoginData->gender = Gender::FEMALE;
263
                else
264
                    mLoginData->gender = Gender::MALE;
265
            }
266
            else
267
            {
268
                mLoginData->gender = Being::intToGender(
269
                    CAST_U8(features.getIntValue("forceAccountGender")));
270
            }
271
272
            if (mEmailField != nullptr)
273
                mLoginData->email = mEmailField->getText();
274
            mLoginData->registerLogin = true;
275
276
            client->setState(State::REGISTER_ATTEMPT);
277
        }
278
    }
279
}
280
281
void RegisterDialog::keyPressed(KeyEvent &event)
282
{
283
    if (event.isConsumed())
284
    {
285
        mRegisterButton->setEnabled(canSubmit());
286
        return;
287
    }
288
    const InputActionT actionId = event.getActionId();
289
    if (actionId == InputAction::GUI_CANCEL)
290
    {
291
        action(ActionEvent(nullptr, mCancelButton->getActionEventId()));
292
    }
293
    else if (actionId == InputAction::GUI_SELECT ||
294
             actionId == InputAction::GUI_SELECT2)
295
    {
296
        action(ActionEvent(nullptr, mRegisterButton->getActionEventId()));
297
    }
298
    else
299
    {
300
        mRegisterButton->setEnabled(canSubmit());
301
    }
302
}
303
304
2
bool RegisterDialog::canSubmit() const
305
{
306
2
    return !mUserField->getText().empty() &&
307
           !mPasswordField->getText().empty() &&
308
           !mConfirmField->getText().empty() &&
309

2
           client->getState() == State::REGISTER &&
310
           ((mEmailField == nullptr) || !mEmailField->getText().empty());
311
}
312
313
void RegisterDialog::close()
314
{
315
    client->setState(State::LOGIN);
316
    Window::close();
317

6
}