GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/logindialog.cpp Lines: 105 196 53.6 %
Date: 2018-06-18 21:15:20 Branches: 100 338 29.6 %

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-2018  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
2
std::string LoginDialog::savedPassword;
58
2
std::string LoginDialog::savedPasswordKey;
59
60
namespace
61
{
62
2
    OpenUrlListener urlListener;
63
}  // namespace
64
65
2
LoginDialog::LoginDialog(LoginData &data,
66
                         ServerInfo *const server,
67
2
                         std::string *const updateHost) :
68
    // TRANSLATORS: login dialog name
69
2
    Window(_("Login"), Modal_false, nullptr, "login.xml"),
70
    ActionListener(),
71
    KeyListener(),
72
    mLoginData(&data),
73
    mServer(server),
74
2
    mUserField(new TextField(this, mLoginData->username,
75

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

2
    mPassField(new PasswordField(this, mLoginData->password)),
77
    // TRANSLATORS: login dialog label
78
2
    mKeepCheck(new CheckBox(this, _("Remember username"),
79

4
        mLoginData->remember, nullptr, std::string())),
80
    // TRANSLATORS: login dialog label
81

4
    mUpdateTypeLabel(new Label(this, _("Update:"))),
82
4
    mUpdateTypeModel(new UpdateTypeModel),
83
2
    mUpdateTypeDropDown(new DropDown(this, mUpdateTypeModel,
84

6
        false, Modal_false, nullptr, std::string())),
85
    // TRANSLATORS: login dialog button
86
2
    mServerButton(new Button(this, _("Change Server"), "server",
87

2
        BUTTON_SKIN, this)),
88
    // TRANSLATORS: login dialog button
89
2
    mLoginButton(new Button(this, _("Login"), "login",
90

2
        BUTTON_SKIN, this)),
91
    // TRANSLATORS: login dialog button
92
2
    mRegisterButton(new Button(this, _("Register"), "register",
93

2
        BUTTON_SKIN, this)),
94
    // TRANSLATORS: login dialog checkbox
95
2
    mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
96
4
        (mLoginData->updateType & UpdateType::Custom) != 0,
97

2
        this, "customhost")),
98

16
    mUpdateHostText(new TextField(this, serverConfig.getValue(
99
        "customUpdateHost", ""),
100

4
        LoseFocusOnTab_true, nullptr, std::string(), false)),
101
    mUpdateListModel(nullptr),
102
    mUpdateHostDropDown(nullptr),
103
    mUpdateHost(updateHost),
104






110
    mServerName(server->hostname)
105
{
106
2
    setCloseButton(true);
107
10
    setWindowName("Login");
108
109
2
    if (charServerHandler != nullptr)
110
2
        charServerHandler->clear();
111
112
2
    mergeUpdateHosts();
113
114
    // TRANSLATORS: login dialog label
115

8
    Label *const serverLabel1 = new Label(this, _("Server:"));
116

2
    Label *const serverLabel2 = new Label(this, mServerName);
117
2
    serverLabel2->adjustSize();
118
    // TRANSLATORS: login dialog label
119

8
    Label *const userLabel = new Label(this, _("Name:"));
120
    // TRANSLATORS: login dialog label
121

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

6
    if (mPassField->getText().empty() &&
139
2
        !LoginDialog::savedPassword.empty())
140
    {
141
        mPassField->setText(LoginDialog::savedPassword);
142
    }
143
144
10
    mUpdateTypeDropDown->setActionEventId("updatetype");
145
6
    mUpdateTypeDropDown->setSelected((mLoginData->updateType
146
6
        | UpdateType::Custom) ^ CAST_S32(UpdateType::Custom));
147
148
2
    if (!mCustomUpdateHost->isSelected())
149
2
        mUpdateHostText->setVisible(Visible_false);
150
151
10
    mUserField->setActionEventId("login");
152
10
    mPassField->setActionEventId("login");
153
154
2
    mUserField->addKeyListener(this);
155
2
    mPassField->addKeyListener(this);
156
2
    mUserField->addActionListener(this);
157
2
    mPassField->addActionListener(this);
158
159
2
    place(0, 0, serverLabel1, 1, 1);
160
2
    place(1, 0, serverLabel2, 8, 1);
161
2
    place(0, 1, userLabel, 1, 1);
162
2
    place(1, 1, mUserField, 8, 1);
163
2
    place(0, 2, passLabel, 1, 1);
164
2
    place(1, 2, mPassField, 8, 1);
165
2
    place(0, 6, mUpdateTypeLabel, 1, 1);
166

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


4
    return !mUserField->getText().empty() &&
309





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

8
}