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