mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-23 08:48:07 -06:00
Merge pull request #7832 from glassez/rss-import
Implement Import/Export RSS rules. Closes #7721
This commit is contained in:
@@ -54,8 +54,13 @@
|
||||
#include "autoexpandabledialog.h"
|
||||
#include "ui_automatedrssdownloader.h"
|
||||
|
||||
const QString EXT_JSON {QStringLiteral(".json")};
|
||||
const QString EXT_LEGACY {QStringLiteral(".rssrules")};
|
||||
|
||||
AutomatedRssDownloader::AutomatedRssDownloader(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, m_formatFilterJSON(QString("%1 (*%2)").arg(tr("Rules")).arg(EXT_JSON))
|
||||
, m_formatFilterLegacy(QString("%1 (*%2)").arg(tr("Rules (legacy)")).arg(EXT_LEGACY))
|
||||
, m_ui(new Ui::AutomatedRssDownloader)
|
||||
, m_currentRuleItem(nullptr)
|
||||
{
|
||||
@@ -384,31 +389,73 @@ void AutomatedRssDownloader::on_browseSP_clicked()
|
||||
|
||||
void AutomatedRssDownloader::on_exportBtn_clicked()
|
||||
{
|
||||
// if (m_editableRuleList->isEmpty()) {
|
||||
// QMessageBox::warning(this, tr("Invalid action"), 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;
|
||||
// if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive))
|
||||
// save_path += ".rssrules";
|
||||
// if (!m_editableRuleList->serialize(save_path)) {
|
||||
// QMessageBox::warning(this, tr("I/O Error"), tr("Failed to create the destination file"));
|
||||
// return;
|
||||
// }
|
||||
if (RSS::AutoDownloader::instance()->rules().isEmpty()) {
|
||||
QMessageBox::warning(this, tr("Invalid action")
|
||||
, tr("The list is empty, there is nothing to export."));
|
||||
return;
|
||||
}
|
||||
|
||||
QString selectedFilter {m_formatFilterJSON};
|
||||
QString path = QFileDialog::getSaveFileName(
|
||||
this, tr("Export RSS rules"), QDir::homePath()
|
||||
, QString("%1;;%2").arg(m_formatFilterJSON).arg(m_formatFilterLegacy), &selectedFilter);
|
||||
if (path.isEmpty()) return;
|
||||
|
||||
const RSS::AutoDownloader::RulesFileFormat format {
|
||||
(selectedFilter == m_formatFilterJSON)
|
||||
? RSS::AutoDownloader::RulesFileFormat::JSON
|
||||
: RSS::AutoDownloader::RulesFileFormat::Legacy
|
||||
};
|
||||
|
||||
if (format == RSS::AutoDownloader::RulesFileFormat::JSON) {
|
||||
if (!path.endsWith(EXT_JSON, Qt::CaseInsensitive))
|
||||
path += EXT_JSON;
|
||||
}
|
||||
else {
|
||||
if (!path.endsWith(EXT_LEGACY, Qt::CaseInsensitive))
|
||||
path += EXT_LEGACY;
|
||||
}
|
||||
|
||||
QFile file {path};
|
||||
if (!file.open(QFile::WriteOnly)
|
||||
|| (file.write(RSS::AutoDownloader::instance()->exportRules(format)) == -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()
|
||||
{
|
||||
// // Ask for filter path
|
||||
// QString load_path = QFileDialog::getOpenFileName(this, tr("Please point to the RSS download rules file"), QDir::homePath(), tr("Rules list") + QString(" (*.rssrules *.filters)"));
|
||||
// if (load_path.isEmpty() || !QFile::exists(load_path)) return;
|
||||
// // Load it
|
||||
// if (!m_editableRuleList->unserialize(load_path)) {
|
||||
// QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file"));
|
||||
// return;
|
||||
// }
|
||||
QString selectedFilter {m_formatFilterJSON};
|
||||
QString path = QFileDialog::getOpenFileName(
|
||||
this, tr("Import RSS rules"), QDir::homePath()
|
||||
, QString("%1;;%2").arg(m_formatFilterJSON).arg(m_formatFilterLegacy), &selectedFilter);
|
||||
if (path.isEmpty() || !QFile::exists(path))
|
||||
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;
|
||||
}
|
||||
|
||||
const RSS::AutoDownloader::RulesFileFormat format {
|
||||
(selectedFilter == m_formatFilterJSON)
|
||||
? RSS::AutoDownloader::RulesFileFormat::JSON
|
||||
: RSS::AutoDownloader::RulesFileFormat::Legacy
|
||||
};
|
||||
|
||||
try {
|
||||
RSS::AutoDownloader::instance()->importRules(file.readAll(),format);
|
||||
}
|
||||
catch (const RSS::ParsingError &error) {
|
||||
QMessageBox::critical(
|
||||
this, tr("Import Error")
|
||||
, tr("Failed to import the selected rules file. Reason: %1").arg(error.message()));
|
||||
}
|
||||
}
|
||||
|
||||
void AutomatedRssDownloader::displayRulesListMenu()
|
||||
|
||||
@@ -96,6 +96,9 @@ private:
|
||||
void updateFeedList();
|
||||
void addFeedArticlesToTree(RSS::Feed *feed, const QStringList &articles);
|
||||
|
||||
const QString m_formatFilterJSON;
|
||||
const QString m_formatFilterLegacy;
|
||||
|
||||
Ui::AutomatedRssDownloader *m_ui;
|
||||
QListWidgetItem *m_currentRuleItem;
|
||||
QShortcut *m_editHotkey;
|
||||
|
||||
@@ -386,7 +386,7 @@
|
||||
<item>
|
||||
<widget class="QPushButton" name="importBtn">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&Import...</string>
|
||||
@@ -396,7 +396,7 @@
|
||||
<item>
|
||||
<widget class="QPushButton" name="exportBtn">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&Export...</string>
|
||||
|
||||
Reference in New Issue
Block a user