mirror-chatterino2/src/singletons/channelmanager.cpp

143 lines
3.1 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "singletons/channelmanager.hpp"
#include "singletons/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-12-31 22:58:35 +01:00
namespace singletons {
2017-04-12 17:46:44 +02:00
2017-12-31 00:50:07 +01:00
ChannelManager &ChannelManager::getInstance()
{
static ChannelManager instance;
return instance;
}
2017-12-31 00:50:07 +01:00
ChannelManager::ChannelManager()
: whispersChannel(new Channel("/whispers"))
, mentionsChannel(new Channel("/mentions"))
, emptyChannel(new Channel(""))
2017-04-12 17:46:44 +02:00
{
}
const std::vector<ChannelPtr> 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<ChannelPtr> 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;
}
ChannelPtr 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-31 00:50:07 +01:00
auto channel = std::make_shared<TwitchChannel>(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
2017-12-31 00:50:07 +01:00
this->ircJoin.invoke(channelName);
2017-04-12 17:46:44 +02:00
return channel;
}
std::get<1>(it.value())++;
return std::get<0>(it.value());
}
ChannelPtr 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) {
2017-12-31 00:50:07 +01:00
this->ircPart.invoke(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(ChannelPtr)> 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
2017-12-31 22:58:35 +01:00
}