From 51f81262d595645814bb5887059555127e1d3c62 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Mon, 31 Jul 2017 01:19:25 +0200 Subject: [PATCH] Clean up highlight code Fix bug if no account is selected --- src/twitch/twitchmessagebuilder.cpp | 123 +++++++++++++++++++--------- src/twitch/twitchmessagebuilder.hpp | 1 + 2 files changed, 87 insertions(+), 37 deletions(-) diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index df12bc642..462c1eaad 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -34,6 +34,10 @@ TwitchMessageBuilder::TwitchMessageBuilder(Channel *_channel, Resources &_resour SharedMessage TwitchMessageBuilder::parse() { + SettingsManager &settings = SettingsManager::getInstance(); + + this->originalMessage = this->ircMessage->content(); + // The timestamp is always appended to the builder // Whether or not will be rendered is decided/checked later this->appendTimestamp(); @@ -53,42 +57,8 @@ SharedMessage TwitchMessageBuilder::parse() this->parseUsername(); // highlights - const QString &originalMessage = ircMessage->content(); - this->originalMessage = originalMessage; - SettingsManager &settings = SettingsManager::getInstance(); - static auto player = new QMediaPlayer; - if (settings.customHighlightSound.get()) { - player->setMedia(QUrl(settings.pathHighlightSound.get())); - } else { - player->setMedia(QUrl("qrc:/sounds/ping2.wav")); - } - if (settings.enableHighlights.get() && - ircMessage->nick().compare(settings.selectedUser.get(), Qt::CaseInsensitive)) { - if (settings.enableHighlightsSelf.get() && - originalMessage.contains(settings.selectedUser.get(), Qt::CaseInsensitive)) { - this->setHighlight(true); - if (settings.enableHighlightSound.get()) { - player->play(); - } - if (settings.enableHighlightTaskbar.get()) { - QApplication::alert(windowManager.getMainWindow().window(), 2500); - } - } else { - QStringList lines = settings.highlightProperties.get().keys(); - for (QString string : lines) { - if (originalMessage.contains(string, Qt::CaseInsensitive)) { - this->setHighlight(true); - // Sound - if (settings.highlightProperties.get().value(string).first) { - player->play(); - } - // Taskbar - if (settings.highlightProperties.get().value(string).second) { - QApplication::alert(windowManager.getMainWindow().window(), 2500); - } - } - } - } + if (settings.enableHighlights.get()) { + this->parseHighlights(); } // bits @@ -126,7 +96,7 @@ SharedMessage TwitchMessageBuilder::parse() // words QColor textColor = ircMessage->isAction() ? this->usernameColor : this->colorScheme.Text; - QStringList splits = originalMessage.split(' '); + QStringList splits = this->originalMessage.split(' '); long int i = 0; @@ -367,6 +337,85 @@ void TwitchMessageBuilder::parseUsername() QString(), Link(Link::UserInfo, this->userName))); } +void TwitchMessageBuilder::parseHighlights() +{ + static auto player = new QMediaPlayer; + SettingsManager &settings = SettingsManager::getInstance(); + + if (this->ircMessage->nick() == settings.selectedUser.get()) { + // Do nothing. Highlights cannot be triggered by yourself + return; + } + + if (settings.customHighlightSound.get()) { + player->setMedia(QUrl(settings.pathHighlightSound.get())); + } else { + player->setMedia(QUrl("qrc:/sounds/ping2.wav")); + } + + struct Highlight { + Highlight(const QString &_target, bool _sound, bool _alert) + : target(_target) + , sound(_sound) + , alert(_alert) + { + } + + QString target; + bool sound; + bool alert; + }; + + // TODO: This vector should only be rebuilt upon highlights being changed + std::vector activeHighlights; + + if (settings.enableHighlightsSelf.get() && settings.selectedUser.get().size() > 0) { + activeHighlights.emplace_back(settings.selectedUser.get(), + settings.enableHighlightSound.get(), + settings.enableHighlightTaskbar.get()); + } + const auto &highlightProperties = settings.highlightProperties.get(); + + for (auto it = highlightProperties.begin(); it != highlightProperties.end(); ++it) { + auto properties = it.value(); + activeHighlights.emplace_back(it.key(), properties.first, properties.second); + } + + bool doHighlight = true; + bool playSound = false; + bool doAlert = false; + + for (const Highlight &highlight : activeHighlights) { + if (this->originalMessage.contains(highlight.target, Qt::CaseInsensitive)) { + doHighlight = true; + + if (highlight.sound) { + playSound = true; + } + + if (highlight.alert) { + doAlert = true; + } + + if (playSound && doAlert) { + // Break if no further action can be taken from other highlights + // This might change if highlights can have custom colors/sounds/actions + break; + } + } + } + + this->setHighlight(doHighlight); + + if (playSound) { + player->play(); + } + + if (doAlert) { + QApplication::alert(windowManager.getMainWindow().window(), 2500); + } +} + void TwitchMessageBuilder::appendModerationButtons() { // mod buttons diff --git a/src/twitch/twitchmessagebuilder.hpp b/src/twitch/twitchmessagebuilder.hpp index 96efbc812..b7271cf79 100644 --- a/src/twitch/twitchmessagebuilder.hpp +++ b/src/twitch/twitchmessagebuilder.hpp @@ -59,6 +59,7 @@ private: void parseRoomID(); void parseChannelName(); void parseUsername(); + void parseHighlights(); void appendModerationButtons(); void appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage, const QString &emote,