GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/dialogsmanager.cpp Lines: 3 54 5.6 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/dialogsmanager.h"
25
26
#include "configuration.h"
27
#include "settings.h"
28
29
#include "being/playerinfo.h"
30
31
#include "const/sound.h"
32
33
#include "gui/widgets/createwidget.h"
34
35
#include "gui/windows/confirmdialog.h"
36
37
#ifndef DYECMD
38
#include "gui/widgets/selldialog.h"
39
40
#include "gui/windows/buyselldialog.h"
41
#include "gui/windows/buydialog.h"
42
#include "gui/windows/updaterwindow.h"
43
44
#include "listeners/playerpostdeathlistener.h"
45
#endif  // DYECMD
46
47
#include "listeners/weightlistener.h"
48
49
#include "net/inventoryhandler.h"
50
51
#include "resources/db/deaddb.h"
52
#include "resources/db/groupdb.h"
53
54
#include "utils/gettext.h"
55
56
#include "debug.h"
57
58
#ifdef WIN32
59
#undef ERROR
60
#endif  // WIN32
61
62
Window *deathNotice = nullptr;
63
DialogsManager *dialogsManager = nullptr;
64
OkDialog *weightNotice = nullptr;
65
time_t weightNoticeTime = 0;
66
67
#ifndef DYECMD
68
namespace
69
{
70
1
    PlayerPostDeathListener postDeathListener;
71
1
    WeightListener weightListener;
72
}  // namespace
73
#endif  // DYECMD
74
75
DialogsManager::DialogsManager() :
76
    AttributeListener(),
77
    PlayerDeathListener()
78
{
79
}
80
81
void DialogsManager::closeDialogs()
82
{
83
#ifndef DYECMD
84
    NpcDialog::clearDialogs();
85
    BuyDialog::closeAll();
86
    BuySellDialog::closeAll();
87
    NpcDialog::closeAll();
88
    SellDialog::closeAll();
89
    if (inventoryHandler != nullptr)
90
        inventoryHandler->destroyStorage();
91
#endif  // DYECMD
92
93
    if (deathNotice != nullptr)
94
    {
95
        deathNotice->scheduleDelete();
96
        deathNotice = nullptr;
97
    }
98
}
99
100
void DialogsManager::createUpdaterWindow()
101
{
102
#ifndef DYECMD
103
    CREATEWIDGETV(updaterWindow, UpdaterWindow,
104
        settings.updateHost,
105
        settings.oldUpdates,
106
        false,
107
        UpdateType::Normal);
108
#endif  // DYECMD
109
}
110
111
Window *DialogsManager::openErrorDialog(const std::string &header,
112
                                        const std::string &message,
113
                                        const Modal modal)
114
{
115
    if (settings.supportUrl.empty() || config.getBoolValue("hidesupport"))
116
    {
117
        OkDialog *const dialog = CREATEWIDGETR(OkDialog,
118
            header,
119
            message,
120
            // TRANSLATORS: ok dialog button
121
            _("Close"),
122
            DialogType::ERROR,
123
            modal,
124
            ShowCenter_true,
125
            nullptr,
126
            260);
127
        return dialog;
128
    }
129
    ConfirmDialog *const dialog = CREATEWIDGETR(ConfirmDialog,
130
        header,
131
        strprintf("%s %s", message.c_str(),
132
        // TRANSLATORS: error message question
133
        _("Do you want to open support page?")),
134
        SOUND_ERROR,
135
        false,
136
        modal,
137
        nullptr);
138
    return dialog;
139
}
140
141
void DialogsManager::playerDeath()
142
{
143
#ifndef DYECMD
144
    if (deathNotice == nullptr)
145
    {
146
        if (GroupDb::isAllowCommand(ServerCommandType::alive))
147
        {
148
            CREATEWIDGETV(deathNotice, ConfirmDialog,
149
                std::string(),
150
                DeadDB::getRandomString(),
151
                // TRANSLATORS: ok dialog button
152
                _("Revive"),
153
                // TRANSLATORS: ok dialog button
154
                _("GM revive"),
155
                SOUND_REQUEST,
156
                false,
157
                Modal_false,
158
                nullptr);
159
        }
160
        else
161
        {
162
            CREATEWIDGETV(deathNotice, OkDialog,
163
                std::string(),
164
                DeadDB::getRandomString(),
165
                // TRANSLATORS: ok dialog button
166
                _("Revive"),
167
                DialogType::OK,
168
                Modal_false,
169
                ShowCenter_true,
170
                nullptr,
171
                260);
172
        }
173
        deathNotice->addActionListener(&postDeathListener);
174
    }
175
#endif  // DYECMD
176
}
177
178
#ifndef DYECMD
179
void DialogsManager::attributeChanged(const AttributesT id,
180
                                      const int64_t oldVal,
181
                                      const int64_t newVal)
182
{
183
    if (id == Attributes::TOTAL_WEIGHT)
184
    {
185
        if ((weightNotice == nullptr) && config.getBoolValue("weightMsg"))
186
        {
187
            int percent = settings.overweightPercent;
188
            if (percent < 1)
189
                percent = 50;
190
            const int max = PlayerInfo::getAttribute(
191
                Attributes::MAX_WEIGHT) * percent / 100;
192
            const int total = CAST_S32(oldVal);
193
            if (newVal >= max && total < max)
194
            {
195
                weightNoticeTime = cur_time + 5;
196
                CREATEWIDGETV(weightNotice, OkDialog,
197
                    std::string(),
198
                    // TRANSLATORS: weight message
199
                    _("You are carrying more than "
200
                    "half your weight. You are "
201
                    "unable to regain health."),
202
                    // TRANSLATORS: ok dialog button
203
                    _("OK"),
204
                    DialogType::OK,
205
                    Modal_false,
206
                    ShowCenter_true,
207
                    nullptr,
208
                    260);
209
                weightNotice->addActionListener(
210
                    &weightListener);
211
            }
212
            else if (newVal < max && total >= max)
213
            {
214
                weightNoticeTime = cur_time + 5;
215
                CREATEWIDGETV(weightNotice, OkDialog,
216
                    std::string(),
217
                    // TRANSLATORS: weight message
218
                    _("You are carrying less than "
219
                    "half your weight. You "
220
                    "can regain health."),
221
                    // TRANSLATORS: ok dialog button
222
                    _("OK"),
223
                    DialogType::OK,
224
                    Modal_false,
225
                    ShowCenter_true,
226
                    nullptr,
227
                    260);
228
                weightNotice->addActionListener(
229
                    &weightListener);
230
            }
231
        }
232
    }
233

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