mirror-chatterino2/channels.cpp

83 lines
1.6 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-18 04:33:30 +01:00
Channel Channels::whispers(QString("/whispers"));
Channel Channels::mentions(QString("/mentions"));
Channel Channels::empty(QString(""));
2017-01-18 04:33:30 +01:00
QMap<QString, std::tuple<Channel *, int>> Channels::channels;
Channel *
Channels::addChannel(const QString &channel)
{
2017-01-17 00:15:44 +01:00
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()) {
2017-01-17 00:15:44 +01:00
auto _c = new Channel(c);
2017-01-18 04:33:30 +01:00
Channels::channels.insert(c, std::tuple<Channel *, int>(_c, 1));
2017-01-17 00:15:44 +01:00
IrcManager::joinChannel(c);
return _c;
} else {
std::get<1>(a.value())++;
}
2017-01-17 00:15:44 +01:00
return std::get<0>(a.value());
}
Channel *
Channels::getChannel(const QString &channel)
{
2017-01-17 00:15:44 +01:00
QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") {
2017-01-18 04:33:30 +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-18 04:33:30 +01:00
return &Channels::mentions;
2017-01-17 00:15:44 +01:00
}
2017-01-18 04:33:30 +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()) {
return NULL;
}
return std::get<0>(a.value());
}
void
Channels::removeChannel(const QString &channel)
{
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);
delete std::get<0>(a.value());
}
}
2017-01-18 21:30:23 +01:00
}