GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/notifymanager.cpp Lines: 1 71 1.4 %
Date: 2018-06-18 21:15:20 Branches: 2 81 2.5 %

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

6
}  // namespace NotifyManager