Mana
Loading...
Searching...
No Matches
customserverdialog.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2011-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
23#include "gui/okdialog.h"
24#include "gui/sdlinput.h"
25#include "gui/serverdialog.h"
26
27#include "gui/widgets/button.h"
29#include "gui/widgets/label.h"
30#include "gui/widgets/layout.h"
32
33#include "utils/gettext.h"
34
35std::string TypeListModel::getElementAt(int elementIndex)
36{
37 if (elementIndex == 0)
38 return "TmwAthena";
39#ifdef MANASERV_SUPPORT
40 if (elementIndex == 1)
41 return "ManaServ";
42#endif
43 return "Unknown";
44}
45
47 Window(_("Custom Server"), true, parent),
48 mServerDialog(parent),
49 mIndex(index)
50{
51 setWindowName("CustomServerDialog");
52
53 auto *nameLabel = new Label(_("Name:"));
54 auto *serverAdressLabel = new Label(_("Address:"));
55 auto *portLabel = new Label(_("Port:"));
56#ifdef MANASERV_SUPPORT
57 auto *typeLabel = new Label(_("Server type:"));
58#endif
59 auto *descriptionLabel = new Label(_("Description:"));
60 mServerAddressField = new TextField(std::string());
61 mPortField = new TextField(std::string());
62
63#ifdef MANASERV_SUPPORT
64 mTypeListModel = std::make_unique<TypeListModel>();
65 mTypeField = new DropDown(mTypeListModel.get());
66 mTypeField->setSelected(0); // TmwAthena by default for now.
67#endif
68
69 mNameField = new TextField(std::string());
70 mDescriptionField = new TextField(std::string());
71
72 mOkButton = new Button(_("Ok"), "addServer", this);
73 mCancelButton = new Button(_("Cancel"), "cancel", this);
74
75 mServerAddressField->addActionListener(this);
76 mPortField->addActionListener(this);
77
78 place(0, 0, nameLabel);
79 place(1, 0, mNameField, 4).setPadding(2);
80 place(0, 1, serverAdressLabel);
82 place(0, 2, portLabel);
83 place(1, 2, mPortField, 4).setPadding(2);
84#ifdef MANASERV_SUPPORT
85 place(0, 3, typeLabel);
86 place(1, 3, mTypeField).setPadding(2);
87#endif
88 place(0, 4, descriptionLabel);
90 place(4, 5, mOkButton);
91 place(3, 5, mCancelButton);
92
95
96 addKeyListener(this);
97
98 // Add the entry's info when in modify mode.
99 if (index > -1)
100 {
101 const ServerInfo &serverInfo = mServerDialog->mServers[index];
102 mNameField->setText(serverInfo.name);
103 mDescriptionField->setText(serverInfo.description);
104 mServerAddressField->setText(serverInfo.hostname);
105 mPortField->setText(toString(serverInfo.port));
106#ifdef MANASERV_SUPPORT
107 mTypeField->setSelected(serverInfo.type == ServerType::TmwAthena ?
108 0 : 1);
109#endif
110 }
111
112 setVisible(true);
113
114 mNameField->requestFocus();
115}
116
118
119void CustomServerDialog::action(const gcn::ActionEvent &event)
120{
121 if (event.getId() == "ok")
122 {
123 // Give focus back to the server dialog.
124 mServerAddressField->requestFocus();
125 }
126 if (event.getId() == "addServer")
127 {
128 // Check the given information
129 if (mServerAddressField->getText().empty())
130 {
131 auto *dlg = new OkDialog(_("Error"),
132 _("Please type in at least the address of the server."));
133 dlg->addActionListener(this);
134 }
135 else
136 {
137 mCancelButton->setEnabled(false);
138 mOkButton->setEnabled(false);
139
140 ServerInfo serverInfo;
141 serverInfo.name = mNameField->getText();
142 serverInfo.description = mDescriptionField->getText();
143 serverInfo.hostname = mServerAddressField->getText();
144#ifdef MANASERV_SUPPORT
145 switch (mTypeField->getSelected())
146 {
147 case 0:
148 serverInfo.type = ServerType::TmwAthena;
149 break;
150 case 1:
151 serverInfo.type = ServerType::ManaServ;
152 break;
153 default:
154 serverInfo.type = ServerType::Unknown;
155 }
156#else
157 serverInfo.type = ServerType::TmwAthena;
158#endif
159 if (mPortField->getText().empty())
160 serverInfo.port = ServerInfo::defaultPortForServerType(serverInfo.type);
161 else
162 serverInfo.port = static_cast<uint16_t>(atoi(mPortField->getText().c_str()));
163
164 // Tell the server has to be saved
165 serverInfo.save = true;
166
167 //Add server
170 }
171 }
172 else if (event.getId() == "cancel")
173 {
175 }
176}
177
178void CustomServerDialog::keyPressed(gcn::KeyEvent &keyEvent)
179{
180 gcn::Key key = keyEvent.getKey();
181
182 if (key.getValue() == Key::ESCAPE)
183 {
185 }
186 else if (key.getValue() == Key::ENTER)
187 {
188 action(gcn::ActionEvent(nullptr, mOkButton->getActionEventId()));
189 }
190}
Button widget.
Definition button.h:38
TextField * mServerAddressField
CustomServerDialog(ServerDialog *parent, int index=-1)
TextField * mDescriptionField
~CustomServerDialog() override
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
void keyPressed(gcn::KeyEvent &keyEvent) override
ServerDialog * mServerDialog
A drop down box from which you can select different values.
Definition dropdown.h:34
Label widget.
Definition label.h:34
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
An 'Ok' button dialog.
Definition okdialog.h:34
The server choice dialog.
void saveCustomServers(const ServerInfo &currentServer=ServerInfo(), int index=-1)
Saves the new server entry in the custom server list.
ServerInfos mServers
std::string hostname
Definition serverinfo.h:42
uint16_t port
Definition serverinfo.h:43
ServerType type
Definition serverinfo.h:40
std::string name
Definition serverinfo.h:41
static uint16_t defaultPortForServerType(ServerType type)
Definition serverinfo.h:85
std::string description
Definition serverinfo.h:45
A text field.
Definition textfield.h:72
std::string getElementAt(int elementIndex) override
Used to get an element from the list.
A window.
Definition window.h:59
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
Window * getParentWindow() const
Returns the parent window.
Definition window.h:232
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
void reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
Definition window.cpp:748
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
void scheduleDelete()
Schedule this window for deletion.
Definition window.cpp:299
void setLocationRelativeTo(gcn::Widget *widget)
Sets the location relative to the given widget.
Definition window.cpp:178
#define _(s)
Definition gettext.h:38
@ ENTER
Definition sdlinput.h:78
@ ESCAPE
Definition sdlinput.h:96
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68