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

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