GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/dialogsmanager.cpp Lines: 3 45 6.7 %
Date: 2018-05-19 03:07:18 Branches: 2 90 2.2 %

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

6
}
218
#else  // DYECMD
219
220
void DialogsManager::attributeChanged(const AttributesT id A_UNUSED,
221
                                      const int64_t oldVal A_UNUSED,
222
                                      const int64_t newVal A_UNUSED)
223
{
224
}
225
#endif  // DYECMD