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

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