From 727ccd2ff43053cd699183649807fcc68896dd7e Mon Sep 17 00:00:00 2001 From: Lajamerr Mittesdine Date: Wed, 11 Jul 2018 07:50:05 -0400 Subject: [PATCH] Add support for spotify hyperlinking (#597) * Add support for spotify hyperlinking * Change open link in browser to open link --- src/common/LinkParser.cpp | 13 ++++++++++--- src/messages/MessageBuilder.cpp | 7 +++---- src/widgets/helper/ChannelView.cpp | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/common/LinkParser.cpp b/src/common/LinkParser.cpp index 5580a69c0..b6bf12443 100644 --- a/src/common/LinkParser.cpp +++ b/src/common/LinkParser.cpp @@ -20,9 +20,16 @@ LinkParser::LinkParser(const QString &unparsedString) // Read the TLDs in and replace the newlines with pipes QString tldData = t1.readAll().replace(newLineRegex, "|"); - const QString urlRegExp = + const QString hyperlinkRegExp = "^" - // protocol identifier + // Identifier for spotify + "(?x-mi:(spotify:(?:" + "(?:artist|album|track|user:[^:]+:playlist):" + "[a-zA-Z0-9]+|user:[^:]+|search:" + "(?:[-\\w$\\.+!*'(),]+|%[a-fA-F0-9]{2})+)))" + // If nothing matches then just go on + "|" + // Identifier for http and ftp "(?:(?:https?|ftps?)://)?" // user:pass authentication "(?:\\S+(?::\\S*)?@)?" @@ -53,7 +60,7 @@ LinkParser::LinkParser(const QString &unparsedString) "(?:[/?#]\\S*)?" "$"; - return QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption); + return QRegularExpression(hyperlinkRegExp, QRegularExpression::CaseInsensitiveOption); }(); this->match_ = linkRegex.match(unparsedString); diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index 0054b887d..01a6ea629 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -49,6 +49,7 @@ QString MessageBuilder::matchLink(const QString &string) static QRegularExpression httpRegex("\\bhttps?://", QRegularExpression::CaseInsensitiveOption); static QRegularExpression ftpRegex("\\bftps?://", QRegularExpression::CaseInsensitiveOption); + static QRegularExpression spotifyRegex("\\bspotify:", QRegularExpression::CaseInsensitiveOption); if (!linkParser.hasMatch()) { return QString(); @@ -56,10 +57,8 @@ QString MessageBuilder::matchLink(const QString &string) QString captured = linkParser.getCaptured(); - if (!captured.contains(httpRegex)) { - if (!captured.contains(ftpRegex)) { - captured.insert(0, "http://"); - } + if (!captured.contains(httpRegex) && !captured.contains(ftpRegex) && !captured.contains(spotifyRegex)) { + captured.insert(0, "http://"); } return captured; diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index bb6ec30a0..926845d2c 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -1067,7 +1067,7 @@ void ChannelView::addContextMenuItems(const MessageLayoutElement *hoveredElement if (hoveredElement->getLink().type == Link::Url) { QString url = hoveredElement->getLink().value; - menu->addAction("Open link in browser", [url] { QDesktopServices::openUrl(QUrl(url)); }); + menu->addAction("Open link", [url] { QDesktopServices::openUrl(QUrl(url)); }); menu->addAction("Copy link", [url] { QApplication::clipboard()->setText(url); }); menu->addSeparator();