ManaPlus
pincodemanager.cpp
Go to the documentation of this file.
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"
28 
30 
35 
36 #include "net/charserverhandler.h"
37 
38 #include "utils/gettext.h"
39 
40 #include "debug.h"
41 
43 
45  mOldPincode(),
46  mNewPincode(),
47  mSeed(0U),
48  mAccountId(BeingId_zero),
50  mState(PincodeState::None),
51  mLockFlag(false)
52 {
53 }
54 
56 {
57 }
58 
60 {
61  mSeed = 0;
64  mNewPincode.clear();
65 }
66 
68 {
69  if (mDialog == PincodeDialog)
70  mDialog = nullptr;
71 }
72 
74 {
75  switch (mState)
76  {
77  case PincodeState::Ask:
79  // TRANSLATORS: dialog caption
80  _("Pincode"),
81  // TRANSLATORS: dialog label
82  _("Enter pincode"),
83  mSeed,
84  nullptr);
88  break;
90  mNewPincode.clear();
92  // TRANSLATORS: dialog caption
93  _("New pincode"),
94  // TRANSLATORS: dialog label
95  _("Enter new pincode"),
96  mSeed,
97  nullptr);
101  break;
103  mOldPincode.clear();
104  mNewPincode.clear();
106  // TRANSLATORS: dialog caption
107  _("Change pincode"),
108  // TRANSLATORS: dialog label
109  _("Enter old pincode"),
110  mSeed,
111  nullptr);
113  mDialog->setActionEventId("ok");
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;
128  // TRANSLATORS: dialog caption
129  _("New pincode"),
130  // TRANSLATORS: dialog label
131  _("Confirm new pincode"),
132  mSeed,
133  nullptr);
135  mDialog->setActionEventId("ok");
137  }
138  else
139  { // pincode confirmation
140  if (mNewPincode != pincode)
141  {
142  mNewPincode.clear();
144  // TRANSLATORS: error header
145  _("Pincode"),
146  // TRANSLATORS: error message
147  _("Wrong pincode confirmation!"),
148  // TRANSLATORS: ok dialog button
149  _("OK"),
151  Modal_true,
153  nullptr,
154  260);
156  }
157  else
158  {
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;
172  // TRANSLATORS: dialog caption
173  _("Change pincode"),
174  // TRANSLATORS: dialog label
175  _("Enter new pincode"),
176  mSeed,
177  nullptr);
179  mDialog->setActionEventId("ok");
181  }
182  else if (mNewPincode.empty())
183  { // set first new pincode
184  mNewPincode = pincode;
186  // TRANSLATORS: dialog caption
187  _("Change pincode"),
188  // TRANSLATORS: dialog label
189  _("Confirm new pincode"),
190  mSeed,
191  nullptr);
193  mDialog->setActionEventId("ok");
195  }
196  else
197  { // new pincode confirmation
198  if (mNewPincode != pincode)
199  {
200  mOldPincode.clear();
201  mNewPincode.clear();
203  // TRANSLATORS: error header
204  _("Pincode"),
205  // TRANSLATORS: error message
206  _("Wrong pincode confirmation!"),
207  // TRANSLATORS: ok dialog button
208  _("OK"),
210  Modal_true,
212  nullptr,
213  260);
215  }
216  else
217  {
219  mOldPincode,
220  mNewPincode);
221  mOldPincode.clear();
222  mNewPincode.clear();
223  }
224  }
225 }
226 
227 void PincodeManager::sendPincode(const std::string &pincode)
228 {
230  pincode);
231 }
232 
234 {
236  if (client)
237  client->focusWindow();
238 }
239 
241 {
242  // +++ here can be handled locked account by pin code.
243  // but hercules for now not have this feature
245  updateState();
246 }
247 
249 {
251  updateState();
252 }
253 
255 {
256  if (mDialog)
257  {
259  mDialog = nullptr;
260  }
261 }
262 
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
274  break;
275  case 1: // ask for pin
277  break;
278  case 2: // create new pin
279  case 4: // create new pin?
280  {
282  break;
283  }
284  case 3: // pin must be changed
286  break;
287  case 8: // pincode was incorrect
288  case 5: // client show error?
289  if (mLockFlag)
291  else
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)
300  return false;
301  }
302  if (client)
304  return true;
305 }
const BeingId BeingId_zero
Definition: beingid.h:30
ChangePincodeListener changePincodeListener
Net::CharServerHandler * charServerHandler
Definition: net.cpp:85
void focusWindow()
Definition: client.cpp:809
void updatePinState()
Definition: client.cpp:817
virtual void setNewPincode(const BeingId accountId, const std::string &pin) const =0
virtual void sendCheckPincode(const BeingId accountId, const std::string &pin) const =0
virtual void changePincode(const BeingId accountId, const std::string &oldPin, const std::string &newPin) const =0
void sendPincode(const std::string &pincode)
void setState(const PincodeStateT state)
std::string mNewPincode
std::string mOldPincode
Window * mDialog
void clearDialog(const PincodeDialog *const PincodeDialog)
PincodeStateT mState
bool processPincodeStatus(const uint16_t state)
void setNewPincode(const std::string &pincode)
void changePincode(const std::string &pincode)
BeingId mAccountId
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
virtual void requestFocus()
Definition: widget.cpp:204
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
virtual void scheduleDelete()
Definition: window.cpp:831
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:25
Client * client
Definition: client.cpp:118
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
const bool Modal_true
Definition: modal.h:30
NpcDialog * mDialog
Definition: npcrecv.cpp:42
NewPincodeListener newPincodeListener
NewPincodeRetryListener newPincodeRetryListener
PincodeListener pincodeListener
PincodeManager pincodeManager
const bool ShowCenter_true
Definition: showcenter.h:30