aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/blockersubscription.cpp
blob: 9275209b42ab84e078d395ffe5c4ce6372291ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/** 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 <http://www.gnu.org/licenses/>.
 ** 
 ******************************************************************************/

#include "blockersubscription.h"

#include <QFile>
#include <QTextStream>

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<BlockerRule*> BlockerSubscription::urlBlacklist()
{
    return m_urlBlacklist;
}

const QList<BlockerRule*> 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;
    }
}