From 380c05028306680972f848808da17d9e6f55635e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 21 Dec 2018 14:55:18 +0100 Subject: Add adblocklist class --- lib/urlfilter/formats/adblockrule.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'lib/urlfilter/formats/adblockrule.h') diff --git a/lib/urlfilter/formats/adblockrule.h b/lib/urlfilter/formats/adblockrule.h index 3331cac..6be3cdf 100644 --- a/lib/urlfilter/formats/adblockrule.h +++ b/lib/urlfilter/formats/adblockrule.h @@ -14,24 +14,35 @@ #include #include +class Matcher +{ +public: + virtual bool hasMatch(const QString &where) const = 0; +}; + template -class ContentsMatcher +class ContentsMatcher : public Matcher { public: ContentsMatcher(const QString &pattern, FilterLeaf::UrlMatchType matchType) { this->matchType = matchType; patternLength = pattern.length(); - matcher.setPattern(pattern); + if constexpr(std::is_same_v) { matcher.setPatternOptions(matcher.patternOptions() | QRegularExpression::CaseInsensitiveOption); + matcher.setPattern(pattern); } else if constexpr(std::is_same_v) { matcher.setCaseSensitivity(Qt::CaseInsensitive); + matcher.setPattern(pattern); + } else if constexpr(std::is_same_v) { + matcher = QUrl::fromUserInput(pattern).host(); +// qDebug("matcher: %s", qUtf8Printable(matcher)); } } - bool hasMatch(const QString &where) const + bool hasMatch(const QString &where) const override { if constexpr(std::is_same_v) { switch (matchType) { @@ -58,6 +69,13 @@ public: if(matchType != FilterLeaf::RegularExpressionMatch) qWarning("ContentsMatcher is a regular expression, but not doing a regular expression match!"); return matcher.match(where).hasMatch(); + } else if constexpr(std::is_same_v) { + // TODO: fix + if(matchType == FilterLeaf::DomainMatch) { +// qDebug("matching %s", qUtf8Printable(QUrl(where).host())); + return QUrl(where).host().endsWith(matcher); + } else + return matcher == where; } else { qWarning("Matcher has no backend, returning false"); return false; @@ -65,7 +83,7 @@ public: } private: - int patternLength; + int patternLength; T matcher; FilterLeaf::UrlMatchType matchType; }; -- cgit v1.2.1