GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/logindialog.cpp Lines: 100 191 52.4 %
Date: 2017-11-29 Branches: 104 346 30.1 %

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

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

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

2
        mLoginData->remember)),
77
    // TRANSLATORS: login dialog label
78

4
    mUpdateTypeLabel(new Label(this, _("Update:"))),
79
4
    mUpdateTypeModel(new UpdateTypeModel),
80

2
    mUpdateTypeDropDown(new DropDown(this, mUpdateTypeModel)),
81
    // TRANSLATORS: login dialog button
82

4
    mServerButton(new Button(this, _("Change Server"), "server", this)),
83
    // TRANSLATORS: login dialog button
84

4
    mLoginButton(new Button(this, _("Login"), "login", this)),
85
    // TRANSLATORS: login dialog button
86

4
    mRegisterButton(new Button(this, _("Register"), "register", this)),
87
    // TRANSLATORS: login dialog checkbox
88
2
    mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
89
4
        (mLoginData->updateType & UpdateType::Custom) != 0,
90

2
        this, "customhost")),
91

16
    mUpdateHostText(new TextField(this, serverConfig.getValue(
92

2
        "customUpdateHost", ""))),
93
    mUpdateListModel(nullptr),
94
    mUpdateHostDropDown(nullptr),
95
    mUpdateHost(updateHost),
96








132
    mServerName(server->hostname)
97
{
98
2
    setCloseButton(true);
99
10
    setWindowName("Login");
100
101
2
    if (charServerHandler != nullptr)
102
2
        charServerHandler->clear();
103
104
2
    mergeUpdateHosts();
105
106
    // TRANSLATORS: login dialog label
107

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

2
    Label *const serverLabel2 = new Label(this, mServerName);
109
2
    serverLabel2->adjustSize();
110
    // TRANSLATORS: login dialog label
111

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

8
    Label *const passLabel = new Label(this, _("Password:"));
114
4
    if (mServer->updateHosts.size() > 1)
115
    {
116
        mUpdateListModel = new UpdateListModel(mServer);
117
        mUpdateHostDropDown = new DropDown(this, mUpdateListModel,
118
            false, Modal_false, this, "updateselect");
119
        const std::string str = serverConfig.getValue("updateHost2", "");
120
        if (!str.empty())
121
            mUpdateHostDropDown->setSelectedString(str);
122
    }
123
    else
124
    {
125
2
        mUpdateListModel = nullptr;
126
2
        mUpdateHostDropDown = nullptr;
127
    }
128
2
    mUpdateHostText->adjustSize();
129
130

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

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


2
    return !mUserField->getText().empty() &&
300





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

8
}