GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/dialogsmanager.cpp Lines: 3 54 5.6 %
Date: 2018-09-20 Branches: 2 102 2.0 %

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/dialogsmanager.h"
24
25
#include "configuration.h"
26
#include "settings.h"
27
28
#include "being/playerinfo.h"
29
30
#include "const/sound.h"
31
32
#include "gui/widgets/createwidget.h"
33
34
#include "gui/windows/confirmdialog.h"
35
36
#ifndef DYECMD
37
#include "gui/widgets/selldialog.h"
38
39
#include "gui/windows/buyselldialog.h"
40
#include "gui/windows/buydialog.h"
41
#include "gui/windows/updaterwindow.h"
42
43
#include "listeners/playerpostdeathlistener.h"
44
#endif  // DYECMD
45
46
#include "listeners/weightlistener.h"
47
48
#include "net/inventoryhandler.h"
49
50
#include "resources/db/deaddb.h"
51
#include "resources/db/groupdb.h"
52
53
#include "utils/gettext.h"
54
55
#include "debug.h"
56
57
#ifdef WIN32
58
#undef ERROR
59
#endif  // WIN32
60
61
Window *deathNotice = nullptr;
62
DialogsManager *dialogsManager = nullptr;
63
OkDialog *weightNotice = nullptr;
64
time_t weightNoticeTime = 0;
65
66
#ifndef DYECMD
67
namespace
68
{
69
1
    PlayerPostDeathListener postDeathListener;
70
1
    WeightListener weightListener;
71
}  // namespace
72
#endif  // DYECMD
73
74
DialogsManager::DialogsManager() :
75
    AttributeListener(),
76
    PlayerDeathListener()
77
{
78
}
79
80
void DialogsManager::closeDialogs()
81
{
82
#ifndef DYECMD
83
    NpcDialog::clearDialogs();
84
    BuyDialog::closeAll();
85
    BuySellDialog::closeAll();
86
    NpcDialog::closeAll();
87
    SellDialog::closeAll();
88
    if (inventoryHandler != nullptr)
89
        inventoryHandler->destroyStorage();
90
#endif  // DYECMD
91
92
    if (deathNotice != nullptr)
93
    {
94
        deathNotice->scheduleDelete();
95
        deathNotice = nullptr;
96
    }
97
}
98
99
void DialogsManager::createUpdaterWindow()
100
{
101
#ifndef DYECMD
102
    CREATEWIDGETV(updaterWindow, UpdaterWindow,
103
        settings.updateHost,
104
        settings.oldUpdates,
105
        false,
106
        UpdateType::Normal);
107
#endif  // DYECMD
108
}
109
110
Window *DialogsManager::openErrorDialog(const std::string &header,
111
                                        const std::string &message,
112
                                        const Modal modal)
113
{
114
    if (settings.supportUrl.empty() || config.getBoolValue("hidesupport"))
115
    {
116
        OkDialog *const dialog = CREATEWIDGETR(OkDialog,
117
            header,
118
            message,
119
            // TRANSLATORS: ok dialog button
120
            _("Close"),
121
            DialogType::ERROR,
122
            modal,
123
            ShowCenter_true,
124
            nullptr,
125
            260);
126
        return dialog;
127
    }
128
    ConfirmDialog *const dialog = CREATEWIDGETR(ConfirmDialog,
129
        header,
130
        strprintf("%s %s", message.c_str(),
131
        // TRANSLATORS: error message question
132
        _("Do you want to open support page?")),
133
        SOUND_ERROR,
134
        false,
135
        modal,
136
        nullptr);
137
    return dialog;
138
}
139
140
void DialogsManager::playerDeath()
141
{
142
#ifndef DYECMD
143
    if (deathNotice == nullptr)
144
    {
145
        if (GroupDb::isAllowCommand(ServerCommandType::alive))
146
        {
147
            CREATEWIDGETV(deathNotice, ConfirmDialog,
148
                std::string(),
149
                DeadDB::getRandomString(),
150
                // TRANSLATORS: ok dialog button
151
                _("Revive"),
152
                // TRANSLATORS: ok dialog button
153
                _("GM revive"),
154
                SOUND_REQUEST,
155
                false,
156
                Modal_false,
157
                nullptr);
158
        }
159
        else
160
        {
161
            CREATEWIDGETV(deathNotice, OkDialog,
162
                std::string(),
163
                DeadDB::getRandomString(),
164
                // TRANSLATORS: ok dialog button
165
                _("Revive"),
166
                DialogType::OK,
167
                Modal_false,
168
                ShowCenter_true,
169
                nullptr,
170
                260);
171
        }
172
        deathNotice->addActionListener(&postDeathListener);
173
    }
174
#endif  // DYECMD
175
}
176
177
#ifndef DYECMD
178
void DialogsManager::attributeChanged(const AttributesT id,
179
                                      const int64_t oldVal,
180
                                      const int64_t newVal)
181
{
182
    if (id == Attributes::TOTAL_WEIGHT)
183
    {
184
        if ((weightNotice == nullptr) && config.getBoolValue("weightMsg"))
185
        {
186
            int percent = settings.overweightPercent;
187
            if (percent < 1)
188
                percent = 50;
189
            const int max = PlayerInfo::getAttribute(
190
                Attributes::MAX_WEIGHT) * percent / 100;
191
            const int total = CAST_S32(oldVal);
192
            if (newVal >= max && total < max)
193
            {
194
                weightNoticeTime = cur_time + 5;
195
                CREATEWIDGETV(weightNotice, OkDialog,
196
                    std::string(),
197
                    // TRANSLATORS: weight message
198
                    _("You are carrying more than "
199
                    "half your weight. You are "
200
                    "unable to regain health."),
201
                    // TRANSLATORS: ok dialog button
202
                    _("OK"),
203
                    DialogType::OK,
204
                    Modal_false,
205
                    ShowCenter_true,
206
                    nullptr,
207
                    260);
208
                weightNotice->addActionListener(
209
                    &weightListener);
210
            }
211
            else if (newVal < max && total >= max)
212
            {
213
                weightNoticeTime = cur_time + 5;
214
                CREATEWIDGETV(weightNotice, OkDialog,
215
                    std::string(),
216
                    // TRANSLATORS: weight message
217
                    _("You are carrying less than "
218
                    "half your weight. You "
219
                    "can regain health."),
220
                    // TRANSLATORS: ok dialog button
221
                    _("OK"),
222
                    DialogType::OK,
223
                    Modal_false,
224
                    ShowCenter_true,
225
                    nullptr,
226
                    260);
227
                weightNotice->addActionListener(
228
                    &weightListener);
229
            }
230
        }
231
    }
232

3
}
233
#else  // DYECMD
234
235
void DialogsManager::attributeChanged(const AttributesT id A_UNUSED,
236
                                      const int64_t oldVal A_UNUSED,
237
                                      const int64_t newVal A_UNUSED)
238
{
239
}
240
#endif  // DYECMD