Include backtrace in error message when LuaErrors occur
[minetest-c55.git] / src / guiPasswordChange.cpp
blobe2f9994be29cf0fe9926dc9350e616e1ea3a22e7
1 /*
2 Part of Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
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.
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.
19 #include "guiPasswordChange.h"
20 #include "debug.h"
21 #include "serialization.h"
22 #include <string>
23 #include <IGUICheckBox.h>
24 #include <IGUIEditBox.h>
25 #include <IGUIButton.h>
26 #include <IGUIStaticText.h>
27 #include <IGUIFont.h>
29 #include "gettext.h"
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;
37 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
38 gui::IGUIElement* parent, s32 id,
39 IMenuManager *menumgr,
40 Client* client
42 GUIModalMenu(env, parent, id, menumgr),
43 m_client(client)
47 GUIPasswordChange::~GUIPasswordChange()
49 removeChildren();
52 void GUIPasswordChange::removeChildren()
55 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
56 if(e != NULL)
57 e->remove();
60 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
61 if(e != NULL)
62 e->remove();
65 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
66 if(e != NULL)
67 e->remove();
70 gui::IGUIElement *e = getElementFromId(ID_change);
71 if(e != NULL)
72 e->remove();
76 void GUIPasswordChange::regenerateGui(v2u32 screensize)
79 Remove stuff
81 removeChildren();
84 Calculate new sizes and positions
86 core::rect<s32> rect(
87 screensize.X/2 - 580/2,
88 screensize.Y/2 - 300/2,
89 screensize.X/2 + 580/2,
90 screensize.Y/2 + 300/2
93 DesiredRect = rect;
94 recalculateAbsolutePosition(false);
96 v2s32 size = rect.getSize();
97 v2s32 topleft_client(40, 0);
99 const wchar_t *text;
102 Add stuff
104 s32 ypos = 50;
106 core::rect<s32> rect(0, 0, 150, 20);
107 rect += topleft_client + v2s32(25, ypos+6);
108 text = wgettext("Old Password");
109 Environment->addStaticText(text, rect, false, true, this, -1);
110 delete[] text;
113 core::rect<s32> rect(0, 0, 230, 30);
114 rect += topleft_client + v2s32(160, ypos);
115 gui::IGUIEditBox *e =
116 Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
117 Environment->setFocus(e);
118 e->setPasswordBox(true);
120 ypos += 50;
122 core::rect<s32> rect(0, 0, 150, 20);
123 rect += topleft_client + v2s32(25, ypos+6);
124 text = wgettext("New Password");
125 Environment->addStaticText(text, rect, false, true, this, -1);
126 delete[] text;
129 core::rect<s32> rect(0, 0, 230, 30);
130 rect += topleft_client + v2s32(160, ypos);
131 gui::IGUIEditBox *e =
132 Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
133 e->setPasswordBox(true);
135 ypos += 50;
137 core::rect<s32> rect(0, 0, 150, 20);
138 rect += topleft_client + v2s32(25, ypos+6);
139 text = wgettext("Confirm Password");
140 Environment->addStaticText(text, rect, false, true, this, -1);
141 delete[] text;
144 core::rect<s32> rect(0, 0, 230, 30);
145 rect += topleft_client + v2s32(160, ypos);
146 gui::IGUIEditBox *e =
147 Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
148 e->setPasswordBox(true);
151 ypos += 50;
153 core::rect<s32> rect(0, 0, 140, 30);
154 rect = rect + v2s32(size.X/2-140/2, ypos);
155 text = wgettext("Change");
156 Environment->addButton(rect, this, ID_change, text);
157 delete[] text;
160 ypos += 50;
162 core::rect<s32> rect(0, 0, 300, 20);
163 rect += topleft_client + v2s32(35, ypos);
164 text = wgettext("Passwords do not match!");
165 IGUIElement *e =
166 Environment->addStaticText(
167 text,
168 rect, false, true, this, ID_message);
169 e->setVisible(false);
170 delete[] text;
174 void GUIPasswordChange::drawMenu()
176 gui::IGUISkin* skin = Environment->getSkin();
177 if (!skin)
178 return;
179 video::IVideoDriver* driver = Environment->getVideoDriver();
181 video::SColor bgcolor(140,0,0,0);
182 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
184 gui::IGUIElement::draw();
187 bool GUIPasswordChange::acceptInput()
189 std::wstring oldpass;
190 std::wstring newpass;
191 gui::IGUIElement *e;
192 e = getElementFromId(ID_oldPassword);
193 if(e != NULL)
194 oldpass = e->getText();
195 e = getElementFromId(ID_newPassword1);
196 if(e != NULL)
197 newpass = e->getText();
198 e = getElementFromId(ID_newPassword2);
199 if(e != NULL && newpass != e->getText())
201 e = getElementFromId(ID_message);
202 if(e != NULL)
203 e->setVisible(true);
204 return false;
206 m_client->sendChangePassword(wide_to_utf8(oldpass),
207 wide_to_utf8(newpass));
208 return true;
211 bool GUIPasswordChange::OnEvent(const SEvent& event)
213 if(event.EventType==EET_KEY_INPUT_EVENT)
215 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
217 quitMenu();
218 return true;
220 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
222 if(acceptInput())
223 quitMenu();
224 return true;
227 if(event.EventType==EET_GUI_EVENT)
229 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
230 && isVisible())
232 if(!canTakeFocus(event.GUIEvent.Element))
234 dstream<<"GUIPasswordChange: Not allowing focus change."
235 <<std::endl;
236 // Returning true disables focus change
237 return true;
240 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
242 switch(event.GUIEvent.Caller->getID())
244 case ID_change:
245 if(acceptInput())
246 quitMenu();
247 return true;
250 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
252 switch(event.GUIEvent.Caller->getID())
254 case ID_oldPassword:
255 case ID_newPassword1:
256 case ID_newPassword2:
257 if(acceptInput())
258 quitMenu();
259 return true;
264 return Parent ? Parent->OnEvent(event) : false;