mirror-chatterino2/channel.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

#include "channel.h"
2017-01-05 16:07:20 +01:00
#include "message.h"
2017-01-07 20:43:55 +01:00
Channel Channel::whispers = Channel(QString("/whispers"));
Channel Channel::mentions = Channel(QString("/mentions"));
2017-01-03 21:19:33 +01:00
QMap<QString, Channel*> Channel::channels = QMap<QString, Channel*>();
Channel::Channel(QString channel)
2017-01-05 20:49:33 +01:00
: m_name((channel.length() > 0 && channel[0] == '#') ? channel.mid(1) : channel)
2017-01-07 20:43:55 +01:00
, m_bttvChannelEmotes()
, m_ffzChannelEmotes()
, m_messages()
, m_messageMutex()
, m_subLink("https://www.twitch.tv/" + m_name + "/subscribe?ref=in_chat_subscriber_link")
, m_channelLink("https://twitch.tv/" + m_name)
, m_popoutPlayerLink("https://player.twitch.tv/?channel=" + m_name)
{
2017-01-07 20:43:55 +01:00
}
2017-01-05 16:07:20 +01:00
2017-01-03 21:19:33 +01:00
Channel* Channel::addChannel(const QString &channel)
{
auto c = getChannel(channel);
if (c == NULL) {
c = new Channel(channel);
channels.insert(channel, c);
return c;
}
c->referenceCount++;
return c;
}
Channel* Channel::getChannel(const QString &channel)
{
if (channel == "/whispers") {
return const_cast<Channel*>(&whispers);
}
if (channel == "/mentions") {
return const_cast<Channel*>(&mentions);
}
auto a = channels.find(channel);
if (a == channels.end()) {
2017-01-05 16:07:20 +01:00
return NULL;
2017-01-03 21:19:33 +01:00
}
2017-01-05 16:07:20 +01:00
return a.value();
2017-01-03 21:19:33 +01:00
}
void Channel::removeChannel(const QString &channel)
{
auto c = getChannel(channel);
if (c == NULL) return;
c->referenceCount--;
if (c->referenceCount == 0) {
channels.remove(channel);
delete c;
}
}
2017-01-07 20:43:55 +01:00
QVector<Message*> Channel::getMessagesClone()
2017-01-05 16:07:20 +01:00
{
2017-01-07 20:43:55 +01:00
m_messageMutex.lock();
QVector M = QVector<Message*>(*m_messages);
m_messageMutex.unlock();
2017-01-05 16:07:20 +01:00
return M;
}
void Channel::addMessage(Message *message)
{
2017-01-07 20:43:55 +01:00
m_messageMutex.lock();
2017-01-05 16:07:20 +01:00
// messages
2017-01-07 20:43:55 +01:00
m_messageMutex.unlock();
2017-01-05 16:07:20 +01:00
}