mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Added support for local links in messages and correct detect links like "www.123.com"
This commit is contained in:
parent
7fcc11f01b
commit
f476b752d1
|
@ -177,38 +177,43 @@ void ChatMessage::hideDate()
|
||||||
|
|
||||||
QString ChatMessage::detectAnchors(const QString &str)
|
QString ChatMessage::detectAnchors(const QString &str)
|
||||||
{
|
{
|
||||||
QString out = str;
|
QString out;
|
||||||
|
|
||||||
// detect URIs
|
// detect URIs
|
||||||
QRegExp exp("("
|
QRegExp exp("("
|
||||||
"(?:\\b)(www\\.|http[s]?|ftp)://" // (protocol)://(printable - non-special character)
|
"(?:\\b)((www\\.)|(http[s]?|ftp)://)" // (protocol)://(printable - non-special character)
|
||||||
// http://ONEORMOREALHPA-DIGIT
|
// http://ONEORMOREALHPA-DIGIT
|
||||||
"\\w+\\S+)" // any other character, lets domains and other
|
"\\w+\\S+)" // any other character, lets domains and other
|
||||||
"|(^tox:[@\\w]+$)"); // starts with `tox` then : and only alpha-digits till the end
|
"|(?:\\b)(file:///.*$)" //link to a local file, valid until the end of the line
|
||||||
|
"|(?:\\b)(tox:[a-zA-Z\\d]{76}$)" //link with full user address
|
||||||
|
"|(?:\\b)(tox:\\S+@\\S+)"); // starts with `tox` then : and only alpha-digits till the end
|
||||||
// also accepts tox:agilob@net as simplified TOX ID
|
// also accepts tox:agilob@net as simplified TOX ID
|
||||||
int offset = 0;
|
//support for multi-line text
|
||||||
while ((offset = exp.indexIn(out, offset)) != -1)
|
QStringList messageLines = str.split("\n");
|
||||||
|
QStringList outLines;
|
||||||
|
for (int i = 0; i < messageLines.size(); ++i)
|
||||||
{
|
{
|
||||||
QString url = exp.cap();
|
out = messageLines.at(i);
|
||||||
|
int offset = 0;
|
||||||
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||||
if (url == "tox:\"")
|
|
||||||
{
|
{
|
||||||
offset += url.length();
|
QString url = exp.cap();
|
||||||
continue;
|
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
||||||
|
if (url == "tox:\"")
|
||||||
|
{
|
||||||
|
offset += url.length();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// add scheme if not specified
|
||||||
|
if (exp.cap(2) == "www.")
|
||||||
|
url.prepend("http://");
|
||||||
|
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
||||||
|
out.replace(offset, exp.cap().length(), htmledUrl);
|
||||||
|
offset += htmledUrl.length();
|
||||||
}
|
}
|
||||||
|
outLines.push_back(out);
|
||||||
// add scheme if not specified
|
|
||||||
if (exp.cap(1) == "www.")
|
|
||||||
url.prepend("http://");
|
|
||||||
|
|
||||||
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
|
||||||
out.replace(offset, exp.cap().length(), htmledUrl);
|
|
||||||
|
|
||||||
offset += htmledUrl.length();
|
|
||||||
}
|
}
|
||||||
|
return outLines.join("\n");
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ChatMessage::detectQuotes(const QString& str, MessageType type)
|
QString ChatMessage::detectQuotes(const QString& str, MessageType type)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user