GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/editserverdialog.cpp Lines: 81 137 59.1 %
Date: 2017-11-29 Branches: 110 273 40.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2012  The Mana Developers
4
 *  Copyright (C) 2012-2017  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
2
EditServerDialog::EditServerDialog(ServerDialog *const parent,
44
                                   const ServerInfo &server,
45
2
                                   const int index) :
46
    // TRANSLATORS: edit server dialog name
47
2
    Window(_("Edit Server"), Modal_true, parent),
48
    ActionListener(),
49
    KeyListener(),
50

6
    mServerAddressField(new TextField(this, std::string())),
51

6
    mPortField(new TextField(this, std::string())),
52

6
    mNameField(new TextField(this, std::string())),
53

6
    mDescriptionField(new TextField(this, std::string())),
54

6
    mOnlineListUrlField(new TextField(this, std::string())),
55

2
    mPacketVersionField(new IntTextField(this, 0, 0, maxPacketVersion)),
56
    // TRANSLATORS: edit server dialog button
57

4
    mConnectButton(new Button(this, _("Connect"), "connect", this)),
58
    // TRANSLATORS: edit server dialog button
59

4
    mOkButton(new Button(this, _("OK"), "addServer", this)),
60
    // TRANSLATORS: edit server dialog button
61

4
    mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
62
    // TRANSLATORS: edit server dialog label
63
2
    mPersistentIp(new CheckBox(this, _("Use same ip"),
64

2
                  true, this, "persistentIp")),
65
4
    mTypeListModel(new TypeListModel),
66

2
    mTypeField(new DropDown(this, mTypeListModel, false, Modal_true)),
67
    mServerDialog(parent),
68
    mServer(server),
69









136
    mIndex(index)
70
{
71
10
    setWindowName("EditServerDialog");
72
73
    // TRANSLATORS: edit server dialog label
74

8
    Label *const nameLabel = new Label(this, _("Name:"));
75
    // TRANSLATORS: edit server dialog label
76

8
    Label *const serverAdressLabel = new Label(this, _("Address:"));
77
    // TRANSLATORS: edit server dialog label
78

8
    Label *const portLabel = new Label(this, _("Port:"));
79
    // TRANSLATORS: edit server dialog label
80

8
    Label *const typeLabel = new Label(this, _("Server type:"));
81
    // TRANSLATORS: edit server dialog label
82

8
    Label *const descriptionLabel = new Label(this, _("Description:"));
83
    // TRANSLATORS: edit server dialog label
84

8
    Label *const onlineListUrlLabel = new Label(this, _("Online list url:"));
85
    // TRANSLATORS: edit server dialog label
86

8
    Label *const packetVersionLabel = new Label(this, _("Packet version:"));
87
2
    mPortField->setNumeric(true);
88
4
    mPortField->setRange(1, 65535);
89
90
2
    mTypeField->setSelected(0);  // TmwAthena by default
91
92
2
    mServerAddressField->addActionListener(this);
93
2
    mPortField->addActionListener(this);
94
95
2
    place(0, 0, nameLabel);
96
4
    place(1, 0, mNameField, 4).setPadding(3);
97
2
    place(0, 1, serverAdressLabel);
98
4
    place(1, 1, mServerAddressField, 4).setPadding(3);
99
2
    place(0, 2, portLabel);
100
4
    place(1, 2, mPortField, 4).setPadding(3);
101
2
    place(0, 3, typeLabel);
102

4
    place(1, 3, mTypeField).setPadding(3);
103
2
    place(0, 4, descriptionLabel);
104
4
    place(1, 4, mDescriptionField, 4).setPadding(3);
105
2
    place(0, 5, onlineListUrlLabel);
106
4
    place(1, 5, mOnlineListUrlField, 4).setPadding(3);
107
2
    place(0, 6, packetVersionLabel);
108
4
    place(1, 6, mPacketVersionField, 4).setPadding(3);
109
4
    place(0, 7, mPersistentIp, 4).setPadding(3);
110
2
    place(0, 8, mConnectButton);
111
2
    place(4, 8, mOkButton);
112
2
    place(3, 8, mCancelButton);
113
114
    // Do this manually instead of calling reflowLayout so we can enforce a
115
    // minimum width.
116
2
    int width = 0;
117
2
    int height = 0;
118

2
    getLayout().reflow(width, height);
119
2
    if (width < 300)
120
    {
121
        width = 300;
122
        getLayout().reflow(width, height);
123
    }
124
2
    if (height < 120)
125
    {
126
        height = 120;
127
        getLayout().reflow(width, height);
128
    }
129
130
2
    setContentSize(width, height);
131
132
4
    setMinWidth(getWidth());
133
4
    setMinHeight(getHeight());
134
6
    setDefaultSize(getWidth(), getHeight(), ImagePosition::CENTER);
135
136
2
    setResizable(false);
137
2
    addKeyListener(this);
138
139
2
    loadWindowState();
140
141
2
    mNameField->setText(mServer.name);
142
2
    mDescriptionField->setText(mServer.description);
143
2
    mOnlineListUrlField->setText(mServer.onlineListUrl);
144
2
    mServerAddressField->setText(mServer.hostname);
145
2
    mPacketVersionField->setValue(mServer.packetVersion);
146

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

6
}