2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2013 Jaydeep Solanki <jaydp17@gmail.com>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2013-03-26 21:46:38 +00:00
|
|
|
|
2013-03-26 21:50:58 +00:00
|
|
|
#ifndef URL_UTILS_H
|
|
|
|
#define URL_UTILS_H
|
|
|
|
|
2020-01-26 11:40:33 +00:00
|
|
|
#include <QRegularExpression>
|
2013-03-26 21:44:55 +00:00
|
|
|
|
|
|
|
namespace UrlUtils
|
|
|
|
{
|
|
|
|
QString getUrl(QString txt)
|
2013-03-26 21:43:03 +00:00
|
|
|
{
|
|
|
|
// match the url
|
2020-01-26 11:40:33 +00:00
|
|
|
QRegularExpression reg(QStringLiteral("\\b((https?|ftp)://(www\\d{0,3}[.])?[\\S]+)|((www\\d{0,3}[.])[\\S]+)"));
|
2013-03-26 21:43:03 +00:00
|
|
|
// blocks from detecting invalid urls
|
2020-01-26 11:40:33 +00:00
|
|
|
QRegularExpression reg1(QStringLiteral("[\\w'\"\\(\\)]+https?://|[\\w'\"\\(\\)]+ftp://|[\\w'\"\\(\\)]+www\\d{0,3}[.]"));
|
|
|
|
txt = txt.remove(QLatin1Char('\n'));
|
|
|
|
|
|
|
|
if (reg1.match(txt).hasMatch()) { // return early if there is a match (url is not valid)
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegularExpressionMatch match = reg.match(txt);
|
|
|
|
QString url = match.captured();
|
|
|
|
if (match.hasMatch() && QUrl(url).isValid()) {
|
|
|
|
if (url.startsWith(QLatin1String("www"))) {
|
2016-07-11 20:05:18 +00:00
|
|
|
url.prepend(QLatin1String("http://"));
|
2013-03-26 21:43:03 +00:00
|
|
|
}
|
2020-01-26 11:40:33 +00:00
|
|
|
return url;
|
2013-03-26 21:43:03 +00:00
|
|
|
}
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2020-01-26 11:40:33 +00:00
|
|
|
return QString();
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
2014-03-02 12:59:49 +00:00
|
|
|
}
|
2013-03-26 21:43:03 +00:00
|
|
|
|
2013-03-26 21:50:58 +00:00
|
|
|
#endif
|