/** LICENSE ********************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see .
 **
 ******************************************************************************/
#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;
    }
    // clear all lists
    m_urlBlacklist.clear();
    m_urlWhitelist.clear();
    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
                rules++;
                BlockerRule *rule = new BlockerRule(line, this);
                if(rule->isValid()) {
                    if(rule->isException()) {
                        m_urlWhitelist.append(rule);
                    } else {
                        m_urlBlacklist.append(rule);
                    }
                }
            }
        }
    }
    qDebug("Loaded %i/%i rules from subscription %s", m_urlBlacklist.count() + m_urlWhitelist.count(), 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;
}
const QList BlockerSubscription::urlBlacklist()
{
    return m_urlBlacklist;
}
const QList BlockerSubscription::urlWhitelist()
{
    return m_urlWhitelist;
}
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;
    }
}