2017-06-11 09:31:45 +02:00
|
|
|
#include "channelmanager.hpp"
|
|
|
|
#include "ircmanager.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
using namespace chatterino::twitch;
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
ChannelManager::ChannelManager(WindowManager &_windowManager, EmoteManager &_emoteManager,
|
|
|
|
IrcManager &_ircManager)
|
|
|
|
: windowManager(_windowManager)
|
|
|
|
, emoteManager(_emoteManager)
|
|
|
|
, ircManager(_ircManager)
|
2017-09-16 00:05:06 +02:00
|
|
|
, whispersChannel(new TwitchChannel(_emoteManager, _ircManager, "/whispers", true))
|
|
|
|
, mentionsChannel(new TwitchChannel(_emoteManager, _ircManager, "/mentions", true))
|
|
|
|
, emptyChannel(new TwitchChannel(_emoteManager, _ircManager, "", true))
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
const std::vector<std::shared_ptr<Channel>> ChannelManager::getItems()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
QMutexLocker locker(&this->channelsMutex);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
std::vector<std::shared_ptr<Channel>> items;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
for (auto &item : this->twitchChannels.values()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
items.push_back(std::get<0>(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
std::shared_ptr<TwitchChannel> ChannelManager::addTwitchChannel(const QString &rawChannelName)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
QString channelName = rawChannelName.toLower();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
if (channelName.length() > 1 && channelName.at(0) == '/') {
|
2017-09-16 00:05:06 +02:00
|
|
|
return this->getTwitchChannel(channelName);
|
2017-07-23 09:53:50 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
if (channelName.length() > 0 && channelName.at(0) == '#') {
|
|
|
|
channelName = channelName.mid(1);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
QMutexLocker locker(&this->channelsMutex);
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
auto it = this->twitchChannels.find(channelName);
|
|
|
|
|
|
|
|
if (it == this->twitchChannels.end()) {
|
|
|
|
auto channel =
|
|
|
|
std::make_shared<TwitchChannel>(this->emoteManager, this->ircManager, channelName);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
this->twitchChannels.insert(channelName, std::make_tuple(channel, 1));
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
this->ircManager.joinChannel(channelName);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::get<1>(it.value())++;
|
|
|
|
|
|
|
|
return std::get<0>(it.value());
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
std::shared_ptr<TwitchChannel> ChannelManager::getTwitchChannel(const QString &channel)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
QMutexLocker locker(&this->channelsMutex);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
QString c = channel.toLower();
|
|
|
|
|
|
|
|
if (channel.length() > 1 && channel.at(0) == '/') {
|
|
|
|
if (c == "/whispers") {
|
2017-07-23 09:53:50 +02:00
|
|
|
return whispersChannel;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == "/mentions") {
|
2017-07-23 09:53:50 +02:00
|
|
|
return mentionsChannel;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
return emptyChannel;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
auto a = this->twitchChannels.find(c);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
if (a == this->twitchChannels.end()) {
|
2017-07-23 09:53:50 +02:00
|
|
|
return emptyChannel;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::get<0>(a.value());
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelManager::removeTwitchChannel(const QString &channel)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
QMutexLocker locker(&this->channelsMutex);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
if (channel.length() > 1 && channel.at(0) == '/') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString c = channel.toLower();
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
auto a = this->twitchChannels.find(c);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
if (a == this->twitchChannels.end()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::get<1>(a.value())--;
|
|
|
|
|
|
|
|
if (std::get<1>(a.value()) == 0) {
|
2017-06-13 21:13:58 +02:00
|
|
|
this->ircManager.partChannel(c);
|
2017-09-16 00:05:06 +02:00
|
|
|
this->twitchChannels.remove(c);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 23:13:01 +02:00
|
|
|
const std::string &ChannelManager::getUserID(const std::string &username)
|
|
|
|
{
|
2017-08-12 15:58:46 +02:00
|
|
|
/* TODO: Implement
|
2017-06-15 23:13:01 +02:00
|
|
|
auto it = this->usernameToID.find(username);
|
|
|
|
|
|
|
|
if (it != std::end(this->usernameToID)) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-08-12 15:58:46 +02:00
|
|
|
static std::string temporary = "xd";
|
|
|
|
return temporary;
|
2017-06-15 23:13:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
EmoteManager &ChannelManager::getEmoteManager()
|
|
|
|
{
|
|
|
|
return this->emoteManager;
|
|
|
|
}
|
|
|
|
|
2017-09-17 02:13:57 +02:00
|
|
|
WindowManager &ChannelManager::getWindowManager()
|
|
|
|
{
|
|
|
|
return this->windowManager;
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|