ManaPlus
logindialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "client.h"
27 #include "configuration.h"
28 
29 #include "const/sound.h"
30 
31 #include "fs/paths.h"
32 
35 
37 
38 #include "gui/widgets/button.h"
39 #include "gui/widgets/checkbox.h"
41 #include "gui/widgets/dropdown.h"
42 #include "gui/widgets/label.h"
44 #include "gui/widgets/layoutcell.h"
45 
47 
48 #include "net/charserverhandler.h"
49 #include "net/logindata.h"
50 #include "net/loginhandler.h"
52 
53 #include "utils/delete2.h"
54 #include "utils/stdmove.h"
55 
56 #include "debug.h"
57 
58 std::string LoginDialog::savedPassword;
60 
61 namespace
62 {
64 } // namespace
65 
67  ServerInfo *const server,
68  std::string *const updateHost) :
69  // TRANSLATORS: login dialog name
70  Window(_("Login"), Modal_false, nullptr, "login.xml"),
72  KeyListener(),
73  mLoginData(&data),
74  mServer(server),
75  mUserField(new TextField(this, mLoginData->username,
76  LoseFocusOnTab_true, nullptr, std::string(), false)),
77  mPassField(new PasswordField(this, mLoginData->password)),
78 #ifdef SAVE_PASSWORD
79  // TRANSLATORS: login dialog label
80  mKeepCheck(new CheckBox(this, _("Remember user and password"),
81 #else
82  // TRANSLATORS: login dialog label
83  mKeepCheck(new CheckBox(this, _("Remember username"),
84 #endif
85  mLoginData->remember, nullptr, std::string())),
86  // TRANSLATORS: login dialog label
87  mUpdateTypeLabel(new Label(this, _("Update:"))),
88  mUpdateTypeModel(new UpdateTypeModel),
89  mUpdateTypeDropDown(new DropDown(this, mUpdateTypeModel,
90  false, Modal_false, nullptr, std::string())),
91  // TRANSLATORS: login dialog button
92  mServerButton(new Button(this, _("Change Server"), "server",
93  BUTTON_SKIN, this)),
94  // TRANSLATORS: login dialog button
95  mLoginButton(new Button(this, _("Login"), "login",
96  BUTTON_SKIN, this)),
97  // TRANSLATORS: login dialog button
98  mRegisterButton(new Button(this, _("Register"), "register",
99  BUTTON_SKIN, this)),
100  // TRANSLATORS: login dialog checkbox
101  mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
102  (mLoginData->updateType & UpdateType::Custom) != 0,
103  this, "customhost")),
104  mUpdateHostText(new TextField(this, serverConfig.getValue(
105  "customUpdateHost", ""),
106  LoseFocusOnTab_true, nullptr, std::string(), false)),
107  mUpdateListModel(nullptr),
108  mUpdateHostDropDown(nullptr),
109  mUpdateHost(updateHost),
110  mServerName(server->hostname)
111 {
112  setCloseButton(true);
113  setWindowName("Login");
114 
115  if (charServerHandler != nullptr)
117 
119 
120  // TRANSLATORS: login dialog label
121  Label *const serverLabel1 = new Label(this, _("Server:"));
122  Label *const serverLabel2 = new Label(this, mServerName);
123  serverLabel2->adjustSize();
124  // TRANSLATORS: login dialog label
125  Label *const userLabel = new Label(this, _("Name:"));
126  // TRANSLATORS: login dialog label
127  Label *const passLabel = new Label(this, _("Password:"));
128  if (mServer->updateHosts.size() > 1)
129  {
132  false, Modal_false, this, "updateselect");
133  const std::string str = serverConfig.getValue("updateHost2", "");
134  if (!str.empty())
136  }
137  else
138  {
139  mUpdateListModel = nullptr;
140  mUpdateHostDropDown = nullptr;
141  }
143 
144  if (mPassField->getText().empty() &&
146  {
148  }
149 
150  mUpdateTypeDropDown->setActionEventId("updatetype");
153 
156 
157  mUserField->setActionEventId("login");
158  mPassField->setActionEventId("login");
159 
160  mUserField->addKeyListener(this);
161  mPassField->addKeyListener(this);
164 
165  place(0, 0, serverLabel1, 1, 1);
166  place(1, 0, serverLabel2, 8, 1);
167  place(0, 1, userLabel, 1, 1);
168  place(1, 1, mUserField, 8, 1);
169  place(0, 2, passLabel, 1, 1);
170  place(1, 2, mPassField, 8, 1);
171  place(0, 6, mUpdateTypeLabel, 1, 1);
172  place(1, 6, mUpdateTypeDropDown, 8, 1);
173  int n = 7;
174  if (mUpdateHostDropDown != nullptr)
175  {
176  place(0, 7, mUpdateHostDropDown, 9, 1);
177  n += 1;
178  }
179  place(0, n, mCustomUpdateHost, 9, 1);
180  place(0, n + 1, mUpdateHostText, 9, 1);
181  place(0, n + 2, mKeepCheck, 9, 1);
183  place(2, n + 3, mServerButton, 1, 1);
184  place(3, n + 3, mLoginButton, 1, 1);
185 
186  addKeyListener(this);
187 }
188 
190 {
193 
194  const int h = 200;
195  if (mUpdateHostDropDown != nullptr)
196  setContentSize(310, 250);
197  setContentSize(310, h);
198 #ifdef ANDROID
200 #else // ANDROID
201 
202  setDefaultSize(310, h, ImagePosition::CENTER, 0, 0);
203 #endif // ANDROID
204 
205  center();
206  loadWindowState();
207  reflowLayout(0, 0);
208 
209 #ifdef SAVE_PASSWORD
211 #else
212  if (mUserField->getText().empty())
214  else
216 #endif
217 
219  if (loginHandler != nullptr)
220  {
222  || !mLoginData->registerUrl.empty());
223  }
224  else
225  {
226  mRegisterButton->setEnabled(false);
227  }
228 }
229 
231 {
232  if (mUpdateTypeDropDown != nullptr)
234  if (mUpdateHostDropDown != nullptr)
236 
239 }
240 
242 {
243  const std::string &eventId = event.getId();
244  if (eventId == "login" && canSubmit())
245  {
246  prepareUpdate();
247  mLoginData->registerLogin = false;
249  }
250  else if (eventId == "server")
251  {
252  close();
253  }
254  else if (eventId == "register")
255  {
257  {
258  prepareUpdate();
260  }
261  else if (!mLoginData->registerUrl.empty())
262  {
263  const std::string &url = mLoginData->registerUrl;
264  urlListener.url = url;
266  // TRANSLATORS: question dialog
267  _("Open register url"),
268  url,
270  false,
271  Modal_true,
272  nullptr);
274  }
275  }
276  else if (eventId == "customhost")
277  {
280  }
281  else if (eventId == "updateselect")
282  {
285  }
286 }
287 
289 {
290  if (event.isConsumed())
291  {
293  return;
294  }
295 
296  const InputActionT actionId = event.getActionId();
297  if (actionId == InputAction::GUI_CANCEL)
298  {
300  }
301 #ifdef USE_SDL2
302  else if (actionId == InputAction::GUI_SELECT2)
303 #else // USE_SDL2
304  else if (actionId == InputAction::GUI_SELECT ||
305  actionId == InputAction::GUI_SELECT2)
306 #endif // USE_SDL2
307  {
309  }
310  else
311  {
313  }
314 }
315 
317 {
318  return !mUserField->getText().empty() &&
319  !mPassField->getText().empty() &&
321 }
322 
324 {
328  UpdateTypeT updateType = static_cast<UpdateTypeT>(
330 
332  && !mUpdateHostText->getText().empty())
333  {
334  updateType = static_cast<UpdateTypeT>(
335  updateType | UpdateType::Custom);
336  serverConfig.setValue("customUpdateHost",
338 
340  {
343  }
344  else
345  {
346  mLoginData->updateHost.clear();
347  (*mUpdateHost).clear();
348  }
349  }
350  else
351  {
352  std::string str;
353  if (mUpdateHostDropDown != nullptr)
354  {
355  const int sel = mUpdateHostDropDown->getSelected();
356  if (sel >= 0)
357  {
358  const HostsGroup &group = mServer->updateHosts[sel];
359  if (!group.hosts.empty())
360  {
361  str = group.hosts[0];
362  mLoginData->updateHosts = group.hosts;
363  serverConfig.setValue("updateHost2", group.name);
364  }
365  else
366  {
367  serverConfig.setValue("updateHost2", "");
368  }
369  }
370  }
371  else if (mLoginData->updateHost.empty()
372  && !mLoginData->updateHosts.empty())
373  {
374  str = mLoginData->updateHosts[0];
375  serverConfig.setValue("updateHost2", str);
376  }
377  if (!str.empty() && checkPath(str))
378  {
379  mLoginData->updateHost = str;
380  *mUpdateHost = STD_MOVE(str);
381  }
382  else
383  {
384  mLoginData->updateHost.clear();
385  (*mUpdateHost).clear();
386  }
387  }
388 
389  mLoginData->updateType = updateType;
390  serverConfig.setValue("updateType", CAST_S32(updateType));
391 
392  mRegisterButton->setEnabled(false);
393  mServerButton->setEnabled(false);
394  mLoginButton->setEnabled(false);
395 
397  if (mLoginData->remember)
399  else
401 }
402 
404 {
406  Window::close();
407 }
408 
410 {
411  HostsGroup group;
412 
413  group.name = mServer->defaultHostName;
414  if (group.name.empty())
415  {
416  // TRANSLATORS: update hosts group default name
417  group.name = _("default updates");
418  }
419  group.hosts = mLoginData->updateHosts;
420  mServer->updateHosts.insert(mServer->updateHosts.begin(), group);
421 }
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Net::CharServerHandler * charServerHandler
Definition: net.cpp:85
Definition: button.h:102
void setSelected(const bool selected)
Definition: checkbox.h:156
bool isSelected() const
Definition: checkbox.h:147
void setState(const StateT state)
Definition: client.h:66
StateT getState() const
Definition: client.h:69
std::string getValue(const std::string &key, const std::string &deflt) const
void setValue(const std::string &key, const std::string &value)
int getSelected() const
Definition: dropdown.cpp:509
void hideDrop(bool event)
Definition: dropdown.cpp:378
void setSelected(int selected)
Definition: dropdown.cpp:514
void setSelectedString(const std::string &str)
Definition: dropdown.cpp:445
bool isConsumed() const
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
LayoutCell & setHAlign(const Alignment a)
Definition: layoutcell.h:84
std::string registerUrl
Definition: logindata.h:69
std::string updateHost
Definition: logindata.h:62
StringVect updateHosts
Definition: logindata.h:63
std::string username
Definition: logindata.h:59
std::string password
Definition: logindata.h:60
UpdateTypeT updateType
Definition: logindata.h:65
bool remember
Definition: logindata.h:74
bool registerLogin
Definition: logindata.h:75
Button * mLoginButton
Definition: logindialog.h:103
TextField * mPassField
Definition: logindialog.h:97
void keyPressed(KeyEvent &event)
UpdateListModel * mUpdateListModel
Definition: logindialog.h:107
static std::string savedPasswordKey
Definition: logindialog.h:79
static std::string savedPassword
Definition: logindialog.h:80
ServerInfo * mServer
Definition: logindialog.h:94
std::string * mUpdateHost
Definition: logindialog.h:110
Button * mServerButton
Definition: logindialog.h:102
TextField * mUserField
Definition: logindialog.h:96
LoginData * mLoginData
Definition: logindialog.h:93
bool canSubmit() const
void postInit()
LoginDialog(LoginData &data, ServerInfo *const server, std::string *const updateHost)
Definition: logindialog.cpp:66
void mergeUpdateHosts()
std::string mServerName
Definition: logindialog.h:111
CheckBox * mCustomUpdateHost
Definition: logindialog.h:105
TextField * mUpdateHostText
Definition: logindialog.h:106
CheckBox * mKeepCheck
Definition: logindialog.h:98
Label * mUpdateTypeLabel
Definition: logindialog.h:99
void prepareUpdate()
Button * mRegisterButton
Definition: logindialog.h:104
UpdateTypeModel * mUpdateTypeModel
Definition: logindialog.h:100
DropDown * mUpdateHostDropDown
Definition: logindialog.h:108
DropDown * mUpdateTypeDropDown
Definition: logindialog.h:101
void action(const ActionEvent &event)
virtual void clear() const =0
virtual bool isRegistrationEnabled() const =0
std::vector< HostsGroup > updateHosts
Definition: serverinfo.h:54
std::string defaultHostName
Definition: serverinfo.h:52
const std::string & getText() const
Definition: textfield.h:224
void setText(const std::string &text)
Definition: textfield.cpp:803
void adjustSize()
Definition: textfield.cpp:716
void setVisible(Visible visible)
Definition: widget.cpp:225
void setEnabled(const bool enabled)
Definition: widget.h:352
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
virtual void requestFocus()
Definition: widget.cpp:204
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
const std::string & getActionEventId() const
Definition: widget.h:605
Definition: window.h:102
void center()
Definition: window.cpp:1417
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setContentSize(int width, int height)
Definition: window.cpp:492
void reflowLayout(int w, int h)
Definition: window.cpp:1396
void setWindowName(const std::string &name)
Definition: window.h:355
void postInit()
Definition: window.cpp:249
void setCloseButton(const bool flag)
Definition: window.cpp:749
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
Configuration serverConfig
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
Client * client
Definition: client.cpp:118
#define _(s)
Definition: gettext.h:35
InputAction ::T InputActionT
Definition: inputaction.h:717
#define nullptr
Definition: localconsts.h:45
Net::LoginHandler * loginHandler
Definition: net.cpp:90
const bool LoseFocusOnTab_true
uint32_t data
const bool Modal_false
Definition: modal.h:30
const bool Modal_true
Definition: modal.h:30
bool url(InputEvent &event)
Definition: commands.cpp:64
std::string mUpdateHost
Definition: loginrecv.cpp:44
@ REGISTER_PREP
Definition: state.h:53
@ LOGIN_ATTEMPT
Definition: state.h:41
@ SWITCH_SERVER
Definition: state.h:62
@ LOGIN
Definition: state.h:40
bool checkPath(const std::string &path)
Definition: paths.cpp:121
ConfirmDialog * confirmDlg
Definition: traderecv.cpp:49
static const std::string SOUND_REQUEST
Definition: sound.h:29
#define STD_MOVE(var)
Definition: stdmove.h:28
std::string name
Definition: hostsgroup.h:39
StringVect hosts
Definition: hostsgroup.h:40
std::string url
UpdateType ::T UpdateTypeT
Definition: updatetype.h:36
bool Visible
Definition: visible.h:30
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30