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-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "client.h"
26 #include "configuration.h"
27 
28 #include "const/sound.h"
29 
30 #include "fs/paths.h"
31 
34 
36 
37 #include "gui/widgets/button.h"
38 #include "gui/widgets/checkbox.h"
40 #include "gui/widgets/dropdown.h"
41 #include "gui/widgets/label.h"
43 #include "gui/widgets/layoutcell.h"
44 
46 
47 #include "net/charserverhandler.h"
48 #include "net/logindata.h"
49 #include "net/loginhandler.h"
51 
52 #include "utils/delete2.h"
53 #include "utils/stdmove.h"
54 
55 #include "debug.h"
56 
57 std::string LoginDialog::savedPassword;
59 
60 namespace
61 {
63 } // namespace
64 
66  ServerInfo *const server,
67  std::string *const updateHost) :
68  // TRANSLATORS: login dialog name
69  Window(_("Login"), Modal_false, nullptr, "login.xml"),
71  KeyListener(),
72  mLoginData(&data),
73  mServer(server),
74  mUserField(new TextField(this, mLoginData->username,
75  LoseFocusOnTab_true, nullptr, std::string(), false)),
76  mPassField(new PasswordField(this, mLoginData->password)),
77  // TRANSLATORS: login dialog label
78  mKeepCheck(new CheckBox(this, _("Remember username"),
79  mLoginData->remember, nullptr, std::string())),
80  // TRANSLATORS: login dialog label
81  mUpdateTypeLabel(new Label(this, _("Update:"))),
82  mUpdateTypeModel(new UpdateTypeModel),
83  mUpdateTypeDropDown(new DropDown(this, mUpdateTypeModel,
84  false, Modal_false, nullptr, std::string())),
85  // TRANSLATORS: login dialog button
86  mServerButton(new Button(this, _("Change Server"), "server", this)),
87  // TRANSLATORS: login dialog button
88  mLoginButton(new Button(this, _("Login"), "login", this)),
89  // TRANSLATORS: login dialog button
90  mRegisterButton(new Button(this, _("Register"), "register", this)),
91  // TRANSLATORS: login dialog checkbox
92  mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
93  (mLoginData->updateType & UpdateType::Custom) != 0,
94  this, "customhost")),
95  mUpdateHostText(new TextField(this, serverConfig.getValue(
96  "customUpdateHost", ""),
97  LoseFocusOnTab_true, nullptr, std::string(), false)),
98  mUpdateListModel(nullptr),
99  mUpdateHostDropDown(nullptr),
100  mUpdateHost(updateHost),
101  mServerName(server->hostname)
102 {
103  setCloseButton(true);
104  setWindowName("Login");
105 
106  if (charServerHandler != nullptr)
108 
110 
111  // TRANSLATORS: login dialog label
112  Label *const serverLabel1 = new Label(this, _("Server:"));
113  Label *const serverLabel2 = new Label(this, mServerName);
114  serverLabel2->adjustSize();
115  // TRANSLATORS: login dialog label
116  Label *const userLabel = new Label(this, _("Name:"));
117  // TRANSLATORS: login dialog label
118  Label *const passLabel = new Label(this, _("Password:"));
119  if (mServer->updateHosts.size() > 1)
120  {
123  false, Modal_false, this, "updateselect");
124  const std::string str = serverConfig.getValue("updateHost2", "");
125  if (!str.empty())
127  }
128  else
129  {
130  mUpdateListModel = nullptr;
131  mUpdateHostDropDown = nullptr;
132  }
134 
135  if (mPassField->getText().empty() &&
137  {
139  }
140 
141  mUpdateTypeDropDown->setActionEventId("updatetype");
144 
147 
148  mUserField->setActionEventId("login");
149  mPassField->setActionEventId("login");
150 
151  mUserField->addKeyListener(this);
152  mPassField->addKeyListener(this);
155 
156  place(0, 0, serverLabel1, 1, 1);
157  place(1, 0, serverLabel2, 8, 1);
158  place(0, 1, userLabel, 1, 1);
159  place(1, 1, mUserField, 8, 1);
160  place(0, 2, passLabel, 1, 1);
161  place(1, 2, mPassField, 8, 1);
162  place(0, 6, mUpdateTypeLabel, 1, 1);
163  place(1, 6, mUpdateTypeDropDown, 8, 1);
164  int n = 7;
165  if (mUpdateHostDropDown != nullptr)
166  {
167  place(0, 7, mUpdateHostDropDown, 9, 1);
168  n += 1;
169  }
170  place(0, n, mCustomUpdateHost, 9, 1);
171  place(0, n + 1, mUpdateHostText, 9, 1);
172  place(0, n + 2, mKeepCheck, 9, 1);
174  place(2, n + 3, mServerButton, 1, 1);
175  place(3, n + 3, mLoginButton, 1, 1);
176 
177  addKeyListener(this);
178 }
179 
181 {
184 
185  const int h = 200;
186  if (mUpdateHostDropDown != nullptr)
187  setContentSize(310, 250);
188  setContentSize(310, h);
189 #ifdef ANDROID
191 #else // ANDROID
192 
193  setDefaultSize(310, h, ImagePosition::CENTER, 0, 0);
194 #endif // ANDROID
195 
196  center();
197  loadWindowState();
198  reflowLayout(0, 0);
199 
200  if (mUserField->getText().empty())
202  else
204 
206  if (loginHandler != nullptr)
207  {
209  || !mLoginData->registerUrl.empty());
210  }
211  else
212  {
213  mRegisterButton->setEnabled(false);
214  }
215 }
216 
218 {
219  if (mUpdateTypeDropDown != nullptr)
221  if (mUpdateHostDropDown != nullptr)
223 
226 }
227 
229 {
230  const std::string &eventId = event.getId();
231  if (eventId == "login" && canSubmit())
232  {
233  prepareUpdate();
234  mLoginData->registerLogin = false;
236  }
237  else if (eventId == "server")
238  {
239  close();
240  }
241  else if (eventId == "register")
242  {
244  {
245  prepareUpdate();
247  }
248  else if (!mLoginData->registerUrl.empty())
249  {
250  const std::string &url = mLoginData->registerUrl;
251  urlListener.url = url;
253  // TRANSLATORS: question dialog
254  _("Open register url"),
255  url,
257  false,
258  Modal_true,
259  nullptr);
260  confirmDlg->addActionListener(&urlListener);
261  }
262  }
263  else if (eventId == "customhost")
264  {
267  }
268  else if (eventId == "updateselect")
269  {
272  }
273 }
274 
276 {
277  if (event.isConsumed())
278  {
280  return;
281  }
282 
283  const InputActionT actionId = event.getActionId();
284  if (actionId == InputAction::GUI_CANCEL)
285  {
287  }
288 #ifdef USE_SDL2
289  else if (actionId == InputAction::GUI_SELECT2)
290 #else // USE_SDL2
291  else if (actionId == InputAction::GUI_SELECT ||
292  actionId == InputAction::GUI_SELECT2)
293 #endif // USE_SDL2
294  {
296  }
297  else
298  {
300  }
301 }
302 
304 {
305  return !mUserField->getText().empty() &&
306  !mPassField->getText().empty() &&
308 }
309 
311 {
315  UpdateTypeT updateType = static_cast<UpdateTypeT>(
317 
319  && !mUpdateHostText->getText().empty())
320  {
321  updateType = static_cast<UpdateTypeT>(
322  updateType | UpdateType::Custom);
323  serverConfig.setValue("customUpdateHost",
325 
327  {
330  }
331  else
332  {
333  mLoginData->updateHost.clear();
334  (*mUpdateHost).clear();
335  }
336  }
337  else
338  {
339  std::string str;
340  if (mUpdateHostDropDown != nullptr)
341  {
342  const int sel = mUpdateHostDropDown->getSelected();
343  if (sel >= 0)
344  {
345  const HostsGroup &group = mServer->updateHosts[sel];
346  if (!group.hosts.empty())
347  {
348  str = group.hosts[0];
349  mLoginData->updateHosts = group.hosts;
350  serverConfig.setValue("updateHost2", group.name);
351  }
352  else
353  {
354  serverConfig.setValue("updateHost2", "");
355  }
356  }
357  }
358  else if (mLoginData->updateHost.empty()
359  && !mLoginData->updateHosts.empty())
360  {
361  str = mLoginData->updateHosts[0];
362  serverConfig.setValue("updateHost2", str);
363  }
364  if (!str.empty() && checkPath(str))
365  {
367  *mUpdateHost = STD_MOVE(str);
368  }
369  else
370  {
371  mLoginData->updateHost.clear();
372  (*mUpdateHost).clear();
373  }
374  }
375 
376  mLoginData->updateType = updateType;
377  serverConfig.setValue("updateType", CAST_S32(updateType));
378 
379  mRegisterButton->setEnabled(false);
380  mServerButton->setEnabled(false);
381  mLoginButton->setEnabled(false);
382 
384  if (mLoginData->remember)
386  else
388 }
389 
391 {
393  Window::close();
394 }
395 
397 {
398  HostsGroup group;
399 
400  group.name = mServer->defaultHostName;
401  if (group.name.empty())
402  {
403  // TRANSLATORS: update hosts group default name
404  group.name = _("default updates");
405  }
406  group.hosts = mLoginData->updateHosts;
407  mServer->updateHosts.insert(mServer->updateHosts.begin(), group);
408 }
std::string defaultHostName
Definition: serverinfo.h:51
void setDefaultSize()
Definition: window.cpp:1193
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1379
#define _(s)
Definition: gettext.h:34
std::string * mUpdateHost
Definition: logindialog.h:109
const bool LoseFocusOnTab_true
std::string registerUrl
Definition: logindata.h:68
void setState(const StateT state)
Definition: client.h:65
const bool Visible_true
Definition: visible.h:29
void adjustSize()
Definition: textfield.cpp:715
void loadWindowState()
Definition: window.cpp:1082
TextField * mPassField
Definition: logindialog.h:96
void hideDrop(bool event)
Definition: dropdown.cpp:378
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void setVisible(Visible visible)
Definition: widget.cpp:224
bool registerLogin
Definition: logindata.h:74
UpdateListModel * mUpdateListModel
Definition: logindialog.h:106
CheckBox * mKeepCheck
Definition: logindialog.h:97
CheckBox * mCustomUpdateHost
Definition: logindialog.h:104
Definition: window.h:98
Definition: button.h:94
Button * mServerButton
Definition: logindialog.h:101
bool checkPath(const std::string &path)
Definition: paths.cpp:106
StringVect hosts
Definition: hostsgroup.h:39
static const std::string SOUND_REQUEST
Definition: sound.h:28
const std::string & getActionEventId() const
Definition: widget.h:604
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Client * client
Definition: client.cpp:113
TextField * mUpdateHostText
Definition: logindialog.h:105
#define delete2(var)
Definition: delete2.h:24
Configuration serverConfig
std::string mUpdateHost
Definition: loginrecv.cpp:43
StringVect updateHosts
Definition: logindata.h:62
LoginData * mLoginData
Definition: logindialog.h:92
virtual void clear() const =0
std::string url
Label * mUpdateTypeLabel
Definition: logindialog.h:98
void setText(const std::string &text)
Definition: textfield.cpp:802
void keyPressed(KeyEvent &event)
virtual void close()
Definition: window.cpp:897
#define new
Definition: debug_new.h:147
void setSelectedString(const std::string &str)
Definition: dropdown.cpp:445
void setContentSize(int width, int height)
Definition: window.cpp:491
Button * mLoginButton
Definition: logindialog.h:102
DropDown * mUpdateHostDropDown
Definition: logindialog.h:107
std::string updateHost
Definition: logindata.h:61
#define CAST_S32
Definition: cast.h:29
virtual void requestFocus()
Definition: widget.cpp:203
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
Net::LoginHandler * loginHandler
Definition: net.cpp:83
LoginDialog(LoginData &data, ServerInfo *const server, std::string *const updateHost)
Definition: logindialog.cpp:65
std::string name
Definition: hostsgroup.h:38
#define fromBool(val, name)
Definition: booldefines.h:48
uint32_t data
virtual bool isRegistrationEnabled() const =0
Button * mRegisterButton
Definition: logindialog.h:103
void mergeUpdateHosts()
std::string username
Definition: logindata.h:58
InputAction ::T InputActionT
Definition: inputaction.h:714
#define nullptr
Definition: localconsts.h:44
std::vector< HostsGroup > updateHosts
Definition: serverinfo.h:53
void action(const ActionEvent &event)
DropDown * mUpdateTypeDropDown
Definition: logindialog.h:100
TextField * mUserField
Definition: logindialog.h:95
void setCloseButton(const bool flag)
Definition: window.cpp:744
static std::string savedPassword
Definition: logindialog.h:79
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
bool isConsumed() const
void setEnabled(const bool enabled)
Definition: widget.h:351
#define STD_MOVE(var)
Definition: stdmove.h:27
UpdateTypeT updateType
Definition: logindata.h:64
void prepareUpdate()
std::string getValue(const std::string &key, const std::string &deflt) const
bool canSubmit() const
virtual void setVisible(Visible visible)
Definition: window.cpp:773
void center()
Definition: window.cpp:1412
bool Visible
Definition: visible.h:29
ConfirmDialog * confirmDlg
Definition: traderecv.cpp:48
void postInit()
StateT getState() const
Definition: client.h:68
std::string password
Definition: logindata.h:59
ServerInfo * mServer
Definition: logindialog.h:93
UpdateType ::T UpdateTypeT
Definition: updatetype.h:35
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
LayoutCell & setHAlign(const Alignment a)
Definition: layoutcell.h:77
bool remember
Definition: logindata.h:73
void setSelected(const bool selected)
Definition: checkbox.h:155
bool isSelected() const
Definition: checkbox.h:146
std::string mServerName
Definition: logindialog.h:110
Net::CharServerHandler * charServerHandler
Definition: net.cpp:79
void setSelected(int selected)
Definition: dropdown.cpp:514
void adjustSize()
Definition: label.cpp:197
void setValue(const std::string &key, const std::string &value)
bool url(InputEvent &event)
Definition: commands.cpp:63
const std::string & getText() const
Definition: textfield.h:223
int getSelected() const
Definition: dropdown.cpp:509
void reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248
UpdateTypeModel * mUpdateTypeModel
Definition: logindialog.h:99
static std::string savedPasswordKey
Definition: logindialog.h:78
void setWindowName(const std::string &name)
Definition: window.h:354