mirror-chatterino2/channels.cpp

107 lines
2.4 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 {
2017-01-30 19:14:25 +01:00
std::shared_ptr<Channel> Channels::whispers(new Channel(QString("/whispers")));
std::shared_ptr<Channel> Channels::mentions(new Channel(QString("/mentions")));
std::shared_ptr<Channel> Channels::empty(new Channel(QString("")));
2017-01-30 19:14:25 +01:00
QMap<QString, std::tuple<std::shared_ptr<Channel>, int>> Channels::channels;
QMutex Channels::channelsMutex;
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") {
2017-01-30 19:14:25 +01:00
return Channels::whispers;
2017-01-17 00:15:44 +01:00
}
2017-01-17 00:15:44 +01:00
if (c == "/mentions") {
2017-01-30 19:14:25 +01:00
return Channels::mentions;
2017-01-17 00:15:44 +01:00
}
2017-01-30 19:14:25 +01:00
return Channels::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()) {
2017-01-30 19:14:25 +01:00
return Channels::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);
}
}
2017-01-18 21:30:23 +01:00
}