mirror-chatterino2/src/singletons/channelmanager.cpp
Rasmus Karlsson 556dbe0456 Fix whisper receiving
Added setting to display them inline under "Special Channels" page

Store a twitch users color in the TwitchUser struct
this is useful if we ever want to use the users own color.
The users own color is only updated once he has written once in chat

Add helper method for calling function only on normal channels

Fixes #54
2018-02-04 16:33:46 +01:00

149 lines
3.2 KiB
C++

#include "singletons/channelmanager.hpp"
#include "singletons/ircmanager.hpp"
using namespace chatterino::twitch;
namespace chatterino {
namespace singletons {
ChannelManager &ChannelManager::getInstance()
{
static ChannelManager instance;
return instance;
}
ChannelManager::ChannelManager()
: whispersChannel(new Channel("/whispers"))
, mentionsChannel(new Channel("/mentions"))
, emptyChannel(new Channel(""))
{
}
const std::vector<ChannelPtr> ChannelManager::getItems()
{
QMutexLocker locker(&this->channelsMutex);
std::vector<ChannelPtr> items;
for (auto &item : this->twitchChannels.values()) {
items.push_back(std::get<0>(item));
}
return items;
}
ChannelPtr ChannelManager::addTwitchChannel(const QString &rawChannelName)
{
QString channelName = rawChannelName.toLower();
if (channelName.length() > 1 && channelName.at(0) == '/') {
return this->getTwitchChannel(channelName);
}
if (channelName.length() > 0 && channelName.at(0) == '#') {
channelName = channelName.mid(1);
}
QMutexLocker locker(&this->channelsMutex);
auto it = this->twitchChannels.find(channelName);
if (it == this->twitchChannels.end()) {
auto channel = std::make_shared<TwitchChannel>(channelName);
this->twitchChannels.insert(channelName, std::make_tuple(channel, 1));
this->ircJoin.invoke(channelName);
return channel;
}
std::get<1>(it.value())++;
return std::get<0>(it.value());
}
ChannelPtr ChannelManager::getTwitchChannel(const QString &channel)
{
QMutexLocker locker(&this->channelsMutex);
QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") {
return whispersChannel;
}
if (c == "/mentions") {
return mentionsChannel;
}
return emptyChannel;
}
auto a = this->twitchChannels.find(c);
if (a == this->twitchChannels.end()) {
return emptyChannel;
}
return std::get<0>(a.value());
}
void ChannelManager::removeTwitchChannel(const QString &channel)
{
QMutexLocker locker(&this->channelsMutex);
if (channel.length() > 1 && channel.at(0) == '/') {
return;
}
QString c = channel.toLower();
auto a = this->twitchChannels.find(c);
if (a == this->twitchChannels.end()) {
return;
}
std::get<1>(a.value())--;
if (std::get<1>(a.value()) == 0) {
this->ircPart.invoke(c);
this->twitchChannels.remove(c);
}
}
const std::string &ChannelManager::getUserID(const std::string &username)
{
/* TODO: Implement
auto it = this->usernameToID.find(username);
if (it != std::end(this->usernameToID)) {
return *it;
}
*/
static std::string temporary = "xd";
return temporary;
}
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);
}
void ChannelManager::doOnAllNormalChannels(std::function<void(ChannelPtr)> func)
{
for (const auto &channel : this->twitchChannels) {
func(std::get<0>(channel));
}
}
} // namespace chatterino
}