]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiPasswordChange.cpp
Merge branch 'master' of https://github.com/erlehmann/minetest-delta.git into upstrea...
[dragonfireclient.git] / src / guiPasswordChange.cpp
1 /*
2 Part of Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 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
24 const int ID_oldPassword = 256;
25 const int ID_newPassword1 = 257;
26 const int ID_newPassword2 = 258;
27 const int ID_change = 259;
28 const int ID_message = 260;
29
30 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
31                 gui::IGUIElement* parent, s32 id,
32                 IMenuManager *menumgr,
33                 Client* client
34 ):
35         GUIModalMenu(env, parent, id, menumgr),
36         m_client(client)
37 {
38 }
39
40 GUIPasswordChange::~GUIPasswordChange()
41 {
42         removeChildren();
43 }
44
45 void GUIPasswordChange::removeChildren()
46 {
47         {
48                 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
49                 if(e != NULL)
50                         e->remove();
51         }
52         {
53                 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
54                 if(e != NULL)
55                         e->remove();
56         }
57         {
58                 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
59                 if(e != NULL)
60                         e->remove();
61         }
62         {
63                 gui::IGUIElement *e = getElementFromId(ID_change);
64                 if(e != NULL)
65                         e->remove();
66         }
67 }
68
69 void GUIPasswordChange::regenerateGui(v2u32 screensize)
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         v2s32 size_client = size - v2s32(40, 0);
92
93         /*
94                 Add stuff
95         */
96         s32 ypos = 50;
97         {
98                 core::rect<s32> rect(0, 0, 110, 20);
99                 rect += topleft_client + v2s32(35, ypos+6);
100                 const wchar_t *text = L"Old Password";
101                 Environment->addStaticText(text, rect, false, true, this, -1);
102         }
103         {
104                 core::rect<s32> rect(0, 0, 230, 30);
105                 rect += topleft_client + v2s32(160, ypos);
106                 gui::IGUIEditBox *e = 
107                 Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
108                 Environment->setFocus(e);
109                 e->setPasswordBox(true);
110         }
111         ypos += 50;
112         {
113                 core::rect<s32> rect(0, 0, 110, 20);
114                 rect += topleft_client + v2s32(35, ypos+6);
115                 const wchar_t *text = L"New Password";
116                 Environment->addStaticText(text, rect, false, true, this, -1);
117         }
118         {
119                 core::rect<s32> rect(0, 0, 230, 30);
120                 rect += topleft_client + v2s32(160, ypos);
121                 gui::IGUIEditBox *e = 
122                 Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
123                 e->setPasswordBox(true);
124         }
125         ypos += 50;
126         {
127                 core::rect<s32> rect(0, 0, 110, 20);
128                 rect += topleft_client + v2s32(35, ypos+6);
129                 const wchar_t *text = L"Confirm Password";
130                 Environment->addStaticText(text, rect, false, true, this, -1);
131         }
132         {
133                 core::rect<s32> rect(0, 0, 230, 30);
134                 rect += topleft_client + v2s32(160, ypos);
135                 gui::IGUIEditBox *e = 
136                 Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
137                 e->setPasswordBox(true);
138         }
139
140         ypos += 50;
141         {
142                 core::rect<s32> rect(0, 0, 140, 30);
143                 rect = rect + v2s32(size.X/2-140/2, ypos);
144                 Environment->addButton(rect, this, ID_change, L"Change");
145         }
146
147         ypos += 50;
148         {
149                 core::rect<s32> rect(0, 0, 300, 20);
150                 rect += topleft_client + v2s32(35, ypos);
151                 const wchar_t *text = L"Passwords do not match!";
152                 IGUIElement *e = 
153                 Environment->addStaticText(text, rect, false, true, this, ID_message);
154                 e->setVisible(false);
155         }
156
157 }
158
159 void GUIPasswordChange::drawMenu()
160 {
161         gui::IGUISkin* skin = Environment->getSkin();
162         if (!skin)
163                 return;
164         video::IVideoDriver* driver = Environment->getVideoDriver();
165         
166         video::SColor bgcolor(140,0,0,0);
167         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
168
169         gui::IGUIElement::draw();
170 }
171
172 bool GUIPasswordChange::acceptInput()
173 {
174                 std::wstring oldpass;
175                 std::wstring newpass;
176                 gui::IGUIElement *e;
177                 e = getElementFromId(ID_oldPassword);
178                 if(e != NULL)
179                         oldpass = e->getText();
180                 e = getElementFromId(ID_newPassword1);
181                 if(e != NULL)
182                         newpass = e->getText();
183                 e = getElementFromId(ID_newPassword2);
184                 if(e != NULL && newpass != e->getText())
185                 {
186                         e = getElementFromId(ID_message);
187                         if(e != NULL)
188                                 e->setVisible(true);
189                         return false;
190                 }
191                 m_client->sendChangePassword(oldpass, newpass);
192                 return true;
193 }
194
195 bool GUIPasswordChange::OnEvent(const SEvent& event)
196 {
197         if(event.EventType==EET_KEY_INPUT_EVENT)
198         {
199                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
200                 {
201                         quitMenu();
202                         return true;
203                 }
204                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
205                 {
206                         if(acceptInput())
207                                 quitMenu();
208                         return true;
209                 }
210         }
211         if(event.EventType==EET_GUI_EVENT)
212         {
213                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
214                                 && isVisible())
215                 {
216                         if(!canTakeFocus(event.GUIEvent.Element))
217                         {
218                                 dstream<<"GUIPasswordChange: Not allowing focus change."
219                                                 <<std::endl;
220                                 // Returning true disables focus change
221                                 return true;
222                         }
223                 }
224                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
225                 {
226                         switch(event.GUIEvent.Caller->getID())
227                         {
228                         case ID_change:
229                                 if(acceptInput())
230                                         quitMenu();
231                                 return true;
232                         }
233                 }
234                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
235                 {
236                         switch(event.GUIEvent.Caller->getID())
237                         {
238                         case ID_oldPassword:
239                         case ID_newPassword1:
240                         case ID_newPassword2:
241                                 if(acceptInput())
242                                         quitMenu();
243                                 return true;
244                         }
245                 }
246         }
247
248         return Parent ? Parent->OnEvent(event) : false;
249 }
250