File indexing completed on 2024-05-12 04:57:50

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2016  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 /**
0019  * Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
0020  *
0021  * Redistribution and use in source and binary forms, with or without
0022  * modification, are permitted provided that the following conditions
0023  * are met:
0024  * 1. Redistributions of source code must retain the above copyright
0025  *    notice, this list of conditions and the following disclaimer.
0026  * 2. Redistributions in binary form must reproduce the above copyright
0027  *    notice, this list of conditions and the following disclaimer in the
0028  *    documentation and/or other materials provided with the distribution.
0029  * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
0030  *    may be used to endorse or promote products derived from this software
0031  *    without specific prior written permission.
0032  *
0033  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0034  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0035  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0036  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0037  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0038  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0039  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0040  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0041  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0042  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0043  * SUCH DAMAGE.
0044  */
0045 
0046 #ifndef ADBLOCKSUBSCRIPTION_H
0047 #define ADBLOCKSUBSCRIPTION_H
0048 
0049 #include <QVector>
0050 #include <QUrl>
0051 
0052 #include "qzcommon.h"
0053 #include "adblockrule.h"
0054 #include "adblocksearchtree.h"
0055 
0056 class QUrl;
0057 class QNetworkReply;
0058 
0059 class FALKON_EXPORT AdBlockSubscription : public QObject
0060 {
0061     Q_OBJECT
0062 public:
0063     explicit AdBlockSubscription(const QString &title, QObject* parent = nullptr);
0064     ~AdBlockSubscription();
0065 
0066     QString title() const;
0067 
0068     QString filePath() const;
0069     void setFilePath(const QString &path);
0070 
0071     QUrl url() const;
0072     void setUrl(const QUrl &url);
0073 
0074     virtual void loadSubscription(const QStringList &disabledRules);
0075     virtual void saveSubscription();
0076 
0077     const AdBlockRule* rule(int offset) const;
0078     QVector<AdBlockRule*> allRules() const;
0079 
0080     const AdBlockRule* enableRule(int offset);
0081     const AdBlockRule* disableRule(int offset);
0082 
0083     virtual bool canEditRules() const;
0084     virtual bool canBeRemoved() const;
0085 
0086     virtual int addRule(AdBlockRule* rule);
0087     virtual bool removeRule(int offset);
0088     virtual const AdBlockRule* replaceRule(AdBlockRule* rule, int offset);
0089 
0090 public Q_SLOTS:
0091     void updateSubscription();
0092 
0093 Q_SIGNALS:
0094     void subscriptionChanged();
0095     void subscriptionUpdated();
0096     void subscriptionError(const QString &message);
0097 
0098 protected Q_SLOTS:
0099     void subscriptionDownloaded();
0100 
0101 protected:
0102     virtual bool saveDownloadedData(const QByteArray &data);
0103 
0104     QNetworkReply *m_reply;
0105     QVector<AdBlockRule*> m_rules;
0106 
0107 private:
0108     QString m_title;
0109     QString m_filePath;
0110 
0111     QUrl m_url;
0112     bool m_updated;
0113 };
0114 
0115 class AdBlockCustomList : public AdBlockSubscription
0116 {
0117     Q_OBJECT
0118 public:
0119     explicit AdBlockCustomList(QObject* parent = nullptr);
0120 
0121     void loadSubscription(const QStringList &disabledRules) override;
0122     void saveSubscription() override;
0123 
0124     bool canEditRules() const override;
0125     bool canBeRemoved() const override;
0126 
0127     bool containsFilter(const QString &filter) const;
0128     bool removeFilter(const QString &filter);
0129 
0130     int addRule(AdBlockRule* rule) override;
0131     bool removeRule(int offset) override;
0132     const AdBlockRule* replaceRule(AdBlockRule* rule, int offset) override;
0133 };
0134 
0135 #endif // ADBLOCKSUBSCRIPTION_H
0136