~aleteoryx/muditaos

ref: d5de12f7cea071555de4058ebf62b160a86a36af muditaos/module-apps/application-desktop/widgets/PinLock.cpp -rw-r--r-- 2.1 KiB
d5de12f7 — Radosław Wicik [EGD-3852] clean include in service (#928) 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PinLock.hpp"
#include "PinLockHandler.hpp"

namespace gui
{
    PinLock::PinLock(gui::PinLockHandler *handler) : handler(handler)
    {}

    void PinLock::consumeInvalidPinState() noexcept
    {
        if (state == State::InvalidPin) {
            state = State::EnterPin;
        }
    }

    void PinLock::putNextChar(unsigned int c) noexcept
    {
        if (state == State::EnterPin && charCount < pinValue.size()) {
            pinValue[charCount++] = c;
        }
    }

    void PinLock::verifyPin() noexcept
    {
        if (charCount == pinValue.size()) {
            handler->handle(pinValue);
            clearAttempt();
        }
    }

    void PinLock::popChar() noexcept
    {
        if (state == State::EnterPin && charCount > 0) {
            charCount--;
            pinValue[charCount] = 0;
        }
    }

    void PinLock::clearAttempt() noexcept
    {
        for (auto &c : pinValue) {
            c = 0;
        }
        charCount = 0;
    }

    bool PinLock::unlock() noexcept
    {
        if (state == State::VerifiedPin || pinValue.size() == 0) {
            state = State::Unlocked;
            return true;
        }
        return false;
    }

    void PinLock::lock() noexcept
    {
        if (state == State::Unlocked) {
            state = State::EnterPin;
        }
    }

    bool PinLock::isLocked() const noexcept
    {
        return state != State::Unlocked;
    }

    std::string PinLock::getLockInfo(const InfoName name) const
    {
        try {
            return additionalLockInfo.at(name);
        }
        catch (std::out_of_range &) {
            return std::string{};
        }
    }

    void PinLock::reset(LockType newType, State newState, unsigned int attempts, unsigned int size) noexcept
    {
        type              = newType;
        state             = newState;
        remainingAttempts = attempts;
        pinValue          = std::vector<unsigned int>(size, 0);
    }
} // namespace gui