GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/logindialog.cpp Lines: 105 197 53.3 %
Date: 2020-06-04 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
 *
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/logindialog.h"
24
25
#include "client.h"
26
#include "configuration.h"
27
28
#include "const/sound.h"
29
30
#include "fs/paths.h"
31
32
#include "gui/models/updatelistmodel.h"
33
#include "gui/models/updatetypemodel.h"
34
35
#include "gui/windows/confirmdialog.h"
36
37
#include "gui/widgets/button.h"
38
#include "gui/widgets/checkbox.h"
39
#include "gui/widgets/createwidget.h"
40
#include "gui/widgets/dropdown.h"
41
#include "gui/widgets/label.h"
42
#include "gui/widgets/passwordfield.h"
43
#include "gui/widgets/layoutcell.h"
44
45
#include "listeners/openurllistener.h"
46
47
#include "net/charserverhandler.h"
48
#include "net/logindata.h"
49
#include "net/loginhandler.h"
50
#include "net/updatetypeoperators.h"
51
52
#include "utils/delete2.h"
53
#include "utils/stdmove.h"
54
55
#include "debug.h"
56
57
1
std::string LoginDialog::savedPassword;
58
1
std::string LoginDialog::savedPasswordKey;
59
60
namespace
61
{
62
1
    OpenUrlListener urlListener;
63
}  // namespace
64
65
1
LoginDialog::LoginDialog(LoginData &data,
66
                         ServerInfo *const server,
67
1
                         std::string *const updateHost) :
68
    // TRANSLATORS: login dialog name
69
1
    Window(_("Login"), Modal_false, nullptr, "login.xml"),
70
    ActionListener(),
71
    KeyListener(),
72
    mLoginData(&data),
73
    mServer(server),
74
1
    mUserField(new TextField(this, mLoginData->username,
75

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

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

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

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

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

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

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

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

1
        this, "customhost")),
103

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

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






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

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

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

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

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

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

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

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


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





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

4
}