/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "adblockrule.h" #include #include AdBlockRule::AdBlockRule(FilterLeaf::UrlMatchType matchType, const QString &filter, FilterLeaf::Action action) { this->matchType = matchType; this->m_request = filter; this->m_isBlocking = (action == FilterLeaf::Block); //matcher.setPattern(filter); if(matchType == FilterLeaf::RegularExpressionMatch) regExp = new QRegExp(filter); else stringMatcher = new QStringMatcher(filter); } void AdBlockRule::mergeOptions(const QHash &options) { this->resourceTypeOptions.unite(options); } bool AdBlockRule::match(const QUrl &requestUrl) const { switch(matchType) { case FilterLeaf::RegularExpressionMatch: return (regExp->indexIn(requestUrl.toString()) != -1); default: return false; } } bool AdBlockRule::match(const QUrl &requestUrl, QWebEngineUrlRequestInfo::ResourceType type) const { // if request is of the required type, or there are no types set (== apply to all requests) if(this->resourceTypeOptions.contains(type) || this->resourceTypeOptions.isEmpty()) { switch(matchType) { case FilterLeaf::RegularExpressionMatch: return (regExp->indexIn(requestUrl.toString()) != -1); default: qWarning("Match type not implemented, returning false!"); return false; } } // request type is not matched return false; } std::pair AdBlockRule::action() const { if(m_isBlocking) return std::make_pair(FilterLeaf::Block, QVariant()); else return std::make_pair(FilterLeaf::Allow, QVariant()); }