GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/pincodemanager.cpp Lines: 6 107 5.6 %
Date: 2021-03-17 Branches: 2 91 2.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2018-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 "pincodemanager.h"
23
24
#include "client.h"
25
26
#include "gui/windows/okdialog.h"
27
#include "gui/windows/pincodedialog.h"
28
29
#include "gui/widgets/createwidget.h"
30
31
#include "listeners/changepincodelistener.h"
32
#include "listeners/newpincodelistener.h"
33
#include "listeners/newpincoderetrylistener.h"
34
#include "listeners/pincodelistener.h"
35
36
#include "net/charserverhandler.h"
37
38
#include "utils/gettext.h"
39
40
#include "debug.h"
41
42
1
PincodeManager pincodeManager;
43
44
1
PincodeManager::PincodeManager() :
45
    mOldPincode(),
46
    mNewPincode(),
47
    mSeed(0U),
48
    mAccountId(BeingId_zero),
49
    mDialog(nullptr),
50
    mState(PincodeState::None),
51
3
    mLockFlag(false)
52
{
53
}
54
55
3
PincodeManager::~PincodeManager()
56
{
57
1
}
58
59
void PincodeManager::init()
60
{
61
    mSeed = 0;
62
    mAccountId = BeingId_zero;
63
    mState = PincodeState::None;
64
    mNewPincode.clear();
65
}
66
67
void PincodeManager::clearDialog(const PincodeDialog *const PincodeDialog)
68
{
69
    if (mDialog == PincodeDialog)
70
        mDialog = nullptr;
71
}
72
73
void PincodeManager::updateState()
74
{
75
    switch (mState)
76
    {
77
        case PincodeState::Ask:
78
            CREATEWIDGETV(mDialog, PincodeDialog,
79
                // TRANSLATORS: dialog caption
80
                _("Pincode"),
81
                // TRANSLATORS: dialog label
82
                _("Enter pincode"),
83
                mSeed,
84
                nullptr);
85
            mDialog->requestFocus();
86
            mDialog->setActionEventId("ok");
87
            mDialog->addActionListener(&pincodeListener);
88
            break;
89
        case PincodeState::Create:
90
            mNewPincode.clear();
91
            CREATEWIDGETV(mDialog, PincodeDialog,
92
                // TRANSLATORS: dialog caption
93
                _("New pincode"),
94
                // TRANSLATORS: dialog label
95
                _("Enter new pincode"),
96
                mSeed,
97
                nullptr);
98
            mDialog->requestFocus();
99
            mDialog->setActionEventId("ok");
100
            mDialog->addActionListener(&newPincodeListener);
101
            break;
102
        case PincodeState::Change:
103
            mOldPincode.clear();
104
            mNewPincode.clear();
105
            CREATEWIDGETV(mDialog, PincodeDialog,
106
                // TRANSLATORS: dialog caption
107
                _("Change pincode"),
108
                // TRANSLATORS: dialog label
109
                _("Enter old pincode"),
110
                mSeed,
111
                nullptr);
112
            mDialog->requestFocus();
113
            mDialog->setActionEventId("ok");
114
            mDialog->addActionListener(&changePincodeListener);
115
            break;
116
        case PincodeState::None:
117
        default:
118
            break;
119
    }
120
}
121
122
void PincodeManager::setNewPincode(const std::string &pincode)
123
{
124
    if (mNewPincode.empty())
125
    {   // first pincode
126
        mNewPincode = pincode;
127
        CREATEWIDGETV(mDialog, PincodeDialog,
128
            // TRANSLATORS: dialog caption
129
            _("New pincode"),
130
            // TRANSLATORS: dialog label
131
            _("Confirm new pincode"),
132
            mSeed,
133
            nullptr);
134
        mDialog->requestFocus();
135
        mDialog->setActionEventId("ok");
136
        mDialog->addActionListener(&newPincodeListener);
137
    }
138
    else
139
    {   // pincode confirmation
140
        if (mNewPincode != pincode)
141
        {
142
            mNewPincode.clear();
143
            CREATEWIDGETV(mDialog, OkDialog,
144
                // TRANSLATORS: error header
145
                _("Pincode"),
146
                // TRANSLATORS: error message
147
                _("Wrong pincode confirmation!"),
148
                // TRANSLATORS: ok dialog button
149
                _("OK"),
150
                DialogType::ERROR,
151
                Modal_true,
152
                ShowCenter_true,
153
                nullptr,
154
                260);
155
            mDialog->addActionListener(&newPincodeRetryListener);
156
        }
157
        else
158
        {
159
            charServerHandler->setNewPincode(mAccountId,
160
                mNewPincode);
161
            mNewPincode.clear();
162
        }
163
    }
164
}
165
166
void PincodeManager::changePincode(const std::string &pincode)
167
{
168
    if (mOldPincode.empty())
169
    {   // set old pincode
170
        mOldPincode = pincode;
171
        CREATEWIDGETV(mDialog, PincodeDialog,
172
            // TRANSLATORS: dialog caption
173
            _("Change pincode"),
174
            // TRANSLATORS: dialog label
175
            _("Enter new pincode"),
176
            mSeed,
177
            nullptr);
178
        mDialog->requestFocus();
179
        mDialog->setActionEventId("ok");
180
        mDialog->addActionListener(&changePincodeListener);
181
    }
182
    else if (mNewPincode.empty())
183
    {   // set first new pincode
184
        mNewPincode = pincode;
185
        CREATEWIDGETV(mDialog, PincodeDialog,
186
            // TRANSLATORS: dialog caption
187
            _("Change pincode"),
188
            // TRANSLATORS: dialog label
189
            _("Confirm new pincode"),
190
            mSeed,
191
            nullptr);
192
        mDialog->requestFocus();
193
        mDialog->setActionEventId("ok");
194
        mDialog->addActionListener(&changePincodeListener);
195
    }
196
    else
197
    {   // new pincode confirmation
198
        if (mNewPincode != pincode)
199
        {
200
            mOldPincode.clear();
201
            mNewPincode.clear();
202
            CREATEWIDGETV(mDialog, OkDialog,
203
                // TRANSLATORS: error header
204
                _("Pincode"),
205
                // TRANSLATORS: error message
206
                _("Wrong pincode confirmation!"),
207
                // TRANSLATORS: ok dialog button
208
                _("OK"),
209
                DialogType::ERROR,
210
                Modal_true,
211
                ShowCenter_true,
212
                nullptr,
213
                260);
214
            mDialog->addActionListener(&newPincodeRetryListener);
215
        }
216
        else
217
        {
218
            charServerHandler->changePincode(mAccountId,
219
                mOldPincode,
220
                mNewPincode);
221
            mOldPincode.clear();
222
            mNewPincode.clear();
223
        }
224
    }
225
}
226
227
void PincodeManager::sendPincode(const std::string &pincode)
228
{
229
    charServerHandler->sendCheckPincode(mAccountId,
230
        pincode);
231
}
232
233
void PincodeManager::pinOk()
234
{
235
    mState = PincodeState::None;
236
    if (client)
237
        client->focusWindow();
238
}
239
240
void PincodeManager::lockedPin()
241
{
242
    // +++ here can be handled locked account by pin code.
243
    // but hercules for now not have this feature
244
    mState = PincodeState::Ask;
245
    updateState();
246
}
247
248
void PincodeManager::wrongPin()
249
{
250
    mState = PincodeState::Ask;
251
    updateState();
252
}
253
254
void PincodeManager::closeDialogs()
255
{
256
    if (mDialog)
257
    {
258
        mDialog->scheduleDelete();
259
        mDialog = nullptr;
260
    }
261
}
262
263
bool PincodeManager::isBlocked()
264
{
265
    return mState != PincodeState::None;
266
}
267
268
bool PincodeManager::processPincodeStatus(const uint16_t state)
269
{
270
    switch (state)
271
    {
272
        case 0:  // pin ok
273
            pincodeManager.pinOk();
274
            break;
275
        case 1:  // ask for pin
276
            pincodeManager.setState(PincodeState::Ask);
277
            break;
278
        case 2:  // create new pin
279
        case 4:  // create new pin?
280
        {
281
            pincodeManager.setState(PincodeState::Create);
282
            break;
283
        }
284
        case 3:  // pin must be changed
285
            pincodeManager.setState(PincodeState::Change);
286
            break;
287
        case 8:  // pincode was incorrect
288
        case 5:  // client show error?
289
            if (mLockFlag)
290
                pincodeManager.lockedPin();
291
            else
292
                pincodeManager.wrongPin();
293
            return true;
294
        case 6:  // Unable to use your KSSN number
295
        case 7:  // char select window shows a button
296
            break;
297
        default:
298
            if (client)
299
                client->updatePinState();
300
            return false;
301
    }
302
    if (client)
303
        client->updatePinState();
304
    return true;
305

3
}