27 #include <KLocalizedString>
28 #include <KProtocolInfo>
33 #include <QNetworkReply>
35 #include <QWebElementCollection>
38 #define HIDABLE_ELEMENTS QLatin1String("audio,img,embed,object,iframe,frame,video")
41 class NullNetworkReply :
public QNetworkReply
44 NullNetworkReply(
const QNetworkRequest &req,
QObject* parent = 0)
45 :QNetworkReply(parent)
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()));
56 virtual void abort() {}
57 virtual qint64 bytesAvailable()
const {
return 0; }
60 virtual qint64 readData(
char*, qint64) {
return -1;}
63 namespace MessageViewer {
66 : KIO::AccessManager(parent)
68 QString c = KGlobal::locale()->language();
70 if (c == QLatin1String(
"C"))
71 c = QLatin1String(
"en-US");
73 c = c.replace(QLatin1Char(
'_') , QLatin1Char(
'-'));
75 c.append(QLatin1String(
", en-US; q=0.8, en; q=0.6"));
77 mAcceptLanguage = c.toLatin1();
85 if (op == QNetworkAccessManager::GetOperation)
89 if (KProtocolInfo::isHelperProtocol(req.url())) {
90 (void)
new KRun(req.url(), qobject_cast<
QWidget*>(req.originatingObject()));
91 return new NullNetworkReply(req,
this);
94 QNetworkRequest request = req;
95 request.setRawHeader(
"Accept-Language", mAcceptLanguage);
97 return KIO::AccessManager::createRequest(op, req, outgoingData);
100 QWebFrame* frame = qobject_cast<QWebFrame*>(req.originatingObject());
102 if (!mBlockedRequests.contains(frame))
103 connect(frame, SIGNAL(loadFinished(
bool)),
this, SLOT(slotApplyHidingBlockedElements(
bool)));
104 mBlockedRequests.insert(frame, req.url());
107 return new NullNetworkReply(req,
this);
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"));
116 src = (*it).evaluateJavaScript(QLatin1String(
"this.src")).toString();
119 const QUrl resolvedUrl (baseUrl.resolved(src));
120 if (url == resolvedUrl) {
122 (*it).removeFromDocument();
127 void MyNetworkAccessManager::slotApplyHidingBlockedElements(
bool ok)
138 QWebFrame* frame = qobject_cast<QWebFrame*>(sender());
142 QList<QUrl> urls = mBlockedRequests.values(frame);
146 QWebElementCollection collection = frame->findAllElements(
HIDABLE_ELEMENTS);
147 if (frame->parentFrame())
150 Q_FOREACH(
const QUrl &url, urls)
156 #include "networkaccessmanager.moc"
static void hideBlockedElements(const QUrl &url, QWebElementCollection &collection)
MyNetworkAccessManager(QObject *parent=0)
static GlobalSettings * self()
static AdBlockManager * self()
Entry point.
bool blockRequest(const QNetworkRequest &request)
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData=0)