Translation to Portuguese of Brazil for Minetest
[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         v2s32 size_client = size - v2s32(40, 0);
99
100         /*
101                 Add stuff
102         */
103         s32 ypos = 50;
104         changeCtype("");
105         {
106                 core::rect<s32> rect(0, 0, 110, 20);
107                 rect += topleft_client + v2s32(35, ypos+6);
108                 wchar_t* text = wgettext("Old Password");
109                 Environment->addStaticText(text, rect, false, true, this, -1);
110                 delete[] text;
111         }
112         changeCtype("C");
113         {
114                 core::rect<s32> rect(0, 0, 230, 30);
115                 rect += topleft_client + v2s32(160, ypos);
116                 gui::IGUIEditBox *e = 
117                 Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
118                 Environment->setFocus(e);
119                 e->setPasswordBox(true);
120         }
121         ypos += 50;
122         changeCtype("");
123         {
124                 core::rect<s32> rect(0, 0, 110, 20);
125                 rect += topleft_client + v2s32(35, ypos+6);
126                 wchar_t* text = wgettext("New Password");
127                 Environment->addStaticText(text, rect, false, true, this, -1);
128                 delete[] text;
129         }
130         changeCtype("C");
131         {
132                 core::rect<s32> rect(0, 0, 230, 30);
133                 rect += topleft_client + v2s32(160, ypos);
134                 gui::IGUIEditBox *e = 
135                 Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
136                 e->setPasswordBox(true);
137         }
138         ypos += 50;
139         changeCtype("");
140         {
141                 core::rect<s32> rect(0, 0, 110, 20);
142                 rect += topleft_client + v2s32(35, ypos+6);
143                 wchar_t* text = wgettext("Confirm Password");
144                 Environment->addStaticText(text, rect, false, true, this, -1);
145                 delete[] text;
146         }
147         changeCtype("C");
148         {
149                 core::rect<s32> rect(0, 0, 230, 30);
150                 rect += topleft_client + v2s32(160, ypos);
151                 gui::IGUIEditBox *e = 
152                 Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
153                 e->setPasswordBox(true);
154         }
155
156         ypos += 50;
157         changeCtype("");
158         {
159                 core::rect<s32> rect(0, 0, 140, 30);
160                 rect = rect + v2s32(size.X/2-140/2, ypos);
161                 wchar_t* text = wgettext("Change");
162                 Environment->addButton(rect, this, ID_change, text);
163                 delete[] text;
164         }
165
166         ypos += 50;
167         {
168                 core::rect<s32> rect(0, 0, 300, 20);
169                 rect += topleft_client + v2s32(35, ypos);
170                 wchar_t* text = wgettext("Passwords do not match!");
171                 IGUIElement *e = 
172                 Environment->addStaticText(
173                         text,
174                         rect, false, true, this, ID_message);
175                 e->setVisible(false);
176                 delete[] text;
177         }
178         changeCtype("C");
179
180 }
181
182 void GUIPasswordChange::drawMenu()
183 {
184         gui::IGUISkin* skin = Environment->getSkin();
185         if (!skin)
186                 return;
187         video::IVideoDriver* driver = Environment->getVideoDriver();
188         
189         video::SColor bgcolor(140,0,0,0);
190         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
191
192         gui::IGUIElement::draw();
193 }
194
195 bool GUIPasswordChange::acceptInput()
196 {
197                 std::wstring oldpass;
198                 std::wstring newpass;
199                 gui::IGUIElement *e;
200                 e = getElementFromId(ID_oldPassword);
201                 if(e != NULL)
202                         oldpass = e->getText();
203                 e = getElementFromId(ID_newPassword1);
204                 if(e != NULL)
205                         newpass = e->getText();
206                 e = getElementFromId(ID_newPassword2);
207                 if(e != NULL && newpass != e->getText())
208                 {
209                         e = getElementFromId(ID_message);
210                         if(e != NULL)
211                                 e->setVisible(true);
212                         return false;
213                 }
214                 m_client->sendChangePassword(oldpass, newpass);
215                 return true;
216 }
217
218 bool GUIPasswordChange::OnEvent(const SEvent& event)
219 {
220         if(event.EventType==EET_KEY_INPUT_EVENT)
221         {
222                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
223                 {
224                         quitMenu();
225                         return true;
226                 }
227                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
228                 {
229                         if(acceptInput())
230                                 quitMenu();
231                         return true;
232                 }
233         }
234         if(event.EventType==EET_GUI_EVENT)
235         {
236                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
237                                 && isVisible())
238                 {
239                         if(!canTakeFocus(event.GUIEvent.Element))
240                         {
241                                 dstream<<"GUIPasswordChange: Not allowing focus change."
242                                                 <<std::endl;
243                                 // Returning true disables focus change
244                                 return true;
245                         }
246                 }
247                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
248                 {
249                         switch(event.GUIEvent.Caller->getID())
250                         {
251                         case ID_change:
252                                 if(acceptInput())
253                                         quitMenu();
254                                 return true;
255                         }
256                 }
257                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
258                 {
259                         switch(event.GUIEvent.Caller->getID())
260                         {
261                         case ID_oldPassword:
262                         case ID_newPassword1:
263                         case ID_newPassword2:
264                                 if(acceptInput())
265                                         quitMenu();
266                                 return true;
267                         }
268                 }
269         }
270
271         return Parent ? Parent->OnEvent(event) : false;
272 }
273