Add support for spotify hyperlinking (#597)

* Add support for spotify hyperlinking

* Change open link in browser to open link
This commit is contained in:
Lajamerr Mittesdine 2018-07-11 07:50:05 -04:00 committed by fourtf
parent 77f904fae4
commit 727ccd2ff4
3 changed files with 14 additions and 8 deletions

View file

@ -20,9 +20,16 @@ LinkParser::LinkParser(const QString &unparsedString)
// Read the TLDs in and replace the newlines with pipes // Read the TLDs in and replace the newlines with pipes
QString tldData = t1.readAll().replace(newLineRegex, "|"); 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?)://)?" "(?:(?:https?|ftps?)://)?"
// user:pass authentication // user:pass authentication
"(?:\\S+(?::\\S*)?@)?" "(?:\\S+(?::\\S*)?@)?"
@ -53,7 +60,7 @@ LinkParser::LinkParser(const QString &unparsedString)
"(?:[/?#]\\S*)?" "(?:[/?#]\\S*)?"
"$"; "$";
return QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption); return QRegularExpression(hyperlinkRegExp, QRegularExpression::CaseInsensitiveOption);
}(); }();
this->match_ = linkRegex.match(unparsedString); this->match_ = linkRegex.match(unparsedString);

View file

@ -49,6 +49,7 @@ QString MessageBuilder::matchLink(const QString &string)
static QRegularExpression httpRegex("\\bhttps?://", QRegularExpression::CaseInsensitiveOption); static QRegularExpression httpRegex("\\bhttps?://", QRegularExpression::CaseInsensitiveOption);
static QRegularExpression ftpRegex("\\bftps?://", QRegularExpression::CaseInsensitiveOption); static QRegularExpression ftpRegex("\\bftps?://", QRegularExpression::CaseInsensitiveOption);
static QRegularExpression spotifyRegex("\\bspotify:", QRegularExpression::CaseInsensitiveOption);
if (!linkParser.hasMatch()) { if (!linkParser.hasMatch()) {
return QString(); return QString();
@ -56,11 +57,9 @@ QString MessageBuilder::matchLink(const QString &string)
QString captured = linkParser.getCaptured(); QString captured = linkParser.getCaptured();
if (!captured.contains(httpRegex)) { if (!captured.contains(httpRegex) && !captured.contains(ftpRegex) && !captured.contains(spotifyRegex)) {
if (!captured.contains(ftpRegex)) {
captured.insert(0, "http://"); captured.insert(0, "http://");
} }
}
return captured; return captured;
} }

View file

@ -1067,7 +1067,7 @@ void ChannelView::addContextMenuItems(const MessageLayoutElement *hoveredElement
if (hoveredElement->getLink().type == Link::Url) { if (hoveredElement->getLink().type == Link::Url) {
QString url = hoveredElement->getLink().value; 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->addAction("Copy link", [url] { QApplication::clipboard()->setText(url); });
menu->addSeparator(); menu->addSeparator();