GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/pincodedialog.cpp Lines: 1 53 1.9 %
Date: 2021-03-17 Branches: 2 74 2.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/windows/pincodedialog.h"
23
24
#include "pincodemanager.h"
25
26
#include "gui/widgets/button.h"
27
#include "gui/widgets/label.h"
28
#include "gui/widgets/passwordfield.h"
29
#include "gui/widgets/pincode.h"
30
31
#include "utils/gettext.h"
32
33
#include "gui/fonts/font.h"
34
35
#include "debug.h"
36
37
int PincodeDialog::instances = 0;
38
39
PincodeDialog::PincodeDialog(const std::string &restrict title,
40
                             const std::string &restrict msg,
41
                             uint32_t seed,
42
                             Window *const parent) :
43
    Window(title, Modal_true, parent, "pincode.xml"),
44
    ActionListener(),
45
    mPasswordField(new PasswordField(this, std::string())),
46
    mPincode(new Pincode(this, mPasswordField)),
47
    // TRANSLATORS: text dialog button
48
    mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this))
49
{
50
    setStickyButtonLock(true);
51
52
    Label *const textLabel = new Label(this, msg);
53
    Button *const cancelButton = new Button(this,
54
        // TRANSLATORS: text dialog button
55
        _("Cancel"),
56
        "CANCEL",
57
        BUTTON_SKIN,
58
        this);
59
60
    place(0, 0, textLabel, 4, 1);
61
    place(0, 1, mPincode, 4, 1);
62
    place(0, 2, mPasswordField, 4, 1);
63
    place(2, 3, mOkButton, 1, 1);
64
    place(3, 3, cancelButton, 1, 1);
65
66
    mPasswordField->setEnabled(false);
67
    mPasswordField->setActionEventId("pincode");
68
    mPasswordField->addActionListener(this);
69
70
    mPincode->shuffle(seed);
71
    int width = getFont()->getWidth(title);
72
    if (width < textLabel->getWidth())
73
        width = textLabel->getWidth();
74
    if (width < mPincode->getWidth())
75
        width = mPincode->getWidth();
76
    reflowLayout(CAST_S32(width + 20), 0);
77
    updateButtons();
78
}
79
80
void PincodeDialog::postInit()
81
{
82
    Window::postInit();
83
    if (getParent() != nullptr)
84
    {
85
        setLocationRelativeTo(getParent());
86
        getParent()->moveToTop(this);
87
    }
88
    setVisible(Visible_true);
89
    requestModalFocus();
90
    mPincode->requestFocus();
91
92
    instances++;
93
}
94
95
PincodeDialog::~PincodeDialog()
96
{
97
    instances--;
98
    pincodeManager.clearDialog(this);
99
}
100
101
void PincodeDialog::action(const ActionEvent &event)
102
{
103
    const std::string &eventId = event.getId();
104
    if (eventId == "pincode")
105
    {
106
        updateButtons();
107
        return;
108
    }
109
    if (eventId == "CANCEL")
110
        setActionEventId("~" + getActionEventId());
111
112
    distributeActionEvent();
113
    close();
114
}
115
116
const std::string &PincodeDialog::getMsg() const
117
{
118
    return mPasswordField->getText();
119
}
120
121
void PincodeDialog::close()
122
{
123
    pincodeManager.clearDialog(this);
124
    scheduleDelete();
125
}
126
127
void PincodeDialog::updateButtons()
128
{
129
    if (mPasswordField->getText().size() == 4)
130
    {
131
        mOkButton->setEnabled(true);
132
    }
133
    else
134
    {
135
        mOkButton->setEnabled(false);
136
    }
137

3
}