aboutsummaryrefslogtreecommitdiff
path: root/lib/web/urlfilter/filterdomain.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/web/urlfilter/filterdomain.cpp')
-rw-r--r--lib/web/urlfilter/filterdomain.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/web/urlfilter/filterdomain.cpp b/lib/web/urlfilter/filterdomain.cpp
new file mode 100644
index 0000000..53bc7db
--- /dev/null
+++ b/lib/web/urlfilter/filterdomain.cpp
@@ -0,0 +1,62 @@
+#include "filterdomain.h"
+#include <QVector>
+
+bool isMatchingDomain(const QString &domain, const QString &filter)
+{
+ // domain and filter are the same
+ if(domain == filter) {
+ return true;
+ }
+
+ // domain can't be matched by filter if it doesn't end with filter
+ // ex. example2.com isn't matched by example.com
+ if(!domain.endsWith(filter)) {
+ return false;
+ }
+
+ // match with subdomains
+ // ex. subdomain.example.com is matched by example.com
+ int index = domain.indexOf(filter);
+
+ // match if (domain ends with filter) && (filter has been found) and (character before filter is '.')
+ return index > 0 && domain[index - 1] == QLatin1Char('.');
+}
+
+FilterDomain::FilterDomain(MatchType type, QObject *parent)
+ : QObject(parent)
+{
+ setType(type);
+}
+
+void FilterDomain::setType(MatchType type)
+{
+ m_type = type;
+}
+
+void FilterDomain::addDomain(const QString &domain)
+{
+ if(!domain.isEmpty())
+ m_domains.append(domain);
+}
+
+bool FilterDomain::hasMatch(const QString &host) const
+{
+ // match all domains -> this rule applies to all domains
+ if(m_type == WhitelistAll)
+ return true;
+
+ // match no domains -> this rule applies to no domains
+ if(m_type == BlacklistAll)
+ return false;
+
+ // is this a whitelist or blacklist domain rule?
+ // should it apply to added domains or not
+ const bool whitelist = (m_type == Whitelist);
+
+ for(const auto &domain : qAsConst(m_domains)) {
+ if(isMatchingDomain(host, domain))
+ return whitelist;
+ }
+
+ return !whitelist;
+}