Cleanup in content_mapblock (#5746)
[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 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         m_oldpass(L""),
46         m_newpass(L""),
47         m_newpass_confirm(L"")
48 {
49 }
50
51 GUIPasswordChange::~GUIPasswordChange()
52 {
53         removeChildren();
54 }
55
56 void GUIPasswordChange::removeChildren()
57 {
58         const core::list<gui::IGUIElement *> &children = getChildren();
59         core::list<gui::IGUIElement *> children_copy;
60         for (core::list<gui::IGUIElement *>::ConstIterator i = children.begin();
61                         i != children.end(); i++) {
62                 children_copy.push_back(*i);
63         }
64         for (core::list<gui::IGUIElement *>::Iterator i = children_copy.begin();
65                         i != children_copy.end(); i++) {
66                 (*i)->remove();
67         }
68 }
69 void GUIPasswordChange::regenerateGui(v2u32 screensize)
70 {
71         /*
72                 save current input
73         */
74         acceptInput();
75
76         /*
77                 Remove stuff
78         */
79         removeChildren();
80
81         /*
82                 Calculate new sizes and positions
83         */
84         core::rect<s32> rect(
85                         screensize.X/2 - 580/2,
86                         screensize.Y/2 - 300/2,
87                         screensize.X/2 + 580/2,
88                         screensize.Y/2 + 300/2
89         );
90
91         DesiredRect = rect;
92         recalculateAbsolutePosition(false);
93
94         v2s32 size = rect.getSize();
95         v2s32 topleft_client(40, 0);
96
97         const wchar_t *text;
98
99         /*
100                 Add stuff
101         */
102         s32 ypos = 50;
103         {
104                 core::rect<s32> rect(0, 0, 150, 20);
105                 rect += topleft_client + v2s32(25, ypos + 6);
106                 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 = Environment->addEditBox(
114                                 m_oldpass.c_str(), 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, 150, 20);
121                 rect += topleft_client + v2s32(25, ypos + 6);
122                 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 = Environment->addEditBox(
130                                 m_newpass.c_str(), rect, true, this, ID_newPassword1);
131                 e->setPasswordBox(true);
132         }
133         ypos += 50;
134         {
135                 core::rect<s32> rect(0, 0, 150, 20);
136                 rect += topleft_client + v2s32(25, ypos + 6);
137                 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 = Environment->addEditBox(
145                                 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
146                 e->setPasswordBox(true);
147         }
148
149         ypos += 50;
150         {
151                 core::rect<s32> rect(0, 0, 100, 30);
152                 rect = rect + v2s32(size.X / 4 + 56, ypos);
153                 text = wgettext("Change");
154                 Environment->addButton(rect, this, ID_change, text);
155                 delete[] text;
156         }
157         {
158                 core::rect<s32> rect(0, 0, 100, 30);
159                 rect = rect + v2s32(size.X / 4 + 185, ypos);
160                 text = wgettext("Cancel");
161                 Environment->addButton(rect, this, ID_cancel, text);
162                 delete[] text;
163         }
164
165         ypos += 50;
166         {
167                 core::rect<s32> rect(0, 0, 300, 20);
168                 rect += topleft_client + v2s32(35, ypos);
169                 text = wgettext("Passwords do not match!");
170                 IGUIElement *e =
171                         Environment->addStaticText(
172                         text, rect, false, true, this, ID_message);
173                 e->setVisible(false);
174                 delete[] text;
175         }
176 }
177
178 void GUIPasswordChange::drawMenu()
179 {
180         gui::IGUISkin *skin = Environment->getSkin();
181         if (!skin)
182                 return;
183         video::IVideoDriver *driver = Environment->getVideoDriver();
184
185         video::SColor bgcolor(140, 0, 0, 0);
186         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
187
188         gui::IGUIElement::draw();
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                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
221                         quitMenu();
222                         return true;
223                 }
224                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
225                         acceptInput();
226                         if (processInput())
227                                 quitMenu();
228                         return true;
229                 }
230         }
231         if (event.EventType == EET_GUI_EVENT) {
232                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
233                                 isVisible()) {
234                         if (!canTakeFocus(event.GUIEvent.Element)) {
235                                 dstream << "GUIPasswordChange: Not allowing focus change."
236                                         << std::endl;
237                                 // Returning true disables focus change
238                                 return true;
239                         }
240                 }
241                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
242                         switch (event.GUIEvent.Caller->getID()) {
243                         case ID_change:
244                                 acceptInput();
245                                 if (processInput())
246                                         quitMenu();
247                                 return true;
248                         case ID_cancel:
249                                 quitMenu();
250                                 return true;
251                         }
252                 }
253                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
254                         switch (event.GUIEvent.Caller->getID()) {
255                         case ID_oldPassword:
256                         case ID_newPassword1:
257                         case ID_newPassword2:
258                                 acceptInput();
259                                 if (processInput())
260                                         quitMenu();
261                                 return true;
262                         }
263                 }
264         }
265
266         return Parent ? Parent->OnEvent(event) : false;
267 }