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",
87  BUTTON_SKIN, this)),
88  // TRANSLATORS: login dialog button
89  mLoginButton(new Button(this, _("Login"), "login",
90  BUTTON_SKIN, this)),
91  // TRANSLATORS: login dialog button
92  mRegisterButton(new Button(this, _("Register"), "register",
93  BUTTON_SKIN, this)),
94  // TRANSLATORS: login dialog checkbox
95  mCustomUpdateHost(new CheckBox(this, _("Custom update host"),
96  (mLoginData->updateType & UpdateType::Custom) != 0,
97  this, "customhost")),
98  mUpdateHostText(new TextField(this, serverConfig.getValue(
99  "customUpdateHost", ""),
100  LoseFocusOnTab_true, nullptr, std::string(), false)),
101  mUpdateListModel(nullptr),
102  mUpdateHostDropDown(nullptr),
103  mUpdateHost(updateHost),
104  mServerName(server->hostname)
105 {
106  setCloseButton(true);
107  setWindowName("Login");
108 
109  if (charServerHandler != nullptr)
111 
113 
114  // TRANSLATORS: login dialog label
115  Label *const serverLabel1 = new Label(this, _("Server:"));
116  Label *const serverLabel2 = new Label(this, mServerName);
117  serverLabel2->adjustSize();
118  // TRANSLATORS: login dialog label
119  Label *const userLabel = new Label(this, _("Name:"));
120  // TRANSLATORS: login dialog label
121  Label *const passLabel = new Label(this, _("Password:"));
122  if (mServer->updateHosts.size() > 1)
123  {
126  false, Modal_false, this, "updateselect");
127  const std::string str = serverConfig.getValue("updateHost2", "");
128  if (!str.empty())
130  }
131  else
132  {
133  mUpdateListModel = nullptr;
134  mUpdateHostDropDown = nullptr;
135  }
137 
138  if (mPassField->getText().empty() &&
140  {
142  }
143 
144  mUpdateTypeDropDown->setActionEventId("updatetype");
147 
150 
151  mUserField->setActionEventId("login");
152  mPassField->setActionEventId("login");
153 
154  mUserField->addKeyListener(this);
155  mPassField->addKeyListener(this);
158 
159  place(0, 0, serverLabel1, 1, 1);
160  place(1, 0, serverLabel2, 8, 1);
161  place(0, 1, userLabel, 1, 1);
162  place(1, 1, mUserField, 8, 1);
163  place(0, 2, passLabel, 1, 1);
164  place(1, 2, mPassField, 8, 1);
165  place(0, 6, mUpdateTypeLabel, 1, 1);
166  place(1, 6, mUpdateTypeDropDown, 8, 1);
167  int n = 7;
168  if (mUpdateHostDropDown != nullptr)
169  {
170  place(0, 7, mUpdateHostDropDown, 9, 1);
171  n += 1;
172  }
173  place(0, n, mCustomUpdateHost, 9, 1);
174  place(0, n + 1, mUpdateHostText, 9, 1);
175  place(0, n + 2, mKeepCheck, 9, 1);
177  place(2, n + 3, mServerButton, 1, 1);
178  place(3, n + 3, mLoginButton, 1, 1);
179 
180  addKeyListener(this);
181 }
182 
184 {
187 
188  const int h = 200;
189  if (mUpdateHostDropDown != nullptr)
190  setContentSize(310, 250);
191  setContentSize(310, h);
192 #ifdef ANDROID
194 #else // ANDROID
195 
196  setDefaultSize(310, h, ImagePosition::CENTER, 0, 0);
197 #endif // ANDROID
198 
199  center();
200  loadWindowState();
201  reflowLayout(0, 0);
202 
203  if (mUserField->getText().empty())
205  else
207 
209  if (loginHandler != nullptr)
210  {
212  || !mLoginData->registerUrl.empty());
213  }
214  else
215  {
216  mRegisterButton->setEnabled(false);
217  }
218 }
219 
221 {
222  if (mUpdateTypeDropDown != nullptr)
224  if (mUpdateHostDropDown != nullptr)
226 
229 }
230 
232 {
233  const std::string &eventId = event.getId();
234  if (eventId == "login" && canSubmit())
235  {
236  prepareUpdate();
237  mLoginData->registerLogin = false;
239  }
240  else if (eventId == "server")
241  {
242  close();
243  }
244  else if (eventId == "register")
245  {
247  {
248  prepareUpdate();
250  }
251  else if (!mLoginData->registerUrl.empty())
252  {
253  const std::string &url = mLoginData->registerUrl;
254  urlListener.url = url;
256  // TRANSLATORS: question dialog
257  _("Open register url"),
258  url,
260  false,
261  Modal_true,
262  nullptr);
263  confirmDlg->addActionListener(&urlListener);
264  }
265  }
266  else if (eventId == "customhost")
267  {
270  }
271  else if (eventId == "updateselect")
272  {
275  }
276 }
277 
279 {
280  if (event.isConsumed())
281  {
283  return;
284  }
285 
286  const InputActionT actionId = event.getActionId();
287  if (actionId == InputAction::GUI_CANCEL)
288  {
290  }
291 #ifdef USE_SDL2
292  else if (actionId == InputAction::GUI_SELECT2)
293 #else // USE_SDL2
294  else if (actionId == InputAction::GUI_SELECT ||
295  actionId == InputAction::GUI_SELECT2)
296 #endif // USE_SDL2
297  {
299  }
300  else
301  {
303  }
304 }
305 
307 {
308  return !mUserField->getText().empty() &&
309  !mPassField->getText().empty() &&
311 }
312 
314 {
318  UpdateTypeT updateType = static_cast<UpdateTypeT>(
320 
322  && !mUpdateHostText->getText().empty())
323  {
324  updateType = static_cast<UpdateTypeT>(
325  updateType | UpdateType::Custom);
326  serverConfig.setValue("customUpdateHost",
328 
330  {
333  }
334  else
335  {
336  mLoginData->updateHost.clear();
337  (*mUpdateHost).clear();
338  }
339  }
340  else
341  {
342  std::string str;
343  if (mUpdateHostDropDown != nullptr)
344  {
345  const int sel = mUpdateHostDropDown->getSelected();
346  if (sel >= 0)
347  {
348  const HostsGroup &group = mServer->updateHosts[sel];
349  if (!group.hosts.empty())
350  {
351  str = group.hosts[0];
352  mLoginData->updateHosts = group.hosts;
353  serverConfig.setValue("updateHost2", group.name);
354  }
355  else
356  {
357  serverConfig.setValue("updateHost2", "");
358  }
359  }
360  }
361  else if (mLoginData->updateHost.empty()
362  && !mLoginData->updateHosts.empty())
363  {
364  str = mLoginData->updateHosts[0];
365  serverConfig.setValue("updateHost2", str);
366  }
367  if (!str.empty() && checkPath(str))
368  {
370  *mUpdateHost = STD_MOVE(str);
371  }
372  else
373  {
374  mLoginData->updateHost.clear();
375  (*mUpdateHost).clear();
376  }
377  }
378 
379  mLoginData->updateType = updateType;
380  serverConfig.setValue("updateType", CAST_S32(updateType));
381 
382  mRegisterButton->setEnabled(false);
383  mServerButton->setEnabled(false);
384  mLoginButton->setEnabled(false);
385 
387  if (mLoginData->remember)
389  else
391 }
392 
394 {
396  Window::close();
397 }
398 
400 {
401  HostsGroup group;
402 
403  group.name = mServer->defaultHostName;
404  if (group.name.empty())
405  {
406  // TRANSLATORS: update hosts group default name
407  group.name = _("default updates");
408  }
409  group.hosts = mLoginData->updateHosts;
410  mServer->updateHosts.insert(mServer->updateHosts.begin(), group);
411 }
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:96
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:117
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 std::string BUTTON_SKIN
Definition: button.h:88
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
Net::LoginHandler * loginHandler
Definition: net.cpp:86
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:715
#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:83
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:81
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