36 #include <KIO/FileCopyJob>
37 #include <KStandardDirs>
38 #include <KNotification>
43 #include <QWebElement>
44 #include <QNetworkReply>
45 #include <QNetworkRequest>
46 #include <QtConcurrentRun>
51 using namespace MessageViewer;
52 QWeakPointer<AdBlockManager> AdBlockManager::s_adBlockManager;
57 if (s_adBlockManager.isNull())
61 return s_adBlockManager.data();
68 AdBlockManager::AdBlockManager(
QObject *parent)
72 _settingsLoaded = QtConcurrent::run(
this, &AdBlockManager::loadSettings);
99 void AdBlockManager::loadSettings()
101 KConfig config(QLatin1String(
"messagevieweradblockrc"));
104 _hostWhiteList.
clear();
105 _hostBlackList.
clear();
110 _elementHiding.
clear();
116 QDateTime today = QDateTime::currentDateTime();
119 const QStringList itemList = config.groupList().filter( QRegExp( QLatin1String(
"FilterList \\d+") ) );
120 Q_FOREACH(
const QString &item, itemList) {
121 KConfigGroup filtersGroup(&config, item);
122 const bool isFilterEnabled = filtersGroup.readEntry(QLatin1String(
"FilterEnabled"),
false);
123 if (!isFilterEnabled) {
126 const QString url = filtersGroup.readEntry(QLatin1String(
"url"));
130 const QString path = filtersGroup.readEntry(QLatin1String(
"path"));
134 const QDateTime lastDateTime = filtersGroup.readEntry(QLatin1String(
"lastUpdate"), QDateTime());
135 if (!lastDateTime.isValid() || today > lastDateTime.addDays(days) || !QFile(path).exists()) {
136 updateSubscription(path, url);
143 const QString localRulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_local"));
144 loadRules(localRulesFilePath);
148 void AdBlockManager::loadRules(
const QString &rulesFilePath)
150 QFile ruleFile(rulesFilePath);
151 if (!ruleFile.open(QFile::ReadOnly | QFile::Text)) {
152 kDebug() <<
"Unable to open rule file" << rulesFilePath;
156 QTextStream in(&ruleFile);
159 QString stringRule = in.readLine();
160 loadRuleString(stringRule);
165 void AdBlockManager::loadRuleString(
const QString &stringRule)
168 if (stringRule.startsWith(QLatin1Char(
'!')))
172 if (stringRule.startsWith(QLatin1Char(
'[')))
177 if (stringRule.isEmpty())
181 if (stringRule.startsWith(QLatin1String(
"@@")))
186 const QString filter = stringRule.mid(2);
187 if (filter.isEmpty())
196 if (stringRule.contains(QLatin1String(
"##")))
198 _elementHiding.
addRule(stringRule);
216 if (request.url().scheme() != QLatin1String(
"http")
217 && request.url().scheme() != QLatin1String(
"https"))
221 const QString referer = QString::fromLatin1(request.rawHeader(
"referer"));
222 Q_FOREACH(
const QString & host, whiteRefererList)
224 if (referer.contains(host))
228 QString urlString = request.url().toString();
231 const QString urlStringLowerCase = urlString.toLower();
232 const QString host = request.url().host();
235 if (_hostWhiteList.
match(host))
237 kDebug() <<
"ADBLOCK: WHITE RULE (@@) Matched by string: " << urlString;
243 if (filter.
match(request, urlString, urlStringLowerCase))
245 kDebug() <<
"ADBLOCK: WHITE RULE (@@) Matched by string: " << urlString;
251 if (_hostBlackList.
match(host))
253 kDebug() <<
"ADBLOCK: BLACK RULE Matched by string: " << urlString;
259 if (filter.
match(request, urlString, urlStringLowerCase))
261 kDebug() <<
"ADBLOCK: BLACK RULE Matched by string: " << urlString;
271 void AdBlockManager::updateSubscription(
const QString &path,
const QString &url)
273 KUrl subUrl = KUrl(url);
275 const QString rulesFilePath = path;
276 KUrl destUrl = KUrl(rulesFilePath);
278 KIO::FileCopyJob* job = KIO::file_copy(subUrl , destUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
279 job->metaData().insert(QLatin1String(
"ssl_no_client_cert"), QLatin1String(
"TRUE"));
280 job->metaData().insert(QLatin1String(
"ssl_no_ui"), QLatin1String(
"TRUE"));
281 job->metaData().insert(QLatin1String(
"UseCache"), QLatin1String(
"false"));
282 job->metaData().insert(QLatin1String(
"cookies"), QLatin1String(
"none"));
283 job->metaData().insert(QLatin1String(
"no-auth"), QLatin1String(
"true"));
285 connect(job, SIGNAL(finished(KJob*)),
this, SLOT(slotFinished(KJob*)));
289 void AdBlockManager::slotFinished(KJob *job)
292 KNotification *notify =
new KNotification( QLatin1String(
"adblock-list-download-failed") );
293 notify->setComponentData( KComponentData(
"messageviewer") );
294 notify->setText( i18n(
"Download new ad-block list was failed." ) );
299 KNotification *notify =
new KNotification( QLatin1String(
"adblock-list-download-done") );
300 notify->setComponentData( KComponentData(
"messageviewer") );
301 notify->setText( i18n(
"Download new ad-block list was done." ) );
304 KIO::FileCopyJob *fJob = qobject_cast<KIO::FileCopyJob *>(job);
305 KUrl url = fJob->destUrl();
306 url.setProtocol(QString());
307 loadRules(url.url());
311 bool AdBlockManager::subscriptionFileExists(
int i)
313 const QString n = QString::number(i + 1);
315 QString rulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_") + n);
316 return QFile::exists(rulesFilePath);
322 _settingsLoaded.waitForFinished();
325 const QString localRulesFilePath = KStandardDirs::locateLocal(
"appdata" , QLatin1String(
"adblockrules_local"));
327 QFile ruleFile(localRulesFilePath);
328 if (!ruleFile.open(QFile::ReadOnly)) {
329 kDebug() <<
"Unable to open rule file" << localRulesFilePath;
333 QTextStream in(&ruleFile);
334 while (!in.atEnd()) {
335 QString readStringRule = in.readLine();
336 if (stringRule == readStringRule) {
342 if (!ruleFile.open(QFile::WriteOnly | QFile::Append)) {
343 kDebug() <<
"Unable to open rule file" << localRulesFilePath;
347 QTextStream out(&ruleFile);
348 out << stringRule <<
'\n';
353 loadRuleString(stringRule);
366 return ! _hostWhiteList.
match(host);
370 void AdBlockManager::applyHidingRules(QWebFrame *frame)
378 connect(frame, SIGNAL(loadFinished(
bool)),
this, SLOT(applyHidingRules(
bool)));
382 void AdBlockManager::applyHidingRules(
bool ok)
387 QWebFrame *frame = qobject_cast<QWebFrame *>(sender());
394 QString mainPageHost = page->
loadingUrl().host();
396 if (hosts.contains(mainPageHost))
399 QWebElement document = frame->documentElement();
401 _elementHiding.
apply(document, mainPageHost);
404 #include "adblockmanager.moc"
bool match(const QNetworkRequest &request, const QString &encodedUrl, const QString &encodedUrlLowerCase) const
bool isAdblockEnabledForHost(const QString &host)
void addCustomRule(const QString &, bool reloadPage=true)
static GlobalSettings * self()
static AdBlockManager * self()
Entry point.
bool blockRequest(const QNetworkRequest &request)
bool tryAddFilter(const QString &filter)
void apply(QWebElement &document, const QString &domain) const
bool match(const QString &host) const
bool addRule(const QString &rule)