mirror-chatterino2/src/channelmanager.cpp

126 lines
3 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(WindowManager &_windowManager, EmoteManager &_emoteManager,
IrcManager &_ircManager)
: windowManager(_windowManager)
, emoteManager(_emoteManager)
, ircManager(_ircManager)
2017-07-23 09:53:50 +02:00
, whispersChannel(new Channel(_windowManager, _emoteManager, _ircManager, "/whispers", true))
, mentionsChannel(new Channel(_windowManager, _emoteManager, _ircManager, "/mentions", true))
, emptyChannel(new Channel(_windowManager, _emoteManager, _ircManager, "", true))
2017-04-12 17:46:44 +02:00
{
}
const std::vector<std::shared_ptr<Channel>> ChannelManager::getItems()
2017-04-12 17:46:44 +02:00
{
2017-07-23 09:53:50 +02:00
QMutexLocker locker(&this->channelsMutex);
2017-04-12 17:46:44 +02:00
std::vector<std::shared_ptr<Channel>> items;
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
for (auto &item : this->channels.values()) {
2017-04-12 17:46:44 +02:00
items.push_back(std::get<0>(item));
}
return items;
}
2017-07-23 09:53:50 +02:00
std::shared_ptr<Channel> ChannelManager::addChannel(const QString &rawChannelName)
2017-04-12 17:46:44 +02:00
{
2017-07-23 09:53:50 +02:00
QString channelName = rawChannelName.toLower();
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
if (channelName.length() > 1 && channelName.at(0) == '/') {
return this->getChannel(channelName);
}
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
if (channelName.length() > 0 && channelName.at(0) == '#') {
channelName = channelName.mid(1);
2017-04-12 17:46:44 +02:00
}
2017-07-23 09:53:50 +02:00
QMutexLocker locker(&this->channelsMutex);
auto it = this->channels.find(channelName);
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
if (it == this->channels.end()) {
auto channel = std::make_shared<Channel>(this->windowManager, this->emoteManager,
this->ircManager, channelName);
this->channels.insert(channelName, std::make_tuple(channel, 1));
2017-04-12 17:46:44 +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)
{
2017-07-23 09:53:50 +02:00
QMutexLocker locker(&this->channelsMutex);
2017-04-12 17:46:44 +02:00
QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") {
2017-07-23 09:53:50 +02:00
return whispersChannel;
2017-04-12 17:46:44 +02:00
}
if (c == "/mentions") {
2017-07-23 09:53:50 +02:00
return mentionsChannel;
2017-04-12 17:46:44 +02:00
}
2017-07-23 09:53:50 +02:00
return emptyChannel;
2017-04-12 17:46:44 +02:00
}
2017-07-23 09:53:50 +02:00
auto a = this->channels.find(c);
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
if (a == this->channels.end()) {
return emptyChannel;
2017-04-12 17:46:44 +02:00
}
return std::get<0>(a.value());
}
void ChannelManager::removeChannel(const QString &channel)
{
2017-07-23 09:53:50 +02:00
QMutexLocker locker(&this->channelsMutex);
2017-04-12 17:46:44 +02:00
if (channel.length() > 1 && channel.at(0) == '/') {
return;
}
QString c = channel.toLower();
2017-07-23 09:53:50 +02:00
auto a = this->channels.find(c);
2017-04-12 17:46:44 +02:00
2017-07-23 09:53:50 +02:00
if (a == this->channels.end()) {
2017-04-12 17:46:44 +02:00
return;
}
std::get<1>(a.value())--;
if (std::get<1>(a.value()) == 0) {
this->ircManager.partChannel(c);
2017-07-23 09:53:50 +02:00
this->channels.remove(c);
2017-04-12 17:46:44 +02:00
}
}
2017-06-15 23:13:01 +02:00
const std::string &ChannelManager::getUserID(const std::string &username)
{
auto it = this->usernameToID.find(username);
/*
if (it != std::end(this->usernameToID)) {
return *it;
}
*/
return "xd";
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino