GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/pincodedialog.cpp Lines: 1 57 1.8 %
Date: 2018-06-18 21:15:20 Branches: 2 76 2.6 %

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 "input/keyboardconfig.h"
26
27
#include "gui/widgets/button.h"
28
#include "gui/widgets/label.h"
29
#include "gui/widgets/passwordfield.h"
30
#include "gui/widgets/pincode.h"
31
32
#include "utils/gettext.h"
33
34
#include "gui/fonts/font.h"
35
36
#include "debug.h"
37
38
int PincodeDialog::instances = 0;
39
40
PincodeDialog::PincodeDialog(const std::string &restrict title,
41
                             const std::string &restrict msg,
42
                             uint32_t seed,
43
                             Window *const parent) :
44
    Window(title, Modal_true, parent, "pincode.xml"),
45
    ActionListener(),
46
    mPasswordField(new PasswordField(this, std::string())),
47
    mPincode(new Pincode(this, mPasswordField)),
48
    // TRANSLATORS: text dialog button
49
    mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this)),
50
    mEnabledKeyboard(keyboard.isEnabled())
51
{
52
    setStickyButtonLock(true);
53
54
    keyboard.setEnabled(false);
55
56
    Label *const textLabel = new Label(this, msg);
57
    Button *const cancelButton = new Button(this,
58
        // TRANSLATORS: text dialog button
59
        _("Cancel"),
60
        "CANCEL",
61
        BUTTON_SKIN,
62
        this);
63
64
    place(0, 0, textLabel, 4, 1);
65
    place(0, 1, mPincode, 4, 1);
66
    place(0, 2, mPasswordField, 4, 1);
67
    place(2, 3, mOkButton, 1, 1);
68
    place(3, 3, cancelButton, 1, 1);
69
70
    mPasswordField->setEnabled(false);
71
    mPasswordField->setActionEventId("pincode");
72
    mPasswordField->addActionListener(this);
73
74
    mPincode->shuffle(seed);
75
    int width = getFont()->getWidth(title);
76
    if (width < textLabel->getWidth())
77
        width = textLabel->getWidth();
78
    if (width < mPincode->getWidth())
79
        width = mPincode->getWidth();
80
    reflowLayout(CAST_S32(width + 20), 0);
81
    updateButtons();
82
}
83
84
void PincodeDialog::postInit()
85
{
86
    Window::postInit();
87
    if (getParent() != nullptr)
88
    {
89
        setLocationRelativeTo(getParent());
90
        getParent()->moveToTop(this);
91
    }
92
    setVisible(Visible_true);
93
    requestModalFocus();
94
    mPincode->requestFocus();
95
96
    instances++;
97
}
98
99
PincodeDialog::~PincodeDialog()
100
{
101
    instances--;
102
    pincodeManager.clearDialog(this);
103
}
104
105
void PincodeDialog::action(const ActionEvent &event)
106
{
107
    const std::string &eventId = event.getId();
108
    if (eventId == "pincode")
109
    {
110
        updateButtons();
111
        return;
112
    }
113
    if (eventId == "CANCEL")
114
        setActionEventId("~" + getActionEventId());
115
116
    distributeActionEvent();
117
    close();
118
}
119
120
const std::string &PincodeDialog::getMsg() const
121
{
122
    return mPasswordField->getText();
123
}
124
125
void PincodeDialog::close()
126
{
127
    keyboard.setEnabled(mEnabledKeyboard);
128
    pincodeManager.clearDialog(this);
129
    scheduleDelete();
130
}
131
132
void PincodeDialog::updateButtons()
133
{
134
    if (mPasswordField->getText().size() == 4)
135
    {
136
        mOkButton->setEnabled(true);
137
    }
138
    else
139
    {
140
        mOkButton->setEnabled(false);
141
    }
142

6
}