mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-07 05:31:25 -07:00
Implement Import/Export RSS rules in legacy format
This commit is contained in:
parent
c58d0af88f
commit
b8fc415870
6 changed files with 128 additions and 40 deletions
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include "rss_autodownloader.h"
|
#include "rss_autodownloader.h"
|
||||||
|
|
||||||
|
#include <QDataStream>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
@ -174,6 +175,35 @@ void AutoDownloader::removeRule(const QString &ruleName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray AutoDownloader::exportRulesToLegacyFormat() const
|
||||||
|
{
|
||||||
|
QVariantHash dict;
|
||||||
|
for (const auto &rule : rules())
|
||||||
|
dict[rule.name()] = rule.toLegacyDict();
|
||||||
|
|
||||||
|
QByteArray data;
|
||||||
|
QDataStream out(&data, QIODevice::WriteOnly);
|
||||||
|
out.setVersion(QDataStream::Qt_4_5);
|
||||||
|
out << dict;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AutoDownloader::importRulesFromLegacyFormat(const QByteArray &data)
|
||||||
|
{
|
||||||
|
QDataStream in(data);
|
||||||
|
in.setVersion(QDataStream::Qt_4_5);
|
||||||
|
QVariantHash dict;
|
||||||
|
in >> dict;
|
||||||
|
if (in.status() != QDataStream::Ok)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (const QVariant &val : dict)
|
||||||
|
insertRule(AutoDownloadRule::fromLegacyDict(val.toHash()));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void AutoDownloader::process()
|
void AutoDownloader::process()
|
||||||
{
|
{
|
||||||
if (m_processingQueue.isEmpty()) return; // processing was disabled
|
if (m_processingQueue.isEmpty()) return; // processing was disabled
|
||||||
|
@ -317,7 +347,7 @@ void AutoDownloader::loadRulesLegacy()
|
||||||
SettingsPtr settings = Profile::instance().applicationSettings(QStringLiteral("qBittorrent-rss"));
|
SettingsPtr settings = Profile::instance().applicationSettings(QStringLiteral("qBittorrent-rss"));
|
||||||
QVariantHash rules = settings->value(QStringLiteral("download_rules")).toHash();
|
QVariantHash rules = settings->value(QStringLiteral("download_rules")).toHash();
|
||||||
foreach (const QVariant &ruleVar, rules) {
|
foreach (const QVariant &ruleVar, rules) {
|
||||||
auto rule = AutoDownloadRule::fromVariantHash(ruleVar.toHash());
|
auto rule = AutoDownloadRule::fromLegacyDict(ruleVar.toHash());
|
||||||
if (!rule.name().isEmpty())
|
if (!rule.name().isEmpty())
|
||||||
insertRule(rule);
|
insertRule(rule);
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,9 @@ namespace RSS
|
||||||
bool renameRule(const QString &ruleName, const QString &newRuleName);
|
bool renameRule(const QString &ruleName, const QString &newRuleName);
|
||||||
void removeRule(const QString &ruleName);
|
void removeRule(const QString &ruleName);
|
||||||
|
|
||||||
|
QByteArray exportRulesToLegacyFormat() const;
|
||||||
|
bool importRulesFromLegacyFormat(const QByteArray &data);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void processingStateChanged(bool enabled);
|
void processingStateChanged(bool enabled);
|
||||||
void ruleAdded(const QString &ruleName);
|
void ruleAdded(const QString &ruleName);
|
||||||
|
|
|
@ -63,11 +63,29 @@ namespace
|
||||||
QJsonValue triStateBoolToJsonValue(const TriStateBool &triStateBool)
|
QJsonValue triStateBoolToJsonValue(const TriStateBool &triStateBool)
|
||||||
{
|
{
|
||||||
switch (static_cast<int>(triStateBool)) {
|
switch (static_cast<int>(triStateBool)) {
|
||||||
case 0: return false; break;
|
case 0: return false;
|
||||||
case 1: return true; break;
|
case 1: return true;
|
||||||
default: return QJsonValue();
|
default: return QJsonValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TriStateBool addPausedLegacyToTriStateBool(int val)
|
||||||
|
{
|
||||||
|
switch (val) {
|
||||||
|
case 1: return TriStateBool::True; // always
|
||||||
|
case 2: return TriStateBool::False; // never
|
||||||
|
default: return TriStateBool::Undefined; // default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int triStateBoolToAddPausedLegacy(const TriStateBool &triStateBool)
|
||||||
|
{
|
||||||
|
switch (static_cast<int>(triStateBool)) {
|
||||||
|
case 0: return 2; // never
|
||||||
|
case 1: return 1; // always
|
||||||
|
default: return 0; // default
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString Str_Name(QStringLiteral("name"));
|
const QString Str_Name(QStringLiteral("name"));
|
||||||
|
@ -378,21 +396,37 @@ AutoDownloadRule AutoDownloadRule::fromJsonObject(const QJsonObject &jsonObj, co
|
||||||
return rule;
|
return rule;
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoDownloadRule AutoDownloadRule::fromVariantHash(const QVariantHash &varHash)
|
QVariantHash AutoDownloadRule::toLegacyDict() const
|
||||||
{
|
{
|
||||||
AutoDownloadRule rule(varHash.value("name").toString());
|
return {{"name", name()},
|
||||||
|
{"must_contain", mustContain()},
|
||||||
|
{"must_not_contain", mustNotContain()},
|
||||||
|
{"save_path", savePath()},
|
||||||
|
{"affected_feeds", feedURLs()},
|
||||||
|
{"enabled", isEnabled()},
|
||||||
|
{"category_assigned", assignedCategory()},
|
||||||
|
{"use_regex", useRegex()},
|
||||||
|
{"add_paused", triStateBoolToAddPausedLegacy(addPaused())},
|
||||||
|
{"episode_filter", episodeFilter()},
|
||||||
|
{"last_match", lastMatch()},
|
||||||
|
{"ignore_days", ignoreDays()}};
|
||||||
|
}
|
||||||
|
|
||||||
rule.setUseRegex(varHash.value("use_regex", false).toBool());
|
AutoDownloadRule AutoDownloadRule::fromLegacyDict(const QVariantHash &dict)
|
||||||
rule.setMustContain(varHash.value("must_contain").toString());
|
{
|
||||||
rule.setMustNotContain(varHash.value("must_not_contain").toString());
|
AutoDownloadRule rule(dict.value("name").toString());
|
||||||
rule.setEpisodeFilter(varHash.value("episode_filter").toString());
|
|
||||||
rule.setFeedURLs(varHash.value("affected_feeds").toStringList());
|
rule.setUseRegex(dict.value("use_regex", false).toBool());
|
||||||
rule.setEnabled(varHash.value("enabled", false).toBool());
|
rule.setMustContain(dict.value("must_contain").toString());
|
||||||
rule.setSavePath(varHash.value("save_path").toString());
|
rule.setMustNotContain(dict.value("must_not_contain").toString());
|
||||||
rule.setCategory(varHash.value("category_assigned").toString());
|
rule.setEpisodeFilter(dict.value("episode_filter").toString());
|
||||||
rule.setAddPaused(TriStateBool(varHash.value("add_paused").toInt() - 1));
|
rule.setFeedURLs(dict.value("affected_feeds").toStringList());
|
||||||
rule.setLastMatch(varHash.value("last_match").toDateTime());
|
rule.setEnabled(dict.value("enabled", false).toBool());
|
||||||
rule.setIgnoreDays(varHash.value("ignore_days").toInt());
|
rule.setSavePath(dict.value("save_path").toString());
|
||||||
|
rule.setCategory(dict.value("category_assigned").toString());
|
||||||
|
rule.setAddPaused(addPausedLegacyToTriStateBool(dict.value("add_paused").toInt()));
|
||||||
|
rule.setLastMatch(dict.value("last_match").toDateTime());
|
||||||
|
rule.setIgnoreDays(dict.value("ignore_days").toInt());
|
||||||
|
|
||||||
return rule;
|
return rule;
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,9 @@ namespace RSS
|
||||||
|
|
||||||
QJsonObject toJsonObject() const;
|
QJsonObject toJsonObject() const;
|
||||||
static AutoDownloadRule fromJsonObject(const QJsonObject &jsonObj, const QString &name = "");
|
static AutoDownloadRule fromJsonObject(const QJsonObject &jsonObj, const QString &name = "");
|
||||||
static AutoDownloadRule fromVariantHash(const QVariantHash &varHash);
|
|
||||||
|
QVariantHash toLegacyDict() const;
|
||||||
|
static AutoDownloadRule fromLegacyDict(const QVariantHash &dict);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool matches(const QString &articleTitle, const QString &expression) const;
|
bool matches(const QString &articleTitle, const QString &expression) const;
|
||||||
|
|
|
@ -384,31 +384,50 @@ void AutomatedRssDownloader::on_browseSP_clicked()
|
||||||
|
|
||||||
void AutomatedRssDownloader::on_exportBtn_clicked()
|
void AutomatedRssDownloader::on_exportBtn_clicked()
|
||||||
{
|
{
|
||||||
// if (m_editableRuleList->isEmpty()) {
|
if (RSS::AutoDownloader::instance()->rules().isEmpty()) {
|
||||||
// QMessageBox::warning(this, tr("Invalid action"), tr("The list is empty, there is nothing to export."));
|
QMessageBox::warning(this, tr("Invalid action")
|
||||||
// return;
|
, tr("The list is empty, there is nothing to export."));
|
||||||
// }
|
return;
|
||||||
// // Ask for a save path
|
}
|
||||||
// QString save_path = QFileDialog::getSaveFileName(this, tr("Where would you like to save the list?"), QDir::homePath(), tr("Rules list (*.rssrules)"));
|
|
||||||
// if (save_path.isEmpty()) return;
|
QString path = QFileDialog::getSaveFileName(
|
||||||
// if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive))
|
this, tr("Where would you like to save the list?")
|
||||||
// save_path += ".rssrules";
|
, QDir::homePath(), tr("Rules list (legacy)") + QString(" (*.rssrules)"));
|
||||||
// if (!m_editableRuleList->serialize(save_path)) {
|
if (path.isEmpty()) return;
|
||||||
// QMessageBox::warning(this, tr("I/O Error"), tr("Failed to create the destination file"));
|
|
||||||
// return;
|
if (!path.endsWith(".rssrules", Qt::CaseInsensitive))
|
||||||
// }
|
path += ".rssrules";
|
||||||
|
|
||||||
|
QFile file(path);
|
||||||
|
if (!file.open(QFile::WriteOnly)
|
||||||
|
|| (file.write(RSS::AutoDownloader::instance()->exportRulesToLegacyFormat()) == -1)) {
|
||||||
|
QMessageBox::critical(
|
||||||
|
this, tr("I/O Error")
|
||||||
|
, tr("Failed to create the destination file. Reason: %1").arg(file.errorString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AutomatedRssDownloader::on_importBtn_clicked()
|
void AutomatedRssDownloader::on_importBtn_clicked()
|
||||||
{
|
{
|
||||||
// // Ask for filter path
|
QString path = QFileDialog::getOpenFileName(
|
||||||
// QString load_path = QFileDialog::getOpenFileName(this, tr("Please point to the RSS download rules file"), QDir::homePath(), tr("Rules list") + QString(" (*.rssrules *.filters)"));
|
this, tr("Please point to the RSS download rules file")
|
||||||
// if (load_path.isEmpty() || !QFile::exists(load_path)) return;
|
, QDir::homePath(), tr("Rules list (legacy)") + QString(" (*.rssrules)"));
|
||||||
// // Load it
|
if (path.isEmpty() || !QFile::exists(path))
|
||||||
// if (!m_editableRuleList->unserialize(load_path)) {
|
return;
|
||||||
// QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file"));
|
|
||||||
// return;
|
QFile file(path);
|
||||||
// }
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
QMessageBox::critical(
|
||||||
|
this, tr("I/O Error")
|
||||||
|
, tr("Failed to open the file. Reason: %1").arg(file.errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!RSS::AutoDownloader::instance()->importRulesFromLegacyFormat(file.readAll())) {
|
||||||
|
QMessageBox::critical(
|
||||||
|
this, tr("Import Error")
|
||||||
|
, tr("Failed to import the selected rules file."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AutomatedRssDownloader::displayRulesListMenu()
|
void AutomatedRssDownloader::displayRulesListMenu()
|
||||||
|
|
|
@ -386,7 +386,7 @@
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="importBtn">
|
<widget class="QPushButton" name="importBtn">
|
||||||
<property name="enabled">
|
<property name="enabled">
|
||||||
<bool>false</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Import...</string>
|
<string>&Import...</string>
|
||||||
|
@ -396,7 +396,7 @@
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="exportBtn">
|
<widget class="QPushButton" name="exportBtn">
|
||||||
<property name="enabled">
|
<property name="enabled">
|
||||||
<bool>false</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Export...</string>
|
<string>&Export...</string>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue