mirror-chatterino2/src/channelmanager.cpp

123 lines
2.5 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "channelmanager.hpp"
#include "ircmanager.hpp"
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
ChannelManager ChannelManager::instance;
ChannelManager::ChannelManager()
: _channels()
, _channelsMutex()
, _whispers(new Channel(QString("/whispers")))
, _mentions(new Channel(QString("/mentions")))
, _empty(new Channel(QString("")))
{
}
std::shared_ptr<Channel> ChannelManager::getWhispers()
2017-04-12 17:46:44 +02:00
{
return _whispers;
}
std::shared_ptr<Channel> ChannelManager::getMentions()
2017-04-12 17:46:44 +02:00
{
return _mentions;
}
std::shared_ptr<Channel> ChannelManager::getEmpty()
2017-04-12 17:46:44 +02:00
{
return _empty;
}
const std::vector<std::shared_ptr<Channel>> ChannelManager::getItems()
2017-04-12 17:46:44 +02:00
{
QMutexLocker locker(&_channelsMutex);
std::vector<std::shared_ptr<Channel>> items;
2017-04-12 17:46:44 +02:00
for (auto &item : _channels.values()) {
items.push_back(std::get<0>(item));
}
return items;
}
std::shared_ptr<Channel> ChannelManager::addChannel(const QString &channel)
2017-04-12 17:46:44 +02:00
{
QMutexLocker locker(&_channelsMutex);
QString channelName = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
return getChannel(channel);
}
auto it = _channels.find(channelName);
if (it == _channels.end()) {
auto channel = std::shared_ptr<Channel>(new Channel(channelName));
2017-04-12 17:46:44 +02:00
_channels.insert(channelName, std::make_tuple(channel, 1));
2017-06-06 16:06:13 +02:00
IrcManager::getInstance().joinChannel(channelName);
2017-04-12 17:46:44 +02:00
return channel;
}
std::get<1>(it.value())++;
return std::get<0>(it.value());
}
std::shared_ptr<Channel> ChannelManager::getChannel(const QString &channel)
{
QMutexLocker locker(&_channelsMutex);
QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") {
return _whispers;
}
if (c == "/mentions") {
return _mentions;
}
return _empty;
}
auto a = _channels.find(c);
if (a == _channels.end()) {
return _empty;
}
return std::get<0>(a.value());
}
void ChannelManager::removeChannel(const QString &channel)
{
QMutexLocker locker(&_channelsMutex);
if (channel.length() > 1 && channel.at(0) == '/') {
return;
}
QString c = channel.toLower();
auto a = _channels.find(c);
if (a == _channels.end()) {
return;
}
std::get<1>(a.value())--;
if (std::get<1>(a.value()) == 0) {
IrcManager::getInstance().partChannel(c);
_channels.remove(c);
}
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino