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