]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiPasswordChange.cpp
clientmap, clientmedia: code modernization
[dragonfireclient.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 "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 "gettext.h"
28
29 const int ID_oldPassword = 256;
30 const int ID_newPassword1 = 257;
31 const int ID_newPassword2 = 258;
32 const int ID_change = 259;
33 const int ID_message = 260;
34 const int ID_cancel = 261;
35
36 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
37                 gui::IGUIElement* parent, s32 id,
38                 IMenuManager *menumgr,
39                 Client* client
40 ):
41         GUIModalMenu(env, parent, id, menumgr),
42         m_client(client)
43 {
44 }
45
46 GUIPasswordChange::~GUIPasswordChange()
47 {
48         removeChildren();
49 }
50
51 void GUIPasswordChange::removeChildren()
52 {
53         const core::list<gui::IGUIElement *> &children = getChildren();
54         core::list<gui::IGUIElement *> children_copy;
55         for (core::list<gui::IGUIElement *>::ConstIterator i = children.begin();
56                         i != children.end(); i++) {
57                 children_copy.push_back(*i);
58         }
59         for (core::list<gui::IGUIElement *>::Iterator i = children_copy.begin();
60                         i != children_copy.end(); i++) {
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         core::rect<s32> rect(
80                         screensize.X/2 - 580/2,
81                         screensize.Y/2 - 300/2,
82                         screensize.X/2 + 580/2,
83                         screensize.Y/2 + 300/2
84         );
85
86         DesiredRect = rect;
87         recalculateAbsolutePosition(false);
88
89         v2s32 size = rect.getSize();
90         v2s32 topleft_client(40, 0);
91
92         const wchar_t *text;
93
94         /*
95                 Add stuff
96         */
97         s32 ypos = 50;
98         {
99                 core::rect<s32> rect(0, 0, 150, 20);
100                 rect += topleft_client + v2s32(25, ypos + 6);
101                 text = wgettext("Old Password");
102                 Environment->addStaticText(text, rect, false, true, this, -1);
103                 delete[] text;
104         }
105         {
106                 core::rect<s32> rect(0, 0, 230, 30);
107                 rect += topleft_client + v2s32(160, ypos);
108                 gui::IGUIEditBox *e = Environment->addEditBox(
109                                 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
110                 Environment->setFocus(e);
111                 e->setPasswordBox(true);
112         }
113         ypos += 50;
114         {
115                 core::rect<s32> rect(0, 0, 150, 20);
116                 rect += topleft_client + v2s32(25, ypos + 6);
117                 text = wgettext("New Password");
118                 Environment->addStaticText(text, rect, false, true, this, -1);
119                 delete[] text;
120         }
121         {
122                 core::rect<s32> rect(0, 0, 230, 30);
123                 rect += topleft_client + v2s32(160, ypos);
124                 gui::IGUIEditBox *e = Environment->addEditBox(
125                                 m_newpass.c_str(), rect, true, this, ID_newPassword1);
126                 e->setPasswordBox(true);
127         }
128         ypos += 50;
129         {
130                 core::rect<s32> rect(0, 0, 150, 20);
131                 rect += topleft_client + v2s32(25, ypos + 6);
132                 text = wgettext("Confirm Password");
133                 Environment->addStaticText(text, rect, false, true, this, -1);
134                 delete[] text;
135         }
136         {
137                 core::rect<s32> rect(0, 0, 230, 30);
138                 rect += topleft_client + v2s32(160, ypos);
139                 gui::IGUIEditBox *e = Environment->addEditBox(
140                                 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
141                 e->setPasswordBox(true);
142         }
143
144         ypos += 50;
145         {
146                 core::rect<s32> rect(0, 0, 100, 30);
147                 rect = rect + v2s32(size.X / 4 + 56, ypos);
148                 text = wgettext("Change");
149                 Environment->addButton(rect, this, ID_change, text);
150                 delete[] text;
151         }
152         {
153                 core::rect<s32> rect(0, 0, 100, 30);
154                 rect = rect + v2s32(size.X / 4 + 185, ypos);
155                 text = wgettext("Cancel");
156                 Environment->addButton(rect, this, ID_cancel, text);
157                 delete[] text;
158         }
159
160         ypos += 50;
161         {
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, rect, false, true, this, ID_message);
168                 e->setVisible(false);
169                 delete[] text;
170         }
171 }
172
173 void GUIPasswordChange::drawMenu()
174 {
175         gui::IGUISkin *skin = Environment->getSkin();
176         if (!skin)
177                 return;
178         video::IVideoDriver *driver = Environment->getVideoDriver();
179
180         video::SColor bgcolor(140, 0, 0, 0);
181         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
182
183         gui::IGUIElement::draw();
184 }
185
186 void GUIPasswordChange::acceptInput()
187 {
188         gui::IGUIElement *e;
189         e = getElementFromId(ID_oldPassword);
190         if (e != NULL)
191                 m_oldpass = e->getText();
192         e = getElementFromId(ID_newPassword1);
193         if (e != NULL)
194                 m_newpass = e->getText();
195         e = getElementFromId(ID_newPassword2);
196         if (e != NULL)
197                 m_newpass_confirm = e->getText();
198 }
199
200 bool GUIPasswordChange::processInput()
201 {
202         if (m_newpass != m_newpass_confirm) {
203                 gui::IGUIElement *e = getElementFromId(ID_message);
204                 if (e != NULL)
205                         e->setVisible(true);
206                 return false;
207         }
208         m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
209         return true;
210 }
211
212 bool GUIPasswordChange::OnEvent(const SEvent &event)
213 {
214         if (event.EventType == EET_KEY_INPUT_EVENT) {
215                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
216                         quitMenu();
217                         return true;
218                 }
219                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
220                         acceptInput();
221                         if (processInput())
222                                 quitMenu();
223                         return true;
224                 }
225         }
226         if (event.EventType == EET_GUI_EVENT) {
227                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
228                                 isVisible()) {
229                         if (!canTakeFocus(event.GUIEvent.Element)) {
230                                 dstream << "GUIPasswordChange: Not allowing focus change."
231                                         << std::endl;
232                                 // Returning true disables focus change
233                                 return true;
234                         }
235                 }
236                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
237                         switch (event.GUIEvent.Caller->getID()) {
238                         case ID_change:
239                                 acceptInput();
240                                 if (processInput())
241                                         quitMenu();
242                                 return true;
243                         case ID_cancel:
244                                 quitMenu();
245                                 return true;
246                         }
247                 }
248                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
249                         switch (event.GUIEvent.Caller->getID()) {
250                         case ID_oldPassword:
251                         case ID_newPassword1:
252                         case ID_newPassword2:
253                                 acceptInput();
254                                 if (processInput())
255                                         quitMenu();
256                                 return true;
257                         }
258                 }
259         }
260
261         return Parent ? Parent->OnEvent(event) : false;
262 }