Commit dfb054f2 authored by Laurent Montel's avatar Laurent Montel 😁

Implement add subscription

parent 7c73a7b9
......@@ -21,11 +21,14 @@
#include "adblockmatcher.h"
#include "adblocksubscription.h"
#include "adblockinterceptor_debug.h"
#include "adblockutil.h"
#include "globalsettings_webengineurlinterceptoradblock.h"
#include <KConfig>
#include <KConfigGroup>
#include <QDateTime>
#include <QDir>
#include <QSaveFile>
#include <QStandardPaths>
#include <QStandardPaths>
#include <QTextStream>
#include <QTimer>
......@@ -180,3 +183,34 @@ void AdblockManager::updateAllSubscriptions()
KConfigGroup general = config.group(QStringLiteral("General"));
general.writeEntry(QStringLiteral("lastUpdate"), QDateTime::currentDateTime());
}
AdBlockSubscription *AdblockManager::addSubscription(const QString &title, const QString &url)
{
if (title.isEmpty() || url.isEmpty()) {
return nullptr;
}
QString fileName = AdBlock::AdblockUtil::filterCharsFromFilename(title.toLower()) + QStringLiteral(".txt");
QString filePath = AdBlock::AdblockUtil::ensureUniqueFilename(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock/"), fileName);
QByteArray data = QStringLiteral("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1();
QSaveFile file(filePath);
if (!file.open(QFile::WriteOnly)) {
qCWarning(ADBLOCKINTERCEPTOR_LOG) << "AdBlockManager: Cannot write to file" << filePath;
return nullptr;
}
file.write(data);
file.commit();
AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
subscription->setUrl(QUrl(url));
subscription->setFilePath(filePath);
subscription->loadSubscription(mDisabledRules);
mSubscriptions.insert(mSubscriptions.count() - 1, subscription);
// connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
// connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
return subscription;
}
......@@ -48,6 +48,8 @@ public:
void updateAllSubscriptions();
AdBlockSubscription* addSubscription(const QString &title, const QString &url);
Q_SIGNALS:
void enabledChanged(bool);
......
......@@ -32,6 +32,8 @@
* ============================================================ */
#include "adblockutil.h"
#include <QDir>
#include <QFileInfo>
#include <QMap>
#include <QStandardPaths>
......@@ -90,3 +92,49 @@ QString AdBlock::AdblockUtil::localFilterPath()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock/customlist.txt");
}
QString AdBlock::AdblockUtil::filterCharsFromFilename(const QString &name)
{
QString value = name;
value.replace(QLatin1Char('/'), QLatin1Char('-'));
value.remove(QLatin1Char('\\'));
value.remove(QLatin1Char(':'));
value.remove(QLatin1Char('*'));
value.remove(QLatin1Char('?'));
value.remove(QLatin1Char('"'));
value.remove(QLatin1Char('<'));
value.remove(QLatin1Char('>'));
value.remove(QLatin1Char('|'));
return value;
}
QString AdBlock::AdblockUtil::ensureUniqueFilename(const QString &name, const QString &appendFormat)
{
Q_ASSERT(appendFormat.contains(QStringLiteral("%1")));
QFileInfo info(name);
if (!info.exists())
return name;
const QDir dir = info.absoluteDir();
const QString fileName = info.fileName();
int i = 1;
while (info.exists()) {
QString file = fileName;
int index = file.lastIndexOf(QLatin1Char('.'));
const QString appendString = appendFormat.arg(i);
if (index == -1)
file.append(appendString);
else
file = file.left(index) + appendString + file.mid(index);
info.setFile(dir, file);
i++;
}
return info.absoluteFilePath();
}
......@@ -48,6 +48,8 @@ public:
static QMap<QString, QString> listSubscriptions();
static QString localFilterPath();
static QString filterCharsFromFilename(const QString &name);
static QString ensureUniqueFilename(const QString &name, const QString &appendFormat);
};
}
#endif
......@@ -286,30 +286,6 @@ void AdBlockSettingWidget::save()
saveSpinBox(mUi->spinBox, AdBlock::AdBlockSettings::self()->adBlockUpdateIntervalItem());
// automatic filters
//save custom list
KConfig config(QStringLiteral("AdBlockadblockrc"));
const QStringList list = config.groupList().filter(QRegularExpression(QStringLiteral("FilterList \\d+")));
for (const QString &group : list) {
config.deleteGroup(group);
}
// make sure the directory exists, otherwise saving the filters will fail
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock"));
const int numberItem(mUi->automaticFiltersListWidget->count());
for (int i = 0; i < numberItem; ++i) {
QListWidgetItem *subItem = mUi->automaticFiltersListWidget->item(i);
KConfigGroup grp = config.group(QStringLiteral("FilterList %1").arg(i));
grp.writeEntry(QStringLiteral("FilterEnabled"), subItem->checkState() == Qt::Checked);
grp.writeEntry(QStringLiteral("url"), subItem->data(UrlList).toString());
grp.writeEntry(QStringLiteral("name"), subItem->text());
QString path = subItem->data(PathList).toString();
if (path.isEmpty()) {
path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock/adblockrules-%1").arg(i);
}
grp.writeEntry(QStringLiteral("path"), path);
}
config.sync();
// local filters
const QString localRulesFilePath = AdBlock::AdblockUtil::localFilterPath();
......@@ -331,7 +307,7 @@ void AdBlockSettingWidget::save()
disableCustomFilter << stringRule;
}
}
KConfig config(QStringLiteral("AdBlockadblockrc"));
if (!disableCustomFilter.isEmpty()) {
KConfigGroup grp = config.group(QStringLiteral("DisableRules"));
grp.writeEntry("DisableRules", disableCustomFilter);
......@@ -378,13 +354,15 @@ void AdBlockSettingWidget::slotAddFilter()
QString name;
QString url;
dlg->selectedList(name, url);
QListWidgetItem *subItem = new QListWidgetItem(mUi->automaticFiltersListWidget);
subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
subItem->setCheckState(Qt::Checked);
subItem->setText(name);
subItem->setData(UrlList, url);
subItem->setData(PathList, QString());
hasChanged();
if (AdblockManager::self()->addSubscription(name, url)) {
QListWidgetItem *subItem = new QListWidgetItem(mUi->automaticFiltersListWidget);
subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
subItem->setCheckState(Qt::Checked);
subItem->setText(name);
subItem->setData(UrlList, url);
subItem->setData(PathList, QString());
hasChanged();
}
}
delete dlg;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment