mirror-chatterino2/src/channelmanager.cpp

140 lines
3.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
using namespace chatterino::twitch;
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
ChannelManager *ChannelManager::instance = nullptr;
2017-12-17 02:18:13 +01:00
ChannelManager::ChannelManager(WindowManager &_windowManager, IrcManager &_ircManager)
: windowManager(_windowManager)
, ircManager(_ircManager)
2017-12-17 02:18:13 +01:00
, whispersChannel(new TwitchChannel(_ircManager, "/whispers", true))
, mentionsChannel(new TwitchChannel(_ircManager, "/mentions", true))
, emptyChannel(new TwitchChannel(_ircManager, "", true))
2017-04-12 17:46:44 +02:00
{
ChannelManager::instance = this;
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
for (auto &item : this->twitchChannels.values()) {
2017-04-12 17:46:44 +02:00
items.push_back(std::get<0>(item));
}
return items;
}
std::shared_ptr<TwitchChannel> ChannelManager::addTwitchChannel(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->getTwitchChannel(channelName);
2017-07-23 09:53:50 +02:00
}
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->twitchChannels.find(channelName);
if (it == this->twitchChannels.end()) {
2017-12-17 02:18:13 +01:00
auto channel = std::make_shared<TwitchChannel>(this->ircManager, channelName);
2017-04-12 17:46:44 +02:00
this->twitchChannels.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<TwitchChannel> ChannelManager::getTwitchChannel(const QString &channel)
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
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
}
auto a = this->twitchChannels.find(c);
2017-04-12 17:46:44 +02:00
if (a == this->twitchChannels.end()) {
2017-07-23 09:53:50 +02:00
return emptyChannel;
2017-04-12 17:46:44 +02:00
}
return std::get<0>(a.value());
}
void ChannelManager::removeTwitchChannel(const QString &channel)
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
if (channel.length() > 1 && channel.at(0) == '/') {
return;
}
QString c = channel.toLower();
auto a = this->twitchChannels.find(c);
2017-04-12 17:46:44 +02:00
if (a == this->twitchChannels.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);
this->twitchChannels.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)
{
2017-08-12 15:58:46 +02:00
/* TODO: Implement
2017-06-15 23:13:01 +02:00
auto it = this->usernameToID.find(username);
if (it != std::end(this->usernameToID)) {
return *it;
}
*/
2017-08-12 15:58:46 +02:00
static std::string temporary = "xd";
return temporary;
2017-06-15 23:13:01 +02:00
}
void ChannelManager::doOnAll(std::function<void(std::shared_ptr<TwitchChannel>)> func)
{
for (const auto &channel : this->twitchChannels) {
func(std::get<0>(channel));
}
func(this->whispersChannel);
func(this->mentionsChannel);
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino