diff --git a/src/chatlog/content/text.cpp b/src/chatlog/content/text.cpp index 118c8d25c..032b85c8b 100644 --- a/src/chatlog/content/text.cpp +++ b/src/chatlog/content/text.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include Text::Text(const QString& txt, QFont font, bool enableElide) @@ -52,6 +53,7 @@ void Text::setText(const QString& txt) text = txt; text.replace("\n", "
"); + detectAnchors(); ensureIntegrity(); freeResources(); } @@ -257,3 +259,30 @@ int Text::cursorFromPos(QPointF scenePos) const return -1; } + +void Text::detectAnchors() +{ + // detect urls + QRegExp exp("(?:\\b)(www\\.|http[s]?:\\/\\/|ftp:\\/\\/|tox:\\/\\/|tox:)\\S+"); + int offset = 0; + while ((offset = exp.indexIn(text, offset)) != -1) + { + QString url = exp.cap(); + + // 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(1) == "www.") + url.prepend("http://"); + + QString htmledUrl = QString("%1").arg(url); + text.replace(offset, exp.cap().length(), htmledUrl); + + offset += htmledUrl.length(); + } +} diff --git a/src/chatlog/content/text.h b/src/chatlog/content/text.h index 4739abab2..61b5ee866 100644 --- a/src/chatlog/content/text.h +++ b/src/chatlog/content/text.h @@ -1,13 +1,3 @@ -#ifndef TEXT_H -#define TEXT_H - -#include "../chatlinecontent.h" - -#include -#include - -class CustomTextDocument; - /* Copyright (C) 2014 by Project Tox @@ -24,6 +14,16 @@ class CustomTextDocument; See the COPYING file for more details. */ +#ifndef TEXT_H +#define TEXT_H + +#include "../chatlinecontent.h" + +#include +#include + +class CustomTextDocument; + class Text : public ChatLineContent { public: @@ -61,6 +61,8 @@ protected: int cursorFromPos(QPointF scenePos) const; + void detectAnchors(); + private: CustomTextDocument* doc = nullptr; QString text;