GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/logindialog.cpp Lines: 105 197 53.3 %
Date: 2021-03-17 Branches: 100 342 29.2 %

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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/windows/logindialog.h"
25
26
#include "client.h"
27
#include "configuration.h"
28
29
#include "const/sound.h"
30
31
#include "fs/paths.h"
32
33
#include "gui/models/updatelistmodel.h"
34
#include "gui/models/updatetypemodel.h"
35
36
#include "gui/windows/confirmdialog.h"
37
38
#include "gui/widgets/button.h"
39
#include "gui/widgets/checkbox.h"
40
#include "gui/widgets/createwidget.h"
41
#include "gui/widgets/dropdown.h"
42
#include "gui/widgets/label.h"
43
#include "gui/widgets/passwordfield.h"
44
#include "gui/widgets/layoutcell.h"
45
46
#include "listeners/openurllistener.h"
47
48
#include "net/charserverhandler.h"
49
#include "net/logindata.h"
50
#include "net/loginhandler.h"
51
#include "net/updatetypeoperators.h"
52
53
#include "utils/delete2.h"
54
#include "utils/stdmove.h"
55
56
#include "debug.h"
57
58
1
std::string LoginDialog::savedPassword;
59
1
std::string LoginDialog::savedPasswordKey;
60
61
namespace
62
{
63
1
    OpenUrlListener urlListener;
64
}  // namespace
65
66
1
LoginDialog::LoginDialog(LoginData &data,
67
                         ServerInfo *const server,
68
1
                         std::string *const updateHost) :
69
    // TRANSLATORS: login dialog name
70
1
    Window(_("Login"), Modal_false, nullptr, "login.xml"),
71
    ActionListener(),
72
    KeyListener(),
73
    mLoginData(&data),
74
    mServer(server),
75
1
    mUserField(new TextField(this, mLoginData->username,
76

3
        LoseFocusOnTab_true, nullptr, std::string(), false)),
77

1
    mPassField(new PasswordField(this, mLoginData->password)),
78
#ifdef SAVE_PASSWORD
79
    // TRANSLATORS: login dialog label
80
    mKeepCheck(new CheckBox(this, _("Remember user and password"),
81
#else
82
    // TRANSLATORS: login dialog label
83
1
    mKeepCheck(new CheckBox(this, _("Remember username"),
84
#endif
85

2
    mLoginData->remember, nullptr, std::string())),
86
    // TRANSLATORS: login dialog label
87

2
    mUpdateTypeLabel(new Label(this, _("Update:"))),
88
2
    mUpdateTypeModel(new UpdateTypeModel),
89
1
    mUpdateTypeDropDown(new DropDown(this, mUpdateTypeModel,
90

3
        false, Modal_false, nullptr, std::string())),
91
    // TRANSLATORS: login dialog button
92
1
    mServerButton(new Button(this, _("Change Server"), "server",
93

1
        BUTTON_SKIN, this)),
94
    // TRANSLATORS: login dialog button
95
1
    mLoginButton(new Button(this, _("Login"), "login",
96

1
        BUTTON_SKIN, this)),
97
    // TRANSLATORS: login dialog button
98
1
    mRegisterButton(new Button(this, _("Register"), "register",
99

1
        BUTTON_SKIN, this)),
100
    // TRANSLATORS: login dialog checkbox
101
1
    mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
102
2
        (mLoginData->updateType & UpdateType::Custom) != 0,
103

1
        this, "customhost")),
104

8
    mUpdateHostText(new TextField(this, serverConfig.getValue(
105
        "customUpdateHost", ""),
106

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
107
    mUpdateListModel(nullptr),
108
    mUpdateHostDropDown(nullptr),
109
    mUpdateHost(updateHost),
110






55
    mServerName(server->hostname)
111
{
112
1
    setCloseButton(true);
113
5
    setWindowName("Login");
114
115
1
    if (charServerHandler != nullptr)
116
1
        charServerHandler->clear();
117
118
1
    mergeUpdateHosts();
119
120
    // TRANSLATORS: login dialog label
121

4
    Label *const serverLabel1 = new Label(this, _("Server:"));
122

1
    Label *const serverLabel2 = new Label(this, mServerName);
123
1
    serverLabel2->adjustSize();
124
    // TRANSLATORS: login dialog label
125

4
    Label *const userLabel = new Label(this, _("Name:"));
126
    // TRANSLATORS: login dialog label
127

4
    Label *const passLabel = new Label(this, _("Password:"));
128
2
    if (mServer->updateHosts.size() > 1)
129
    {
130
        mUpdateListModel = new UpdateListModel(mServer);
131
        mUpdateHostDropDown = new DropDown(this, mUpdateListModel,
132
            false, Modal_false, this, "updateselect");
133
        const std::string str = serverConfig.getValue("updateHost2", "");
134
        if (!str.empty())
135
            mUpdateHostDropDown->setSelectedString(str);
136
    }
137
    else
138
    {
139
1
        mUpdateListModel = nullptr;
140
1
        mUpdateHostDropDown = nullptr;
141
    }
142
1
    mUpdateHostText->adjustSize();
143
144

2
    if (mPassField->getText().empty() &&
145
1
        !LoginDialog::savedPassword.empty())
146
    {
147
        mPassField->setText(LoginDialog::savedPassword);
148
    }
149
150
5
    mUpdateTypeDropDown->setActionEventId("updatetype");
151
2
    mUpdateTypeDropDown->setSelected((mLoginData->updateType
152

3
        | UpdateType::Custom) ^ CAST_S32(UpdateType::Custom));
153
154
1
    if (!mCustomUpdateHost->isSelected())
155
1
        mUpdateHostText->setVisible(Visible_false);
156
157
5
    mUserField->setActionEventId("login");
158
5
    mPassField->setActionEventId("login");
159
160
1
    mUserField->addKeyListener(this);
161
1
    mPassField->addKeyListener(this);
162
1
    mUserField->addActionListener(this);
163
1
    mPassField->addActionListener(this);
164
165
1
    place(0, 0, serverLabel1, 1, 1);
166
1
    place(1, 0, serverLabel2, 8, 1);
167
1
    place(0, 1, userLabel, 1, 1);
168
1
    place(1, 1, mUserField, 8, 1);
169
1
    place(0, 2, passLabel, 1, 1);
170
1
    place(1, 2, mPassField, 8, 1);
171
1
    place(0, 6, mUpdateTypeLabel, 1, 1);
172

1
    place(1, 6, mUpdateTypeDropDown, 8, 1);
173
1
    int n = 7;
174
1
    if (mUpdateHostDropDown != nullptr)
175
    {
176
        place(0, 7, mUpdateHostDropDown, 9, 1);
177
        n += 1;
178
    }
179
1
    place(0, n, mCustomUpdateHost, 9, 1);
180
1
    place(0, n + 1, mUpdateHostText, 9, 1);
181
1
    place(0, n + 2, mKeepCheck, 9, 1);
182
2
    place(0, n + 3, mRegisterButton, 1, 1).setHAlign(LayoutCell::LEFT);
183
1
    place(2, n + 3, mServerButton, 1, 1);
184
1
    place(3, n + 3, mLoginButton, 1, 1);
185
186
1
    addKeyListener(this);
187
1
}
188
189
1
void LoginDialog::postInit()
190
{
191
1
    Window::postInit();
192
1
    setVisible(Visible_true);
193
194
1
    const int h = 200;
195
1
    if (mUpdateHostDropDown != nullptr)
196
        setContentSize(310, 250);
197
1
    setContentSize(310, h);
198
#ifdef ANDROID
199
    setDefaultSize(310, h, ImagePosition::UPPER_CENTER, 0, 0);
200
#else  // ANDROID
201
202
1
    setDefaultSize(310, h, ImagePosition::CENTER, 0, 0);
203
#endif  // ANDROID
204
205
1
    center();
206
1
    loadWindowState();
207
1
    reflowLayout(0, 0);
208
209
#ifdef SAVE_PASSWORD
210
    mPassField->setText(LoginDialog::savedPassword);
211
#else
212
1
    if (mUserField->getText().empty())
213
1
        mUserField->requestFocus();
214
    else
215
        mPassField->requestFocus();
216
#endif
217
218
2
    mLoginButton->setEnabled(canSubmit());
219
1
    if (loginHandler != nullptr)
220
    {
221
        mRegisterButton->setEnabled(loginHandler->isRegistrationEnabled()
222
            || !mLoginData->registerUrl.empty());
223
    }
224
    else
225
    {
226
1
        mRegisterButton->setEnabled(false);
227
    }
228
1
}
229
230
6
LoginDialog::~LoginDialog()
231
{
232
1
    if (mUpdateTypeDropDown != nullptr)
233
1
        mUpdateTypeDropDown->hideDrop(false);
234
1
    if (mUpdateHostDropDown != nullptr)
235
        mUpdateHostDropDown->hideDrop(false);
236
237
2
    delete2(mUpdateTypeModel)
238
2
    delete2(mUpdateListModel)
239
2
}
240
241
void LoginDialog::action(const ActionEvent &event)
242
{
243
    const std::string &eventId = event.getId();
244
    if (eventId == "login" && canSubmit())
245
    {
246
        prepareUpdate();
247
        mLoginData->registerLogin = false;
248
        client->setState(State::LOGIN_ATTEMPT);
249
    }
250
    else if (eventId == "server")
251
    {
252
        close();
253
    }
254
    else if (eventId == "register")
255
    {
256
        if (loginHandler->isRegistrationEnabled())
257
        {
258
            prepareUpdate();
259
            client->setState(State::REGISTER_PREP);
260
        }
261
        else if (!mLoginData->registerUrl.empty())
262
        {
263
            const std::string &url = mLoginData->registerUrl;
264
            urlListener.url = url;
265
            ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog,
266
                // TRANSLATORS: question dialog
267
                _("Open register url"),
268
                url,
269
                SOUND_REQUEST,
270
                false,
271
                Modal_true,
272
                nullptr);
273
            confirmDlg->addActionListener(&urlListener);
274
        }
275
    }
276
    else if (eventId == "customhost")
277
    {
278
        mUpdateHostText->setVisible(fromBool(
279
            mCustomUpdateHost->isSelected(), Visible));
280
    }
281
    else if (eventId == "updateselect")
282
    {
283
        mCustomUpdateHost->setSelected(false);
284
        mUpdateHostText->setVisible(Visible_false);
285
    }
286
}
287
288
void LoginDialog::keyPressed(KeyEvent &event)
289
{
290
    if (event.isConsumed())
291
    {
292
        mLoginButton->setEnabled(canSubmit());
293
        return;
294
    }
295
296
    const InputActionT actionId = event.getActionId();
297
    if (actionId == InputAction::GUI_CANCEL)
298
    {
299
        action(ActionEvent(nullptr, mServerButton->getActionEventId()));
300
    }
301
#ifdef USE_SDL2
302
    else if (actionId == InputAction::GUI_SELECT2)
303
#else  // USE_SDL2
304
    else if (actionId == InputAction::GUI_SELECT ||
305
             actionId == InputAction::GUI_SELECT2)
306
#endif  // USE_SDL2
307
    {
308
        action(ActionEvent(nullptr, mLoginButton->getActionEventId()));
309
    }
310
    else
311
    {
312
        mLoginButton->setEnabled(canSubmit());
313
    }
314
}
315
316
bool LoginDialog::canSubmit() const
317
{
318


1
    return !mUserField->getText().empty() &&
319





1
        !mPassField->getText().empty() &&
320
        client->getState() == State::LOGIN;
321
}
322
323
void LoginDialog::prepareUpdate()
324
{
325
    mLoginData->username = mUserField->getText();
326
    mLoginData->password = mPassField->getText();
327
    mLoginData->remember = mKeepCheck->isSelected();
328
    UpdateTypeT updateType = static_cast<UpdateTypeT>(
329
        mUpdateTypeDropDown->getSelected());
330
331
    if (mCustomUpdateHost->isSelected()
332
        && !mUpdateHostText->getText().empty())
333
    {
334
        updateType = static_cast<UpdateTypeT>(
335
            updateType | UpdateType::Custom);
336
        serverConfig.setValue("customUpdateHost",
337
            mUpdateHostText->getText());
338
339
        if (checkPath(mUpdateHostText->getText()))
340
        {
341
            mLoginData->updateHost = mUpdateHostText->getText();
342
            *mUpdateHost = mLoginData->updateHost;
343
        }
344
        else
345
        {
346
            mLoginData->updateHost.clear();
347
            (*mUpdateHost).clear();
348
        }
349
    }
350
    else
351
    {
352
        std::string str;
353
        if (mUpdateHostDropDown != nullptr)
354
        {
355
            const int sel = mUpdateHostDropDown->getSelected();
356
            if (sel >= 0)
357
            {
358
                const HostsGroup &group = mServer->updateHosts[sel];
359
                if (!group.hosts.empty())
360
                {
361
                    str = group.hosts[0];
362
                    mLoginData->updateHosts = group.hosts;
363
                    serverConfig.setValue("updateHost2", group.name);
364
                }
365
                else
366
                {
367
                    serverConfig.setValue("updateHost2", "");
368
                }
369
            }
370
        }
371
        else if (mLoginData->updateHost.empty()
372
                 && !mLoginData->updateHosts.empty())
373
        {
374
            str = mLoginData->updateHosts[0];
375
            serverConfig.setValue("updateHost2", str);
376
        }
377
        if (!str.empty() && checkPath(str))
378
        {
379
            mLoginData->updateHost = str;
380
            *mUpdateHost = STD_MOVE(str);
381
        }
382
        else
383
        {
384
            mLoginData->updateHost.clear();
385
            (*mUpdateHost).clear();
386
        }
387
    }
388
389
    mLoginData->updateType = updateType;
390
    serverConfig.setValue("updateType", CAST_S32(updateType));
391
392
    mRegisterButton->setEnabled(false);
393
    mServerButton->setEnabled(false);
394
    mLoginButton->setEnabled(false);
395
396
    LoginDialog::savedPassword = mPassField->getText();
397
    if (mLoginData->remember)
398
        LoginDialog::savedPasswordKey = mServerName;
399
    else
400
        LoginDialog::savedPasswordKey = "-";
401
}
402
403
void LoginDialog::close()
404
{
405
    client->setState(State::SWITCH_SERVER);
406
    Window::close();
407
}
408
409
1
void LoginDialog::mergeUpdateHosts()
410
{
411
2
    HostsGroup group;
412
413
2
    group.name = mServer->defaultHostName;
414
1
    if (group.name.empty())
415
    {
416
        // TRANSLATORS: update hosts group default name
417
1
        group.name = _("default updates");
418
    }
419
1
    group.hosts = mLoginData->updateHosts;
420
3
    mServer->updateHosts.insert(mServer->updateHosts.begin(), group);
421

4
}