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",
64  BUTTON_SKIN, this)),
65  // TRANSLATORS: edit server dialog button
66  mOkButton(new Button(this, _("OK"), "addServer", BUTTON_SKIN, this)),
67  // TRANSLATORS: edit server dialog button
68  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
69  // TRANSLATORS: edit server dialog label
70  mPersistentIp(new CheckBox(this, _("Use same ip"),
71  true, this, "persistentIp")),
72  mTypeListModel(new TypeListModel),
73  mTypeField(new DropDown(this, mTypeListModel,
74  false, Modal_true, nullptr, std::string())),
75  mServerDialog(parent),
76  mServer(server),
77  mIndex(index)
78 {
79  setWindowName("EditServerDialog");
80 
81  // TRANSLATORS: edit server dialog label
82  Label *const nameLabel = new Label(this, _("Name:"));
83  // TRANSLATORS: edit server dialog label
84  Label *const serverAdressLabel = new Label(this, _("Address:"));
85  // TRANSLATORS: edit server dialog label
86  Label *const portLabel = new Label(this, _("Port:"));
87  // TRANSLATORS: edit server dialog label
88  Label *const typeLabel = new Label(this, _("Server type:"));
89  // TRANSLATORS: edit server dialog label
90  Label *const descriptionLabel = new Label(this, _("Description:"));
91  // TRANSLATORS: edit server dialog label
92  Label *const onlineListUrlLabel = new Label(this, _("Online list url:"));
93  // TRANSLATORS: edit server dialog label
94  Label *const packetVersionLabel = new Label(this, _("Packet version:"));
95  mPortField->setNumeric(true);
96  mPortField->setRange(1, 65535);
97 
98  mTypeField->setSelected(0); // TmwAthena by default
99 
102 
103  place(0, 0, nameLabel, 1, 1);
104  place(1, 0, mNameField, 4, 1).setPadding(3);
105  place(0, 1, serverAdressLabel, 1, 1);
106  place(1, 1, mServerAddressField, 4, 1).setPadding(3);
107  place(0, 2, portLabel, 1, 1);
108  place(1, 2, mPortField, 4, 1).setPadding(3);
109  place(0, 3, typeLabel, 1, 1);
110  place(1, 3, mTypeField, 1, 1).setPadding(3);
111  place(0, 4, descriptionLabel, 1, 1);
112  place(1, 4, mDescriptionField, 4, 1).setPadding(3);
113  place(0, 5, onlineListUrlLabel, 1, 1);
114  place(1, 5, mOnlineListUrlField, 4, 1).setPadding(3);
115  place(0, 6, packetVersionLabel, 1, 1);
116  place(1, 6, mPacketVersionField, 4, 1).setPadding(3);
117  place(0, 7, mPersistentIp, 4, 1).setPadding(3);
118  place(0, 8, mConnectButton, 1, 1);
119  place(4, 8, mOkButton, 1, 1);
120  place(3, 8, mCancelButton, 1, 1);
121 
122  // Do this manually instead of calling reflowLayout so we can enforce a
123  // minimum width.
124  int width = 0;
125  int height = 0;
126  getLayout().reflow(width, height);
127  if (width < 300)
128  {
129  width = 300;
130  getLayout().reflow(width, height);
131  }
132  if (height < 120)
133  {
134  height = 120;
135  getLayout().reflow(width, height);
136  }
137 
138  setContentSize(width, height);
139 
143 
144  setResizable(false);
145  addKeyListener(this);
146 
147  loadWindowState();
148 
156 
157  switch (mServer.type)
158  {
159  case ServerType::EATHENA:
160 #ifdef TMWA_SUPPORT
162 #else // TMWA_SUPPORT
164 #endif // TMWA_SUPPORT
165  break;
166  default:
167  case ServerType::UNKNOWN:
170  break;
171  case ServerType::EVOL2:
172 #ifdef TMWA_SUPPORT
174 #else // TMWA_SUPPORT
176 #endif // TMWA_SUPPORT
177  break;
178  }
179 
181 }
182 
184 {
185  delete mTypeListModel;
186 }
187 
189 {
193 }
194 
196 {
197  const std::string &eventId = event.getId();
198 
199  if (eventId == "ok")
200  {
201  // Give focus back to the server dialog.
203  }
204  if (eventId == "addServer" || eventId == "connect")
205  {
206  // Check the given information
207  if (mServerAddressField->getText().empty()
208  || mPortField->getText().empty())
209  {
210  OkDialog *const dlg = CREATEWIDGETR(OkDialog,
211  // TRANSLATORS: edit server dialog error header
212  _("Error"),
213  // TRANSLATORS: edit server dialog error message
214  _("Please at least type both the address and the port "
215  "of the server."),
216  // TRANSLATORS: ok dialog button
217  _("OK"),
219  Modal_true,
221  nullptr,
222  260);
223  dlg->addActionListener(this);
224  }
225  else
226  {
227  mCancelButton->setEnabled(false);
228  mOkButton->setEnabled(false);
229 
235  mServer.port = CAST_S16(atoi(
236  mPortField->getText().c_str()));
238 
239  if (mTypeField != nullptr)
240  {
241  switch (mTypeField->getSelected())
242  {
243 #ifdef TMWA_SUPPORT
244  case 0:
246  break;
247  case 1:
249  break;
250  case 2:
252  break;
253 #else // TMWA_SUPPORT
254 
255  case 0:
257  break;
258  case 1:
260  break;
261 #endif // TMWA_SUPPORT
262 
263  default:
265  break;
266  }
267  }
268  else
269  {
271  }
272 
273  // Tell the server has to be saved
274  mServer.save = true;
275 
276  // Add server
278  if (eventId == "connect")
280  scheduleDelete();
281  }
282  }
283  else if (eventId == "cancel")
284  {
285  scheduleDelete();
286  }
287 }
288 
290 {
291  if (event.isConsumed())
292  return;
293 
294  const InputActionT actionId = event.getActionId();
295 
296  if (actionId == InputAction::GUI_CANCEL)
297  {
298  scheduleDelete();
299  }
300  else if (actionId == InputAction::GUI_SELECT ||
301  actionId == InputAction::GUI_SELECT2)
302  {
304  }
305 }
TextField * mOnlineListUrlField
void setDefaultSize()
Definition: window.cpp:1197
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1383
#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:1086
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:590
Definition: window.h:98
Definition: button.h:96
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
const std::string BUTTON_SKIN
Definition: button.h:88
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:715
#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:603
void setEnabled(const bool enabled)
Definition: widget.h:351
LayoutCell & setPadding(int p)
Definition: layoutcell.h:59
virtual void scheduleDelete()
Definition: window.cpp:830
Layout & getLayout()
Definition: window.cpp:1364
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:777
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:626
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