added gettext support
[oweals/minetest.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 #include "gettext.h"
25
26 const int ID_oldPassword = 256;
27 const int ID_newPassword1 = 257;
28 const int ID_newPassword2 = 258;
29 const int ID_change = 259;
30 const int ID_message = 260;
31
32 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
33                 gui::IGUIElement* parent, s32 id,
34                 IMenuManager *menumgr,
35                 Client* client
36 ):
37         GUIModalMenu(env, parent, id, menumgr),
38         m_client(client)
39 {
40 }
41
42 GUIPasswordChange::~GUIPasswordChange()
43 {
44         removeChildren();
45 }
46
47 void GUIPasswordChange::removeChildren()
48 {
49         {
50                 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
51                 if(e != NULL)
52                         e->remove();
53         }
54         {
55                 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
56                 if(e != NULL)
57                         e->remove();
58         }
59         {
60                 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
61                 if(e != NULL)
62                         e->remove();
63         }
64         {
65                 gui::IGUIElement *e = getElementFromId(ID_change);
66                 if(e != NULL)
67                         e->remove();
68         }
69 }
70
71 void GUIPasswordChange::regenerateGui(v2u32 screensize)
72 {
73         /*
74                 Remove stuff
75         */
76         removeChildren();
77         
78         /*
79                 Calculate new sizes and positions
80         */
81         core::rect<s32> rect(
82                         screensize.X/2 - 580/2,
83                         screensize.Y/2 - 300/2,
84                         screensize.X/2 + 580/2,
85                         screensize.Y/2 + 300/2
86         );
87         
88         DesiredRect = rect;
89         recalculateAbsolutePosition(false);
90
91         v2s32 size = rect.getSize();
92         v2s32 topleft_client(40, 0);
93         v2s32 size_client = size - v2s32(40, 0);
94
95         /*
96                 Add stuff
97         */
98         s32 ypos = 50;
99         {
100                 core::rect<s32> rect(0, 0, 110, 20);
101                 rect += topleft_client + v2s32(35, ypos+6);
102                 Environment->addStaticText(chartowchar_t(gettext("Old Password")), rect, false, true, this, -1);
103         }
104         {
105                 core::rect<s32> rect(0, 0, 230, 30);
106                 rect += topleft_client + v2s32(160, ypos);
107                 gui::IGUIEditBox *e = 
108                 Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
109                 Environment->setFocus(e);
110                 e->setPasswordBox(true);
111         }
112         ypos += 50;
113         {
114                 core::rect<s32> rect(0, 0, 110, 20);
115                 rect += topleft_client + v2s32(35, ypos+6);
116                 Environment->addStaticText(chartowchar_t(gettext("New Password")), 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                 Environment->addStaticText(chartowchar_t(gettext("Confirm Password")), rect, false, true, this, -1);
130         }
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_newPassword2);
136                 e->setPasswordBox(true);
137         }
138
139         ypos += 50;
140         {
141                 core::rect<s32> rect(0, 0, 140, 30);
142                 rect = rect + v2s32(size.X/2-140/2, ypos);
143                 Environment->addButton(rect, this, ID_change, chartowchar_t(gettext("Change")));
144         }
145
146         ypos += 50;
147         {
148                 core::rect<s32> rect(0, 0, 300, 20);
149                 rect += topleft_client + v2s32(35, ypos);
150                 IGUIElement *e = 
151                 Environment->addStaticText(chartowchar_t(gettext("Passwords do not match!")), rect, false, true, this, ID_message);
152                 e->setVisible(false);
153         }
154
155 }
156
157 void GUIPasswordChange::drawMenu()
158 {
159         gui::IGUISkin* skin = Environment->getSkin();
160         if (!skin)
161                 return;
162         video::IVideoDriver* driver = Environment->getVideoDriver();
163         
164         video::SColor bgcolor(140,0,0,0);
165         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
166
167         gui::IGUIElement::draw();
168 }
169
170 bool GUIPasswordChange::acceptInput()
171 {
172                 std::wstring oldpass;
173                 std::wstring newpass;
174                 gui::IGUIElement *e;
175                 e = getElementFromId(ID_oldPassword);
176                 if(e != NULL)
177                         oldpass = e->getText();
178                 e = getElementFromId(ID_newPassword1);
179                 if(e != NULL)
180                         newpass = e->getText();
181                 e = getElementFromId(ID_newPassword2);
182                 if(e != NULL && newpass != e->getText())
183                 {
184                         e = getElementFromId(ID_message);
185                         if(e != NULL)
186                                 e->setVisible(true);
187                         return false;
188                 }
189                 m_client->sendChangePassword(oldpass, newpass);
190                 return true;
191 }
192
193 bool GUIPasswordChange::OnEvent(const SEvent& event)
194 {
195         if(event.EventType==EET_KEY_INPUT_EVENT)
196         {
197                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
198                 {
199                         quitMenu();
200                         return true;
201                 }
202                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
203                 {
204                         if(acceptInput())
205                                 quitMenu();
206                         return true;
207                 }
208         }
209         if(event.EventType==EET_GUI_EVENT)
210         {
211                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
212                                 && isVisible())
213                 {
214                         if(!canTakeFocus(event.GUIEvent.Element))
215                         {
216                                 dstream<<"GUIPasswordChange: Not allowing focus change."
217                                                 <<std::endl;
218                                 // Returning true disables focus change
219                                 return true;
220                         }
221                 }
222                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
223                 {
224                         switch(event.GUIEvent.Caller->getID())
225                         {
226                         case ID_change:
227                                 if(acceptInput())
228                                         quitMenu();
229                                 return true;
230                         }
231                 }
232                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
233                 {
234                         switch(event.GUIEvent.Caller->getID())
235                         {
236                         case ID_oldPassword:
237                         case ID_newPassword1:
238                         case ID_newPassword2:
239                                 if(acceptInput())
240                                         quitMenu();
241                                 return true;
242                         }
243                 }
244         }
245
246         return Parent ? Parent->OnEvent(event) : false;
247 }
248