GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/quitdialog.cpp Lines: 44 99 44.4 %
Date: 2017-11-29 Branches: 44 153 28.8 %

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-2017  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
2
QuitDialog::QuitDialog(QuitDialog **const pointerToMe) :
50
    // TRANSLATORS: quit dialog name
51
2
    Window(_("Quit"), Modal_true, nullptr, "quit.xml"),
52
    ActionListener(),
53
    KeyListener(),
54
    mOptions(),
55
    // TRANSLATORS: quit dialog button
56

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

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

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

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

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

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







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

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

6
}