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
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
ChannelManager::ChannelManager(WindowManager &_windowManager, EmoteManager &_emoteManager,
|
|
|
|
IrcManager &_ircManager)
|
|
|
|
: windowManager(_windowManager)
|
|
|
|
, emoteManager(_emoteManager)
|
|
|
|
, ircManager(_ircManager)
|
|
|
|
, _whispers(new Channel(_windowManager, _emoteManager, _ircManager, "/whispers", true))
|
|
|
|
, _mentions(new Channel(_windowManager, _emoteManager, _ircManager, "/mentions", true))
|
|
|
|
, _empty(new Channel(_windowManager, _emoteManager, _ircManager, QString(), true))
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
std::shared_ptr<Channel> ChannelManager::getWhispers()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _whispers;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
std::shared_ptr<Channel> ChannelManager::getMentions()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _mentions;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
std::shared_ptr<Channel> ChannelManager::getEmpty()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _empty;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
const std::vector<std::shared_ptr<Channel>> ChannelManager::getItems()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&_channelsMutex);
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
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()) {
|
2017-06-13 21:13:58 +02:00
|
|
|
auto channel = std::shared_ptr<Channel>(
|
|
|
|
new Channel(this->windowManager, this->emoteManager, this->ircManager, channelName));
|
2017-04-12 17:46:44 +02:00
|
|
|
_channels.insert(channelName, std::make_tuple(channel, 1));
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
this->ircManager.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) {
|
2017-06-13 21:13:58 +02:00
|
|
|
this->ircManager.partChannel(c);
|
2017-04-12 17:46:44 +02:00
|
|
|
_channels.remove(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|