]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiPasswordChange.cpp
Use utf-8 for the Irrlicht clipboard (#11538)
[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 #include "porting.h"
29 #include "gettext.h"
30
31 const int ID_oldPassword = 256;
32 const int ID_newPassword1 = 257;
33 const int ID_newPassword2 = 258;
34 const int ID_change = 259;
35 const int ID_message = 260;
36 const int ID_cancel = 261;
37
38 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
39                 gui::IGUIElement* parent, s32 id,
40                 IMenuManager *menumgr,
41                 Client* client,
42                 ISimpleTextureSource *tsrc
43 ):
44         GUIModalMenu(env, parent, id, menumgr),
45         m_client(client),
46         m_tsrc(tsrc)
47 {
48 }
49
50 GUIPasswordChange::~GUIPasswordChange()
51 {
52         removeChildren();
53 }
54
55 void GUIPasswordChange::removeChildren()
56 {
57         const core::list<gui::IGUIElement *> &children = getChildren();
58         core::list<gui::IGUIElement *> children_copy;
59         for (gui::IGUIElement *i : children) {
60                 children_copy.push_back(i);
61         }
62
63         for (gui::IGUIElement *i : children_copy) {
64                 i->remove();
65         }
66 }
67 void GUIPasswordChange::regenerateGui(v2u32 screensize)
68 {
69         /*
70                 save current input
71         */
72         acceptInput();
73
74         /*
75                 Remove stuff
76         */
77         removeChildren();
78
79         /*
80                 Calculate new sizes and positions
81         */
82 #ifdef __ANDROID__
83         const float s = m_gui_scale * porting::getDisplayDensity() / 2;
84 #else
85         const float s = m_gui_scale;
86 #endif
87         DesiredRect = core::rect<s32>(
88                 screensize.X / 2 - 580 * s / 2,
89                 screensize.Y / 2 - 300 * s / 2,
90                 screensize.X / 2 + 580 * s / 2,
91                 screensize.Y / 2 + 300 * s / 2
92         );
93         recalculateAbsolutePosition(false);
94
95         v2s32 size = DesiredRect.getSize();
96         v2s32 topleft_client(40 * s, 0);
97
98         const wchar_t *text;
99
100         /*
101                 Add stuff
102         */
103         s32 ypos = 50 * s;
104         {
105                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
106                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
107                 text = wgettext("Old Password");
108                 Environment->addStaticText(text, rect, false, true, this, -1);
109                 delete[] text;
110         }
111         {
112                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
113                 rect += topleft_client + v2s32(160 * s, ypos);
114                 gui::IGUIEditBox *e = Environment->addEditBox(
115                                 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
116                 Environment->setFocus(e);
117                 e->setPasswordBox(true);
118         }
119         ypos += 50 * s;
120         {
121                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
122                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
123                 text = wgettext("New Password");
124                 Environment->addStaticText(text, rect, false, true, this, -1);
125                 delete[] text;
126         }
127         {
128                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
129                 rect += topleft_client + v2s32(160 * s, ypos);
130                 gui::IGUIEditBox *e = Environment->addEditBox(
131                                 m_newpass.c_str(), rect, true, this, ID_newPassword1);
132                 e->setPasswordBox(true);
133         }
134         ypos += 50 * s;
135         {
136                 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
137                 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
138                 text = wgettext("Confirm Password");
139                 Environment->addStaticText(text, rect, false, true, this, -1);
140                 delete[] text;
141         }
142         {
143                 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
144                 rect += topleft_client + v2s32(160 * s, ypos);
145                 gui::IGUIEditBox *e = Environment->addEditBox(
146                                 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
147                 e->setPasswordBox(true);
148         }
149
150         ypos += 50 * s;
151         {
152                 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
153                 rect = rect + v2s32(size.X / 4 + 56 * s, ypos);
154                 text = wgettext("Change");
155                 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_change, text);
156                 delete[] text;
157         }
158         {
159                 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
160                 rect = rect + v2s32(size.X / 4 + 185 * s, ypos);
161                 text = wgettext("Cancel");
162                 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_cancel, text);
163                 delete[] text;
164         }
165
166         ypos += 50 * s;
167         {
168                 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
169                 rect += topleft_client + v2s32(35 * s, ypos);
170                 text = wgettext("Passwords do not match!");
171                 IGUIElement *e =
172                         Environment->addStaticText(
173                         text, rect, false, true, this, ID_message);
174                 e->setVisible(false);
175                 delete[] text;
176         }
177 }
178
179 void GUIPasswordChange::drawMenu()
180 {
181         gui::IGUISkin *skin = Environment->getSkin();
182         if (!skin)
183                 return;
184         video::IVideoDriver *driver = Environment->getVideoDriver();
185
186         video::SColor bgcolor(140, 0, 0, 0);
187         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
188
189         gui::IGUIElement::draw();
190 #ifdef __ANDROID__
191         getAndroidUIInput();
192 #endif
193 }
194
195 void GUIPasswordChange::acceptInput()
196 {
197         gui::IGUIElement *e;
198         e = getElementFromId(ID_oldPassword);
199         if (e != NULL)
200                 m_oldpass = e->getText();
201         e = getElementFromId(ID_newPassword1);
202         if (e != NULL)
203                 m_newpass = e->getText();
204         e = getElementFromId(ID_newPassword2);
205         if (e != NULL)
206                 m_newpass_confirm = e->getText();
207 }
208
209 bool GUIPasswordChange::processInput()
210 {
211         if (m_newpass != m_newpass_confirm) {
212                 gui::IGUIElement *e = getElementFromId(ID_message);
213                 if (e != NULL)
214                         e->setVisible(true);
215                 return false;
216         }
217         m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
218         return true;
219 }
220
221 bool GUIPasswordChange::OnEvent(const SEvent &event)
222 {
223         if (event.EventType == EET_KEY_INPUT_EVENT) {
224                 // clang-format off
225                 if ((event.KeyInput.Key == KEY_ESCAPE ||
226                                 event.KeyInput.Key == KEY_CANCEL) &&
227                                 event.KeyInput.PressedDown) {
228                         quitMenu();
229                         return true;
230                 }
231                 // clang-format on
232                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
233                         acceptInput();
234                         if (processInput())
235                                 quitMenu();
236                         return true;
237                 }
238         }
239         if (event.EventType == EET_GUI_EVENT) {
240                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
241                                 isVisible()) {
242                         if (!canTakeFocus(event.GUIEvent.Element)) {
243                                 infostream << "GUIPasswordChange: Not allowing focus change."
244                                         << std::endl;
245                                 // Returning true disables focus change
246                                 return true;
247                         }
248                 }
249                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
250                         switch (event.GUIEvent.Caller->getID()) {
251                         case ID_change:
252                                 acceptInput();
253                                 if (processInput())
254                                         quitMenu();
255                                 return true;
256                         case ID_cancel:
257                                 quitMenu();
258                                 return true;
259                         }
260                 }
261                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
262                         switch (event.GUIEvent.Caller->getID()) {
263                         case ID_oldPassword:
264                         case ID_newPassword1:
265                         case ID_newPassword2:
266                                 acceptInput();
267                                 if (processInput())
268                                         quitMenu();
269                                 return true;
270                         }
271                 }
272         }
273
274         return Parent ? Parent->OnEvent(event) : false;
275 }
276
277 std::string GUIPasswordChange::getNameByID(s32 id)
278 {
279         switch (id) {
280         case ID_oldPassword:
281                 return "old_password";
282         case ID_newPassword1:
283                 return "new_password_1";
284         case ID_newPassword2:
285                 return "new_password_2";
286         }
287         return "";
288 }
289
290 #ifdef __ANDROID__
291 bool GUIPasswordChange::getAndroidUIInput()
292 {
293         if (!hasAndroidUIInput())
294                 return false;
295
296         // still waiting
297         if (porting::getInputDialogState() == -1)
298                 return true;
299
300         gui::IGUIElement *e = nullptr;
301         if (m_jni_field_name == "old_password")
302                 e = getElementFromId(ID_oldPassword);
303         else if (m_jni_field_name == "new_password_1")
304                 e = getElementFromId(ID_newPassword1);
305         else if (m_jni_field_name == "new_password_2")
306                 e = getElementFromId(ID_newPassword2);
307         m_jni_field_name.clear();
308
309         if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
310                 return false;
311
312         std::string text = porting::getInputDialogValue();
313         e->setText(utf8_to_wide(text).c_str());
314         return false;
315 }
316 #endif