GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/chat/whispertab.cpp Lines: 1 42 2.4 %
Date: 2021-03-17 Branches: 0 44 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/widgets/tabs/chat/whispertab.h"
25
26
#include "being/localplayer.h"
27
28
#include "net/chathandler.h"
29
30
#include "gui/windows/chatwindow.h"
31
32
#include "gui/widgets/windowcontainer.h"
33
34
#include "utils/chatutils.h"
35
36
#include "debug.h"
37
38
WhisperTab::WhisperTab(const Widget2 *const widget,
39
                       const std::string &caption,
40
                       const std::string &nick) :
41
    ChatTab(widget, caption, nick, nick, ChatTabType::WHISPER),
42
    mNick(nick)
43
{
44
    setWhisperTabColors();
45
}
46
47
WhisperTab::~WhisperTab()
48
{
49
    if (chatWindow != nullptr)
50
        chatWindow->removeWhisper(mNick);
51
}
52
53
void WhisperTab::handleInput(const std::string &msg)
54
{
55
    std::string newMsg;
56
    newMsg = ChatWindow::doReplace(msg);
57
    chatHandler->privateMessage(mNick, newMsg);
58
59
    if (localPlayer != nullptr)
60
        chatLog(localPlayer->getName(), newMsg);
61
    else
62
        chatLog("?", newMsg);
63
}
64
65
void WhisperTab::handleCommandStr(const std::string &msg)
66
{
67
    if (msg == "close")
68
    {
69
        delete this;
70
        return;
71
    }
72
73
    const size_t pos = msg.find(' ');
74
    const std::string type(msg, 0, pos);
75
76
    if (type == "me")
77
    {
78
        const std::string args(msg, pos == std::string::npos
79
            ? msg.size() : pos + 1);
80
        std::string str = textToMe(args);
81
        chatHandler->privateMessage(mNick, str);
82
        if (localPlayer != nullptr)
83
            chatLog(localPlayer->getName(), str);
84
        else
85
            chatLog("?", str);
86
    }
87
    else
88
    {
89
        ChatTab::handleCommandStr(msg);
90
    }
91
}
92
93
bool WhisperTab::handleCommand(const std::string &restrict type,
94
                               const std::string &restrict args A_UNUSED)
95
{
96
    if (type == "close")
97
    {
98
        if (windowContainer != nullptr)
99
            windowContainer->scheduleDelete(this);
100
        else
101
            delete this;
102
        if (chatWindow != nullptr)
103
            chatWindow->defaultTab();
104
    }
105
    else
106
    {
107
        return false;
108
    }
109
110
    return true;
111
}
112
113
void WhisperTab::getAutoCompleteList(StringVect &names) const
114
{
115
    names.push_back(mNick);
116
}
117
118
void WhisperTab::getAutoCompleteCommands(StringVect& commands) const
119
{
120
    commands.push_back("/close");
121
}
122
123
void WhisperTab::setWhisperTabColors()
124
{
125
    setTabColors(ThemeColorId::WHISPER_TAB);
126
}
127
128
void WhisperTab::setWhisperTabOfflineColors()
129
{
130
    setTabColors(ThemeColorId::WHISPER_TAB_OFFLINE);
131
2
}