GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/editserverdialog.cpp Lines: 87 137 63.5 %
Date: 2021-03-17 Branches: 103 261 39.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2012  The Mana Developers
4
 *  Copyright (C) 2012-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
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
23
#include "gui/windows/editserverdialog.h"
24
25
#include "const/net/maxpacketversion.h"
26
27
#include "gui/models/typelistmodel.h"
28
29
#include "gui/windows/okdialog.h"
30
#include "gui/windows/serverdialog.h"
31
32
#include "gui/widgets/button.h"
33
#include "gui/widgets/checkbox.h"
34
#include "gui/widgets/createwidget.h"
35
#include "gui/widgets/dropdown.h"
36
#include "gui/widgets/inttextfield.h"
37
#include "gui/widgets/label.h"
38
#include "gui/widgets/layout.h"
39
40
#include "utils/gettext.h"
41
42
#include "debug.h"
43
44
1
EditServerDialog::EditServerDialog(ServerDialog *const parent,
45
                                   const ServerInfo &server,
46
1
                                   const int index) :
47
    // TRANSLATORS: edit server dialog name
48
1
    Window(_("Edit Server"), Modal_true, parent, "editserverdialog.xml"),
49
    ActionListener(),
50
    KeyListener(),
51
2
    mServerAddressField(new TextField(this, std::string(),
52

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
53
2
    mPortField(new TextField(this, std::string(),
54

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
55
2
    mNameField(new TextField(this, std::string(),
56

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
57
2
    mDescriptionField(new TextField(this, std::string(),
58

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
59
2
    mOnlineListUrlField(new TextField(this, std::string(),
60

2
        LoseFocusOnTab_true, nullptr, std::string(), false)),
61
    mPacketVersionField(new IntTextField(this, 0, 0, maxPacketVersion,
62

1
        Enable_true, 0)),
63
    // TRANSLATORS: edit server dialog button
64
1
    mConnectButton(new Button(this, _("Connect"), "connect",
65

1
        BUTTON_SKIN, this)),
66
    // TRANSLATORS: edit server dialog button
67

2
    mOkButton(new Button(this, _("OK"), "addServer", BUTTON_SKIN, this)),
68
    // TRANSLATORS: edit server dialog button
69

2
    mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
70
    // TRANSLATORS: edit server dialog label
71
1
    mPersistentIp(new CheckBox(this, _("Use same ip"),
72

1
        true, this, "persistentIp")),
73
2
    mTypeListModel(new TypeListModel),
74
1
    mTypeField(new DropDown(this, mTypeListModel,
75

3
        false, Modal_true, nullptr, std::string())),
76
    mServerDialog(parent),
77
    mServer(server),
78





51
    mIndex(index)
79
{
80
5
    setWindowName("EditServerDialog");
81
82
    // TRANSLATORS: edit server dialog label
83

4
    Label *const nameLabel = new Label(this, _("Name:"));
84
    // TRANSLATORS: edit server dialog label
85

4
    Label *const serverAdressLabel = new Label(this, _("Address:"));
86
    // TRANSLATORS: edit server dialog label
87

4
    Label *const portLabel = new Label(this, _("Port:"));
88
    // TRANSLATORS: edit server dialog label
89

4
    Label *const typeLabel = new Label(this, _("Server type:"));
90
    // TRANSLATORS: edit server dialog label
91

4
    Label *const descriptionLabel = new Label(this, _("Description:"));
92
    // TRANSLATORS: edit server dialog label
93

4
    Label *const onlineListUrlLabel = new Label(this, _("Online list url:"));
94
    // TRANSLATORS: edit server dialog label
95

4
    Label *const packetVersionLabel = new Label(this, _("Packet version:"));
96
1
    mPortField->setNumeric(true);
97
2
    mPortField->setRange(1, 65535);
98
99
1
    mTypeField->setSelected(0);  // TmwAthena by default
100
101
1
    mServerAddressField->addActionListener(this);
102
1
    mPortField->addActionListener(this);
103
104
1
    place(0, 0, nameLabel, 1, 1);
105
2
    place(1, 0, mNameField, 4, 1).setPadding(3);
106
1
    place(0, 1, serverAdressLabel, 1, 1);
107
2
    place(1, 1, mServerAddressField, 4, 1).setPadding(3);
108
1
    place(0, 2, portLabel, 1, 1);
109
2
    place(1, 2, mPortField, 4, 1).setPadding(3);
110
1
    place(0, 3, typeLabel, 1, 1);
111

2
    place(1, 3, mTypeField, 1, 1).setPadding(3);
112
1
    place(0, 4, descriptionLabel, 1, 1);
113
2
    place(1, 4, mDescriptionField, 4, 1).setPadding(3);
114
1
    place(0, 5, onlineListUrlLabel, 1, 1);
115
2
    place(1, 5, mOnlineListUrlField, 4, 1).setPadding(3);
116
1
    place(0, 6, packetVersionLabel, 1, 1);
117
2
    place(1, 6, mPacketVersionField, 4, 1).setPadding(3);
118
2
    place(0, 7, mPersistentIp, 4, 1).setPadding(3);
119
1
    place(0, 8, mConnectButton, 1, 1);
120
1
    place(4, 8, mOkButton, 1, 1);
121
1
    place(3, 8, mCancelButton, 1, 1);
122
123
    // Do this manually instead of calling reflowLayout so we can enforce a
124
    // minimum width.
125
1
    int width = 0;
126
1
    int height = 0;
127

1
    getLayout().reflow(width, height);
128
1
    if (width < 300)
129
    {
130
        width = 300;
131
        getLayout().reflow(width, height);
132
    }
133
1
    if (height < 120)
134
    {
135
        height = 120;
136
        getLayout().reflow(width, height);
137
    }
138
139
1
    setContentSize(width, height);
140
141
2
    setMinWidth(getWidth());
142
2
    setMinHeight(getHeight());
143
3
    setDefaultSize(getWidth(), getHeight(), ImagePosition::CENTER, 0, 0);
144
145
1
    setResizable(false);
146
1
    addKeyListener(this);
147
148
1
    loadWindowState();
149
150
1
    mNameField->setText(mServer.name);
151
1
    mDescriptionField->setText(mServer.description);
152
1
    mOnlineListUrlField->setText(mServer.onlineListUrl);
153
1
    mServerAddressField->setText(mServer.hostname);
154
1
    mPacketVersionField->setValue(mServer.packetVersion);
155

2
    mPortField->setText(toString(mServer.port));
156
2
    mPersistentIp->setSelected(mServer.persistentIp);
157
158
1
    switch (mServer.type)
159
    {
160
        case ServerType::EATHENA:
161
#ifdef TMWA_SUPPORT
162
            mTypeField->setSelected(1);
163
#else   // TMWA_SUPPORT
164
            mTypeField->setSelected(0);
165
#endif  // TMWA_SUPPORT
166
            break;
167
        default:
168
        case ServerType::UNKNOWN:
169
        case ServerType::TMWATHENA:
170
1
            mTypeField->setSelected(0);
171
            break;
172
        case ServerType::EVOL2:
173
#ifdef TMWA_SUPPORT
174
            mTypeField->setSelected(2);
175
#else   // TMWA_SUPPORT
176
            mTypeField->setSelected(1);
177
#endif  // TMWA_SUPPORT
178
            break;
179
    }
180
181
1
    setLocationRelativeTo(getParentWindow());
182
1
}
183
184
5
EditServerDialog::~EditServerDialog()
185
{
186
1
    delete mTypeListModel;
187
2
}
188
189
1
void EditServerDialog::postInit()
190
{
191
1
    Window::postInit();
192
1
    setVisible(Visible_true);
193
1
    mNameField->requestFocus();
194
1
}
195
196
void EditServerDialog::action(const ActionEvent &event)
197
{
198
    const std::string &eventId = event.getId();
199
200
    if (eventId == "ok")
201
    {
202
        // Give focus back to the server dialog.
203
        mServerAddressField->requestFocus();
204
    }
205
    if (eventId == "addServer" || eventId == "connect")
206
    {
207
        // Check the given information
208
        if (mServerAddressField->getText().empty()
209
            || mPortField->getText().empty())
210
        {
211
            OkDialog *const dlg = CREATEWIDGETR(OkDialog,
212
                // TRANSLATORS: edit server dialog error header
213
                _("Error"),
214
                // TRANSLATORS: edit server dialog error message
215
                _("Please at least type both the address and the port "
216
                  "of the server."),
217
                // TRANSLATORS: ok dialog button
218
                _("OK"),
219
                DialogType::ERROR,
220
                Modal_true,
221
                ShowCenter_true,
222
                nullptr,
223
                260);
224
            dlg->addActionListener(this);
225
        }
226
        else
227
        {
228
            mCancelButton->setEnabled(false);
229
            mOkButton->setEnabled(false);
230
231
            mServer.name = mNameField->getText();
232
            mServer.description = mDescriptionField->getText();
233
            mServer.onlineListUrl = mOnlineListUrlField->getText();
234
            mServer.hostname = mServerAddressField->getText();
235
            mServer.packetVersion = mPacketVersionField->getValue();
236
            mServer.port = CAST_S16(atoi(
237
                mPortField->getText().c_str()));
238
            mServer.persistentIp = mPersistentIp->isSelected();
239
240
            if (mTypeField != nullptr)
241
            {
242
                switch (mTypeField->getSelected())
243
                {
244
#ifdef TMWA_SUPPORT
245
                    case 0:
246
                        mServer.type = ServerType::TMWATHENA;
247
                        break;
248
                    case 1:
249
                        mServer.type = ServerType::EATHENA;
250
                        break;
251
                    case 2:
252
                        mServer.type = ServerType::EVOL2;
253
                        break;
254
#else  // TMWA_SUPPORT
255
256
                    case 0:
257
                        mServer.type = ServerType::EATHENA;
258
                        break;
259
                    case 1:
260
                        mServer.type = ServerType::EVOL2;
261
                        break;
262
#endif  // TMWA_SUPPORT
263
264
                    default:
265
                        mServer.type = ServerType::UNKNOWN;
266
                        break;
267
                }
268
            }
269
            else
270
            {
271
                mServer.type = ServerType::TMWATHENA;
272
            }
273
274
            // Tell the server has to be saved
275
            mServer.save = true;
276
277
            // Add server
278
            mServerDialog->updateServer(mServer, mIndex);
279
            if (eventId == "connect")
280
                mServerDialog->connectToSelectedServer();
281
            scheduleDelete();
282
        }
283
    }
284
    else if (eventId == "cancel")
285
    {
286
        scheduleDelete();
287
    }
288
}
289
290
void EditServerDialog::keyPressed(KeyEvent &event)
291
{
292
    if (event.isConsumed())
293
        return;
294
295
    const InputActionT actionId = event.getActionId();
296
297
    if (actionId == InputAction::GUI_CANCEL)
298
    {
299
        scheduleDelete();
300
    }
301
    else if (actionId == InputAction::GUI_SELECT ||
302
             actionId == InputAction::GUI_SELECT2)
303
    {
304
        action(ActionEvent(nullptr, mOkButton->getActionEventId()));
305
    }
306

3
}