• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • adblock
adblockelementhiding.cpp
Go to the documentation of this file.
1 /* ============================================================
2 *
3 * This file is a part of the rekonq project
4 * Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
5 * based on code from rekonq
6 * Copyright (C) 2013 by Paul Rohrbach <p.b.r at gmx dot net>
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License or (at your option) version 3 or any later version
13 * accepted by the membership of KDE e.V. (or its successor approved
14 * by the membership of KDE e.V.), which shall act as a proxy
15 * defined in Section 14 of version 3 of the license.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * ============================================================ */
26 
27 
28 // Self Includes
29 #include "adblockelementhiding.h"
30 #include <KDebug>
31 
32 using namespace MessageViewer;
33 AdBlockElementHiding::AdBlockElementHiding()
34 {
35 }
36 
37 
38 bool AdBlockElementHiding::addRule(const QString &rule)
39 {
40  if (!rule.contains(QLatin1String("##")))
41  return false;
42 
43  if (rule.startsWith(QLatin1String("##")))
44  {
45  m_GenericRules.push_back(rule.mid(2));
46  return true;
47  }
48 
49  const QStringList lst = rule.split(QLatin1String("##"));
50  const QString domainSpecificRule = lst[1];
51 
52  QStringList domains = lst[0].split(QLatin1Char(','));
53  Q_FOREACH(const QString &domain, domains) {
54  if(domain.startsWith(QLatin1Char('~'))) {
55  m_DomainSpecificRulesWhitelist.insert(domain.mid(1),
56  domainSpecificRule);
57  continue;
58  }
59 
60  m_DomainSpecificRules.insert(domain, domainSpecificRule);
61  }
62 
63  return true;
64 }
65 
66 void AdBlockElementHiding::apply(QWebElement &document, const QString &domain) const
67 {
68 
69  //first apply generic rules
70  Q_FOREACH(const QString &rule, m_GenericRules) {
71  applyStringRule(document, rule);
72  }
73 
74  //check for whitelisted rules
75  QStringList whiteListedRules;
76  const QStringList subdomainList = generateSubdomainList(domain);
77 
78  Q_FOREACH(const QString &d, subdomainList) {
79  whiteListedRules.append(m_DomainSpecificRulesWhitelist.values(d));
80  }
81 
82  //apply rules if not whitelisted
83  Q_FOREACH(const QString &d, subdomainList) {
84  QList<QString> ruleList = m_DomainSpecificRules.values(d);
85  Q_FOREACH(const QString &rule, ruleList) {
86  if (!whiteListedRules.contains(rule))
87  applyStringRule(document, rule);
88  }
89  }
90 }
91 
92 void AdBlockElementHiding::clear()
93 {
94  m_GenericRules.clear();
95  m_DomainSpecificRules.clear();
96  m_DomainSpecificRulesWhitelist.clear();
97 }
98 
99 void AdBlockElementHiding::applyStringRule(QWebElement &document, const QString &rule) const
100 {
101  QWebElementCollection elements = document.findAll(rule);
102 
103  Q_FOREACH(QWebElement el, elements)
104  {
105  if (el.isNull())
106  continue;
107  kDebug() << "Hide element: " << el.localName();
108  el.removeFromDocument();
109  }
110 }
111 
112 QStringList AdBlockElementHiding::generateSubdomainList(const QString &domain) const
113 {
114  QStringList returnList;
115 
116  int dotPosition = domain.lastIndexOf(QLatin1Char('.'));
117  dotPosition = domain.lastIndexOf(QLatin1Char('.'), dotPosition - 1);
118  while (dotPosition != -1)
119  {
120  returnList.append(domain.mid(dotPosition + 1));
121  dotPosition = domain.lastIndexOf(QLatin1Char('.'), dotPosition - 1);
122  }
123  returnList.append(domain);
124 
125  return returnList;
126 }
QList::clear
void clear()
QList::push_back
void push_back(const T &value)
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
MessageViewer::AdBlockElementHiding::AdBlockElementHiding
AdBlockElementHiding()
Definition: adblockelementhiding.cpp:33
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWebElement::localName
QString localName() const
QList::append
void append(const T &value)
QWebElement
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
QList
QMultiHash::insert
QHash< Key, T >::iterator insert(const Key &key, const T &value)
QStringList
QWebElement::isNull
bool isNull() const
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QLatin1Char
QString::mid
QString mid(int position, int n) const
QLatin1String
MessageViewer::AdBlockElementHiding::apply
void apply(QWebElement &document, const QString &domain) const
Definition: adblockelementhiding.cpp:66
QStringList::split
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
QWebElement::findAll
QWebElementCollection findAll(const QString &selectorQuery) const
MessageViewer::AdBlockElementHiding::clear
void clear()
Definition: adblockelementhiding.cpp:92
QWebElement::removeFromDocument
void removeFromDocument()
QWebElementCollection
adblockelementhiding.h
MessageViewer::AdBlockElementHiding::addRule
bool addRule(const QString &rule)
Definition: adblockelementhiding.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal