File indexing completed on 2024-04-28 15:52:00
0001 /* 0002 SPDX-FileCopyrightText: 2013 Jaydeep Solanki <jaydp17@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef URL_UTILS_H 0008 #define URL_UTILS_H 0009 0010 #include <QRegularExpression> 0011 #include <QUrl> 0012 0013 namespace UrlUtils 0014 { 0015 QString getUrl(QString txt) 0016 { 0017 // match the url 0018 static QRegularExpression reg(QStringLiteral("\\b((https?|ftp)://(www\\d{0,3}[.])?[\\S]+)|((www\\d{0,3}[.])[\\S]+)")); 0019 // blocks from detecting invalid urls 0020 static QRegularExpression reg1(QStringLiteral("[\\w'\"\\(\\)]+https?://|[\\w'\"\\(\\)]+ftp://|[\\w'\"\\(\\)]+www\\d{0,3}[.]")); 0021 txt = txt.remove(QLatin1Char('\n')); 0022 0023 if (reg1.match(txt).hasMatch()) { // return early if there is a match (url is not valid) 0024 return QString(); 0025 } 0026 0027 QRegularExpressionMatch match = reg.match(txt); 0028 QString url = match.captured(); 0029 if (match.hasMatch() && QUrl(url).isValid()) { 0030 if (url.startsWith(QLatin1String("www"))) { 0031 url.prepend(QLatin1String("http://")); 0032 } 0033 return url; 0034 } 0035 0036 return QString(); 0037 } 0038 } 0039 0040 #endif