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

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

3
}