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

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • adblock
networkaccessmanager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE project.
3  * Copyright (c) 2013 Montel Laurent <montel@kde.org>
4  * based on code from kwebkit part Copyright (C) 2009 Dawit Alemayehu <adawit @ kde.org>
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "networkaccessmanager.h"
22 
23 #include "settings/globalsettings.h"
24 #include "adblockmanager.h"
25 
26 #include <KDebug>
27 #include <KLocalizedString>
28 #include <KProtocolInfo>
29 #include <KRun>
30 
31 #include <QTimer>
32 #include <QWidget>
33 #include <QNetworkReply>
34 #include <QWebFrame>
35 #include <QWebElementCollection>
36 
37 
38 #define HIDABLE_ELEMENTS QLatin1String("audio,img,embed,object,iframe,frame,video")
39 
40 /* Null network reply */
41 class NullNetworkReply : public QNetworkReply
42 {
43 public:
44  NullNetworkReply(const QNetworkRequest &req, QObject* parent = 0)
45  :QNetworkReply(parent)
46  {
47  setRequest(req);
48  setUrl(req.url());
49  setHeader(QNetworkRequest::ContentLengthHeader, 0);
50  setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("text/plain"));
51  setError(QNetworkReply::ContentAccessDenied, i18n("Blocked by ad filter"));
52  setAttribute(QNetworkRequest::User, QNetworkReply::ContentAccessDenied);
53  QTimer::singleShot(0, this, SIGNAL(finished()));
54  }
55 
56  virtual void abort() {}
57  virtual qint64 bytesAvailable() const { return 0; }
58 
59 protected:
60  virtual qint64 readData(char*, qint64) {return -1;}
61 };
62 
63 namespace MessageViewer {
64 
65 MyNetworkAccessManager::MyNetworkAccessManager(QObject *parent)
66  : KIO::AccessManager(parent)
67 {
68  QString c = KGlobal::locale()->language();
69 
70  if (c == QLatin1String("C"))
71  c = QLatin1String("en-US");
72  else
73  c = c.replace(QLatin1Char('_') , QLatin1Char('-'));
74 
75  c.append(QLatin1String(", en-US; q=0.8, en; q=0.6"));
76 
77  mAcceptLanguage = c.toLatin1();
78 }
79 
80 QNetworkReply *MyNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
81 {
82  bool blocked = false;
83 
84  // Handle GET operations with AdBlock
85  if (op == QNetworkAccessManager::GetOperation)
86  blocked = AdBlockManager::self()->blockRequest(req);
87 
88  if (!blocked) {
89  if (KProtocolInfo::isHelperProtocol(req.url())) {
90  (void) new KRun(req.url(), qobject_cast<QWidget*>(req.originatingObject()));
91  return new NullNetworkReply(req, this);
92  }
93  // set our "nice" accept-language header...
94  QNetworkRequest request = req;
95  request.setRawHeader("Accept-Language", mAcceptLanguage);
96 
97  return KIO::AccessManager::createRequest(op, req, outgoingData);
98  }
99 
100  QWebFrame* frame = qobject_cast<QWebFrame*>(req.originatingObject());
101  if (frame) {
102  if (!mBlockedRequests.contains(frame))
103  connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(slotApplyHidingBlockedElements(bool)));
104  mBlockedRequests.insert(frame, req.url());
105  }
106 
107  return new NullNetworkReply(req, this);
108 }
109 
110 static void hideBlockedElements(const QUrl& url, QWebElementCollection& collection)
111 {
112  for (QWebElementCollection::iterator it = collection.begin(); it != collection.end(); ++it) {
113  const QUrl baseUrl ((*it).webFrame()->baseUrl());
114  QString src = (*it).attribute(QLatin1String("src"));
115  if (src.isEmpty())
116  src = (*it).evaluateJavaScript(QLatin1String("this.src")).toString();
117  if (src.isEmpty())
118  continue;
119  const QUrl resolvedUrl (baseUrl.resolved(src));
120  if (url == resolvedUrl) {
121  //kDebug() << "*** HIDING ELEMENT: " << (*it).tagName() << resolvedUrl;
122  (*it).removeFromDocument();
123  }
124  }
125 }
126 
127 void MyNetworkAccessManager::slotApplyHidingBlockedElements(bool ok)
128 {
129  if (!ok)
130  return;
131 
132  if(!GlobalSettings::self()->adBlockEnabled())
133  return;
134 
135  if(!GlobalSettings::self()->hideAdsEnabled())
136  return;
137 
138  QWebFrame* frame = qobject_cast<QWebFrame*>(sender());
139  if (!frame)
140  return;
141 
142  QList<QUrl> urls = mBlockedRequests.values(frame);
143  if (urls.isEmpty())
144  return;
145 
146  QWebElementCollection collection = frame->findAllElements(HIDABLE_ELEMENTS);
147  if (frame->parentFrame())
148  collection += frame->parentFrame()->findAllElements(HIDABLE_ELEMENTS);
149 
150  Q_FOREACH(const QUrl &url, urls)
151  hideBlockedElements(url, collection);
152 }
153 
154 }
155 
156 #include "networkaccessmanager.moc"
MessageViewer::hideBlockedElements
static void hideBlockedElements(const QUrl &url, QWebElementCollection &collection)
Definition: networkaccessmanager.cpp:110
globalsettings.h
MessageViewer::MyNetworkAccessManager::MyNetworkAccessManager
MyNetworkAccessManager(QObject *parent=0)
Definition: networkaccessmanager.cpp:65
QWidget
QObject
adblockmanager.h
networkaccessmanager.h
MessageViewer::GlobalSettings::self
static GlobalSettings * self()
Definition: globalsettings.cpp:34
MessageViewer::AdBlockManager::self
static AdBlockManager * self()
Entry point.
Definition: adblockmanager.cpp:55
MessageViewer::AdBlockManager::blockRequest
bool blockRequest(const QNetworkRequest &request)
Definition: adblockmanager.cpp:210
HIDABLE_ELEMENTS
#define HIDABLE_ELEMENTS
Definition: networkaccessmanager.cpp:38
MessageViewer::MyNetworkAccessManager::createRequest
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData=0)
Definition: networkaccessmanager.cpp:80
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 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

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