ManaPlus
editserverdialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2012 The Mana Developers
4  * Copyright (C) 2012-2018 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
25 
27 
28 #include "gui/windows/okdialog.h"
30 
31 #include "gui/widgets/button.h"
32 #include "gui/widgets/checkbox.h"
34 #include "gui/widgets/dropdown.h"
36 #include "gui/widgets/label.h"
37 #include "gui/widgets/layout.h"
38 
39 #include "utils/gettext.h"
40 
41 #include "debug.h"
42 
44  const ServerInfo &server,
45  const int index) :
46  // TRANSLATORS: edit server dialog name
47  Window(_("Edit Server"), Modal_true, parent, "editserverdialog.xml"),
49  KeyListener(),
50  mServerAddressField(new TextField(this, std::string(),
51  LoseFocusOnTab_true, nullptr, std::string(), false)),
52  mPortField(new TextField(this, std::string(),
53  LoseFocusOnTab_true, nullptr, std::string(), false)),
54  mNameField(new TextField(this, std::string(),
55  LoseFocusOnTab_true, nullptr, std::string(), false)),
56  mDescriptionField(new TextField(this, std::string(),
57  LoseFocusOnTab_true, nullptr, std::string(), false)),
58  mOnlineListUrlField(new TextField(this, std::string(),
59  LoseFocusOnTab_true, nullptr, std::string(), false)),
60  mPacketVersionField(new IntTextField(this, 0, 0, maxPacketVersion,
61  Enable_true, 0)),
62  // TRANSLATORS: edit server dialog button
63  mConnectButton(new Button(this, _("Connect"), "connect", this)),
64  // TRANSLATORS: edit server dialog button
65  mOkButton(new Button(this, _("OK"), "addServer", this)),
66  // TRANSLATORS: edit server dialog button
67  mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
68  // TRANSLATORS: edit server dialog label
69  mPersistentIp(new CheckBox(this, _("Use same ip"),
70  true, this, "persistentIp")),
71  mTypeListModel(new TypeListModel),
72  mTypeField(new DropDown(this, mTypeListModel,
73  false, Modal_true, nullptr, std::string())),
74  mServerDialog(parent),
75  mServer(server),
76  mIndex(index)
77 {
78  setWindowName("EditServerDialog");
79 
80  // TRANSLATORS: edit server dialog label
81  Label *const nameLabel = new Label(this, _("Name:"));
82  // TRANSLATORS: edit server dialog label
83  Label *const serverAdressLabel = new Label(this, _("Address:"));
84  // TRANSLATORS: edit server dialog label
85  Label *const portLabel = new Label(this, _("Port:"));
86  // TRANSLATORS: edit server dialog label
87  Label *const typeLabel = new Label(this, _("Server type:"));
88  // TRANSLATORS: edit server dialog label
89  Label *const descriptionLabel = new Label(this, _("Description:"));
90  // TRANSLATORS: edit server dialog label
91  Label *const onlineListUrlLabel = new Label(this, _("Online list url:"));
92  // TRANSLATORS: edit server dialog label
93  Label *const packetVersionLabel = new Label(this, _("Packet version:"));
94  mPortField->setNumeric(true);
95  mPortField->setRange(1, 65535);
96 
97  mTypeField->setSelected(0); // TmwAthena by default
98 
101 
102  place(0, 0, nameLabel, 1, 1);
103  place(1, 0, mNameField, 4, 1).setPadding(3);
104  place(0, 1, serverAdressLabel, 1, 1);
105  place(1, 1, mServerAddressField, 4, 1).setPadding(3);
106  place(0, 2, portLabel, 1, 1);
107  place(1, 2, mPortField, 4, 1).setPadding(3);
108  place(0, 3, typeLabel, 1, 1);
109  place(1, 3, mTypeField, 1, 1).setPadding(3);
110  place(0, 4, descriptionLabel, 1, 1);
111  place(1, 4, mDescriptionField, 4, 1).setPadding(3);
112  place(0, 5, onlineListUrlLabel, 1, 1);
113  place(1, 5, mOnlineListUrlField, 4, 1).setPadding(3);
114  place(0, 6, packetVersionLabel, 1, 1);
115  place(1, 6, mPacketVersionField, 4, 1).setPadding(3);
116  place(0, 7, mPersistentIp, 4, 1).setPadding(3);
117  place(0, 8, mConnectButton, 1, 1);
118  place(4, 8, mOkButton, 1, 1);
119  place(3, 8, mCancelButton, 1, 1);
120 
121  // Do this manually instead of calling reflowLayout so we can enforce a
122  // minimum width.
123  int width = 0;
124  int height = 0;
125  getLayout().reflow(width, height);
126  if (width < 300)
127  {
128  width = 300;
129  getLayout().reflow(width, height);
130  }
131  if (height < 120)
132  {
133  height = 120;
134  getLayout().reflow(width, height);
135  }
136 
137  setContentSize(width, height);
138 
142 
143  setResizable(false);
144  addKeyListener(this);
145 
146  loadWindowState();
147 
155 
156  switch (mServer.type)
157  {
158  case ServerType::EATHENA:
159 #ifdef TMWA_SUPPORT
161 #else // TMWA_SUPPORT
163 #endif // TMWA_SUPPORT
164  break;
165  default:
166  case ServerType::UNKNOWN:
169  break;
170  case ServerType::EVOL2:
171 #ifdef TMWA_SUPPORT
173 #else // TMWA_SUPPORT
175 #endif // TMWA_SUPPORT
176  break;
177  }
178 
180 }
181 
183 {
184  delete mTypeListModel;
185 }
186 
188 {
192 }
193 
195 {
196  const std::string &eventId = event.getId();
197 
198  if (eventId == "ok")
199  {
200  // Give focus back to the server dialog.
202  }
203  if (eventId == "addServer" || eventId == "connect")
204  {
205  // Check the given information
206  if (mServerAddressField->getText().empty()
207  || mPortField->getText().empty())
208  {
209  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
210  // TRANSLATORS: edit server dialog error header
211  _("Error"),
212  // TRANSLATORS: edit server dialog error message
213  _("Please at least type both the address and the port "
214  "of the server."),
215  // TRANSLATORS: ok dialog button
216  _("OK"),
218  Modal_true,
220  nullptr,
221  260);
222  dlg->addActionListener(this);
223  }
224  else
225  {
226  mCancelButton->setEnabled(false);
227  mOkButton->setEnabled(false);
228 
234  mServer.port = CAST_S16(atoi(
235  mPortField->getText().c_str()));
237 
238  if (mTypeField != nullptr)
239  {
240  switch (mTypeField->getSelected())
241  {
242 #ifdef TMWA_SUPPORT
243  case 0:
245  break;
246  case 1:
248  break;
249  case 2:
251  break;
252 #else // TMWA_SUPPORT
253 
254  case 0:
256  break;
257  case 1:
259  break;
260 #endif // TMWA_SUPPORT
261 
262  default:
264  break;
265  }
266  }
267  else
268  {
270  }
271 
272  // Tell the server has to be saved
273  mServer.save = true;
274 
275  // Add server
277  if (eventId == "connect")
279  scheduleDelete();
280  }
281  }
282  else if (eventId == "cancel")
283  {
284  scheduleDelete();
285  }
286 }
287 
289 {
290  if (event.isConsumed())
291  return;
292 
293  const InputActionT actionId = event.getActionId();
294 
295  if (actionId == InputAction::GUI_CANCEL)
296  {
297  scheduleDelete();
298  }
299  else if (actionId == InputAction::GUI_SELECT ||
300  actionId == InputAction::GUI_SELECT2)
301  {
303  }
304 }
TextField * mOnlineListUrlField
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
void setValue(const int value)
int getWidth() const
Definition: widget.h:220
ServerTypeT type
Definition: serverinfo.h:41
void keyPressed(KeyEvent &event)
const bool LoseFocusOnTab_true
const bool Visible_true
Definition: visible.h:29
std::string hostname
Definition: serverinfo.h:44
void loadWindowState()
Definition: window.cpp:1082
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void reflow(int &nW, int &nH)
Definition: layout.cpp:35
void setMinWidth(const int width)
Definition: window.cpp:586
Definition: window.h:98
Definition: button.h:94
DropDown * mTypeField
void setNumeric(const bool numeric)
Definition: textfield.cpp:268
Window * getParentWindow() const
Definition: window.h:309
const std::string & getActionEventId() const
Definition: widget.h:604
const bool Enable_true
Definition: enable.h:29
bool persistentIp
Definition: serverinfo.h:61
void setText(const std::string &text)
Definition: textfield.cpp:802
#define new
Definition: debug_new.h:147
void setContentSize(int width, int height)
Definition: window.cpp:491
virtual void requestFocus()
Definition: widget.cpp:203
static const uint32_t maxPacketVersion
Definition: label.h:87
std::string onlineListUrl
Definition: serverinfo.h:48
TextField * mNameField
uint16_t port
Definition: serverinfo.h:57
void action(const ActionEvent &event)
InputAction ::T InputActionT
Definition: inputaction.h:714
#define nullptr
Definition: localconsts.h:44
TextField * mPortField
const bool ShowCenter_true
Definition: showcenter.h:29
std::string description
Definition: serverinfo.h:46
int getValue() const
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
bool isConsumed() const
void setMinHeight(const int height)
Definition: window.cpp:599
void setEnabled(const bool enabled)
Definition: widget.h:351
LayoutCell & setPadding(int p)
Definition: layoutcell.h:59
virtual void scheduleDelete()
Definition: window.cpp:826
Layout & getLayout()
Definition: window.cpp:1360
TypeListModel * mTypeListModel
TextField * mServerAddressField
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
int packetVersion
Definition: serverinfo.h:59
virtual void setVisible(Visible visible)
Definition: window.cpp:773
std::string name
Definition: serverinfo.h:43
bool save
Definition: serverinfo.h:60
EditServerDialog(ServerDialog *const parent, const ServerInfo &server, const int index)
void connectToSelectedServer()
int getHeight() const
Definition: widget.h:239
ServerDialog * mServerDialog
IntTextField * mPacketVersionField
TextField * mDescriptionField
void setResizable(const bool resize)
Definition: window.cpp:622
void updateServer(const ServerInfo &server, const int index)
#define CAST_S16
Definition: cast.h:27
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
void setRange(const int min, const int max)
Definition: textfield.h:136
void setSelected(const bool selected)
Definition: checkbox.h:155
bool isSelected() const
Definition: checkbox.h:146
void setSelected(int selected)
Definition: dropdown.cpp:514
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:508
const std::string & getText() const
Definition: textfield.h:223
CheckBox * mPersistentIp
int getSelected() const
Definition: dropdown.cpp:509
void postInit()
Definition: window.cpp:248
void setWindowName(const std::string &name)
Definition: window.h:354