GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/pincodemanager.cpp Lines: 6 88 6.8 %
Date: 2018-05-24 20:11:55 Branches: 2 92 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
2
PincodeManager pincodeManager;
42
43
2
PincodeManager::PincodeManager() :
44
    mOldPincode(),
45
    mNewPincode(),
46
    mSeed(0U),
47
    mAccountId(BeingId_zero),
48
    mDialog(nullptr),
49
6
    mState(PincodeState::None)
50
{
51
}
52
53
6
PincodeManager::~PincodeManager()
54
{
55
2
}
56
57
void PincodeManager::init()
58
{
59
    mSeed = 0;
60
    mAccountId = BeingId_zero;
61
    mState = PincodeState::None;
62
    mNewPincode.clear();
63
}
64
65
void PincodeManager::clearDialog(const PincodeDialog *const PincodeDialog)
66
{
67
    if (mDialog == PincodeDialog)
68
        mDialog = nullptr;
69
}
70
71
void PincodeManager::updateState()
72
{
73
    switch (mState)
74
    {
75
        case PincodeState::Ask:
76
            CREATEWIDGETV(mDialog, PincodeDialog,
77
                // TRANSLATORS: dialog caption
78
                _("Pincode"),
79
                // TRANSLATORS: dialog label
80
                _("Enter pincode"),
81
                mSeed,
82
                nullptr);
83
            mDialog->requestFocus();
84
            mDialog->setActionEventId("ok");
85
            mDialog->addActionListener(&pincodeListener);
86
            break;
87
        case PincodeState::Create:
88
            mNewPincode.clear();
89
            CREATEWIDGETV(mDialog, PincodeDialog,
90
                // TRANSLATORS: dialog caption
91
                _("New pincode"),
92
                // TRANSLATORS: dialog label
93
                _("Enter new pincode"),
94
                mSeed,
95
                nullptr);
96
            mDialog->requestFocus();
97
            mDialog->setActionEventId("ok");
98
            mDialog->addActionListener(&newPincodeListener);
99
            break;
100
        case PincodeState::Change:
101
            mOldPincode.clear();
102
            mNewPincode.clear();
103
            CREATEWIDGETV(mDialog, PincodeDialog,
104
                // TRANSLATORS: dialog caption
105
                _("Change pincode"),
106
                // TRANSLATORS: dialog label
107
                _("Enter old pincode"),
108
                mSeed,
109
                nullptr);
110
            mDialog->requestFocus();
111
            mDialog->setActionEventId("ok");
112
            mDialog->addActionListener(&changePincodeListener);
113
            break;
114
        case PincodeState::None:
115
        default:
116
            break;
117
    }
118
}
119
120
void PincodeManager::setNewPincode(const std::string &pincode)
121
{
122
    if (mNewPincode.empty())
123
    {   // first pincode
124
        mNewPincode = pincode;
125
        CREATEWIDGETV(mDialog, PincodeDialog,
126
            // TRANSLATORS: dialog caption
127
            _("New pincode"),
128
            // TRANSLATORS: dialog label
129
            _("Confirm new pincode"),
130
            mSeed,
131
            nullptr);
132
        mDialog->requestFocus();
133
        mDialog->setActionEventId("ok");
134
        mDialog->addActionListener(&newPincodeListener);
135
    }
136
    else
137
    {   // pincode confirmation
138
        if (mNewPincode != pincode)
139
        {
140
            mNewPincode.clear();
141
            CREATEWIDGETV(mDialog, OkDialog,
142
                // TRANSLATORS: error header
143
                _("Pincode"),
144
                // TRANSLATORS: error message
145
                _("Wrong pincode confirmation!"),
146
                // TRANSLATORS: ok dialog button
147
                _("OK"),
148
                DialogType::ERROR,
149
                Modal_true,
150
                ShowCenter_true,
151
                nullptr,
152
                260);
153
            mDialog->addActionListener(&newPincodeRetryListener);
154
        }
155
        else
156
        {
157
            charServerHandler->setNewPincode(mAccountId,
158
                mNewPincode);
159
            mNewPincode.clear();
160
        }
161
    }
162
}
163
164
void PincodeManager::changePincode(const std::string &pincode)
165
{
166
    if (mOldPincode.empty())
167
    {   // set old pincode
168
        mOldPincode = pincode;
169
        CREATEWIDGETV(mDialog, PincodeDialog,
170
            // TRANSLATORS: dialog caption
171
            _("Change pincode"),
172
            // TRANSLATORS: dialog label
173
            _("Enter new pincode"),
174
            mSeed,
175
            nullptr);
176
        mDialog->requestFocus();
177
        mDialog->setActionEventId("ok");
178
        mDialog->addActionListener(&changePincodeListener);
179
    }
180
    else if (mNewPincode.empty())
181
    {   // set first new pincode
182
        mNewPincode = pincode;
183
        CREATEWIDGETV(mDialog, PincodeDialog,
184
            // TRANSLATORS: dialog caption
185
            _("Change pincode"),
186
            // TRANSLATORS: dialog label
187
            _("Confirm new pincode"),
188
            mSeed,
189
            nullptr);
190
        mDialog->requestFocus();
191
        mDialog->setActionEventId("ok");
192
        mDialog->addActionListener(&changePincodeListener);
193
    }
194
    else
195
    {   // new pincode confirmation
196
        if (mNewPincode != pincode)
197
        {
198
            mOldPincode.clear();
199
            mNewPincode.clear();
200
            CREATEWIDGETV(mDialog, OkDialog,
201
                // TRANSLATORS: error header
202
                _("Pincode"),
203
                // TRANSLATORS: error message
204
                _("Wrong pincode confirmation!"),
205
                // TRANSLATORS: ok dialog button
206
                _("OK"),
207
                DialogType::ERROR,
208
                Modal_true,
209
                ShowCenter_true,
210
                nullptr,
211
                260);
212
            mDialog->addActionListener(&newPincodeRetryListener);
213
        }
214
        else
215
        {
216
            charServerHandler->changePincode(mAccountId,
217
                mOldPincode,
218
                mNewPincode);
219
            mOldPincode.clear();
220
            mNewPincode.clear();
221
        }
222
    }
223
}
224
225
void PincodeManager::sendPincode(const std::string &pincode)
226
{
227
    charServerHandler->sendCheckPincode(mAccountId,
228
        pincode);
229
}
230
231
void PincodeManager::pinOk()
232
{
233
    mState = PincodeState::None;
234
    if (client)
235
        client->focusWindow();
236
}
237
238
void PincodeManager::wrongPin()
239
{
240
    mState = PincodeState::Ask;
241
    updateState();
242
}
243
244
void PincodeManager::closeDialogs()
245
{
246
    if (mDialog)
247
    {
248
        mDialog->scheduleDelete();
249
        mDialog = nullptr;
250
    }
251
}
252
253
bool PincodeManager::isBlocked()
254
{
255
    return mState != PincodeState::None;
256

6
}