From 5253ab3f85d62e6bf2b9d069697ca31dafca150f Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 Aug 2018 12:56:42 +0300 Subject: [PATCH 1/7] Added tooltip to links via BTTV API. --- chatterino.pro | 2 + src/providers/bttv/BttvTooltip.cpp | 39 +++++++++++++++++++ src/providers/bttv/BttvTooltip.hpp | 17 ++++++++ src/providers/twitch/TwitchMessageBuilder.cpp | 10 ++++- src/widgets/TooltipWidget.cpp | 5 +++ src/widgets/TooltipWidget.hpp | 1 + src/widgets/helper/ChannelView.cpp | 1 + src/widgets/splits/SplitHeader.cpp | 2 + 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/providers/bttv/BttvTooltip.cpp create mode 100644 src/providers/bttv/BttvTooltip.hpp diff --git a/chatterino.pro b/chatterino.pro index 1c9cf8f11..ebe784deb 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -237,6 +237,7 @@ SOURCES += \ src/messages/Emote.cpp \ src/messages/ImageSet.cpp \ src/providers/bttv/BttvEmotes.cpp \ + src/providers/bttv/BttvTooltip.cpp \ src/providers/ffz/FfzEmotes.cpp \ src/autogenerated/ResourcesAutogen.cpp \ src/singletons/Badges.cpp \ @@ -433,6 +434,7 @@ HEADERS += \ src/messages/ImageSet.hpp \ src/common/Outcome.hpp \ src/providers/bttv/BttvEmotes.hpp \ + src/providers/bttv/BttvTooltip.hpp \ src/providers/ffz/FfzEmotes.hpp \ src/autogenerated/ResourcesAutogen.hpp \ src/singletons/Badges.hpp \ diff --git a/src/providers/bttv/BttvTooltip.cpp b/src/providers/bttv/BttvTooltip.cpp new file mode 100644 index 000000000..df3738411 --- /dev/null +++ b/src/providers/bttv/BttvTooltip.cpp @@ -0,0 +1,39 @@ +#include "providers/bttv/BttvTooltip.hpp" + +#include "common/Common.hpp" +#include "common/NetworkRequest.hpp" + +#include + +namespace chatterino { + +void BttvTooltip::getUrlTooltip(const QString url, + std::function successCallback) +{ + QString requestUrl("https://api.betterttv.net/2/link_resolver/" + + QUrl::toPercentEncoding(url, "", "/:")); + + NetworkRequest request(requestUrl); + request.setCaller(QThread::currentThread()); + request.setTimeout(30000); + request.onSuccess([successCallback](auto result) mutable -> Outcome { + auto root = result.parseJson(); + /* When tooltip is not a string, in this case, + onError runs before onSuccess, + so there is no point in doing "if" condition. */ + auto tooltip = root.value("tooltip").toString(); + successCallback(QUrl::fromPercentEncoding(tooltip.toUtf8())); + + return Success; + }); + + request.onError([successCallback](auto result) { + successCallback("No link info found"); + + return true; + }); + + request.execute(); +} + +} // namespace chatterino diff --git a/src/providers/bttv/BttvTooltip.hpp b/src/providers/bttv/BttvTooltip.hpp new file mode 100644 index 000000000..2c6b2597a --- /dev/null +++ b/src/providers/bttv/BttvTooltip.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace chatterino { + +class BttvTooltip +{ +public: + static void getUrlTooltip(const QString url, + std::function callback); + +private: +}; + +} // namespace chatterino diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index b245c7664..b83bfd29d 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -8,6 +8,7 @@ #include "messages/Message.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchChannel.hpp" +#include "providers/bttv/BttvTooltip.hpp" #include "singletons/Emotes.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" @@ -268,12 +269,19 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) link = Link(Link::Url, linkString); textColor = MessageColor(MessageColor::Link); - this->emplace(lowercaseLinkString, + auto linkMessageElement = this->emplace(lowercaseLinkString, MessageElementFlag::LowercaseLink, textColor) ->setLink(link); this->emplace(string, MessageElementFlag::OriginalLink, textColor) ->setLink(link); + + BttvTooltip::getUrlTooltip( + linkString, [linkMessageElement](QString tooltipText) { + if (!tooltipText.isEmpty()) { + linkMessageElement->setTooltip(tooltipText); + } + }); } // if (!linkString.isEmpty()) { diff --git a/src/widgets/TooltipWidget.cpp b/src/widgets/TooltipWidget.cpp index 084f729ac..5ee0b1daf 100644 --- a/src/widgets/TooltipWidget.cpp +++ b/src/widgets/TooltipWidget.cpp @@ -87,6 +87,11 @@ void TooltipWidget::setText(QString text) this->displayText_->setText(text); } +void TooltipWidget::setWordWrap(bool wrap) +{ + this->displayText_->setWordWrap(wrap); +} + void TooltipWidget::changeEvent(QEvent *) { // clear parents event diff --git a/src/widgets/TooltipWidget.hpp b/src/widgets/TooltipWidget.hpp index 7d8c95dc2..2228e5662 100644 --- a/src/widgets/TooltipWidget.hpp +++ b/src/widgets/TooltipWidget.hpp @@ -19,6 +19,7 @@ public: virtual ~TooltipWidget() override; void setText(QString text); + void setWordWrap(bool wrap); #ifdef USEWINSDK void raise(); diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 2afc42e75..71c1408d7 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -866,6 +866,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) tooltipWidget->hide(); } else { tooltipWidget->moveTo(this, event->globalPos()); + tooltipWidget->setWordWrap(hoverLayoutElement->getLink().isValid()); tooltipWidget->setText(tooltip); tooltipWidget->adjustSize(); tooltipWidget->show(); diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index 611bbaace..42656fde0 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -458,6 +458,8 @@ void SplitHeader::enterEvent(QEvent *event) tooltip->moveTo(this, this->mapToGlobal(this->rect().bottomLeft()), false); tooltip->setText(this->tooltipText_); + tooltip->setWordWrap(false); + tooltip->adjustSize(); tooltip->show(); tooltip->raise(); } From f8c42918a6ac611cc59629968deff96acf8e8e13 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 25 Aug 2018 20:49:50 +0300 Subject: [PATCH 2/7] Added option to turn off link info tooltips. --- src/singletons/Settings.hpp | 1 + src/widgets/helper/ChannelView.cpp | 2 ++ src/widgets/settingspages/FeelPage.cpp | 3 +++ 3 files changed, 6 insertions(+) diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 472c2892a..ff9a87222 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -109,6 +109,7 @@ public: /// Links BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false}; + BoolSetting enableLinkInfoTooltip = {"/links/linkInfoTooltip", false}; BoolSetting enableLowercaseLink = {"/links/linkLowercase", true}; /// Ingored Users diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 71c1408d7..d714884ae 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -864,6 +864,8 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) if (tooltip.isEmpty()) { tooltipWidget->hide(); + } else if (hoverLayoutElement->getLink().isValid() && !getSettings()->enableLinkInfoTooltip) { + tooltipWidget->hide(); } else { tooltipWidget->moveTo(this, event->globalPos()); tooltipWidget->setWordWrap(hoverLayoutElement->getLink().isValid()); diff --git a/src/widgets/settingspages/FeelPage.cpp b/src/widgets/settingspages/FeelPage.cpp index 78b41c45d..b87a2d098 100644 --- a/src/widgets/settingspages/FeelPage.cpp +++ b/src/widgets/settingspages/FeelPage.cpp @@ -50,6 +50,9 @@ FeelPage::FeelPage() form->addRow("Links:", this->createCheckBox("Open links only on double click", getSettings()->linksDoubleClickOnly)); + form->addRow("", + this->createCheckBox("Show link info in tooltips", + getSettings()->enableLinkInfoTooltip)); } layout->addSpacing(16); From 539d8c20d2bd225b31922bd36ca672f5ab64f46b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 25 Aug 2018 21:21:18 +0300 Subject: [PATCH 3/7] Put isValid() in variable. --- src/widgets/helper/ChannelView.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index d714884ae..5b64245ba 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -861,14 +861,15 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) return; } const auto &tooltip = hoverLayoutElement->getCreator().getTooltip(); + bool isLinkValid = hoverLayoutElement->getLink().isValid(); if (tooltip.isEmpty()) { tooltipWidget->hide(); - } else if (hoverLayoutElement->getLink().isValid() && !getSettings()->enableLinkInfoTooltip) { + } else if (isLinkValid && !getSettings()->enableLinkInfoTooltip) { tooltipWidget->hide(); } else { tooltipWidget->moveTo(this, event->globalPos()); - tooltipWidget->setWordWrap(hoverLayoutElement->getLink().isValid()); + tooltipWidget->setWordWrap(isLinkValid); tooltipWidget->setText(tooltip); tooltipWidget->adjustSize(); tooltipWidget->show(); @@ -876,7 +877,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event) } // check if word has a link - if (hoverLayoutElement->getLink().isValid()) { + if (isLinkValid) { this->setCursor(Qt::PointingHandCursor); } else { this->setCursor(Qt::ArrowCursor); From f4828b7d092f1949cbc4197ff13bee22e8bd97eb Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 25 Aug 2018 21:38:14 +0300 Subject: [PATCH 4/7] Rename everything. --- chatterino.pro | 4 ++-- src/providers/{bttv/BttvTooltip.cpp => LinkResolver.cpp} | 4 ++-- src/providers/{bttv/BttvTooltip.hpp => LinkResolver.hpp} | 4 ++-- src/providers/twitch/TwitchMessageBuilder.cpp | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/providers/{bttv/BttvTooltip.cpp => LinkResolver.cpp} (91%) rename src/providers/{bttv/BttvTooltip.hpp => LinkResolver.hpp} (74%) diff --git a/chatterino.pro b/chatterino.pro index ebe784deb..00b866a1b 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -237,7 +237,7 @@ SOURCES += \ src/messages/Emote.cpp \ src/messages/ImageSet.cpp \ src/providers/bttv/BttvEmotes.cpp \ - src/providers/bttv/BttvTooltip.cpp \ + src/providers/LinkResolver.cpp \ src/providers/ffz/FfzEmotes.cpp \ src/autogenerated/ResourcesAutogen.cpp \ src/singletons/Badges.cpp \ @@ -434,7 +434,7 @@ HEADERS += \ src/messages/ImageSet.hpp \ src/common/Outcome.hpp \ src/providers/bttv/BttvEmotes.hpp \ - src/providers/bttv/BttvTooltip.hpp \ + src/providers/LinkResolver.hpp \ src/providers/ffz/FfzEmotes.hpp \ src/autogenerated/ResourcesAutogen.hpp \ src/singletons/Badges.hpp \ diff --git a/src/providers/bttv/BttvTooltip.cpp b/src/providers/LinkResolver.cpp similarity index 91% rename from src/providers/bttv/BttvTooltip.cpp rename to src/providers/LinkResolver.cpp index df3738411..5ebad69e3 100644 --- a/src/providers/bttv/BttvTooltip.cpp +++ b/src/providers/LinkResolver.cpp @@ -1,4 +1,4 @@ -#include "providers/bttv/BttvTooltip.hpp" +#include "providers/LinkResolver.hpp" #include "common/Common.hpp" #include "common/NetworkRequest.hpp" @@ -7,7 +7,7 @@ namespace chatterino { -void BttvTooltip::getUrlTooltip(const QString url, +void LinkResolver::getLinkInfo(const QString url, std::function successCallback) { QString requestUrl("https://api.betterttv.net/2/link_resolver/" + diff --git a/src/providers/bttv/BttvTooltip.hpp b/src/providers/LinkResolver.hpp similarity index 74% rename from src/providers/bttv/BttvTooltip.hpp rename to src/providers/LinkResolver.hpp index 2c6b2597a..919109aaf 100644 --- a/src/providers/bttv/BttvTooltip.hpp +++ b/src/providers/LinkResolver.hpp @@ -5,10 +5,10 @@ namespace chatterino { -class BttvTooltip +class LinkResolver { public: - static void getUrlTooltip(const QString url, + static void getLinkInfo(const QString url, std::function callback); private: diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index b83bfd29d..d19d3252c 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -8,7 +8,7 @@ #include "messages/Message.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchChannel.hpp" -#include "providers/bttv/BttvTooltip.hpp" +#include "providers/LinkResolver.hpp" #include "singletons/Emotes.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" @@ -276,7 +276,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) textColor) ->setLink(link); - BttvTooltip::getUrlTooltip( + LinkResolver::getLinkInfo( linkString, [linkMessageElement](QString tooltipText) { if (!tooltipText.isEmpty()) { linkMessageElement->setTooltip(tooltipText); From c7e21885822d8e0d23f4ee9635e823e9b62e28a7 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 25 Aug 2018 21:47:25 +0300 Subject: [PATCH 5/7] Added tooltip to original link too. --- src/providers/twitch/TwitchMessageBuilder.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index d19d3252c..042830432 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -269,17 +269,18 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) link = Link(Link::Url, linkString); textColor = MessageColor(MessageColor::Link); - auto linkMessageElement = this->emplace(lowercaseLinkString, + auto linkMELowercase = this->emplace(lowercaseLinkString, MessageElementFlag::LowercaseLink, textColor) ->setLink(link); - this->emplace(string, MessageElementFlag::OriginalLink, - textColor) + auto linkMEOriginal = this->emplace(string, + MessageElementFlag::OriginalLink, textColor) ->setLink(link); LinkResolver::getLinkInfo( - linkString, [linkMessageElement](QString tooltipText) { + linkString, [linkMELowercase, linkMEOriginal](QString tooltipText) { if (!tooltipText.isEmpty()) { - linkMessageElement->setTooltip(tooltipText); + linkMELowercase->setTooltip(tooltipText); + linkMEOriginal->setTooltip(tooltipText); } }); } From cb500070f2937085d439fe1f6abc654a421bd545 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 26 Aug 2018 15:41:46 +0300 Subject: [PATCH 6/7] Run away from BTTV API. --- src/providers/LinkResolver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index 5ebad69e3..697b6a7d7 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -10,7 +10,7 @@ namespace chatterino { void LinkResolver::getLinkInfo(const QString url, std::function successCallback) { - QString requestUrl("https://api.betterttv.net/2/link_resolver/" + + QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" + QUrl::toPercentEncoding(url, "", "/:")); NetworkRequest request(requestUrl); From bf529d5503c06a1a0a7ca0a8fe6eb8617ac403d9 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 26 Aug 2018 16:25:51 +0300 Subject: [PATCH 7/7] Rewritten check for incorrect link. --- src/providers/LinkResolver.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index 697b6a7d7..d6917157d 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -18,11 +18,14 @@ void LinkResolver::getLinkInfo(const QString url, request.setTimeout(30000); request.onSuccess([successCallback](auto result) mutable -> Outcome { auto root = result.parseJson(); - /* When tooltip is not a string, in this case, - onError runs before onSuccess, - so there is no point in doing "if" condition. */ - auto tooltip = root.value("tooltip").toString(); - successCallback(QUrl::fromPercentEncoding(tooltip.toUtf8())); + auto statusCode = root.value("status").toInt(); + QString response = QString(); + if (statusCode == 200) { + response = root.value("tooltip").toString(); + } else { + response = root.value("message").toString(); + } + successCallback(QUrl::fromPercentEncoding(response.toUtf8())); return Success; });