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