mirror-chatterino2/channels.cpp

134 lines
2.7 KiB
C++
Raw Normal View History

#include "channels.h"
2017-01-17 00:15:44 +01:00
#include "ircmanager.h"
2017-01-18 21:30:23 +01:00
namespace chatterino {
static std::shared_ptr<Channel> whispers(nullptr);
static std::shared_ptr<Channel> mentions(nullptr);
static std::shared_ptr<Channel> empty(nullptr);
2017-01-30 19:14:25 +01:00
QMap<QString, std::tuple<std::shared_ptr<Channel>, int>> Channels::channels;
QMutex Channels::channelsMutex;
void
initChannels()
{
whispers.reset(new Channel(QString("/whispers")));
mentions.reset(new Channel(QString("/mentions")));
empty.reset(new Channel(QString("")));
}
std::shared_ptr<Channel>
Channels::getWhispers()
{
return whispers;
}
std::shared_ptr<Channel>
Channels::getMentions()
{
return mentions;
}
std::shared_ptr<Channel>
Channels::getEmpty()
{
return empty;
}
2017-01-30 19:14:25 +01:00
const std::vector<std::shared_ptr<Channel>>
Channels::getItems()
{
QMutexLocker locker(&Channels::channelsMutex);
std::vector<std::shared_ptr<Channel>> items;
2017-01-30 19:14:25 +01:00
for (auto &item : Channels::channels.values()) {
items.push_back(std::get<0>(item));
}
return items;
}
std::shared_ptr<Channel>
Channels::addChannel(const QString &channel)
{
2017-01-30 19:14:25 +01:00
QMutexLocker locker(&Channels::channelsMutex);
2017-01-30 19:14:25 +01:00
QString channelName = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
return getChannel(channel);
}
2017-01-30 19:14:25 +01:00
auto it = Channels::channels.find(channelName);
if (it == Channels::channels.end()) {
auto channel = std::shared_ptr<Channel>(new Channel(channelName));
Channels::channels.insert(channelName, std::make_tuple(channel, 1));
2017-01-17 00:15:44 +01:00
2017-01-30 19:14:25 +01:00
IrcManager::sendJoin(channelName);
2017-01-17 00:15:44 +01:00
2017-01-30 19:14:25 +01:00
return channel;
}
2017-01-30 19:14:25 +01:00
std::get<1>(it.value())++;
return std::get<0>(it.value());
}
2017-01-30 19:14:25 +01:00
std::shared_ptr<Channel>
Channels::getChannel(const QString &channel)
{
2017-01-30 19:14:25 +01:00
QMutexLocker locker(&Channels::channelsMutex);
2017-01-17 00:15:44 +01:00
QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") {
return whispers;
2017-01-17 00:15:44 +01:00
}
2017-01-17 00:15:44 +01:00
if (c == "/mentions") {
return mentions;
2017-01-17 00:15:44 +01:00
}
return empty;
}
2017-01-18 04:33:30 +01:00
auto a = Channels::channels.find(c);
2017-01-18 04:33:30 +01:00
if (a == Channels::channels.end()) {
return empty;
}
return std::get<0>(a.value());
}
void
Channels::removeChannel(const QString &channel)
{
2017-01-30 19:14:25 +01:00
QMutexLocker locker(&Channels::channelsMutex);
2017-01-17 00:15:44 +01:00
if (channel.length() > 1 && channel.at(0) == '/') {
return;
}
QString c = channel.toLower();
2017-01-18 04:33:30 +01:00
auto a = Channels::channels.find(c);
2017-01-18 04:33:30 +01:00
if (a == Channels::channels.end()) {
return;
}
std::get<1>(a.value())--;
if (std::get<1>(a.value()) == 0) {
2017-01-17 00:15:44 +01:00
IrcManager::partChannel(c);
2017-01-18 04:33:30 +01:00
Channels::channels.remove(c);
}
}
} // namespace chatterino