]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiPasswordChange.cpp
Replace all uses of core::list with std::list (#12313)
[dragonfireclient.git] / src / gui / guiPasswordChange.cpp
1 /*
2 Part of Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "guiPasswordChange.h"
20 #include "client/client.h"
21 #include "guiButton.h"
22 #include <IGUICheckBox.h>
23 #include <IGUIEditBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
26 #include <IGUIFont.h>
27
28 #ifdef HAVE_TOUCHSCREENGUI
29         #include "client/renderingengine.h"
30 #endif
31
32 #include "porting.h"
33 #include "gettext.h"
34
35 const int ID_oldPassword = 256;
36 const int ID_newPassword1 = 257;
37 const int ID_newPassword2 = 258;
38 const int ID_change = 259;
39 const int ID_message = 260;
40 const int ID_cancel = 261;
41
42 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
43                 gui::IGUIElement* parent, s32 id,
44                 IMenuManager *menumgr,
45                 Client* client,
46                 ISimpleTextureSource *tsrc
47 ):
48         GUIModalMenu(env, parent, id, menumgr),
49         m_client(client),
50         m_tsrc(tsrc)
51 {
52 }
53
54 void GUIPasswordChange::regenerateGui(v2u32 screensize)
55 {
56         /*
57                 save current input
58         */
59         acceptInput();
60
61         /*
62                 Remove stuff
63         */
64         removeAllChildren();
65
66         /*
67                 Calculate new sizes and positions
68         */
69 #ifdef HAVE_TOUCHSCREENGUI
70         const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
71 #else
72         const float s = m_gui_scale;
73 #endif
74         DesiredRect = core::rect<s32>(
75                 screensize.X / 2 - 580 * s / 2,
76                 screensize.Y / 2 - 300 * s / 2,
77                 screensize.X / 2 + 580 * s / 2,
78                 screensize.Y / 2 + 300 * s / 2
79         );
80         recalculateAbsolutePosition(false);
81
82         v2s32 size = DesiredRect.getSize();
83         v2s32 topleft_client(40 * s, 0);
84
85         const wchar_t *text;
86
87         /*
88                 Add stuff
89         */
90         s32 ypos = 50 * s;
91         {
92                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
93                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
94                 text = wgettext("Old Password");
95                 Environment->addStaticText(text, rect, false, true, this, -1);
96                 delete[] text;
97         }
98         {
99                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
100                 rect += topleft_client + v2s32(160 * s, ypos);
101                 gui::IGUIEditBox *e = Environment->addEditBox(
102                                 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
103                 Environment->setFocus(e);
104                 e->setPasswordBox(true);
105         }
106         ypos += 50 * s;
107         {
108                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
109                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
110                 text = wgettext("New Password");
111                 Environment->addStaticText(text, rect, false, true, this, -1);
112                 delete[] text;
113         }
114         {
115                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
116                 rect += topleft_client + v2s32(160 * s, ypos);
117                 gui::IGUIEditBox *e = Environment->addEditBox(
118                                 m_newpass.c_str(), rect, true, this, ID_newPassword1);
119                 e->setPasswordBox(true);
120         }
121         ypos += 50 * s;
122         {
123                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
124                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
125                 text = wgettext("Confirm Password");
126                 Environment->addStaticText(text, rect, false, true, this, -1);
127                 delete[] text;
128         }
129         {
130                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
131                 rect += topleft_client + v2s32(160 * s, ypos);
132                 gui::IGUIEditBox *e = Environment->addEditBox(
133                                 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
134                 e->setPasswordBox(true);
135         }
136
137         ypos += 50 * s;
138         {
139                 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
140                 rect = rect + v2s32(size.X / 4 + 56 * s, ypos);
141                 text = wgettext("Change");
142                 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_change, text);
143                 delete[] text;
144         }
145         {
146                 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
147                 rect = rect + v2s32(size.X / 4 + 185 * s, ypos);
148                 text = wgettext("Cancel");
149                 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_cancel, text);
150                 delete[] text;
151         }
152
153         ypos += 50 * s;
154         {
155                 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
156                 rect += topleft_client + v2s32(35 * s, ypos);
157                 text = wgettext("Passwords do not match!");
158                 IGUIElement *e =
159                         Environment->addStaticText(
160                         text, rect, false, true, this, ID_message);
161                 e->setVisible(false);
162                 delete[] text;
163         }
164 }
165
166 void GUIPasswordChange::drawMenu()
167 {
168         gui::IGUISkin *skin = Environment->getSkin();
169         if (!skin)
170                 return;
171         video::IVideoDriver *driver = Environment->getVideoDriver();
172
173         video::SColor bgcolor(140, 0, 0, 0);
174         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
175
176         gui::IGUIElement::draw();
177 #ifdef __ANDROID__
178         getAndroidUIInput();
179 #endif
180 }
181
182 void GUIPasswordChange::acceptInput()
183 {
184         gui::IGUIElement *e;
185         e = getElementFromId(ID_oldPassword);
186         if (e != NULL)
187                 m_oldpass = e->getText();
188         e = getElementFromId(ID_newPassword1);
189         if (e != NULL)
190                 m_newpass = e->getText();
191         e = getElementFromId(ID_newPassword2);
192         if (e != NULL)
193                 m_newpass_confirm = e->getText();
194 }
195
196 bool GUIPasswordChange::processInput()
197 {
198         if (m_newpass != m_newpass_confirm) {
199                 gui::IGUIElement *e = getElementFromId(ID_message);
200                 if (e != NULL)
201                         e->setVisible(true);
202                 return false;
203         }
204         m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
205         return true;
206 }
207
208 bool GUIPasswordChange::OnEvent(const SEvent &event)
209 {
210         if (event.EventType == EET_KEY_INPUT_EVENT) {
211                 // clang-format off
212                 if ((event.KeyInput.Key == KEY_ESCAPE ||
213                                 event.KeyInput.Key == KEY_CANCEL) &&
214                                 event.KeyInput.PressedDown) {
215                         quitMenu();
216                         return true;
217                 }
218                 // clang-format on
219                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
220                         acceptInput();
221                         if (processInput())
222                                 quitMenu();
223                         return true;
224                 }
225         }
226         if (event.EventType == EET_GUI_EVENT) {
227                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
228                                 isVisible()) {
229                         if (!canTakeFocus(event.GUIEvent.Element)) {
230                                 infostream << "GUIPasswordChange: Not allowing focus change."
231                                         << std::endl;
232                                 // Returning true disables focus change
233                                 return true;
234                         }
235                 }
236                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
237                         switch (event.GUIEvent.Caller->getID()) {
238                         case ID_change:
239                                 acceptInput();
240                                 if (processInput())
241                                         quitMenu();
242                                 return true;
243                         case ID_cancel:
244                                 quitMenu();
245                                 return true;
246                         }
247                 }
248                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
249                         switch (event.GUIEvent.Caller->getID()) {
250                         case ID_oldPassword:
251                         case ID_newPassword1:
252                         case ID_newPassword2:
253                                 acceptInput();
254                                 if (processInput())
255                                         quitMenu();
256                                 return true;
257                         }
258                 }
259         }
260
261         return Parent ? Parent->OnEvent(event) : false;
262 }
263
264 std::string GUIPasswordChange::getNameByID(s32 id)
265 {
266         switch (id) {
267         case ID_oldPassword:
268                 return "old_password";
269         case ID_newPassword1:
270                 return "new_password_1";
271         case ID_newPassword2:
272                 return "new_password_2";
273         }
274         return "";
275 }
276
277 #ifdef __ANDROID__
278 bool GUIPasswordChange::getAndroidUIInput()
279 {
280         if (!hasAndroidUIInput())
281                 return false;
282
283         // still waiting
284         if (porting::getInputDialogState() == -1)
285                 return true;
286
287         gui::IGUIElement *e = nullptr;
288         if (m_jni_field_name == "old_password")
289                 e = getElementFromId(ID_oldPassword);
290         else if (m_jni_field_name == "new_password_1")
291                 e = getElementFromId(ID_newPassword1);
292         else if (m_jni_field_name == "new_password_2")
293                 e = getElementFromId(ID_newPassword2);
294         m_jni_field_name.clear();
295
296         if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
297                 return false;
298
299         std::string text = porting::getInputDialogValue();
300         e->setText(utf8_to_wide(text).c_str());
301         return false;
302 }
303 #endif