ManaPlus
pincodemanager.cpp
Go to the documentation of this file.
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"
27 
29 
34 
35 #include "net/charserverhandler.h"
36 
37 #include "utils/gettext.h"
38 
39 #include "debug.h"
40 
42 
44  mOldPincode(),
45  mNewPincode(),
46  mSeed(0U),
47  mAccountId(BeingId_zero),
49  mState(PincodeState::None),
50  mLockFlag(false)
51 {
52 }
53 
55 {
56 }
57 
59 {
60  mSeed = 0;
63  mNewPincode.clear();
64 }
65 
67 {
68  if (mDialog == PincodeDialog)
69  mDialog = nullptr;
70 }
71 
73 {
74  switch (mState)
75  {
76  case PincodeState::Ask:
78  // TRANSLATORS: dialog caption
79  _("Pincode"),
80  // TRANSLATORS: dialog label
81  _("Enter pincode"),
82  mSeed,
83  nullptr);
87  break;
89  mNewPincode.clear();
91  // TRANSLATORS: dialog caption
92  _("New pincode"),
93  // TRANSLATORS: dialog label
94  _("Enter new pincode"),
95  mSeed,
96  nullptr);
100  break;
102  mOldPincode.clear();
103  mNewPincode.clear();
105  // TRANSLATORS: dialog caption
106  _("Change pincode"),
107  // TRANSLATORS: dialog label
108  _("Enter old pincode"),
109  mSeed,
110  nullptr);
112  mDialog->setActionEventId("ok");
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;
127  // TRANSLATORS: dialog caption
128  _("New pincode"),
129  // TRANSLATORS: dialog label
130  _("Confirm new pincode"),
131  mSeed,
132  nullptr);
134  mDialog->setActionEventId("ok");
136  }
137  else
138  { // pincode confirmation
139  if (mNewPincode != pincode)
140  {
141  mNewPincode.clear();
143  // TRANSLATORS: error header
144  _("Pincode"),
145  // TRANSLATORS: error message
146  _("Wrong pincode confirmation!"),
147  // TRANSLATORS: ok dialog button
148  _("OK"),
150  Modal_true,
152  nullptr,
153  260);
155  }
156  else
157  {
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;
171  // TRANSLATORS: dialog caption
172  _("Change pincode"),
173  // TRANSLATORS: dialog label
174  _("Enter new pincode"),
175  mSeed,
176  nullptr);
178  mDialog->setActionEventId("ok");
180  }
181  else if (mNewPincode.empty())
182  { // set first new pincode
183  mNewPincode = pincode;
185  // TRANSLATORS: dialog caption
186  _("Change pincode"),
187  // TRANSLATORS: dialog label
188  _("Confirm new pincode"),
189  mSeed,
190  nullptr);
192  mDialog->setActionEventId("ok");
194  }
195  else
196  { // new pincode confirmation
197  if (mNewPincode != pincode)
198  {
199  mOldPincode.clear();
200  mNewPincode.clear();
202  // TRANSLATORS: error header
203  _("Pincode"),
204  // TRANSLATORS: error message
205  _("Wrong pincode confirmation!"),
206  // TRANSLATORS: ok dialog button
207  _("OK"),
209  Modal_true,
211  nullptr,
212  260);
214  }
215  else
216  {
218  mOldPincode,
219  mNewPincode);
220  mOldPincode.clear();
221  mNewPincode.clear();
222  }
223  }
224 }
225 
226 void PincodeManager::sendPincode(const std::string &pincode)
227 {
229  pincode);
230 }
231 
233 {
235  if (client)
236  client->focusWindow();
237 }
238 
240 {
241  // +++ here can be handled locked account by pin code.
242  // but hercules for now not have this feature
244  updateState();
245 }
246 
248 {
250  updateState();
251 }
252 
254 {
255  if (mDialog)
256  {
258  mDialog = nullptr;
259  }
260 }
261 
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)
300  return false;
301  }
302  if (client)
304  return true;
305 }
#define _(s)
Definition: gettext.h:34
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
virtual void changePincode(const BeingId accountId, const std::string &oldPin, const std::string &newPin) const =0
NpcDialog * mDialog
Definition: npcrecv.cpp:41
void setNewPincode(const std::string &pincode)
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Client * client
Definition: client.cpp:117
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:24
virtual void sendCheckPincode(const BeingId accountId, const std::string &pin) const =0
BeingId mAccountId
Window * mDialog
bool processPincodeStatus(const uint16_t state)
virtual void requestFocus()
Definition: widget.cpp:203
virtual void setNewPincode(const BeingId accountId, const std::string &pin) const =0
void changePincode(const std::string &pincode)
#define nullptr
Definition: localconsts.h:44
const bool ShowCenter_true
Definition: showcenter.h:29
void sendPincode(const std::string &pincode)
NewPincodeListener newPincodeListener
virtual void scheduleDelete()
Definition: window.cpp:830
void focusWindow()
Definition: client.cpp:808
NewPincodeRetryListener newPincodeRetryListener
PincodeManager pincodeManager
PincodeListener pincodeListener
void clearDialog(const PincodeDialog *const PincodeDialog)
std::string mOldPincode
void setState(const PincodeStateT state)
std::string mNewPincode
PincodeStateT mState
const BeingId BeingId_zero
Definition: beingid.h:29
ChangePincodeListener changePincodeListener
Net::CharServerHandler * charServerHandler
Definition: net.cpp:82
void updatePinState()
Definition: client.cpp:816