From ab814d1e639c0fd32638d69ce9efb506aeee1889 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 23 Jul 2017 09:53:50 +0200 Subject: [PATCH] refactor stuff --- src/application.hpp | 2 +- src/channel.cpp | 44 ++++++----------- src/channel.hpp | 18 +++---- src/channelmanager.cpp | 73 ++++++++++++----------------- src/channelmanager.hpp | 25 +++++----- src/emotemanager.cpp | 11 ++--- src/emotemanager.hpp | 14 ++++-- src/ircmanager.cpp | 4 +- src/twitch/twitchmessagebuilder.cpp | 54 +++++++++++++++------ src/twitch/twitchmessagebuilder.hpp | 2 + src/widgets/accountpopup.cpp | 2 +- src/widgets/chatwidget.cpp | 6 +-- 12 files changed, 123 insertions(+), 132 deletions(-) diff --git a/src/application.hpp b/src/application.hpp index bbe434fbc..0c79f0107 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -2,12 +2,12 @@ #include "channelmanager.hpp" #include "colorscheme.hpp" +#include "completionmanager.hpp" #include "emotemanager.hpp" #include "ircmanager.hpp" #include "messagefactory.hpp" #include "resources.hpp" #include "windowmanager.hpp" -#include "completionmanager.hpp" #include diff --git a/src/channel.cpp b/src/channel.cpp index 04540ad1d..42a41f12a 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -14,27 +14,25 @@ #include #include -#include - using namespace chatterino::messages; namespace chatterino { Channel::Channel(WindowManager &_windowManager, EmoteManager &_emoteManager, - IrcManager &_ircManager, const QString &channel, bool isSpecial) + IrcManager &_ircManager, const QString &channelName, bool isSpecial) : windowManager(_windowManager) , emoteManager(_emoteManager) , ircManager(_ircManager) - , _name((channel.length() > 0 && channel[0] == '#') ? channel.mid(1) : channel) - , bttvChannelEmotes(this->emoteManager.bttvChannels[channel]) - , ffzChannelEmotes(this->emoteManager.ffzChannels[channel]) - , _subLink("https://www.twitch.tv/" + _name + "/subscribe?ref=in_chat_subscriber_link") - , _channelLink("https://twitch.tv/" + _name) - , _popoutPlayerLink("https://player.twitch.tv/?channel=" + _name) + , name(channelName) + , bttvChannelEmotes(this->emoteManager.bttvChannels[channelName]) + , ffzChannelEmotes(this->emoteManager.ffzChannels[channelName]) + , _subLink("https://www.twitch.tv/" + name + "/subscribe?ref=in_chat_subscriber_link") + , _channelLink("https://twitch.tv/" + name) + , _popoutPlayerLink("https://player.twitch.tv/?channel=" + name) // , _loggingChannel(logging::get(_name)) { - qDebug() << "Open channel:" << channel << ". Name: " << _name; - printf("Channel pointer: %p\n", this); + qDebug() << "Open channel:" << this->name; + if (!isSpecial) { this->reloadChannelEmotes(); } @@ -43,24 +41,10 @@ Channel::Channel(WindowManager &_windowManager, EmoteManager &_emoteManager, // // properties // -EmoteManager::EmoteMap &Channel::getBTTVChannelEmotes() -{ - return this->bttvChannelEmotes; -} - -EmoteManager::EmoteMap &Channel::getFFZChannelEmotes() -{ - return this->ffzChannelEmotes; -} bool Channel::isEmpty() const { - return _name.isEmpty(); -} - -const QString &Channel::getName() const -{ - return _name; + return name.isEmpty(); } const QString &Channel::getSubLink() const @@ -126,15 +110,15 @@ void Channel::addMessage(std::shared_ptr message) // private methods void Channel::reloadChannelEmotes() { - printf("[Channel:%s] Reloading channel emotes\n", qPrintable(this->_name)); - this->emoteManager.reloadBTTVChannelEmotes(this->_name); - this->emoteManager.reloadFFZChannelEmotes(this->_name); + printf("[Channel:%s] Reloading channel emotes\n", qPrintable(this->name)); + this->emoteManager.reloadBTTVChannelEmotes(this->name); + this->emoteManager.reloadFFZChannelEmotes(this->name); } void Channel::sendMessage(const QString &message) { qDebug() << "Channel send message: " << message; - this->ircManager.sendMessage(_name, message); + this->ircManager.sendMessage(name, message); } } // namespace chatterino diff --git a/src/channel.hpp b/src/channel.hpp index 9b31df70d..225b1bf0f 100644 --- a/src/channel.hpp +++ b/src/channel.hpp @@ -26,17 +26,12 @@ class Channel { public: explicit Channel(WindowManager &_windowManager, EmoteManager &_emoteManager, - IrcManager &_ircManager, const QString &channel, bool isSpecial = false); + IrcManager &_ircManager, const QString &channelName, bool isSpecial = false); boost::signals2::signal messageRemovedFromStart; boost::signals2::signal messageAppended; - // properties - EmoteManager::EmoteMap &getBTTVChannelEmotes(); - EmoteManager::EmoteMap &getFFZChannelEmotes(); - bool isEmpty() const; - const QString &getName() const; const QString &getSubLink() const; const QString &getChannelLink() const; const QString &getPopoutPlayerLink() const; @@ -53,20 +48,21 @@ public: void sendMessage(const QString &message); std::string roomID; + const QString name; private: WindowManager &windowManager; EmoteManager &emoteManager; IrcManager &ircManager; - // variabeles + // variables messages::LimitedQueue _messages; - QString _name; - - EmoteManager::EmoteMap &bttvChannelEmotes; - EmoteManager::EmoteMap &ffzChannelEmotes; +public: + const EmoteManager::EmoteMap &bttvChannelEmotes; + const EmoteManager::EmoteMap &ffzChannelEmotes; +private: QString _subLink; QString _channelLink; QString _popoutPlayerLink; diff --git a/src/channelmanager.cpp b/src/channelmanager.cpp index d5aa2dd06..3a3dc5e67 100644 --- a/src/channelmanager.cpp +++ b/src/channelmanager.cpp @@ -8,56 +8,45 @@ ChannelManager::ChannelManager(WindowManager &_windowManager, EmoteManager &_emo : windowManager(_windowManager) , emoteManager(_emoteManager) , ircManager(_ircManager) - , _whispers(new Channel(_windowManager, _emoteManager, _ircManager, "/whispers", true)) - , _mentions(new Channel(_windowManager, _emoteManager, _ircManager, "/mentions", true)) - , _empty(new Channel(_windowManager, _emoteManager, _ircManager, QString(), true)) + , whispersChannel(new Channel(_windowManager, _emoteManager, _ircManager, "/whispers", true)) + , mentionsChannel(new Channel(_windowManager, _emoteManager, _ircManager, "/mentions", true)) + , emptyChannel(new Channel(_windowManager, _emoteManager, _ircManager, "", true)) { } -std::shared_ptr ChannelManager::getWhispers() -{ - return _whispers; -} - -std::shared_ptr ChannelManager::getMentions() -{ - return _mentions; -} - -std::shared_ptr ChannelManager::getEmpty() -{ - return _empty; -} - const std::vector> ChannelManager::getItems() { - QMutexLocker locker(&_channelsMutex); + QMutexLocker locker(&this->channelsMutex); std::vector> items; - for (auto &item : _channels.values()) { + for (auto &item : this->channels.values()) { items.push_back(std::get<0>(item)); } return items; } -std::shared_ptr ChannelManager::addChannel(const QString &channel) +std::shared_ptr ChannelManager::addChannel(const QString &rawChannelName) { - QMutexLocker locker(&_channelsMutex); + QString channelName = rawChannelName.toLower(); - QString channelName = channel.toLower(); - - if (channel.length() > 1 && channel.at(0) == '/') { - return getChannel(channel); + if (channelName.length() > 1 && channelName.at(0) == '/') { + return this->getChannel(channelName); } - auto it = _channels.find(channelName); + if (channelName.length() > 0 && channelName.at(0) == '#') { + channelName = channelName.mid(1); + } - if (it == _channels.end()) { - auto channel = std::shared_ptr( - new Channel(this->windowManager, this->emoteManager, this->ircManager, channelName)); - _channels.insert(channelName, std::make_tuple(channel, 1)); + QMutexLocker locker(&this->channelsMutex); + + auto it = this->channels.find(channelName); + + if (it == this->channels.end()) { + auto channel = std::make_shared(this->windowManager, this->emoteManager, + this->ircManager, channelName); + this->channels.insert(channelName, std::make_tuple(channel, 1)); this->ircManager.joinChannel(channelName); @@ -71,26 +60,26 @@ std::shared_ptr ChannelManager::addChannel(const QString &channel) std::shared_ptr ChannelManager::getChannel(const QString &channel) { - QMutexLocker locker(&_channelsMutex); + QMutexLocker locker(&this->channelsMutex); QString c = channel.toLower(); if (channel.length() > 1 && channel.at(0) == '/') { if (c == "/whispers") { - return _whispers; + return whispersChannel; } if (c == "/mentions") { - return _mentions; + return mentionsChannel; } - return _empty; + return emptyChannel; } - auto a = _channels.find(c); + auto a = this->channels.find(c); - if (a == _channels.end()) { - return _empty; + if (a == this->channels.end()) { + return emptyChannel; } return std::get<0>(a.value()); @@ -98,7 +87,7 @@ std::shared_ptr ChannelManager::getChannel(const QString &channel) void ChannelManager::removeChannel(const QString &channel) { - QMutexLocker locker(&_channelsMutex); + QMutexLocker locker(&this->channelsMutex); if (channel.length() > 1 && channel.at(0) == '/') { return; @@ -106,9 +95,9 @@ void ChannelManager::removeChannel(const QString &channel) QString c = channel.toLower(); - auto a = _channels.find(c); + auto a = this->channels.find(c); - if (a == _channels.end()) { + if (a == this->channels.end()) { return; } @@ -116,7 +105,7 @@ void ChannelManager::removeChannel(const QString &channel) if (std::get<1>(a.value()) == 0) { this->ircManager.partChannel(c); - _channels.remove(c); + this->channels.remove(c); } } diff --git a/src/channelmanager.hpp b/src/channelmanager.hpp index e31725718..28c595dd1 100644 --- a/src/channelmanager.hpp +++ b/src/channelmanager.hpp @@ -13,14 +13,14 @@ class IrcManager; class ChannelManager { + WindowManager &windowManager; + EmoteManager &emoteManager; + IrcManager &ircManager; + public: explicit ChannelManager(WindowManager &_windowManager, EmoteManager &_emoteManager, IrcManager &_ircManager); - std::shared_ptr getWhispers(); - std::shared_ptr getMentions(); - std::shared_ptr getEmpty(); - const std::vector> getItems(); std::shared_ptr addChannel(const QString &channel); @@ -29,20 +29,17 @@ public: const std::string &getUserID(const std::string &username); -private: - WindowManager &windowManager; - EmoteManager &emoteManager; - IrcManager &ircManager; + // Special channels + const std::shared_ptr whispersChannel; + const std::shared_ptr mentionsChannel; + const std::shared_ptr emptyChannel; +private: std::map usernameToID; std::map channelDatas; - QMap, int>> _channels; - QMutex _channelsMutex; - - std::shared_ptr _whispers; - std::shared_ptr _mentions; - std::shared_ptr _empty; + QMutex channelsMutex; + QMap, int>> channels; }; } // namespace chatterino diff --git a/src/emotemanager.cpp b/src/emotemanager.cpp index da8c71bcc..438f00ae5 100644 --- a/src/emotemanager.cpp +++ b/src/emotemanager.cpp @@ -115,14 +115,9 @@ ConcurrentMap &EmoteManager::getTwitchEmotes() return _twitchEmotes; } -EmoteManager::EmoteMap &EmoteManager::getBTTVEmotes() -{ - return _bttvEmotes; -} - EmoteManager::EmoteMap &EmoteManager::getFFZEmotes() { - return _ffzEmotes; + return ffzGlobalEmotes; } EmoteManager::EmoteMap &EmoteManager::getChatterinoEmotes() @@ -313,7 +308,7 @@ void EmoteManager::loadBTTVEmotes() tmp.detach(); QString url = tmp.replace("{{id}}", id).replace("{{image}}", "1x"); - EmoteManager::getBTTVEmotes().insert( + this->bttvGlobalEmotes.insert( code, new LazyLoadedImage(*this, this->windowManager, url, 1, code, code + "\nGlobal BTTV Emote")); } @@ -356,7 +351,7 @@ void EmoteManager::loadFFZEmotes() QJsonObject urls = object.value("urls").toObject(); QString url1 = "http:" + urls.value("1").toString(); - EmoteManager::getBTTVEmotes().insert( + this->ffzGlobalEmotes.insert( code, new LazyLoadedImage(*this, this->windowManager, url1, 1, code, code + "\nGlobal FFZ Emote")); } diff --git a/src/emotemanager.hpp b/src/emotemanager.hpp index e6e2a831c..1cce56a2b 100644 --- a/src/emotemanager.hpp +++ b/src/emotemanager.hpp @@ -44,7 +44,6 @@ public: void reloadFFZChannelEmotes(const QString &channelName); ConcurrentMap &getTwitchEmotes(); - EmoteMap &getBTTVEmotes(); EmoteMap &getFFZEmotes(); EmoteMap &getChatterinoEmotes(); EmoteMap &getBTTVChannelEmoteFromCaches(); @@ -91,7 +90,13 @@ public: private: /// Twitch emotes + // username emote code + ConcurrentStdMap> twitchAccountEmotes; + + // emote code ConcurrentMap _twitchEmotes; + + // emote id ConcurrentMap _twitchEmoteFromCache; /// BTTV emotes @@ -99,11 +104,10 @@ private: public: ConcurrentMap bttvChannels; - -private: - EmoteMap _bttvEmotes; + EmoteMap bttvGlobalEmotes; EmoteMap _bttvChannelEmoteFromCaches; +private: void loadBTTVEmotes(); /// FFZ emotes @@ -111,9 +115,9 @@ private: public: ConcurrentMap ffzChannels; + EmoteMap ffzGlobalEmotes; private: - EmoteMap _ffzEmotes; ConcurrentMap _ffzChannelEmoteFromCaches; void loadFFZEmotes(); diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 1c3d56505..6dfbfef33 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -175,8 +175,8 @@ void IrcManager::beginConnecting() this->readConnection->moveToThread(QCoreApplication::instance()->thread()); for (auto &channel : this->channelManager.getItems()) { - this->writeConnection->sendRaw("JOIN #" + channel->getName()); - this->readConnection->sendRaw("JOIN #" + channel->getName()); + this->writeConnection->sendRaw("JOIN #" + channel->name); + this->readConnection->sendRaw("JOIN #" + channel->name); } this->writeConnection->open(); diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index c528988c0..ad04161fc 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -184,27 +184,17 @@ SharedMessage TwitchMessageBuilder::parse() continue; } - // bttv / ffz emotes - EmoteData emoteData; - // TODO: Implement ignored emotes // Format of ignored emotes: // Emote name: "forsenPuke" - if string in ignoredEmotes // Will match emote regardless of source (i.e. bttv, ffz) // Emote source + name: "bttv:nyanPls" - if (emoteManager.getBTTVEmotes().tryGet(string, emoteData) || - this->channel->getBTTVChannelEmotes().tryGet(string, emoteData) || - emoteManager.getFFZEmotes().tryGet(string, emoteData) || - this->channel->getFFZChannelEmotes().tryGet(string, emoteData) || - emoteManager.getChatterinoEmotes().tryGet(string, emoteData)) { - this->appendWord(Word(emoteData.image, Word::BttvEmoteImage, - emoteData.image->getName(), emoteData.image->getTooltip(), - Link(Link::Url, emoteData.image->getUrl()))); - + if (this->tryAppendEmote(string)) { + // Successfully appended an emote continue; } - // actually just a word + // Actually just text QString link = this->matchLink(string); this->appendWord(Word(string, Word::Text, textColor, string, QString(), @@ -255,10 +245,10 @@ void TwitchMessageBuilder::parseRoomID() void TwitchMessageBuilder::parseChannelName() { - QString channelName("#" + this->channel->getName()); + QString channelName("#" + this->channel->name); this->appendWord(Word(channelName, Word::Misc, this->colorScheme.SystemMessageColor, QString(channelName), QString(), - Link(Link::Url, this->channel->getName() + "\n" + this->messageID))); + Link(Link::Url, this->channel->name + "\n" + this->messageID))); } void TwitchMessageBuilder::parseUsername() @@ -358,6 +348,40 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcPrivateMessage *i } } +bool TwitchMessageBuilder::tryAppendEmote(QString &emoteString) +{ + EmoteData emoteData; + + if (emoteManager.bttvGlobalEmotes.tryGet(emoteString, emoteData)) { + // BTTV Global Emote + return this->appendEmote(emoteData); + } else if (this->channel->bttvChannelEmotes.tryGet(emoteString, emoteData)) { + // BTTV Channel Emote + return this->appendEmote(emoteData); + } else if (emoteManager.ffzGlobalEmotes.tryGet(emoteString, emoteData)) { + // FFZ Global Emote + return this->appendEmote(emoteData); + } else if (this->channel->ffzChannelEmotes.tryGet(emoteString, emoteData)) { + // FFZ Channel Emote + return this->appendEmote(emoteData); + } else if (emoteManager.getChatterinoEmotes().tryGet(emoteString, emoteData)) { + // Chatterino Emote + return this->appendEmote(emoteData); + } + + return false; +} + +bool TwitchMessageBuilder::appendEmote(EmoteData &emoteData) +{ + this->appendWord(Word(emoteData.image, Word::BttvEmoteImage, emoteData.image->getName(), + emoteData.image->getTooltip(), + Link(Link::Url, emoteData.image->getUrl()))); + + // Perhaps check for ignored emotes here? + return true; +} + void TwitchMessageBuilder::parseTwitchBadges() { const auto &channelResources = this->resources.channels[this->roomID]; diff --git a/src/twitch/twitchmessagebuilder.hpp b/src/twitch/twitchmessagebuilder.hpp index bbc1f6d65..6399954f6 100644 --- a/src/twitch/twitchmessagebuilder.hpp +++ b/src/twitch/twitchmessagebuilder.hpp @@ -57,6 +57,8 @@ private: void appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage, const QString &emote, std::vector> &vec, EmoteManager &emoteManager); + bool tryAppendEmote(QString &emoteString); + bool appendEmote(EmoteData &emoteData); void parseTwitchBadges(); }; diff --git a/src/widgets/accountpopup.cpp b/src/widgets/accountpopup.cpp index 3c12f536b..ae38c5879 100644 --- a/src/widgets/accountpopup.cpp +++ b/src/widgets/accountpopup.cpp @@ -24,7 +24,7 @@ AccountPopupWidget::AccountPopupWidget(std::shared_ptr &channel) }); connect(_ui->btnPurge, &QPushButton::clicked, [=]() { - qDebug() << "xD: " << _channel->getName(); + qDebug() << "xD: " << _channel->name; printf("Channel pointer in dialog: %p\n", _channel.get()); //_channel->sendMessage(QString(".timeout %1 0").arg(_ui->lblUsername->text())); diff --git a/src/widgets/chatwidget.cpp b/src/widgets/chatwidget.cpp index 73c06178e..8c7b06725 100644 --- a/src/widgets/chatwidget.cpp +++ b/src/widgets/chatwidget.cpp @@ -37,7 +37,7 @@ static int index = 0; ChatWidget::ChatWidget(ChannelManager &_channelManager, NotebookPage *parent) : BaseWidget(parent) , channelManager(_channelManager) - , channel(_channelManager.getEmpty()) + , channel(_channelManager.emptyChannel) , vbox(this) , header(this) , view(this) @@ -130,7 +130,7 @@ void ChatWidget::channelNameUpdated(const std::string &newChannelName) { // remove current channel if (!this->channel->isEmpty()) { - this->channelManager.removeChannel(this->channel->getName()); + this->channelManager.removeChannel(this->channel->name); this->detachChannel(); } @@ -139,7 +139,7 @@ void ChatWidget::channelNameUpdated(const std::string &newChannelName) this->messages.clear(); if (newChannelName.empty()) { - this->channel = this->channelManager.getEmpty(); + this->channel = this->channelManager.emptyChannel; } else { this->setChannel(this->channelManager.addChannel(QString::fromStdString(newChannelName))); }