#include "blockersubscription.h" #include #include BlockerSubscription::BlockerSubscription(QObject *parent) : QObject(parent) { } int BlockerSubscription::loadFromFile(const QString &file) { QFile subfile(file); if(!subfile.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug("Cannot open subscription: %s", qUtf8Printable(file)); return -1; } QTextStream subscription(&subfile); QString header = subscription.readLine(); if(header != "[Adblock Plus 2.0]") { qDebug("Invalid format of subscription: %s", qUtf8Printable(file)); return -1; } int rules = 0; while(!subscription.atEnd()) { QString line = subscription.readLine(); if(!line.isEmpty()) { if(line.startsWith('!')) { parseComment(line); } else { // The line is not a comment BlockerRule *rule = new BlockerRule(line, this); m_urlBlacklist.append(rule); rules++; } } } qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(file)); return rules; } const QString BlockerSubscription::title() { return m_title; } const QString BlockerSubscription::homepage() { return m_homepage; } const QString BlockerSubscription::license() { return m_license; } const QString BlockerSubscription::version() { return m_version; } const QDateTime BlockerSubscription::lastModified() { return m_lastModified; } const QDateTime BlockerSubscription::expires() { return m_expires; } void BlockerSubscription::parseComment(const QString &line) { if(line.startsWith("! Title: ")) { m_title = line.right(line.length() - 9); return; } if(line.startsWith("! Homepage: ")) { m_homepage = line.right(line.length() - 12); return; } if(line.startsWith("! Licence: ")) { m_license = line.right(line.length() - 11); return; } if(line.startsWith("! Version: ")) { m_version = line.right(line.length() - 11); return; } if(line.startsWith("! Last modified: ")) { m_lastModified = QDateTime::fromString(line.right(line.length() - 17), Qt::RFC2822Date); return; } if(line.startsWith("! Expires: ")) { m_expires = m_lastModified.addDays(line.right(line.length() - 11).left(2).toInt()); return; } }