GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/quitdialog.cpp Lines: 44 105 41.9 %
Date: 2018-12-09 Branches: 43 155 27.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
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/quitdialog.h"
24
25
#include "client.h"
26
#include "configuration.h"
27
#include "soundmanager.h"
28
29
#include "const/sound.h"
30
31
#include "gui/dialogsmanager.h"
32
#include "gui/viewport.h"
33
34
#include "gui/widgets/containerplacer.h"
35
#include "gui/widgets/button.h"
36
#include "gui/widgets/radiobutton.h"
37
38
#include "net/charserverhandler.h"
39
#include "net/gamehandler.h"
40
41
#include "resources/map/map.h"
42
43
#include "utils/delete2.h"
44
#include "utils/gettext.h"
45
#include "utils/process.h"
46
47
#include "debug.h"
48
49
1
QuitDialog::QuitDialog(QuitDialog **const pointerToMe) :
50
    // TRANSLATORS: quit dialog name
51
1
    Window(_("Quit"), Modal_true, nullptr, "quit.xml"),
52
    ActionListener(),
53
    KeyListener(),
54
    mOptions(),
55
    // TRANSLATORS: quit dialog button
56

2
    mLogoutQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
57
    // TRANSLATORS: quit dialog button
58

2
    mForceQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
59
    mSwitchAccountServer(new RadioButton(this,
60
        // TRANSLATORS: quit dialog button
61

2
        _("Switch server"), "quitdialog", false)),
62
    mSwitchCharacter(new RadioButton(this,
63
        // TRANSLATORS: quit dialog button
64

2
        _("Switch character"), "quitdialog", false)),
65
    mRate(nullptr),
66
    // TRANSLATORS: quit dialog button
67

2
    mOkButton(new Button(this, _("OK"), "ok", BUTTON_SKIN, this)),
68
    // TRANSLATORS: quit dialog button
69

2
    mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
70
    mMyPointer(pointerToMe),
71







52
    mNeedForceQuit(false)
72
{
73
1
    addKeyListener(this);
74
75
1
    ContainerPlacer placer = getPlacer(0, 0);
76
2
    const StateT state = client->getState();
77
1
    mNeedForceQuit = (state == State::CHOOSE_SERVER
78
1
        || state == State::CONNECT_SERVER || state == State::LOGIN
79
        || state == State::PRE_LOGIN || state == State::LOGIN_ATTEMPT
80

1
        || state == State::UPDATE || state == State::LOAD_DATA);
81
82
    // All states, when we're not logged in to someone.
83
1
    if (mNeedForceQuit)
84
    {
85
1
        placeOption(placer, mForceQuit);
86
    }
87
    else
88
    {
89
        // Only added if we are connected to an accountserver or gameserver
90
        placeOption(placer, mLogoutQuit);
91
        placeOption(placer, mSwitchAccountServer);
92
93
        // Only added if we are connected to a gameserver
94
        if (state == State::GAME)
95
            placeOption(placer, mSwitchCharacter);
96
    }
97
98
/*
99
#ifdef ANDROID
100
    if (config.getBoolValue("rated") == false
101
        && config.getIntValue("gamecount") > 3)
102
    {
103
        // TRANSLATORS: rate button
104
        mRate = new RadioButton(this, _("Rate in google play"), "quitdialog",
105
            false);
106
        placeOption(placer, mRate);
107
        mOptions[mOptions.size() - 1]->setSelected(true);
108
    }
109
    else
110
#endif
111
*/
112
    {
113
1
        mOptions[0]->setSelected(true);
114
    }
115
116
1
    placer = getPlacer(0, 1);
117
1
    placer(1, 0, mOkButton, 1, 1);
118
1
    placer(2, 0, mCancelButton, 1, 1);
119
1
    reflowLayout(200, 0);
120
1
}
121
122
1
void QuitDialog::postInit()
123
{
124
1
    Window::postInit();
125
2
    setLocationRelativeTo(getParent());
126
1
    setVisible(Visible_true);
127
1
    soundManager.playGuiSound(SOUND_SHOW_WINDOW);
128
1
    requestModalFocus();
129
1
    mOkButton->requestFocus();
130
1
}
131
132
6
QuitDialog::~QuitDialog()
133
{
134
1
    if (mMyPointer != nullptr)
135
1
        *mMyPointer = nullptr;
136
1
    delete2(mForceQuit)
137
1
    delete2(mLogoutQuit)
138
1
    delete2(mSwitchAccountServer)
139
1
    delete2(mSwitchCharacter)
140
2
}
141
142
1
void QuitDialog::placeOption(ContainerPlacer &placer,
143
                             RadioButton *const option)
144
{
145
2
    placer(0, CAST_S32(mOptions.size()), option, 3, 1);
146
1
    mOptions.push_back(option);
147
1
}
148
149
void QuitDialog::action(const ActionEvent &event)
150
{
151
    soundManager.playGuiSound(SOUND_HIDE_WINDOW);
152
    if (event.getId() == "ok")
153
    {
154
        if (viewport != nullptr)
155
        {
156
            const Map *const map = viewport->getMap();
157
            if (map != nullptr)
158
                map->saveExtraLayer();
159
        }
160
161
        if (mForceQuit->isSelected())
162
        {
163
            client->setState(State::FORCE_QUIT);
164
        }
165
        else if (mLogoutQuit->isSelected())
166
        {
167
            DialogsManager::closeDialogs();
168
            client->setState(State::EXIT);
169
        }
170
        else if ((mRate != nullptr) && mRate->isSelected())
171
        {
172
            openBrowser("https://play.google.com/store/apps/details?"
173
                "id=org.evolonline.beta.manaplus");
174
            config.setValue("rated", true);
175
            if (mNeedForceQuit)
176
            {
177
                client->setState(State::FORCE_QUIT);
178
            }
179
            else
180
            {
181
                DialogsManager::closeDialogs();
182
                client->setState(State::EXIT);
183
            }
184
        }
185
        else if (gameHandler->isConnected()
186
                 && mSwitchAccountServer->isSelected())
187
        {
188
            DialogsManager::closeDialogs();
189
            client->setState(State::SWITCH_SERVER);
190
        }
191
        else if (mSwitchCharacter->isSelected())
192
        {
193
            if (client->getState() == State::GAME)
194
            {
195
                charServerHandler->switchCharacter();
196
                DialogsManager::closeDialogs();
197
                serverConfig.write();
198
            }
199
        }
200
    }
201
    scheduleDelete();
202
}
203
204
void QuitDialog::keyPressed(KeyEvent &event)
205
{
206
    const InputActionT actionId = event.getActionId();
207
    int dir = 0;
208
209
    PRAGMA45(GCC diagnostic push)
210
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
211
    switch (actionId)
212
    {
213
        case InputAction::GUI_SELECT:
214
        case InputAction::GUI_SELECT2:
215
            action(ActionEvent(nullptr, mOkButton->getActionEventId()));
216
            break;
217
        case InputAction::GUI_CANCEL:
218
            action(ActionEvent(nullptr, mCancelButton->getActionEventId()));
219
            break;
220
        case InputAction::GUI_UP:
221
            dir = -1;
222
            break;
223
        case InputAction::GUI_DOWN:
224
            dir = 1;
225
            break;
226
        default:
227
            break;
228
    }
229
    PRAGMA45(GCC diagnostic pop)
230
231
    if (dir != 0)
232
    {
233
        STD_VECTOR<RadioButton*>::const_iterator it = mOptions.begin();
234
        const STD_VECTOR<RadioButton*>::const_iterator
235
            it_end = mOptions.end();
236
237
        for (; it < it_end; ++it)
238
        {
239
            if ((*it)->isSelected())
240
                break;
241
        }
242
243
        if (it == mOptions.end())
244
        {
245
            if (mOptions[0] != nullptr)
246
                mOptions[0]->setSelected(true);
247
            return;
248
        }
249
        else if (it == mOptions.begin() && dir < 0)
250
        {
251
            it = mOptions.end();
252
        }
253
254
        it += dir;
255
256
        if (it == mOptions.end())
257
            it = mOptions.begin();
258
259
        (*it)->setSelected(true);
260
    }
261

3
}