GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/notifymanager.cpp Lines: 1 69 1.4 %
Date: 2021-03-17 Branches: 2 73 2.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "notifymanager.h"
23
24
#include "soundmanager.h"
25
26
#include "being/localplayer.h"
27
28
#include "net/guildhandler.h"
29
#include "net/partyhandler.h"
30
31
#ifdef TMWA_SUPPORT
32
#include "net/tmwa/guildmanager.h"
33
#endif  // TMWA_SUPPORT
34
35
#include "resources/notifications.h"
36
37
#include "resources/db/sounddb.h"
38
39
#include "utils/stringutils.h"
40
41
#include "debug.h"
42
43
namespace NotifyManager
44
{
45
    static ChatTab *getGuildTab()
46
    {
47
        const Guild *const guild = localPlayer->getGuild();
48
        if (guild != nullptr)
49
        {
50
#ifdef TMWA_SUPPORT
51
            if (guild->getServerGuild())
52
                return guildHandler->getTab();
53
            else if (guildManager != nullptr)
54
                return guildManager->getTab();
55
#else  // TMWA_SUPPORT
56
            return guildHandler->getTab();
57
#endif  // TMWA_SUPPORT
58
        }
59
        return nullptr;
60
    }
61
62
    static void chatLog(ChatTab *const tab, const std::string &str)
63
    {
64
        if (str.empty())
65
            return;
66
        if (tab != nullptr)
67
        {
68
            tab->chatLog(str,
69
                ChatMsgType::BY_SERVER,
70
                IgnoreRecord_false,
71
                TryRemoveColors_true);
72
        }
73
        else if (debugChatTab != nullptr)
74
        {
75
            debugChatTab->chatLog(str,
76
                ChatMsgType::BY_SERVER,
77
                IgnoreRecord_false,
78
                TryRemoveColors_true);
79
        }
80
    }
81
82
    void notify(const unsigned int message)
83
    {
84
        if (message >= NotifyTypes::TYPE_END ||
85
            localChatTab == nullptr)
86
        {
87
            return;
88
        }
89
        const NotificationInfo &info = notifications[message];
90
        if (*info.text == 0)
91
        {
92
            soundManager.playSfx(SoundDB::getSound(message), 0, 0);
93
            return;
94
        }
95
96
        switch (info.flags)
97
        {
98
            case NotifyFlags::EMPTY:
99
                localChatTab->chatLog(gettext(info.text),
100
                    ChatMsgType::BY_SERVER,
101
                    IgnoreRecord_false,
102
                    TryRemoveColors_true);
103
                break;
104
105
            case NotifyFlags::GUILD:
106
            {
107
                if (localPlayer == nullptr)
108
                    return;
109
                ChatTab *const tab = getGuildTab();
110
                chatLog(tab, gettext(info.text));
111
                break;
112
            }
113
114
            case NotifyFlags::PARTY:
115
            {
116
                ChatTab *const tab = partyHandler->getTab();
117
                chatLog(tab, gettext(info.text));
118
                break;
119
            }
120
121
            case NotifyFlags::SPEECH:
122
            {
123
                if (localPlayer != nullptr)
124
                    localPlayer->setSpeech(gettext(info.text));
125
                break;
126
            }
127
128
            case NotifyFlags::INT:
129
            case NotifyFlags::STRING:
130
            case NotifyFlags::GUILD_STRING:
131
            case NotifyFlags::PARTY_STRING:
132
            default:
133
                break;
134
        }
135
        soundManager.playSfx(SoundDB::getSound(message), 0, 0);
136
    }
137
138
    void notify(const unsigned int message, const int num)
139
    {
140
        if (message >= NotifyTypes::TYPE_END ||
141
            localChatTab == nullptr)
142
        {
143
            return;
144
        }
145
        const NotificationInfo &info = notifications[message];
146
        if (info.flags == NotifyFlags::INT &&
147
            *info.text != 0)
148
        {
149
            localChatTab->chatLog(
150
                strprintf(gettext(info.text), num),
151
                ChatMsgType::BY_SERVER,
152
                IgnoreRecord_false,
153
                TryRemoveColors_true);
154
        }
155
        soundManager.playSfx(SoundDB::getSound(message), 0, 0);
156
    }
157
158
    void notify(const unsigned int message, const std::string &str)
159
    {
160
        if (message >= NotifyTypes::TYPE_END ||
161
            localChatTab == nullptr)
162
        {
163
            return;
164
        }
165
        const NotificationInfo &info = notifications[message];
166
        if (*info.text == 0)
167
        {
168
            soundManager.playSfx(SoundDB::getSound(message), 0, 0);
169
            return;
170
        }
171
        switch (info.flags)
172
        {
173
            case NotifyFlags::STRING:
174
            {
175
                localChatTab->chatLog(
176
                    strprintf(gettext(info.text), str.c_str()),
177
                    ChatMsgType::BY_SERVER,
178
                    IgnoreRecord_false,
179
                    TryRemoveColors_true);
180
                break;
181
            }
182
            case NotifyFlags::GUILD_STRING:
183
            {
184
                ChatTab *const tab = getGuildTab();
185
                chatLog(tab, strprintf(gettext(info.text), str.c_str()));
186
                break;
187
            }
188
            case NotifyFlags::PARTY_STRING:
189
            {
190
                ChatTab *const tab = partyHandler->getTab();
191
                chatLog(tab, strprintf(gettext(info.text), str.c_str()));
192
                break;
193
            }
194
            case NotifyFlags::EMPTY:
195
            case NotifyFlags::INT:
196
            case NotifyFlags::GUILD:
197
            case NotifyFlags::PARTY:
198
            case NotifyFlags::SPEECH:
199
            default:
200
                break;
201
        }
202
        soundManager.playSfx(SoundDB::getSound(message), 0, 0);
203
    }
204
205
    int getIndexBySound(const std::string &sound)
206
    {
207
        for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
208
        {
209
            if (notifications[f].sound == sound)
210
                return f;
211
        }
212
        return 0;
213
    }
214

3
}  // namespace NotifyManager