GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/notifymanager.cpp Lines: 1 69 1.4 %
Date: 2017-11-29 Branches: 2 83 2.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  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
            tab->chatLog(str, ChatMsgType::BY_SERVER);
67
        else if (debugChatTab != nullptr)
68
            debugChatTab->chatLog(str, ChatMsgType::BY_SERVER);
69
    }
70
71
    void notify(const unsigned int message)
72
    {
73
        if (message >= NotifyTypes::TYPE_END ||
74
            localChatTab == nullptr)
75
        {
76
            return;
77
        }
78
        const NotificationInfo &info = notifications[message];
79
        if (*info.text == 0)
80
        {
81
            soundManager.playSfx(SoundDB::getSound(message));
82
            return;
83
        }
84
85
        switch (info.flags)
86
        {
87
            case NotifyFlags::EMPTY:
88
                localChatTab->chatLog(gettext(info.text),
89
                    ChatMsgType::BY_SERVER);
90
                break;
91
92
            case NotifyFlags::GUILD:
93
            {
94
                if (localPlayer == nullptr)
95
                    return;
96
                ChatTab *const tab = getGuildTab();
97
                chatLog(tab, gettext(info.text));
98
                break;
99
            }
100
101
            case NotifyFlags::PARTY:
102
            {
103
                ChatTab *const tab = partyHandler->getTab();
104
                chatLog(tab, gettext(info.text));
105
                break;
106
            }
107
108
            case NotifyFlags::SPEECH:
109
            {
110
                if (localPlayer != nullptr)
111
                    localPlayer->setSpeech(gettext(info.text));
112
                break;
113
            }
114
115
            case NotifyFlags::INT:
116
            case NotifyFlags::STRING:
117
            case NotifyFlags::GUILD_STRING:
118
            case NotifyFlags::PARTY_STRING:
119
            default:
120
                break;
121
        }
122
        soundManager.playSfx(SoundDB::getSound(message));
123
    }
124
125
    void notify(const unsigned int message, const int num)
126
    {
127
        if (message >= NotifyTypes::TYPE_END ||
128
            localChatTab == nullptr)
129
        {
130
            return;
131
        }
132
        const NotificationInfo &info = notifications[message];
133
        if (info.flags == NotifyFlags::INT &&
134
            *info.text != 0)
135
        {
136
            localChatTab->chatLog(strprintf(gettext(info.text),
137
                num), ChatMsgType::BY_SERVER);
138
        }
139
        soundManager.playSfx(SoundDB::getSound(message));
140
    }
141
142
    void notify(const unsigned int message, const std::string &str)
143
    {
144
        if (message >= NotifyTypes::TYPE_END ||
145
            localChatTab == nullptr)
146
        {
147
            return;
148
        }
149
        const NotificationInfo &info = notifications[message];
150
        if (*info.text == 0)
151
        {
152
            soundManager.playSfx(SoundDB::getSound(message));
153
            return;
154
        }
155
        switch (info.flags)
156
        {
157
            case NotifyFlags::STRING:
158
            {
159
                localChatTab->chatLog(strprintf(gettext(info.text),
160
                    str.c_str()), ChatMsgType::BY_SERVER);
161
                break;
162
            }
163
            case NotifyFlags::GUILD_STRING:
164
            {
165
                ChatTab *const tab = getGuildTab();
166
                chatLog(tab, strprintf(gettext(info.text), str.c_str()));
167
                break;
168
            }
169
            case NotifyFlags::PARTY_STRING:
170
            {
171
                ChatTab *const tab = partyHandler->getTab();
172
                chatLog(tab, strprintf(gettext(info.text), str.c_str()));
173
                break;
174
            }
175
            case NotifyFlags::EMPTY:
176
            case NotifyFlags::INT:
177
            case NotifyFlags::GUILD:
178
            case NotifyFlags::PARTY:
179
            case NotifyFlags::SPEECH:
180
            default:
181
                break;
182
        }
183
        soundManager.playSfx(SoundDB::getSound(message));
184
    }
185
186
    int getIndexBySound(const std::string &sound)
187
    {
188
        for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
189
        {
190
            if (notifications[f].sound == sound)
191
                return f;
192
        }
193
        return 0;
194
    }
195

6
}  // namespace NotifyManager