X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fclickable.cpp;h=0674d0e78b0e045eaa3335cdc8d5dd12411e560d;hp=d19083c825a76f838aed8a2f67b533c187e65a4b;hb=30063c5a63c6111787258b5c4ba8acdb03008667;hpb=55579f53f3bd37f2a20d9be7458bdc54b345a052 diff --git a/src/uisupport/clickable.cpp b/src/uisupport/clickable.cpp index d19083c8..0674d0e7 100644 --- a/src/uisupport/clickable.cpp +++ b/src/uisupport/clickable.cpp @@ -18,19 +18,47 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include + +#include "buffermodel.h" #include "clickable.h" +#include "client.h" + +void Clickable::activate(NetworkId networkId, const QString &text) const { + if(!isValid()) + return; + + QString str = text.mid(start(), length()); + + switch(type()) { + case Clickable::Url: + if(!str.contains("://")) + str = "http://" + str; + QDesktopServices::openUrl(QUrl::fromEncoded(str.toUtf8(), QUrl::TolerantMode)); + break; + case Clickable::Channel: + Client::bufferModel()->switchToOrJoinBuffer(networkId, str); + break; + default: + break; + } +} // NOTE: This method is not threadsafe and not reentrant! // (RegExps are not constant while matching, and they are static here for efficiency) ClickableList ClickableList::fromString(const QString &str) { // For matching URLs + static QString scheme("(?:(?:mailto:|(?:[+.-]?\\w)+://)|www(?=\\.\\S+\\.))"); + static QString authority("(?:(?:[,.;@:]?[-\\w]+)+\\.?|\\[[0-9a-f:.]+\\])(?::\\d+)?"); + static QString urlChars("(?:[,.;:]*[\\w~@/?&=+$()!%#*{}\\[\\]\\|'^-])"); static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)"); - static QString urlChars("(?:[,.;:]*[\\w\\-~@/?&=+$()!%#*|{}\\[\\]'^])"); static QRegExp regExp[] = { // URL // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)), - QRegExp(QString("((?:(?:mailto:|\\w+://)|www\\.)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive), + QRegExp(QString("\\b(%1%2(?:/%3*)?)%4").arg(scheme, authority, urlChars, urlEnd), Qt::CaseInsensitive), // Channel name // We don't match for channel names starting with + or &, because that gives us a lot of false positives. @@ -84,3 +112,11 @@ ClickableList ClickableList::fromString(const QString &str) { } while(type >= 0); return result; } + +Clickable ClickableList::atCursorPos(int idx) { + foreach(const Clickable &click, *this) { + if(idx >= click.start() && idx < click.start() + click.length()) + return click; + } + return Clickable(); +}