aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/filtertree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/urlfilter/filtertree.cpp')
-rw-r--r--lib/urlfilter/filtertree.cpp38
1 files changed, 23 insertions, 15 deletions
diff --git a/lib/urlfilter/filtertree.cpp b/lib/urlfilter/filtertree.cpp
index 8d88140..dcde196 100644
--- a/lib/urlfilter/filtertree.cpp
+++ b/lib/urlfilter/filtertree.cpp
@@ -8,10 +8,10 @@
#include "filtertree.h"
#include "filterleaf.h"
-#include <QTextStream>
#include "formats/hostlistrule.h"
+#include <QTextStream>
-bool loadHostlist(QIODevice &from, FilterTree* tree)
+bool loadHostlist(QIODevice &from, FilterTree *tree)
{
Q_ASSERT(from.isReadable());
QTextStream stream(&from);
@@ -36,11 +36,18 @@ bool loadHostlist(QIODevice &from, FilterTree* tree)
if(!added)
return false;
}
-
}
return true;
}
+FilterTree::~FilterTree()
+{
+ for(auto &branch : m_branches) {
+ qDeleteAll(branch.leaves);
+ branch.leaves.clear();
+ }
+}
+
const QStringList FilterTree::branches() const
{
QStringList branches;
@@ -50,7 +57,7 @@ const QStringList FilterTree::branches() const
return branches;
}
-QVector<const FilterLeaf *> FilterTree::match(const QString& domain, const QString& requestUrl) const
+QVector<const FilterLeaf *> FilterTree::match(const QString &domain, const QString &requestUrl) const
{
QVector<const FilterLeaf *> leaves;
for(const auto &branch : m_branches) {
@@ -61,26 +68,27 @@ QVector<const FilterLeaf *> FilterTree::match(const QString& domain, const QStri
leaves.append(leaf);
}
}
-
}
}
return leaves;
}
-bool FilterTree::addRule(FilterLeaf *rule, const QString& domain)
+bool FilterTree::addRule(FilterLeaf *rule, const QString &domain)
+{
+ branchLock.lock();
+ this->branch(domain).leaves.emplace_back(rule);
+ branchLock.unlock();
+ return true;
+}
+
+FilterTree::Branch & FilterTree::branch(const QString& domain)
{
for(auto &branch : m_branches) {
- if(branch.domain.matches(QUrl(domain))) {
- branch.leaves.emplace_back(rule);
- return true;
- }
+ if(branch.domain.matches(QUrl(domain)))
+ return branch;
}
// no branch was found
Branch branch(domain);
- //branch.domain = domain.toStdString();
- // TODO: for some reason, can't add rule here
- //branch.leaves.emplace_back(rule);
- m_branches.emplace_back(std::move(branch));
- return this->addRule(rule, domain);
+ return m_branches.emplace_back(std::move(branch));
}