Fix memory leak in guiConfirmRegistration
[oweals/minetest.git] / src / gui / guiConfirmRegistration.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
4                 <muhammadrifqipriyosusanto@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "guiConfirmRegistration.h"
22 #include "client.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
26 #include <IGUIFont.h>
27 #include "intlGUIEditBox.h"
28
29 #include "gettext.h"
30
31 // Continuing from guiPasswordChange.cpp
32 const int ID_confirmPassword = 262;
33 const int ID_confirm = 263;
34 const int ID_message = 264;
35 const int ID_cancel = 265;
36
37 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
38                 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
39                 const std::string &playername, const std::string &password,
40                 const std::string &address, bool *aborted) :
41                 GUIModalMenu(env, parent, id, menumgr),
42                 m_client(client), m_playername(playername), m_password(password),
43                 m_address(address), m_aborted(aborted)
44 {
45 }
46
47 GUIConfirmRegistration::~GUIConfirmRegistration()
48 {
49         removeChildren();
50 }
51
52 void GUIConfirmRegistration::removeChildren()
53 {
54         const core::list<gui::IGUIElement *> &children = getChildren();
55         core::list<gui::IGUIElement *> children_copy;
56         for (gui::IGUIElement *i : children)
57                 children_copy.push_back(i);
58         for (gui::IGUIElement *i : children_copy)
59                 i->remove();
60 }
61 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
62 {
63         acceptInput();
64         removeChildren();
65
66         /*
67                 Calculate new sizes and positions
68         */
69         core::rect<s32> rect(screensize.X / 2 - 600 / 2, screensize.Y / 2 - 360 / 2,
70                         screensize.X / 2 + 600 / 2, screensize.Y / 2 + 360 / 2);
71
72         DesiredRect = rect;
73         recalculateAbsolutePosition(false);
74
75         v2s32 size = rect.getSize();
76         v2s32 topleft_client(0, 0);
77
78         const wchar_t *text;
79
80         /*
81                 Add stuff
82         */
83         s32 ypos = 30;
84         {
85                 std::string address = m_address;
86                 if (address.empty())
87                         address = "localhost";
88                 core::rect<s32> rect2(0, 0, 540, 180);
89                 rect2 += topleft_client + v2s32(30, ypos);
90                 static const std::string info_text_template = strgettext(
91                                 "You are about to join the server at %1$s with the "
92                                 "name \"%2$s\" for the first time. If you proceed, a "
93                                 "new account using your credentials will be created "
94                                 "on this server.\n"
95                                 "Please retype your password and click Register and "
96                                 "Join to confirm account creation or click Cancel to "
97                                 "abort.");
98                 char info_text_buf[1024];
99                 snprintf(info_text_buf, sizeof(info_text_buf), info_text_template.c_str(),
100                                 address.c_str(), m_playername.c_str());
101
102                 wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
103                 gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
104                                 Environment, this, ID_message, rect2, false, true);
105                 delete[] info_text_buf_wide;
106                 e->drop();
107                 e->setMultiLine(true);
108                 e->setWordWrap(true);
109                 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
110         }
111
112         ypos += 210;
113         {
114                 core::rect<s32> rect2(0, 0, 540, 30);
115                 rect2 += topleft_client + v2s32(30, ypos);
116                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
117                                 rect2, true, this, ID_confirmPassword);
118                 e->setPasswordBox(true);
119         }
120
121         ypos += 60;
122         {
123                 core::rect<s32> rect2(0, 0, 230, 35);
124                 rect2 = rect2 + v2s32(size.X / 2 - 220, ypos);
125                 text = wgettext("Register and Join");
126                 Environment->addButton(rect2, this, ID_confirm, text);
127                 delete[] text;
128         }
129         {
130                 core::rect<s32> rect2(0, 0, 120, 35);
131                 rect2 = rect2 + v2s32(size.X / 2 + 70, ypos);
132                 text = wgettext("Cancel");
133                 Environment->addButton(rect2, this, ID_cancel, text);
134                 delete[] text;
135         }
136         {
137                 core::rect<s32> rect2(0, 0, 200, 20);
138                 rect2 += topleft_client + v2s32(30, ypos - 40);
139                 text = wgettext("Passwords do not match!");
140                 IGUIElement *e = Environment->addStaticText(
141                                 text, rect2, false, true, this, ID_message);
142                 e->setVisible(false);
143                 delete[] text;
144         }
145 }
146
147 void GUIConfirmRegistration::drawMenu()
148 {
149         gui::IGUISkin *skin = Environment->getSkin();
150         if (!skin)
151                 return;
152         video::IVideoDriver *driver = Environment->getVideoDriver();
153
154         video::SColor bgcolor(140, 0, 0, 0);
155         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
156
157         gui::IGUIElement::draw();
158 }
159
160 void GUIConfirmRegistration::closeMenu(bool goNext)
161 {
162         if (goNext) {
163                 m_client->confirmRegistration();
164         } else {
165                 *m_aborted = true;
166                 infostream << "Connect aborted [Escape]" << std::endl;
167         }
168         quitMenu();
169 }
170
171 void GUIConfirmRegistration::acceptInput()
172 {
173         gui::IGUIElement *e;
174         e = getElementFromId(ID_confirmPassword);
175         if (e)
176                 m_pass_confirm = e->getText();
177 }
178
179 bool GUIConfirmRegistration::processInput()
180 {
181         std::wstring m_password_ws = narrow_to_wide(m_password);
182         if (m_password_ws != m_pass_confirm) {
183                 gui::IGUIElement *e = getElementFromId(ID_message);
184                 if (e)
185                         e->setVisible(true);
186                 return false;
187         }
188         return true;
189 }
190
191 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
192 {
193         if (event.EventType == EET_KEY_INPUT_EVENT) {
194                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
195                         closeMenu(false);
196                         return true;
197                 }
198                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
199                         acceptInput();
200                         if (processInput())
201                                 closeMenu(true);
202                         return true;
203                 }
204         }
205
206         if (event.EventType != EET_GUI_EVENT)
207                 return Parent ? Parent->OnEvent(event) : false;
208
209         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
210                 if (!canTakeFocus(event.GUIEvent.Element)) {
211                         dstream << "GUIConfirmRegistration: Not allowing focus "
212                                    "change."
213                                 << std::endl;
214                         // Returning true disables focus change
215                         return true;
216                 }
217         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
218                 switch (event.GUIEvent.Caller->getID()) {
219                 case ID_confirm:
220                         acceptInput();
221                         if (processInput())
222                                 closeMenu(true);
223                         return true;
224                 case ID_cancel:
225                         closeMenu(false);
226                         return true;
227                 }
228         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
229                 switch (event.GUIEvent.Caller->getID()) {
230                 case ID_confirmPassword:
231                         acceptInput();
232                         if (processInput())
233                                 closeMenu(true);
234                         return true;
235                 }
236         }
237
238         return false;
239 }