GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/editserverdialog.cpp Lines: 87 137 63.5 %
Date: 2018-11-12 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-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
22
#include "gui/windows/editserverdialog.h"
23
24
#include "const/net/maxpacketversion.h"
25
26
#include "gui/models/typelistmodel.h"
27
28
#include "gui/windows/okdialog.h"
29
#include "gui/windows/serverdialog.h"
30
31
#include "gui/widgets/button.h"
32
#include "gui/widgets/checkbox.h"
33
#include "gui/widgets/createwidget.h"
34
#include "gui/widgets/dropdown.h"
35
#include "gui/widgets/inttextfield.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
43
1
EditServerDialog::EditServerDialog(ServerDialog *const parent,
44
                                   const ServerInfo &server,
45
1
                                   const int index) :
46
    // TRANSLATORS: edit server dialog name
47
1
    Window(_("Edit Server"), Modal_true, parent, "editserverdialog.xml"),
48
    ActionListener(),
49
    KeyListener(),
50
2
    mServerAddressField(new TextField(this, std::string(),
51

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

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

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

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

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

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

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

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

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

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

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





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

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

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

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

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

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

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

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

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

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

2
    mPortField->setText(toString(mServer.port));
155
2
    mPersistentIp->setSelected(mServer.persistentIp);
156
157
1
    switch (mServer.type)
158
    {
159
        case ServerType::EATHENA:
160
#ifdef TMWA_SUPPORT
161
            mTypeField->setSelected(1);
162
#else   // TMWA_SUPPORT
163
            mTypeField->setSelected(0);
164
#endif  // TMWA_SUPPORT
165
            break;
166
        default:
167
        case ServerType::UNKNOWN:
168
        case ServerType::TMWATHENA:
169
1
            mTypeField->setSelected(0);
170
            break;
171
        case ServerType::EVOL2:
172
#ifdef TMWA_SUPPORT
173
            mTypeField->setSelected(2);
174
#else   // TMWA_SUPPORT
175
            mTypeField->setSelected(1);
176
#endif  // TMWA_SUPPORT
177
            break;
178
    }
179
180
1
    setLocationRelativeTo(getParentWindow());
181
1
}
182
183
5
EditServerDialog::~EditServerDialog()
184
{
185
1
    delete mTypeListModel;
186
2
}
187
188
1
void EditServerDialog::postInit()
189
{
190
1
    Window::postInit();
191
1
    setVisible(Visible_true);
192
1
    mNameField->requestFocus();
193
1
}
194
195
void EditServerDialog::action(const ActionEvent &event)
196
{
197
    const std::string &eventId = event.getId();
198
199
    if (eventId == "ok")
200
    {
201
        // Give focus back to the server dialog.
202
        mServerAddressField->requestFocus();
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"),
218
                DialogType::ERROR,
219
                Modal_true,
220
                ShowCenter_true,
221
                nullptr,
222
                260);
223
            dlg->addActionListener(this);
224
        }
225
        else
226
        {
227
            mCancelButton->setEnabled(false);
228
            mOkButton->setEnabled(false);
229
230
            mServer.name = mNameField->getText();
231
            mServer.description = mDescriptionField->getText();
232
            mServer.onlineListUrl = mOnlineListUrlField->getText();
233
            mServer.hostname = mServerAddressField->getText();
234
            mServer.packetVersion = mPacketVersionField->getValue();
235
            mServer.port = CAST_S16(atoi(
236
                mPortField->getText().c_str()));
237
            mServer.persistentIp = mPersistentIp->isSelected();
238
239
            if (mTypeField != nullptr)
240
            {
241
                switch (mTypeField->getSelected())
242
                {
243
#ifdef TMWA_SUPPORT
244
                    case 0:
245
                        mServer.type = ServerType::TMWATHENA;
246
                        break;
247
                    case 1:
248
                        mServer.type = ServerType::EATHENA;
249
                        break;
250
                    case 2:
251
                        mServer.type = ServerType::EVOL2;
252
                        break;
253
#else  // TMWA_SUPPORT
254
255
                    case 0:
256
                        mServer.type = ServerType::EATHENA;
257
                        break;
258
                    case 1:
259
                        mServer.type = ServerType::EVOL2;
260
                        break;
261
#endif  // TMWA_SUPPORT
262
263
                    default:
264
                        mServer.type = ServerType::UNKNOWN;
265
                        break;
266
                }
267
            }
268
            else
269
            {
270
                mServer.type = ServerType::TMWATHENA;
271
            }
272
273
            // Tell the server has to be saved
274
            mServer.save = true;
275
276
            // Add server
277
            mServerDialog->updateServer(mServer, mIndex);
278
            if (eventId == "connect")
279
                mServerDialog->connectToSelectedServer();
280
            scheduleDelete();
281
        }
282
    }
283
    else if (eventId == "cancel")
284
    {
285
        scheduleDelete();
286
    }
287
}
288
289
void EditServerDialog::keyPressed(KeyEvent &event)
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
    {
303
        action(ActionEvent(nullptr, mOkButton->getActionEventId()));
304
    }
305

3
}