c8a2214b6e2ea1ddc76ce5e93df1bf657e5f472c
[oweals/minetest.git] / src / 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 "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>
28
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
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         {
55                 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
56                 if(e != NULL)
57                         e->remove();
58         }
59         {
60                 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
61                 if(e != NULL)
62                         e->remove();
63         }
64         {
65                 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
66                 if(e != NULL)
67                         e->remove();
68         }
69         {
70                 gui::IGUIElement *e = getElementFromId(ID_change);
71                 if(e != NULL)
72                         e->remove();
73         }
74 }
75
76 void GUIPasswordChange::regenerateGui(v2u32 screensize)
77 {
78         /*
79                 Remove stuff
80         */
81         removeChildren();
82         
83         /*
84                 Calculate new sizes and positions
85         */
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
91         );
92         
93         DesiredRect = rect;
94         recalculateAbsolutePosition(false);
95
96         v2s32 size = rect.getSize();
97         v2s32 topleft_client(40, 0);
98
99         /*
100                 Add stuff
101         */
102         s32 ypos = 50;
103         {
104                 core::rect<s32> rect(0, 0, 110, 20);
105                 rect += topleft_client + v2s32(35, ypos+6);
106                 wchar_t* text = wgettext("Old Password");
107                 Environment->addStaticText(text, rect, false, true, this, -1);
108                 delete[] text;
109         }
110         {
111                 core::rect<s32> rect(0, 0, 230, 30);
112                 rect += topleft_client + v2s32(160, ypos);
113                 gui::IGUIEditBox *e = 
114                 Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
115                 Environment->setFocus(e);
116                 e->setPasswordBox(true);
117         }
118         ypos += 50;
119         {
120                 core::rect<s32> rect(0, 0, 110, 20);
121                 rect += topleft_client + v2s32(35, ypos+6);
122                 wchar_t* text = wgettext("New Password");
123                 Environment->addStaticText(text, rect, false, true, this, -1);
124                 delete[] text;
125         }
126         {
127                 core::rect<s32> rect(0, 0, 230, 30);
128                 rect += topleft_client + v2s32(160, ypos);
129                 gui::IGUIEditBox *e = 
130                 Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
131                 e->setPasswordBox(true);
132         }
133         ypos += 50;
134         {
135                 core::rect<s32> rect(0, 0, 110, 20);
136                 rect += topleft_client + v2s32(35, ypos+6);
137                 wchar_t* text = wgettext("Confirm Password");
138                 Environment->addStaticText(text, rect, false, true, this, -1);
139                 delete[] text;
140         }
141         {
142                 core::rect<s32> rect(0, 0, 230, 30);
143                 rect += topleft_client + v2s32(160, ypos);
144                 gui::IGUIEditBox *e = 
145                 Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
146                 e->setPasswordBox(true);
147         }
148
149         ypos += 50;
150         {
151                 core::rect<s32> rect(0, 0, 140, 30);
152                 rect = rect + v2s32(size.X/2-140/2, ypos);
153                 wchar_t* text = wgettext("Change");
154                 Environment->addButton(rect, this, ID_change, text);
155                 delete[] text;
156         }
157
158         ypos += 50;
159         {
160                 core::rect<s32> rect(0, 0, 300, 20);
161                 rect += topleft_client + v2s32(35, ypos);
162                 wchar_t* text = wgettext("Passwords do not match!");
163                 IGUIElement *e = 
164                 Environment->addStaticText(
165                         text,
166                         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 }
184
185 bool GUIPasswordChange::acceptInput()
186 {
187                 std::wstring oldpass;
188                 std::wstring newpass;
189                 gui::IGUIElement *e;
190                 e = getElementFromId(ID_oldPassword);
191                 if(e != NULL)
192                         oldpass = e->getText();
193                 e = getElementFromId(ID_newPassword1);
194                 if(e != NULL)
195                         newpass = e->getText();
196                 e = getElementFromId(ID_newPassword2);
197                 if(e != NULL && newpass != e->getText())
198                 {
199                         e = getElementFromId(ID_message);
200                         if(e != NULL)
201                                 e->setVisible(true);
202                         return false;
203                 }
204                 m_client->sendChangePassword(oldpass, newpass);
205                 return true;
206 }
207
208 bool GUIPasswordChange::OnEvent(const SEvent& event)
209 {
210         if(event.EventType==EET_KEY_INPUT_EVENT)
211         {
212                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
213                 {
214                         quitMenu();
215                         return true;
216                 }
217                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
218                 {
219                         if(acceptInput())
220                                 quitMenu();
221                         return true;
222                 }
223         }
224         if(event.EventType==EET_GUI_EVENT)
225         {
226                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
227                                 && isVisible())
228                 {
229                         if(!canTakeFocus(event.GUIEvent.Element))
230                         {
231                                 dstream<<"GUIPasswordChange: Not allowing focus change."
232                                                 <<std::endl;
233                                 // Returning true disables focus change
234                                 return true;
235                         }
236                 }
237                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
238                 {
239                         switch(event.GUIEvent.Caller->getID())
240                         {
241                         case ID_change:
242                                 if(acceptInput())
243                                         quitMenu();
244                                 return true;
245                         }
246                 }
247                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
248                 {
249                         switch(event.GUIEvent.Caller->getID())
250                         {
251                         case ID_oldPassword:
252                         case ID_newPassword1:
253                         case ID_newPassword2:
254                                 if(acceptInput())
255                                         quitMenu();
256                                 return true;
257                         }
258                 }
259         }
260
261         return Parent ? Parent->OnEvent(event) : false;
262 }
263